SymPy - Matrices


Advertisements

In Mathematics, a matrix is a two dimensional array of numbers, symbols or expressions. Theory of matrix manipulation deals with performing arithmetic operations on matrix objects, subject to certain rules.

Linear transformation is one of the important applications of matrices. Many scientific fields, specially related to Physics use matrix related applications.

SymPy package has matrices module that deals with matrix handling. It includes Matrix class whose object represents a matrix.

Note: If you want to execute all the snippets in this chapter individually, you need to import the matrix module as shown below −

>>> from sympy.matrices import Matrix

Example

>>> from sympy.matrices import Matrix 
>>> m=Matrix([[1,2,3],[2,3,1]]) 
>>> m
$\displaystyle \left[\begin{matrix}1 & 2 & 3\\2 & 3 & 1\end{matrix}\right]$

On executing the above command in python shell, following output will be generated −

[1 2 3 2 3 1]

Matrix is created from appropriately sized List objects. You can also obtain a matrix by distributing list items in specified number of rows and columns.

>>> M=Matrix(2,3,[10,40,30,2,6,9]) 
>>> M
$\displaystyle \left[\begin{matrix}10 & 40 & 30\\2 & 6 & 9\end{matrix}\right]$

On executing the above command in python shell, following output will be generated −

[10 40 30 2 6 9]

Matrix is a mutable object. The matrices module also provides ImmutableMatrix class for obtaining immutable matrix.

Basic manipulation

The shape property of Matrix object returns its size.

>>> M.shape

The output for the above code is as follows −

(2,3)

The row() and col() method respectively returns row or column of specified number.

>>> M.row(0)
$\displaystyle \left[\begin{matrix}10 & 40 & 30\end{matrix}\right]$

The output for the above code is as follows −

[10 40 30]

>>> M.col(1)
$\displaystyle \left[\begin{matrix}40\\6\end{matrix}\right]$

The output for the above code is as follows −

[40 6]

Use Python's slice operator to fetch one or more items belonging to row or column.

>>> M.row(1)[1:3]
[6, 9]

Matrix class has row_del() and col_del() methods that deletes specified row/column from given matrix −

>>> M=Matrix(2,3,[10,40,30,2,6,9]) 
>>> M.col_del(1) 
>>> M

On executing the above command in python shell, following output will be generated −

Matrix([[10, 30],[ 2, 9]])

You can apply style to the output using the following command −

$\displaystyle \left[\begin{matrix}10 & 30\\2 & 9\end{matrix}\right]$

You get the following output after executing the above code snippet −

[10 30 2 9]

>>> M.row_del(0) 
>>> M

$\displaystyle \left[\begin{matrix}2 & 9\end{matrix}\right]$

You get the following output after executing the above code snippet −

[2 9]

Similarly, row_insert() and col_insert() methods add rows or columns at specified row or column index

>>> M1=Matrix([[10,30]]) 
>>> M=M.row_insert(0,M1)
>>> M

$\displaystyle \left[\begin{matrix}10 & 30\\2 & 9\end{matrix}\right]$

You get the following output after executing the above code snippet −

[10 40 30 2 9]

>>> M2=Matrix([40,6]) 
>>> M=M.col_insert(1,M2) 
>>> M

$\displaystyle \left[\begin{matrix}10 & 40 & 30\\2 & 6 & 9\end{matrix}\right]$

You get the following output after executing the above code snippet −

[10 40 30 6 9]

Arithmetic Operations

Usual operators +, - and * are defined for performing addition, subtraction and multiplication.

>>> M1=Matrix([[1,2,3],[3,2,1]]) 
>>> M2=Matrix([[4,5,6],[6,5,4]]) 
>>> M1+M2

$\displaystyle \left[\begin{matrix}5 & 7 & 9\\9 & 7 & 5\end{matrix}\right]$

You get the following output after executing the above code snippet −

[5 7 9 9 7 5]

>>> M1-M2
$\displaystyle \left[\begin{matrix}-3 & -3 & -3\\-3 & -3 & -3\end{matrix}\right]$

You get the following output after executing the above code snippet −

[- 3 -3 -3 -3 -3 -3]

Matrix multiplication is possible only if - The number of columns of the 1st matrix must equal the number of rows of the 2nd matrix. - And the result will have the same number of rows as the 1st matrix, and the same number of columns as the 2nd matrix.

>>> M1=Matrix([[1,2,3],[3,2,1]]) 
>>> M2=Matrix([[4,5],[6,6],[5,4]]) 
>>> M1*M2
$\displaystyle \left[\begin{matrix}31 & 29\\29 & 31\end{matrix}\right]$

The output for the above code is as follows −

[31 29 29 31]

>>> M1.T
$\displaystyle \left[\begin{matrix}1 & 3\\2 & 2\\3 & 1\end{matrix}\right]$

The following output is obtained after executing the code −

[1 3 2 2 3 1]

To calculate a determinant of matrix, use det() method. A determinant is a scalar value that can be computed from the elements of a square matrix.0

>>> M=Matrix(3,3,[10,20,30,5,8,12,9,6,15])
>>> M
$\displaystyle \left[\begin{matrix}10 & 20 & 30\\5 & 8 & 12\\9 & 6 & 15\end{matrix}\right]$

The output for the above code is as follows −

[10 20 30 5 8 12 9 6 15]

>>> M.det()

The output for the above code is as follows −

-120

Matrix Constructors

SymPy provides many special type of matrix classes. For example, Identity matrix, matrix of all zeroes and ones, etc. These classes are named as eye, zeros and ones respectively. Identity matrix is a square matrix with elements falling on diagonal are set to 1, rest of the elements are 0.

Example

from sympy.matrices import eye eye(3)

Output

Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
$\displaystyle \left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right]$

The output for the above code is as follows −

[1 0 0 0 1 0 0 0 1]

In diag matrix, elements on diagonal are initialized as per arguments provided.

>>> from sympy.matrices import diag 
>>> diag(1,2,3)

$\displaystyle \left[\begin{matrix}1 & 0 & 0\\0 & 2 & 0\\0 & 0 & 3\end{matrix}\right]$

The output for the above code is as follows −

[1 0 0 0 2 0 0 0 3]

All elements in zeros matrix are initialized to 0.

>>> from sympy.matrices import zeros 
>>> zeros(2,3)

$\displaystyle \left[\begin{matrix}0 & 0 & 0\\0 & 0 & 0\end{matrix}\right]$

The output for the above code is as follows −

[0 0 0 0 0 0]

Similarly, ones is matrix with all elements set to 1.

>>> from sympy.matrices import ones
>>> ones(2,3)

$\displaystyle \left[\begin{matrix}1 & 1 & 1\\1 & 1 & 1\end{matrix}\right]$

The output for the above code is as follows −

[1 1 1 1 1 1]

Advertisements