Getting elementary matrix multiplication equations in sympy

I have 2 matrices, the first of which is sparse with integer coefficients.

import sympy
A = sympy.eye(2)
A.row_op(1, lambda v, j: v + 2*A[0, j])

The second symbolism, and I perform an operation between them:

M = MatrixSymbol('M', 2, 1)
X = A * M + A.col(1)

Now I would like to get elementary equations:

X_{0,0} = A_{0,0}
X_{0,1} = 2*A_{0,0} + A_{0,1}

One way to do this is to specify a matrix in sympy, with each element being a separate character:

rows = []
for i in range(shape[0]):
    col = []
    for j in range(shape[1]):
        col.append(Symbol('%s_{%s,%d}' % (name,i,j)))
    rows.append(col)
M = sympy.Matrix(rows)

Is there a way to do this using the MatrixSymbolabove and then get the resulting elementary equations?

+4
source share
1 answer

Turns out this question has a very obvious answer:

MatrixSymbol in sympy you can index as a matrix, i.e.:

X[i,j]

gives elementary equations.

, MatrixSymbol sympy.Matrix:

X = sympy.Matrix(X)
X        # lists all indices as `X[i, j]`
X[3:4,2] # arbitrary subsets are supported

, / numpy ( ), numpy sympy:

ijstr = lambda i,j: sympy.Symbol(name+"_{"+str(int(i))+","+str(int(j))+"}")
matrix = np.matrix(np.fromfunction(np.vectorize(ijstr), shape))
+4

All Articles