在python中,矩阵可以实现为2D 列表 或二维阵列。从后者形成矩阵,提供了在矩阵中执行各种操作的附加功能。这些行动和 大堆 是“模块中的定义” 努比 “.
矩阵运算:
1.添加():- 此功能用于执行以下操作: 元素矩阵加法 .
2.减法():- 此功能用于执行以下操作: 元素矩阵减法 .
3.除法():- 此功能用于执行以下操作: 元素矩阵除法 .
# Python code to demonstrate matrix operations # add(), subtract() and divide() # importing numpy for matrix operations import numpy # initializing matrices x = numpy.array([[ 1 , 2 ], [ 4 , 5 ]]) y = numpy.array([[ 7 , 8 ], [ 9 , 10 ]]) # using add() to add matrices print ( "The element wise addition of matrix is : " ) print (numpy.add(x,y)) # using subtract() to subtract matrices print ( "The element wise subtraction of matrix is : " ) print (numpy.subtract(x,y)) # using divide() to divide matrices print ( "The element wise division of matrix is : " ) print (numpy.divide(x,y)) |
输出:
The element wise addition of matrix is : [[ 8 10] [13 15]] The element wise subtraction of matrix is : [[-6 -6] [-5 -5]] The element wise division of matrix is : [[ 0.14285714 0.25 ] [ 0.44444444 0.5 ]]
4.乘法():- 此功能用于执行以下操作: 元素矩阵乘法 .
5.点():- 此函数用于计算 矩阵乘法,而不是元素乘法 .
# Python code to demonstrate matrix operations # multiply() and dot() # importing numpy for matrix operations import numpy # initializing matrices x = numpy.array([[ 1 , 2 ], [ 4 , 5 ]]) y = numpy.array([[ 7 , 8 ], [ 9 , 10 ]]) # using multiply() to multiply matrices element wise print ( "The element wise multiplication of matrix is : " ) print (numpy.multiply(x,y)) # using dot() to multiply matrices print ( "The product of matrices is : " ) print (numpy.dot(x,y)) |
输出:
The element wise multiplication of matrix is : [[ 7 16] [36 50]] The product of matrices is : [[25 28] [73 82]]
6.sqrt():- 此函数用于计算 每个元素的平方根 《黑客帝国》。
7.总和(x轴):- 此函数用于 将矩阵中的所有元素相加 .可选的“axis”参数计算 轴为0时的列和 和 行和如果 轴 是1 .
8.“T”:- 这个论点被用来 转置 指定的矩阵。
# Python code to demonstrate matrix operations # sqrt(), sum() and "T" # importing numpy for matrix operations import numpy # initializing matrices x = numpy.array([[ 1 , 2 ], [ 4 , 5 ]]) y = numpy.array([[ 7 , 8 ], [ 9 , 10 ]]) # using sqrt() to print the square root of matrix print ( "The element wise square root is : " ) print (numpy.sqrt(x)) # using sum() to print summation of all elements of matrix print ( "The summation of all matrix element is : " ) print (numpy. sum (y)) # using sum(axis=0) to print summation of all columns of matrix print ( "The column wise summation of all matrix is : " ) print (numpy. sum (y,axis = 0 )) # using sum(axis=1) to print summation of all columns of matrix print ( "The row wise summation of all matrix is : " ) print (numpy. sum (y,axis = 1 )) # using "T" to transpose the matrix print ( "The transpose of given matrix is : " ) print (x.T) |
输出:
The element wise square root is : [[ 1. 1.41421356] [ 2. 2.23606798]] The summation of all matrix element is : 34 The column wise summation of all matrix is : [16 18] The row wise summation of all matrix is : [15 19] The transpose of given matrix is : [[1 4] [2 5]]
本文由 曼吉特·辛格100 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。