MatrixSymbols

MatrixSymbols#

Hide code cell content
import sympy as sp
from IPython.display import display

Here are some examples of computations with MatrixSymbol.

N = sp.Symbol("n", integer=True, positive=True)
i, j = sp.symbols("i j", integer=True, negative=False)
K = sp.MatrixSymbol("K", N, N)
display(K, K[i, j], K[0, 0])
\[\displaystyle K\]
\[\displaystyle {K}_{i,j}\]
\[\displaystyle {K}_{0,0}\]
A = sp.MatrixSymbol("A", N, N)
(A * K)[0, 0]
\[\displaystyle \sum_{i_{1}=0}^{n - 1} {A}_{0,i_{1}} {K}_{i_{1},0}\]

The important thing is that elements of a MatrixSymbol can be substituted:

K[0, 0].subs(K[0, 0], i)
\[\displaystyle i\]
A * K
\[\displaystyle A K\]
(A * K)[0, 0]
\[\displaystyle \sum_{i_{1}=0}^{n - 1} {A}_{0,i_{1}} {K}_{i_{1},0}\]

Now make the matrices \(2 \times 2\) by specifying \(n\):

A_n2 = A.subs(N, 2)
K_n2 = K.subs(N, 2)
(A_n2 * K_n2)[0, 0]
\[\displaystyle {A}_{0,0} {K}_{0,0} + {A}_{0,1} {K}_{1,0}\]
v, w, x, y = sp.symbols("v, w, x, y", real=True)
substitutions = {
    A_n2[0, 0]: v,
    A_n2[0, 1]: w,
    K_n2[0, 0]: x,
    K_n2[1, 0]: y,
}
(A_n2 * K_n2)[0, 0].subs(substitutions)
\[\displaystyle v x + w y\]