python矩阵运算程序_用于矩阵运算的Python程序

这篇博客介绍了如何使用numpy库在Python中进行矩阵运算,包括不同类型的矩阵操作,并给出了具体的示例和输出结果。
摘要由CSDN通过智能技术生成

python矩阵运算程序

There are following matrix operations, that we can implement with the numpy matrix.

我们可以使用numpy矩阵实现以下矩阵运算

Operations that we are performing here, (Here, x and y are the matrices)...

我们在此处执行的操作(此处x和y是矩阵)...

OperationsFunctionOperator
Adding matrix elementsadd(x,y)x+y
Subtracting matrix elementssubtract(x,y)x-y
Multiplying matrix elementsmultiply(x,y)x*y
Dividing matrix elementsdivide(x,y)x/y
Product of the matricesdot(x,y)---
Square root of the matrix elementssqrt(x)---
运作方式 功能 操作员
添加矩阵元素 加(x,y) x + y
减去矩阵元素 减去(x,y) y
乘法矩阵元素 乘(x,y) x * y
划分矩阵元素 除(x,y) x / y
矩阵的乘积 点(x,y) ---
矩阵元素的平方根 sqrt(x) ---

Example:

例:

# Python program for matrix operations

# importing numpy
import numpy as np

mat1 = np.array([[10,20,30],[40,50,60],[70,80,90]])
mat2 = np.array([[1,2,3],[4,5,6],[7,8,9]])

# adding matrices
print("mat1+mat2...")
print(mat1+mat2)
print("np.add(mat1,mat2)...")
print(np.add(mat1,mat2))
print() # prints newline

# subtracting  matrices
print("mat1-mat2...")
print(mat1-mat2)
print("np.subtract(mat1,mat2)...")
print(np.subtract(mat1,mat2))
print() # prints newline

# dividing  matrices
print("mat1/mat2...")
print(mat1/mat2)
print("np.divide(mat1,mat2)...")
print(np.divide(mat1,mat2))
print() # prints newline

# multiplying  matrices
print("mat1*mat2...")
print(mat1*mat2)
print("np.multiply(mat1,mat2)...")
print(np.multiply(mat1,mat2))
print() # prints newline

# producting matrices
print("np.dot(mat1,mat2)...")
print(np.dot(mat1,mat2))
print() # prints newline

# Square root of matrix elements
print("np.sqrt(mat1)...")
print(np.sqrt(mat1))
print() # prints newline

# Square root of matrix elements
print("np.sqrt(mat2)...")
print(np.sqrt(mat2))
print() # prints newline

Output

输出量

mat1+mat2...
[[11 22 33]
 [44 55 66]
 [77 88 99]]
np.add(mat1,mat2)...
[[11 22 33]
mat1+mat2...
[[11 22 33]
 [44 55 66]
 [77 88 99]]
mat1+mat2...
[[11 22 33]
 [44 55 66]
 [77 88 99]]
np.add(mat1,mat2)...
[[11 22 33]
 [44 55 66]
 [77 88 99]]

mat1-mat2...
[[ 9 18 27]
 [36 45 54]
 [63 72 81]]
np.subtract(mat1,mat2)...
[[ 9 18 27]
 [36 45 54]
mat1+mat2...
[[11 22 33]
 [44 55 66]
 [77 88 99]]
np.add(mat1,mat2)...
[[11 22 33]
 [44 55 66]
 [77 88 99]]

mat1-mat2...
[[ 9 18 27]
 [36 45 54]
 [63 72 81]]
np.subtract(mat1,mat2)...
[[ 9 18 27]
 [36 45 54]
 [63 72 81]]

mat1/mat2...
[[10. 10. 10.]
 [10. 10. 10.]
 [10. 10. 10.]]
np.divide(mat1,mat2)...
[[10. 10. 10.]
 [10. 10. 10.]
 [10. 10. 10.]]

mat1*mat2...
[[ 10  40  90]
 [160 250 360]
 [490 640 810]]
np.multiply(mat1,mat2)...
[[ 10  40  90]
 [160 250 360]
 [490 640 810]]

np.dot(mat1,mat2)...
[[ 300  360  420]
 [ 660  810  960]
 [1020 1260 1500]]

np.sqrt(mat1)...
[[3.16227766 4.47213595 5.47722558]
 [6.32455532 7.07106781 7.74596669]
 [8.36660027 8.94427191 9.48683298]]

np.sqrt(mat2)...
[[1.         1.41421356 1.73205081]
 [2.         2.23606798 2.44948974]
 [2.64575131 2.82842712 3.        ]]


翻译自: https://www.includehelp.com/python/program-for-matrix-operations.aspx

python矩阵运算程序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值