Collect coefficients of non-square matrix in sympy

Given three vectors, a(1,4), b(1, 4), c(4,1). I want to do the following

a = MatrixSymbol('a', 1, 4)
b = MatrixSymbol('a', 1, 4)
c = MatrixSymbol('c', 4, 1)

expr = a*c + b*c
c_coeff = .... # How to get the value of (a+b) here

I tried using coeff and collect but it fails because the C matrix is not square.

>Solution :

If it is linear then

>>> from sympy.solvers.solveset import linear_coeffs
>>> linear_coeffs(expr, c)
[a + b, 0]

note that 0 is the constant term; if you added MatrixSymbol('d', 1, 1) to expr the 2nd element would be d; you named ‘b’ as ‘a’ in your example; I named it ‘b’ to get the output shown above.

Leave a Reply