I just answered this on gitter earlier today, but you can just take the 
jacobian of the system to get its matrix form. For example:

In [1]: from sympy import *

In [2]: a, b, c, d = symbols('a, b, c, d')

In [3]: x1, x2, x3, x4 = symbols('x1:5')

In [4]: x = Matrix([x1, x2, x3, x4])

In [5]: system = Matrix([a*x1 + b*x2 + c,
   ...: c*x1 + d*x3 + 2,
   ...: c*x3 + b*x4 + a])

In [6]: A = system.jacobian(x)

In [7]: B = A*x - system

In [8]: A
Out[8]: 
Matrix([
[a, b, 0, 0],
[c, 0, d, 0],
[0, 0, c, b]])

In [9]: B
Out[9]: 
Matrix([
[-c],
[-2],
[-a]])

In [10]: assert A*x - B == system

The functionality I'm adding for my GSoC for linearizing a system of 
equations will also be able to return these matrices in a convenient form. 
But it's not terribly difficult to solve for these arrangements using the 
current functionality.






On Thursday, June 5, 2014 4:22:52 PM UTC-5, Andrei Berceanu wrote:
>
> Was this implemented into sympy at any point? It could be the equivalent 
> of Mathematica's CoefficientArrays function.
>
> On Thursday, November 14, 2013 5:56:22 AM UTC+1, Chris Smith wrote:
>>
>> I forgot that as_independent, without the as_Add=True flag will treat 
>> Muls differently. The following will be more robust:
>>
>> def eqs2matrix(eqs, syms, augment=False):
>>     """
>>     >>> s
>>     [x + 2*y == 4, 2*c + y/2 == 0]
>>     >>> eqs2matrix(s, (x, c))
>>     (Matrix([
>>     [1, 0],
>>     [0, 2]]), Matrix([
>>     [-2*y + 4],
>>     [    -y/2]]))
>>     >>> eqs2matrix([2*c*(x+y)-4],(x, y))
>>     (Matrix([[2*c, 2*c]]), Matrix([[4]]))
>>     """
>>     s = Matrix([si.lhs - si.rhs if isinstance(si, Equality) else si for 
>> si in eqs])
>>     sym = syms
>>     j = s.jacobian(sym)
>>     rhs = -(s - j*Matrix(sym))
>>     rhs.simplify()
>>     if augment:
>>         j.col_insert(0, rhs)
>>     else:
>>         j = (j, rhs)
>>     return j
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sympy+unsubscr...@googlegroups.com.
To post to this group, send email to sympy@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/8fb2dae4-9f46-4c1b-b96f-83033278c27d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to