在Python中进行矩阵和线性代数计算

PHPz
PHPz 转载
2023-08-20 17:41:30 529浏览

在Python中进行矩阵和线性代数计算

在本文中,我们将学习如何使用Python进行矩阵和线性代数计算,例如矩阵乘法、求行列式、解线性方程等。

从NumPy库中可以使用一个矩阵对象来实现。在进行计算时,矩阵与数组对象相对可比。

线性代数是一个庞大的主题,超出了本文的范围。

然而,如果你需要操作矩阵和向量,NumPy是一个很好的起点。

使用的方法

  • 使用Numpy找到矩阵的转置

  • 使用Numpy找到矩阵的逆

  • 矩阵与向量相乘

  • 使用numpy.linalg子包获取矩阵的行列式

  • 使用numpy.linalg找到特征值

  • 使用numpy.linalg解决方程

方法1:使用Numpy找到矩阵的转置

numpy.matrix.T 属性 − 返回给定矩阵的转置。

Example

的中文翻译为:

示例

以下程序使用 numpy.matrix.T 属性返回矩阵的转置 −

# importing NumPy module
import numpy as np

# input matrix
inputMatrix = np.matrix([[6, 1, 5], [2, 0, 8], [1, 4, 3]])
# printing the input matrix
print("Input Matrix:\n", inputMatrix)

# printing the transpose of an input matrix
# by applying the .T attribute of the NumPy matrix of the numpy Module
print("Transpose of an input matrix\n", inputMatrix.T)

输出

在执行时,上述程序将生成以下输出 -

Input Matrix:
 [[6 1 5]
 [2 0 8]
 [1 4 3]]
Transpose of an input matrix
 [[6 2 1]
 [1 0 4]
 [5 8 3]]

方法2:使用Numpy找到矩阵的逆

numpy.matrix.I属性 - 返回给定矩阵的逆矩阵。

Example

的中文翻译为:

示例

以下程序使用 numpy.matrix.I 属性返回矩阵的逆矩阵 −

# importing NumPy module 
import numpy as np

# input matrix 
inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]])
# printing the input matrix
print("Input Matrix:\n", inputMatrix)

# printing the inverse of an input matrix 
# by applying the .I attribute of the NumPy matrix of the numpy Module
print("Inverse of an input matrix:\n", inputMatrix.I)

输出

在执行时,上述程序将生成以下输出 -

Input Matrix:
 [[6 1 5]
 [2 0 8]
 [1 4 3]]
Inverse of an input matrix:
 [[ 0.21333333 -0.11333333 -0.05333333]
 [-0.01333333 -0.08666667  0.25333333]
 [-0.05333333  0.15333333  0.01333333]]

方法三:矩阵与向量相乘

Example

的中文翻译为:

示例

以下程序使用*运算符返回输入矩阵和向量的乘积 -

# importing numpy module 
import numpy as np
 
# input matrix 
inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]])
# printing the input matrix
print("Input Matrix:\n", inputMatrix)

# creating a vector using numpy.matrix() function 
inputVector = np.matrix([[1],[3],[5]])

# printing the multiplication of the input matrix and vector 
print("Multiplication of input matrix and vector:\n", inputMatrix*inputVector)

输出

在执行时,上述程序将生成以下输出 -

Input Matrix:
 [[6 1 5]
 [2 0 8]
 [1 4 3]]
Multiplication of input matrix and vector:
 [[34]
 [42]
 [28]]

方法四:使用numpy.linalg子包获取矩阵的行列式

numpy.linalg.det() 函数 − 计算一个方阵的行列式。

Example

的中文翻译为:

示例

以下程序使用 numpy.linalg.det() 函数返回矩阵的行列式 −

# importing numpy module 
import numpy as np
 
# input matrix 
inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]])
# printing the input matrix
print("Input Matrix:\n", inputMatrix)

# getting the determinant of an input matrix 
outputDet = np.linalg.det(inputMatrix)

# printing the determinant of an input matrix 
print("Determinant of an input matrix:\n", outputDet)

输出

在执行时,上述程序将生成以下输出 -

Input Matrix:
 [[6 1 5]
 [2 0 8]
 [1 4 3]]
Determinant of an input matrix:
 -149.99999999999997

使用numpy.linalg找到特征值的第五种方法

numpy.linalg.eigvals() 函数 − 计算指定方阵/矩阵的特征值和右特征向量。

Example

的中文翻译为:

示例

The following program returns the Eigenvalues of an input matrix using the numpy.linalg.eigvals() function −

# importing NumPy module 
import numpy as np
 
# input matrix 
inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]])
# printing the input matrix
print("Input Matrix:\n", inputMatrix)
 
# getting Eigenvalues of an input matrix 
eigenValues = np.linalg.eigvals(inputMatrix)
 
# printing Eigenvalues of an input matrix 
print("Eigenvalues of an input matrix:\n", eigenValues)

输出

在执行时,上述程序将生成以下输出 -

Input Matrix:
 [[6 1 5]
 [2 0 8]
 [1 4 3]]
Eigenvalues of an input matrix:
 [ 9.55480959  3.69447805 -4.24928765]

方法六:使用numpy.linalg解方程

我们可以解决类似于找到 A*X = B 的 X 值的问题,

其中A是矩阵,B是向量。

Example

的中文翻译为:

示例

以下是使用solve()函数返回x值的程序-

# importing NumPy module 
import numpy as np
 
# input matrix 
inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]])
# printing the input matrix
print("Input Matrix:\n", inputMatrix)
 
# creating a vector using np.matrix() function 
inputVector = np.matrix([[1],[3],[5]])
 
# getting the value of x in an equation inputMatrix * x = inputVector
x_value = np.linalg.solve(inputMatrix, inputVector)
 
# printing x value
print("x value:\n", x_value)
 
# multiplying input matrix with x values 
print("Multiplication of input matrix with x values:\n", inputMatrix * x_value)

输出

在执行时,上述程序将生成以下输出 -

Input Matrix:
 [[6 1 5]
 [2 0 8]
 [1 4 3]]
x value:
 [[-0.39333333]
 [ 0.99333333]
 [ 0.47333333]]
Multiplication of input matrix with x values:
 [[1.]
 [3.]
 [5.]]

结论

在本文中,我们学习了如何使用Python中的NumPy模块执行矩阵和线性代数操作。我们学会了如何计算矩阵的转置、逆和行列式。我们还学习了如何在线性代数中进行一些计算,例如解方程和确定特征值。

以上就是在Python中进行矩阵和线性代数计算的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除