English 中文(简体)
SymPy - Matrices
  • 时间:2024-12-22

SymPy - Matrices


Previous Page Next Page  

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 apppcations of matrices. Many scientific fields, specially related to Physics use matrix related apppcations.

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

Note: If you want to execute all the snippets in this chapter inspanidually, 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[egin{matrix}1 & 2 & 3\2 & 3 & 1end{matrix}
ight]$

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 pst items in specified number of rows and columns.


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

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[egin{matrix}10 & 40 & 30end{matrix}
ight]$

The output for the above code is as follows −

[10 40 30]


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

The output for the above code is as follows −

[40 6]

Use Python s spce 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[egin{matrix}10 & 30\2 & 9end{matrix}
ight]$

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

[10 30 2 9]


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

$displaystyle left[egin{matrix}2 & 9end{matrix}
ight]$

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[egin{matrix}10 & 30\2 & 9end{matrix}
ight]$

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[egin{matrix}10 & 40 & 30\2 & 6 & 9end{matrix}
ight]$

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 multippcation.


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

$displaystyle left[egin{matrix}5 & 7 & 9\9 & 7 & 5end{matrix}
ight]$

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

[5 7 9 9 7 5]


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

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

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

Matrix multippcation 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[egin{matrix}31 & 29\29 & 31end{matrix}
ight]$

The output for the above code is as follows −

[31 29 29 31]


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

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[egin{matrix}10 & 20 & 30\5 & 8 & 12\9 & 6 & 15end{matrix}
ight]$

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 falpng 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[egin{matrix}1 & 0 & 0\0 & 1 & 0\0 & 0 & 1end{matrix}
ight]$

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 initiapzed as per arguments provided.


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

$displaystyle left[egin{matrix}1 & 0 & 0\0 & 2 & 0\0 & 0 & 3end{matrix}
ight]$

The output for the above code is as follows −

[1 0 0 0 2 0 0 0 3]

All elements in zeros matrix are initiapzed to 0.


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

$displaystyle left[egin{matrix}0 & 0 & 0\0 & 0 & 0end{matrix}
ight]$

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[egin{matrix}1 & 1 & 1\1 & 1 & 1end{matrix}
ight]$

The output for the above code is as follows −

[1 1 1 1 1 1]

Advertisements