Jesus Martinez Garcia
Jesus Martinez Garcia

Reputation: 269

SymPy: passing an arbitrary list/tuple of symbols to a solve function

I am trying to solve a consistent linear system which has (a priori) an unknown number n of equations and variables n (same number, so the system is known to have a unique solution, we just don't know what n is).

I create my variables with symbols, which creates a tuple with them, but solve_linear_system doesn't seem to work them with it (even after converted to list). A MWE:

from sympy import *
#The next line is just an example, I don't know size a priori
system=Matrix(((2,1,-1,-2),(2,2,0,-4),(1,1,-1,-1))) 
n=3
dd=symbols('a0:%d'%n)
solve_linear_system(system, dd, rational=true )

The last line returns an empty list. However if I create separate variables by hand, it returns the solutions.

What am I doing wrong?

Upvotes: 2

Views: 1255

Answers (1)

fedemp
fedemp

Reputation: 210

You need to use the * operator to unpack the tuple dd into a list of parameters to solve_linear_system() (as when you type them by hand separated by commas). Try replacing the last line of your example with:

solve_linear_system(system, *dd, rational=True )

It should work fine. Note that in your MW example n is not defined.

Upvotes: 3

Related Questions