在Matlab中表现为矩阵的numpy.matrix的子类。
mymat的Python项目详细描述
简介
这是我关于矩阵运算的模块。它模仿了Matlab语法。 如果你喜欢Matlab和Python,那么这是你的选择。 这将是一个很好的经验操作矩阵类的Matlab。
糖:可以用一个索引来表示矩阵中的元素。(参见以下示例)
组织
MyMat:
- mymat
- matshow
- matdemo
- test_mat
- linalg
- denoise
功能
- 当前版本:
- 修理一些虫子
- 用tkinter演示高斯消去法
- 定义线性评价类
>>> import mymat.matdemo >>> mymat.matdemo.main()
- 主要功能(>;0.1.x):
- mymat,pymat现在是matbase的子类
- 改进一些基本方法,修复一些错误
- 索引可以按保留顺序排列,例如[3:1:-1,1]
- 参见mat_demo中的数值实验(改进)
- 添加更多方法(下面介绍)并且a[is,js]=[]现在是合法的
- 修复一些错误,使代码更健壮
finally, another improvement is that when create a matrix, we use following codes to set dtype (may temporarily)
- 如果是data.dtype!=np.complex128和data.dtype!=对象:
- kwargs.setdefault('dtype',np.float64)
- 主要功能(0.0.x):
- 引入运算符和&;来连接矩阵
- 在setitem中,在update方法的帮助下,允许索引超出matlab的范围(见下文)
- 更正delete的代码,改进多种方法的代码 利用Loop/ExpM(Tylor近似)方法计算P(A)和E^{a
- 添加to tex方法,将矩阵转换为其tex形式
- mymat的默认数据类型是float64(复杂时为complex128),但整数矩阵是int32。因此,如果需要,不要忘记转换数据类型。但这是暂时的。
语法
基本语法
导入:
>>> import mymat >>> A = mymat.MyMat([]) # use import mymat.pymat to import PyMat
运算符(python left,matlab right):
A*B := A*B (B*A := B*A) A/B := A/B == A*B.I (B/A := B/A == B*A.I) A ** B := A .* B (B ** A := B .* A) A//B := A./B (B//A := B./A) A<<B := A.^B (B<<A := B.^A) A^B := A^B A|B := [A,B] A&B :=[A;B]
我们使用matlab类型索引,而不是python类型索引,例如:
>>> A=TestMat(5) [1, 2, 3, 4, 5; 6, 7, 8, 9, 10; 11, 12, 13, 14, 15; 16, 17, 18, 19, 20; 21, 22, 23, 24, 25]: M(5 X 5) >>> A[[3,4,7,10]] # with single index as in matlab [11, 16, 7, 22]: M(1 X 4) >>> A[[2,3],1:4] [6, 7, 8, 9; 11, 12, 13, 14]: M(2 X 4) >>> A[[1,3],[2,4]] # use A.get(([1,3],[2,4])) to get matrix([2, 14]) [2, 4; 12, 14]: M(2 X 2) >>> A[3:1:-1,:] # reversing order [11, 12, 13, 14, 15; 6, 7, 8, 9, 10; 1, 2, 3, 4, 5]: M(3 X 5)
使用delete方法删除某些行或列,如在Matlab中所示:
>>> A=H(7) >>> B=A.delete([1,3],slice(3)) # <=> B=A.copy(); B[[1,3],[1,2,3]]=[] >>> B.shape (5, 4)
线性方程:
>>> le = LinearEquation(A, b) >>> print(le.totex()) # print tex of a linear equation
演示和可视化
演示和数值实验:
>>> import mymat >>> import mymat.matdemo # see Gauss elimination >>> A=mymat.MyMat('1,1,1,6;0,4,-1,5;2,-2,1,1') # or A=mymat.MyMat('1&1&1&6\\0&4&-1&5\\2&-2&1&1') just copying the latex codes >>> mymat.matdemo.guassDemo(A) # show the process of getting the echelon form of A >>> mymat.matdemo.denoiseDemo([n:noised signal(row vector)]) # see a denoising experiment
绘制矩阵:
>>> import mymat.matshow # draw a matrix on axes(require matplotlib) >>> ms = mymat.matshow.MatrixShow(A); ms.show()
方法和功能
其他方法:
__call__: A(ind) == A[ind] delete(ind1=row, ind2=col): delete row-rows and col-columns proj(ind1=row, ind2=col): =0 out of A[row, col], for example A.proj(ind1=COLON, ind2=[2,3])=[2,3] where A=[1,2,3,4] repmat((ind1, ind2)|ind): repeat matrix as in Matlab (like tile) just: cut matrix to a certain size, and supplement zeros if the size is too large. cat: as concatenate equal: (A == B == C).all() apply: A.apply(lambda x:x+1) == A+1 plus: A.plus(n) == A + nI robinson: A.robinson(j, x) == A[j<-x], namely A[:,j]=x used in Cramer rule echelon: get the echelon form (include the corresponding column indexes) tril, triu, diag are similar to matlab row(col)_transform1/2/3: elementary row (column) transforms (Gauss tranforms) comat: get the co-matrix (similar with delete) cofactor: A.cofactor(i,j)=Aij get the cofactor based on comat rho: the spectral radius totex: to tex form of matrix tolineq: to tex form of linear equations wrt augment matrix argmin, argmax
类方法:
MyMat.zeros, MyMat.ones, MyMat.random, MyMat.randint, MyMat.eye
函数和变量:
ind2ind: the most essential function times: translate single index to double index compind: get complementary index (called in proj) COLON: slice(None), COLON2=(COLON,COLON)
矩阵:
FM: Fourier matrix FIM: Fourier inverse matrix FUM: Fourier unitary matrix Ho: Horsehold matrix Ref: reflection matrix H: Hilbert matrix Elm1,Elm2,Elm3: elementary matrices (3 types)