1. Matrix multiplication
In python, matrix multiplication can be implemented using numpy or sympy. Taking numpy implementation as an example, the code is as follows:
import numpy as np a01=(10,size=(4,3)) print(a01) array([[6, 4, 9], [1, 4, 8], [8, 0, 0], [5, 6, 8]]) a02=(10,size=(3,4)) print(a02) array([[5, 7, 7, 9], [6, 4, 6, 2], [5, 2, 8, 2]]) a03=(a01,a02) print(a03) array([[ 99, 76, 138, 80], [ 69, 39, 95, 33], [ 40, 56, 56, 72], [101, 75, 135, 73]])
The product can also be converted into fractional form, the example code is as follows:
import numpy as np from fractions import Fraction a01=(10,size=(4,3)).astype(float) a01[0,0]=0.5 print(a01) array([[0.5, 4. , 3. ], [0. , 4. , 4. ], [4. , 3. , 6. ], [0. , 2. , 6. ]]) a02=(10,size=(3,4)) print(a02) array([[5, 7, 7, 9], [6, 4, 6, 2], [5, 2, 8, 2]]) a03=(a01,a02) print(a03)#a03 is the decimal formarray([[41.5, 25.5, 51.5, 18.5], [44. , 24. , 56. , 16. ], [68. , 52. , 94. , 54. ], [42. , 20. , 60. , 16. ]]) a04=(lambda x: Fraction.from_float(x).limit_denominator())(a03) print(a04)#a04 is the form of fractionarray([[Fraction(83, 2), Fraction(51, 2), Fraction(103, 2), Fraction(37, 2)], [Fraction(44, 1), Fraction(24, 1), Fraction(56, 1), Fraction(16, 1)], [Fraction(68, 1), Fraction(52, 1), Fraction(94, 1), Fraction(54, 1)], [Fraction(42, 1), Fraction(20, 1), Fraction(60, 1), Fraction(16, 1)]], dtype=object)
2. Matrix inversion
Use numpy and sympy to implement it respectively, the code is as follows:
import numpy as np from fractions import Fraction #numpy implementationa01=(5,size=(2,2)) print(a01) array([[4, 0], [3, 4]]) a02 = ((float))#Inverse Matrixprint(a02) array([[ 0.25 , 0. ], [-0.1875, 0.25 ]]) a03=(lambda x: Fraction.from_float(x).limit_denominator())(a02) print(a03)#Fraction Formarray([[Fraction(1, 4), Fraction(0, 1)], [Fraction(-3, 16), Fraction(1, 4)]], dtype=object) #sympy implementationa04=Matrix([[4,0],[3,4]]) print(a04) Matrix([ [4, 0], [3, 4]]) print(()) Matrix([ [ 1/4, 0], [-3/16, 1/4]])
From the analysis of the solution results, sympy will automatically give the simple form of the result. If it is a score, it will be represented by a score, which is better than numpy in terms of display effect.
3. Solving matrix eigenvalues and eigenvectors
Use numpy and sympy to implement it respectively, the code is as follows:
import numpy as np from sympy import Matrix #numpy implementationa01=([[-1,2,0],[0,3,0],[2,1,-1]]) print(a01) array([[-1, 2, 0], [ 0, 3, 0], [ 2, 1, -1]]) eigenvalue, featurevector = (a01) print(eigenvalue) array([-1., -1., 3.]) print(featurevector) array([[ 0.00000000e+00, 1.11022302e-16, 4.08248290e-01], [ 0.00000000e+00, 0.00000000e+00, 8.16496581e-01], [ 1.00000000e+00, -1.00000000e+00, 4.08248290e-01]]) #sympy implementationa02=Matrix([[-1,2,0],[0,3,0],[2,1,-1]]) print(a02) Matrix([ [-1, 2, 0], [ 0, 3, 0], [ 2, 1, -1]]) print(()) {3: 1, -1: 2}#3: 1 means eigenvalue is 3, number is 1, -1: 2 means eigenvalue is -1, number is 2print(()) [(-1, 2, [Matrix([ [0], [0], [1]])]), (3, 1, [Matrix([ [1], [2], [1]])])]
Judging from the solution results, the solution result of sympy is more intuitive than the solution result of numpy.
4. Matrix is about standard type and transformation matrix solution
When using numpy to solve the matrix and the conversion matrix, it is more cumbersome to solve the matrix, so using sympy to solve the matrix. The code is as follows:
from sympy import Matrix a01=Matrix([[-1,2,0],[0,3,0],[2,1,-1]]) print(a01) Matrix([ [-1, 2, 0], [ 0, 3, 0], [ 2, 1, -1]]) p_mat,j_mat=a01.jordan_form()#p_mat is the conversion matrix, j_mat is approximately the standard typeprint(p_mat) Matrix([ [0, 1, 1], [0, 0, 2], [2, 0, 1]]) print(j_mat) Matrix([ [-1, 1, 0], [ 0, -1, 0], [ 0, 0, 3]])
Note that sympy solves about when the standard type 1 is placed in the upper triangle, and some mathematics textbooks place 1 in the lower triangle.
5. Matrix singular value decomposition
sympy does not directly support the solution of matrix singular values, so the solution is used using numpy, the code is as follows:
import numpy as np a02=([[4,0],[3,0],[0,0]]) print(a02) array([[4, 0], [3, 0], [0, 0]]) U, S, Vt = (a02) print(U)#Unital matrix composed of left singular vectorsarray([[-0.8, -0.6, 0. ], [-0.6, 0.8, 0. ], [ 0. , 0. , 1. ]]) print(S)#Singular Valuearray([5., 0.]) print(Vt)#Unital matrix composed of right singular vectorsarray([[-1., -0.], [ 0., 1.]])
A unitary matrix composed of singular vectors is not unique.
6. Solve the matrix equation system
Use sympy to solve the problem, the code is as follows:
from sympy import Matrix, symbols, linsolve A = Matrix([[2,3,1],[4,2,3], [7,1,-1]]) print(A) Matrix([ [2, 3, 1], [4, 2, 3], [7, 1, -1]]) B = Matrix([[4],[17],[1]]) print(B) Matrix([ [ 4], [17], [ 1]]) x, y, z = symbols('x y z') result=linsolve((A,B),x,y,z) print(result) {(1, -1, 5)} A = Matrix([[1,1,1,1],[4,3,5,-1], [2,1,3,-3]]) print(A) Matrix([ [1, 1, 1, 1], [4, 3, 5, -1], [2, 1, 3, -3]]) B = Matrix([[-1],[-1],[1]]) print(B) Matrix([ [-1], [-1], [ 1]]) x, y, z = symbols('x y z') result=linsolve((A,B),x,y,z) print(result) {(2 - 2*z, z - 3, z)}
As can be seen from the example code, sympy can solve the system of equations when the parameter matrix is not singular, or the system of equations when the parameter matrix is singular, and give a general solution.
Summarize
This is the end of this article about using python for matrix operations. For more related python matrix operations, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!