SymPy Logo

Linear Algebra with SymPy

Manipulation of Matrices

  This section is devoted to basic manipulation with matrices.

For a given \( m \times n \) matrix A, its transpose is the \( n \times m \) matrix, denoted either by
\( {\bf A}^T \) or by At or just by \( {\bf A}' , \) whose entries are formed by interchanging the rows with
the columns; that is, \( \left( {\bf A}' \right)_{i,j} = \left( {\bf A}' \right)_{j,i} . \)

 

Example: Let us consider the 3-by-4 matrix
\[ \begin{bmatrix} 34 &22& 19& 12 \\ 72& 87& 162& 122 \\ 69& 69& 420& 89 \end{bmatrix} . \]
Its transpose will be
\[ \begin{bmatrix} 34 & 72 & 69 \\ 22& 87 & 69 \\ 19& 162 & 420 \\ 12 & 122 & 89 \end{bmatrix} . \]
SymPy confirms

A = Matrix([ [34,22,19,12], [72,87,162,122], [69,69,420,89] ])
A = A.T
Out[2]= \( \displaystyle \quad \begin{pmatrix} 34& 72&69 \\ 22& 87& 69 \\ 19& 162&420 \\ 12& 122& 89 \end{pmatrix} \)

 

Theorem: Let A and B denote matrices whose sizes are appropriate for the following operations.

  1. \( \left( {\bf A}^{\mathrm T} \right)^{\mathrm T} = {\bf A} \)
  2. \( \left( {\bf A} + {\bf B} \right)^{\mathrm T} = {\bf A}^{\mathrm T} + {\bf B}^{\mathrm T} \)
  3. For any scalar s, \( \left( s{\bf A} \right)^{\mathrm T} = s\,{\bf A}^{\mathrm T} .\)
  4. \( \left( {\bf A}\,{\bf B} \right)^{\mathrm T} = {\bf B}^{\mathrm T} {\bf A}^{\mathrm T} . \)
  5. If A is a square matrix, then \( \mbox{tr}\left( {\bf A}^{\mathrm T} \right) = \mbox{tr}\left( {\bf A} \right) . \)
  6. If A is a square nonsingular matrix, then \( \left( {\bf A}^{\mathrm T} \right)^{-1} = \left( {\bf A}^{-1} \right)^{\mathrm T} . \)
  7. If A is a square matrix, then \( \det\left( {\bf A}^{\mathrm T} \right) = \det\left( {\bf A} \right) . \)

Here is a list of basic matrix manipulations with SymPy

First we generate 3-by-4 matrix:


M = Matrix([ [1,2,3,4], [5,6,7,8], [9,10,11,12] ])
Out[2]= \( \displaystyle \quad \begin{pmatrix} 1& 2& 3& 4 \\ 5& 6& 7& 8 \\ 9& 10& 11& 12 \end{pmatrix} \)
Insert a column at position 2:

M = M.col_insert(1, Matrix( [21,22,23] ))
Out[4]= \( \displaystyle \quad \begin{pmatrix} 1& 21&2& 3& 4 \\ 5& 22&6& 7& 8 \\ 9& 23&10& 11& 12 \end{pmatrix} \)
Extract row 3:

M[2,:]
Out[5]= {9, 10, 11, 12}
Extract column 2

M[:,1]
Out[6]= \( \displaystyle \quad \begin{pmatrix} 2 \\ 6 \\ 10 \end{pmatrix} \)
Insert a row at position 2:

M.row_insert(1, Matrix([ [30,31,32,33] ]))
Out[8]= \( \displaystyle \quad \begin{pmatrix} 1&2&3&4 \\ 30&31&32&33 \\ 5&6&7&8 \\ 9&10&11&12 \end{pmatrix} \)
Adding two rows or columns. First, we add column 3 = column 3 + column 1:

M[:,2] = M[:,0] + M[:,2]
Out[8]= \( \displaystyle \quad \begin{pmatrix} 1&2&4&4 \\ 5&6&12&8 \\ 9&10&20&12 \end{pmatrix} \)
Now row 2 = row 2 + row 3:

M[1,:] = M[1,:] + M[2,:]
Out[8]= \( \displaystyle \quad \begin{pmatrix} 1&2&3&4 \\ 14&16&18&20 \\ 9&10&11&12 \end{pmatrix} \)
Swapping rows or columns. Swap row 1 and row 3:

M.row_swap(0,2)
Out[11]= \( \displaystyle \quad \begin{pmatrix} 9&10&11&12 \\ 5&6&7&8 \\ 1&2&3&4 \end{pmatrix} \)
Swap column 1 and 3:

M.col_swap(0,2)
Out[13]= \( \displaystyle \quad \begin{pmatrix} 11&10&9&12 \\ 7&6&5&8 \\ 3&2&1&4 \end{pmatrix} \)
Multiply row 2 with 3:

M[1,:] = M[1,:]*3
Out[14]= \( \displaystyle \quad \begin{pmatrix} 1&2&3&4 \\ 15&18&21&24 \\ 9&10&11&12 \end{pmatrix} \)
Multiply column 1 with 4:

M[:,0] = M[:,0] * 4
Out[15]= \( \displaystyle \quad \begin{pmatrix} 4&2&3&4 \\ 20&6&7&8 \\ 36&10&11&12 \end{pmatrix} \)
Replace a block of a matrix:

R = Matrix(5,5,lambda i,j: random.randint(0,10))
Out[2]= \( \displaystyle \quad \begin{pmatrix} 3&8&10&7&1 \\ 9&5&10&7&1 \\ 3&6&0&4&4 \\ 10&3&2&3&6 \\ 7&8&9&3&8 \end{pmatrix} \)
Update the 3-by-4 submatrix by using the short form of Span (;;) to specify the relevant span of rows and columns:

R[0:3, 0:4] = Matrix([ [0,0,0,0], [0,0,0,0], [0,0,0,0] ])
Out[4]= \( \displaystyle \quad \begin{pmatrix} 0&0&0&0&1 \\ 0&0&0&0&1 \\ 0&0&0&0&4 \\ 10&3&2&3&6 \\ 7&8&9&3&8 \end{pmatrix} \)
To pick out a submatrix, you can use Span (;;). First, define a 4×5 matrix:

mat = Matrix(4,5,lambda i,j: random.randint(0,10))
Out[6]= \( \displaystyle \quad \begin{pmatrix} 3&1&8&5&0 \\ 0&0&8&6&8 \\ 8&0&0&10&0 \\ 4&4&8&9&8 \end{pmatrix} \)
Extract the top-left 3×4 submatrix by using Span (;;) to specify the relevant span of rows and columns:

R[0:3, 0:4]
Out[8]= \( \displaystyle \quad \begin{pmatrix} 3&1&8&5 \\ 0&0&8&6 \\ 8&0&0&10 \end{pmatrix} \)
Another option is cut off some rows or/and columns. If one wants to delete, say the second row, type:

mat.row_del(1)

If one wants to delete the first two rows, type:

mat.row_del(0)
mat.row_del(1)

Extract all elements except the outermost rows and columns (negative indices count from the end):

mat[1:3,1:4]
Out[9]= \( \displaystyle \quad \begin{pmatrix} 3&1&8&5&0 \\ 0&0&8&6&8 \\ 8&0&0&10&0 \\ 4&4&8&9&8 \end{pmatrix} \)
Out[10]= {{0, 8, 6, 8}, {0, 0, 10, 0}}
Extract diagonal elements:

Diagonal[mat]
Out[11]= {3, 0, 0, 9}

Diagonal[mat, 2]
Out[12]= {8, 6, 0}
To find sum of diagonal elements (which is called trace), enter:

Total[Diagonal[mat]]
Out[13]= 12