Inverse matrix and elementary transformation of matrix based on python

Applicable environment: Python 3.11 + sympy library + VS code + Jupyter notebook

This article has imported the sympy library by default, with the alias sp, and the specific command to set the cell to multi-line output is:

import sympy as sp
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

1. Inverse matrix and adjoint matrix

To facilitate subsequent calculations, we first construct a first-order digital square matrix,

A=sp.Matrix([[1,2,3],[3,4,5],[2,1,4]])

1. Inverse matrix

We can calculate its determinant in the following two ways.

sp.Matrix.inv(A) #The first way to calculate the inverse matrix is to call inv as a function;
A.inv() #The second method of calculating the inverse matrix uses inv as a property of matrix A;

The results of its operation are:

The same method can be used to calculate the character matrix, but because the inverse matrix form of the character matrix is too complicated, it is not recommended to calculate it this way. The following only takes the first-order matrix as an example:

B=sp.Matrix(2,2,sp.symbols('b(1:3)(1:3)')) # Construct the matrix; ``B.inv() # Find the inverse of the matrix ;

The result of its operation is:

2. Adjoint matrix

The Sympy library also provides methods for directly calculating adjoint matrices.

A.adjugate() #Calculate the adjoint matrix of matrix A; ``B.adjugate() #Calculate the adjoint matrix of matrix B;

The result of its operation is:

2. Elementary transformation of matrices

In fact, in the previous chapter, we used the elementary transformation of matrices to study the elementary transformation of determinants. Therefore, similarly, we can also perform elementary transformations between matrices. First, we still define a matrix

AA=sp.Matrix([[1,1,-1,1,1],[2,0,-4,1,0],[2,-1,-5,-3,6] ,[3,4,-2,4,3]])

1. Elementary row transformation

The three elementary row transformation methods are as follows:

1) Swap two lines
AA.elementary_row_op('n<->m',0,2) #Exchange rows 1 and 3 of the determinant;
2) Times all elements in a certain row
AA.elementary_row_op('n->kn',2,5) #The 3rd row becomes 5 times;
3) The multiples of all elements in a row are added to the corresponding elements in another row
AA.elementary_row_op('n->n + km',0,-2,3) #(-2) multiplication of all elements in row 4 is added to row 1;

The results of the above three programs are as follows:

It should be noted that since the above three commands are all related transformations of the matrix, the three transformations are independent of each other and are not accumulated.

2. Elementary column transformation

The three elementary column transformation methods are as follows:

1) Swap two columns
AA.elementary_col_op('n<->m',0,2) #Exchange columns 1 and 3 of the determinant;
2) Times all elements in a certain column
AA.elementary_col_op('n->kn',2,5) #Column 3 becomes 5 times;
3) The multiples of all elements in a column are added to the corresponding elements in another column
AA.elementary_col_op('n->n + km',0,-2,3) #(-2) multiplication of all elements in column 4 is added to column 1;

The results of the above three programs are as follows:

As mentioned before, since the above three commands are related transformations of the matrix, the three transformations are independent of each other and are not accumulated.

The reason why I did not superimpose the above changes is because the sympy library already provides methods to change the matrix into a ladder matrix, a row simplest matrix and a standard matrix. We can call it directly without making superimposed changes.

2. (row) echelon matrix

Guess what the change command for the echelon matrix should be?

“echelon_form”, simple and crude, right? Of course, due to the non-uniqueness of the (row) echelon matrix, the returned matrix is only one of them.

AA.echelon_form() #Find the (row) echelon matrix of matrix AA;

The running results are as follows:

In addition, you can also use: is_echelon to determine whether a matrix is an echelon matrix.

AA.is_echelon()``(AA.echlon_form()).is_echlon()

The running results are as follows:

3. Row simplest form matrix

The command to get the row simplest form of the matrix is: rref(); but it should be noted that rref, the return value is two parts: the first part is the row simplest form of the matrix we need; the second part is the “first non-zero element” Column” list;

AA.rref() #Find the simplest row matrix of matrix AA and the column of the first non-zero element;

The running results are as follows:

Of course, we can use the following method to get only the simplest row matrix we need now:

AA.rref()[0] #Find the simplest row matrix of matrix AA

Its return value is:

4. Standard matrix

Since the standard form of a matrix can be directly determined by three numbers, it is generally believed that there are many ways to obtain the standard form of a matrix without using any direct commands. Therefore, sympy does not provide corresponding commands. However, if we are interested, we can also use existing commands to obtain the standard form of the matrix.

(((AA.rref()[0]).T).rref()[0]).T

do you understand? That is to first obtain the simplest form of the row, then obtain the simplest form of the row after transposition, and finally transpose.

Of course, if you have already learned the meaning of matrix rank, we can also directly obtain the standard form of the matrix in the following way;

AF=sp.zeros(AA.rows,AA.cols)``AF[:AA.rank(),:AA.rank()]=sp.eye(AA.rank())``AF

The results of the above two programs are:

In addition, you can also download the ipynb file of this article from Baidu Cloud Disk.

Link: https://pan.baidu.com/s/1YUUubCWLow92IE1KBrqM9Q?pwd=qh0e Extraction code: qh0e


———————————END——————- ——–

Digression

Interested friends will receive a complete set of Python learning materials, including interview questions, resume information, etc. See below for details.

CSDN gift package:The most complete “Python learning materials” on the Internet are given away for free! (Safe link, click with confidence)

1. Python learning routes in all directions

The technical points in all directions of Python have been compiled to form a summary of knowledge points in various fields. Its usefulness is that you can find corresponding learning resources according to the following knowledge points to ensure that you learn more comprehensively.

img
img

2. Python essential development tools

The tools have been organized for you, and you can get started directly after installation! img

3. Latest Python study notes

When I learn a certain basic and have my own understanding ability, I will read some books or handwritten notes compiled by my seniors. These notes record their understanding of some technical points in detail. These understandings are relatively unique and can be learned. to a different way of thinking.

img

4. Python video collection

Watch a comprehensive zero-based learning video. Watching videos is the fastest and most effective way to learn. It is easy to get started by following the teacher’s ideas in the video, from basic to in-depth.

img

5. Practical cases

What you learn on paper is ultimately shallow. You must learn to type along with the video and practice it in order to apply what you have learned into practice. At this time, you can learn from some practical cases.

img

6. Interview Guide

Resume template

CSDN gift package:The most complete “Python learning materials” on the Internet are given away for free! (Safe link, click with confidence)

If there is any infringement, please contact us for deletion.