How about something like this:
import sympy M = sympy.Matrix(3, 2, lambda i,j:sympy.var('M_%d%d' % (i+1,j+1)))
Edit: I guess I should add a little explanation. The first two arguments to sympy.Matrix () define the matrix as 3x2 (as you indicated). The third argument is the lambda function, which is essentially a shorthand way to define a function on a single line, rather than formally defining it with def . This function takes as input the variables i and j , which are conveniently matrix indices. For each pair (i, j) that are passed to lambda (i.e., for each element of the matrix), we create a new symbolic variable M_ij . sympy.var () takes a string as input that defines the name of a new symbolic variable. We generate this line "on the fly", using the format string "M_% d% d" and filling it (i + 1, j + 1) . We add 1 to i and j , because you want the matrix to be 1-indexed, not 0-indexed, like the standard in Python.
Brendan wood
source share