Pymathrix是一个用于创建矩阵对象和基本操作的python库。
pymathrix的Python项目详细描述
皮马提克斯
Pymathrix是一个用于创建矩阵对象和基本操作的python库。在
通过使用Pymatrix,您可以:
- 在python中创建矩阵并为其赋值
- 在命令提示符下查看矩阵对象值
- 对矩阵进行加法、减法和Hadamard积
- 转置矩阵
- 乘法矩阵(点积)
- 计算矩阵的行列式(目前只针对2x2和3x3矩阵)
安装
使用包管理器pip安装pymathrix
pipinstallpymathrix
使用
将库导入python代码:
^{pr2}$创建矩阵对象
>>>m=px.matrix(2,3)# this creates a matrix with 2 rows and 3 columns and initialize its values with zoros
打印矩阵
>>>print(m)|000||000|
将值存储到矩阵
>>>m.assign([[-2,5.0,9.2],[1,7.2,-6.4]])# storing 2 dimensional list (matrix with 2x3 dimension)>>>print(m)|-25.09.2||17.2-6.4|>>>m1=px.matrix(1,4)>>>m1.assign([2.5,-4.9,0.8,5.6])# storing 1 dimensional list (matrix with 1x3 dimension)>>>print(m1)|2.5-4.90.85.6|>>>m2=px.matrix(1,1)>>>m2.assign(-6.1)# storing a numerical value (matrix with 1x1 dimension)>>>print(m2)|-6.1|
打印指定的行或列
>>>m.print_row(1)# prints the first row(-2,5.0,9.2)>>>m.print_col(2)# prints the second column(5.0,7.2)
用随机值填充矩阵
>>>m.randomize(-10,10)# this stores random values between -10 and 10 in the matrix>>>print(m)|8.07.49.8||-0.48.46.2|
用指定值填充矩阵
>>>m.all_to(6)|666||666|
将一个矩阵的一行或一列存储到另一个矩阵中
>>>m1=px.matrix(3,4)>>>m1.randomize(-1,1)>>>print("Matrix m1:\n"+str(m1))>>>m2=m1.from_row(2)# storing the 2nd row as an individual matrix into m2>>>print("Matrix m2:\n"+str(m2))Matrixm1:|-0.60.00.3-0.5||0.7-0.80.40.2||-0.1-1.00.20.1|Matrixm2:|0.7-0.80.40.2|>>>m1=px.matrix(3,4)>>>m1.randomize(-1,1)>>>print("Matrix m1:\n"+str(m1))>>>m2=m1.from_col(3)# storing the 3rd column as an individual matrix into m2>>>print("Matrix m2:\n"+str(m2))Matrixm1:|0.20.70.70.4||0.3-0.9-0.20.0||-0.40.60.60.8|Matrixm2:|0.7||-0.2||0.6|
改变矩阵的维数
>>>m=matrix(6,3).randomize(0,10)# creating a matrix with 6 rows>>>m.set_rows(2)# changing the number of rows to 2>>>print(m)|8.51.94.8||3.01.64.1|>>>m=matrix(3,2).randomize(0,10)# creating a matrix with 2 columns>>>m.set_cols(5)# changing the number of columns to 5>>>print(m)|2.33.60.00.00.0||1.89.50.00.00.0||2.49.10.00.00.0|>>>m=matrix(2,3).randomize(0,10)# creating a 2x3 dimensional matrix>>>m.set_dim(3,4)# changing the dimension to 3x4>>>print(m)|6.25.78.70.0||4.94.61.60.0||0.00.00.00.0|
矩阵换位
>>>t=m.transpose()# this stores the transpose of the matrix 'm' into the matrix 't'
- 示例:
>>>m=px.matrix(2,3)>>>m.randomize(-1,1)>>>print("Matrix m:\n"+str(m))>>>print("Matrix m transposed:\n"+str(m.transpose()))Matrixm:|-0.3-0.9-0.9||-0.70.5-0.2|Matrixmtransposed:|-0.3-0.7||-0.90.5||-0.9-0.2|
将两个矩阵相加
>>>m3=px.matrix.add(m1,m2)# adding the matrices m1 and m2 together and storing the result in m3
- 示例:
>>>m1=px.matrix(2,3).randomize(-1,1)# creates the matrix object and randomize its values in one single line>>>m2=px.matrix(2,3).randomize(-1,1)# creates the matrix object and randomize its values in one single line>>>print("Matrix m1:\n"+str(m1))>>>print("Matrix m2:\n"+str(m2))>>>print("Matrix m1 + Matrix m2:\n"+str(px.matrix.add(m1,m2)))Matrixm1:|-0.1-0.5-0.6||-0.70.10.7|Matrixm2:|0.8-1.00.2||0.7-0.80.5|Matrixm1+Matrixm2:|0.7-1.5-0.4||0.0-0.71.2|
矩阵相减
>>>m3=px.matrix.sub(m1,m2)# subtracts the matrices m2 from m1 and stores the result in m3
- 示例:
>>>m1=px.matrix(2,3).randomize(-1,1)>>>m2=px.matrix(2,3).randomize(-1,1)>>>print("Matrix m1:\n"+str(m1))>>>print("Matrix m2:\n"+str(m2))>>>print("Matrix m1 - Matrix m2:\n"+str(px.matrix.sub(m1,m2)))Matrixm1:|-0.1-0.5-0.6||-0.70.10.7|Matrixm2:|0.8-1.00.2||0.7-0.80.5|Matrixm1-Matrixm2:|-0.90.5-0.8||-0.140.90.2|
两个矩阵的Hadamard积
>>>m3=px.matrix.hadamard_prod(m1,m2)# performs the hadamard product
- 示例:
>>>m1=px.matrix(2,3).randomize(-1,1)>>>m2=px.matrix(2,3).randomize(-1,1)>>>print("Matrix m1:\n"+str(m1))>>>print("Matrix m2:\n"+str(m2))>>>print("Matrix m1 * Matrix m2:\n"+str(px.matrix.hadamard_prod(m1,m2)))Matrixm1:|0.2-0.4-1.0||-0.20.0-0.1|Matrixm2:|-0.50.1-1.0||-0.10.50.0|Matrixm1*Matrixm2:|-0.1-0.041.0||0.020.0-0.0|
矩阵乘法(点积)
>>>m3=matrix.dot(m1,m2)# multiplying the matrix m1 by m2 and storing the result in m3
- 示例:
>>>m1=px.matrix(2,3).randomize(-1,1)>>>m2=px.matrix(3,2).randomize(-1,1)>>>print("Matrix m1:\n"+str(m1))>>>print("Matrix m2:\n"+str(m2))>>>print("Matrix m1 o Matrix m2:\n"+str(px.matrix.dot(m1,m2)))Matrixm1:|0.1-0.5-0.1||-0.50.80.1|Matrixm2:|0.90.4||0.8-1.0||-0.3-0.6|Matrixm1oMatrixm2:|-0.280.6||0.16-1.06|
矩阵行列式
^{pr21}$- 示例(2x2矩阵):
>>>m=px.matrix(2,2).randomize(-1,1)>>>print("Matrix m:\n"+str(m))>>>print("Det(m):",m.determinant())Matrixm:|-0.7-0.7||0.50.0|Det(m):0.35
- 示例(3x3):
>>>m=px.matrix(3,3).randomize(-1,1)>>>print("Matrix m:\n"+str(m))>>>print("Det(m):",m.determinant())Matrixm:|0.0-0.90.1||-0.70.30.1||-0.3-0.10.4|Det(m):-0.21
- 项目
标签: