Creating Matrices
>>> from sympy.interactive.printing import init_printing >>> init_printing(use_unicode=False, wrap_line=False, no_global=True) >>> from sympy.matrices import Matrix, eye, zeros, ones, diag, GramSchmidt #构造完整矩阵 >>> M = Matrix([[1,0,0], [0,0,0]]); M [1 0 0] [ ] [0 0 0] #为矩阵添加行 >>> Matrix([M, (0, 0, -1)]) [1 0 0 ] [ ] [0 0 0 ] [ ] [0 0 -1] #构造行矩阵 >>> Matrix([[1, 2, 3]]) [1 2 3] #构造列矩阵 >>> Matrix([1, 2, 3]) [1] [ ] [2] [ ] [3] #用列表构造指定形状矩阵 >>> Matrix(2, 3, [1, 2, 3, 4, 5, 6]) [1 2 3] [ ] [4 5 6] #用函数构造矩阵 >>> def f(i,j): ... if i == j: ... return 1 ... else: ... return 0 ... >>> Matrix(4, 4, f) [1 0 0 0] [ ] [0 1 0 0] [ ] [0 0 1 0] [ ] [0 0 0 1] #lambda函数构造矩阵 >>> Matrix(3, 4, lambda i,j: 1 - (i+j) % 2) [1 0 1 0] [ ] [0 1 0 1] [ ] [1 0 1 0] #构造单位矩阵 >>> eye(4) [1 0 0 0] [ ] [0 1 0 0] [ ] [0 0 1 0] [ ] [0 0 0 1] #构造全零方阵 >>> zeros(2) [0 0] [ ] [0 0] #构造指定形状的矩阵 >>> zeros(2, 5) [0 0 0 0 0] [ ] [0 0 0 0 0] #构造全一矩阵 >>> ones(3) [1 1 1] [ ] [1 1 1] [ ] [1 1 1] #构造指定形状的全一矩阵 >>> ones(1, 3) [1 1 1] #构造对角矩阵 >>> diag(1, Matrix([[1, 2], [3, 4]])) [1 0 0] [ ] [0 1 2] [ ] [0 3 4]
Basic Manipulation
>>> M = Matrix(2, 3, [1, 2, 3, 4, 5, 6]) #当成一维数组访问 >>> M[4] 5 #通过具体下标访问 >>> M[1, 2] 6 >>> M[0, 0] 1 >>> M[1, 1] 5 #切片操作,第一个参数为行数,第二个参数为列数,取不到较大值 >>> M[0:2, 0:2] [1 2] [ ] [4 5] >>> M[2:2, 2] [] >>> M[:, 2] [3] [ ] [6] >>> M[:1, 2] [3] #无法访问不存在的数据,除非切片(返回空值) >>> M[:, 10] # the 10-th column (not there) Traceback (most recent call last): ... IndexError: Index out of range: a[[0, 10]] >>> M[:, 10:11] # the 10-th column (if there) [] >>> M[:, :10] # all columns up to the 10-th [1 2 3] [ ] [4 5 6] #切片方式访问空值 >>> Matrix(0, 3, [])[:, 1] [] #切片复制了一个新的矩阵,直接赋值只是重复引用旧的矩阵 >>> M2 = M[:, :] >>> M2[0, 0] = 100 >>> M[0, 0] == 100 False >>> M2 = M >>> M2[0, 0] = 100 >>> M[0, 0] == 100 True #对切片赋值 >>> M = Matrix(([1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16])) >>> M[2:,2:] = Matrix(2,2,lambda i,j: 0) >>> M [1 2 3 4] [ ] [5 6 7 8] [ ] [9 10 0 0] [ ] [13 14 0 0] #矩阵的加减乘乘方操作 >>> M = Matrix(([1,2,3],[4,5,6],[7,8,9])) >>> M - M [0 0 0] [ ] [0 0 0] [ ] [0 0 0] >>> M + M [2 4 6 ] [ ] [8 10 12] [ ] [14 16 18] >>> M * M [30 36 42 ] [ ] [66 81 96 ] [ ] [102 126 150] >>> M2 = Matrix(3,1,[1,5,0]) >>> M*M2 [11] [ ] [29] [ ] [47] >>> M**2 [30 36 42 ] [ ] [66 81 96 ] [ ] [102 126 150] #删除矩阵的行列 >>> M.row_del(0) >>> M [4 5 6] [ ] [7 8 9] >>> M.col_del(1) >>> M [4 6] [ ] [7 9] >>> v1 = Matrix([1,2,3]) >>> v2 = Matrix([4,5,6]) #向量积 >>> v3 = v1.cross(v2) #标量积 >>> v1.dot(v2) 32 >>> v2.dot(v3) 0 >>> v1.dot(v3) 0 #增加行 >>> M1 = eye(3) >>> M2 = zeros(3, 4) >>> M1.row_join(M2) [1 0 0 0 0 0 0] [ ] [0 1 0 0 0 0 0] [ ] [0 0 1 0 0 0 0] >>> M3 = zeros(4, 3) #增加列 >>> M1.col_join(M3) [1 0 0] [ ] [0 1 0] [ ] [0 0 1] [ ] [0 0 0] [ ] [0 0 0] [ ] [0 0 0] [ ] [0 0 0]
Operations on entries
#常数乘矩阵 >>> M = eye(3) >>> 2*M [2 0 0] [ ] [0 2 0] [ ] [0 0 2] >>> 3*M [3 0 0] [ ] [0 3 0] [ ] [0 0 3] #对矩阵元素应用函数 >>> f = lambda x: 2*x >>> eye(3).applyfunc(f) [2 0 0] [ ] [0 2 0] [ ] [0 0 2] #subs对矩阵元素进行替换 >>> from sympy import Symbol >>> x = Symbol('x') >>> M = eye(3) * x >>> M [x 0 0] [ ] [0 x 0] [ ] [0 0 x] >>> M.subs(x, 4) [4 0 0] [ ] [0 4 0] [ ] [0 0 4] >>> y = Symbol('y') >>> M.subs(x, y) [y 0 0] [ ] [0 y 0] [ ] [0 0 y]
Linear algebra
#计算矩阵行列式 >>> M = Matrix(( [1, 2, 3], [3, 6, 2], [2, 0, 1] )) >>> M.det() -28 >>> M2 = eye(3) >>> M2.det() 1 >>> M3 = Matrix(( [1, 0, 0], [1, 0, 0], [1, 0, 0] )) >>> M3.det() 0 #矩阵求逆 >>> M.inv(method="LU") [-3/14 1/14 1/2 ] [ ] [-1/28 5/28 -1/4] [ ] [ 3/7 -1/7 0 ] >>> M * M.inv(method="LU") [1 0 0] [ ] [0 1 0] [ ] [0 0 1] #LUsolve解方程 >>> A = Matrix([ [2, 3, 5], [3, 6, 2], [8, 3, 6] ]) >>> x = Matrix(3,1,[3,7,5]) >>> b = A*x >>> soln = A.LUsolve(b) >>> soln [3] [ ] [7] [ ] [5]
sympy.matrices.dense.randMatrix(r, c=None, min=0, max=99, seed=None, symmetric=False, percent=100, prng=None)
>>> from sympy.matrices import randMatrix >>> randMatrix(3) [25, 45, 27] [44, 54, 9] [23, 96, 46] >>> randMatrix(3, 2) [87, 29] [23, 37] [90, 26] >>> randMatrix(3, 3, 0, 2) [0, 2, 0] [2, 0, 1] [0, 0, 1] >>> randMatrix(3, symmetric=True) [85, 26, 29] [26, 71, 43] [29, 43, 57] >>> A = randMatrix(3, seed=1) >>> B = randMatrix(3, seed=2) >>> A == B False >>> A == randMatrix(3, seed=1) True >>> randMatrix(3, symmetric=True, percent=50) [0, 68, 43] [0, 68, 0] [0, 91, 34]