Search code examples
pythonsympymaplemaxima

"GenerateMatrix" equivalent command in SymPy


In Maple there is a command "GenerateMatrix" that will take a collection of linear equations and generate a symbolic matrix A and symbolic vector b in "Ax=b" where "x" is a vector of variables specified by the command. The help page can be found here if this isn't clear:

http://www.maplesoft.com/support/help/maple/view.aspx?path=LinearAlgebra%2FGenerateMatrix

In Maxima a similar operation can be accomplished using the "augcoefmatrix" command. Is there a similar command in SymPy? How could this command be implemented if the command does not exist?


Solution

  • The SymPy analog is linear_eq_to_matrix. Usage example:

    from sympy import *
    variables = symbols('x y z')
    equations = [3*x-z+2, y-3*z-5, x+y-z]
    A, b = linear_eq_to_matrix(equations, variables)
    linsolve((A, b), variables)