用python实现矩阵运算
时间: 2023-12-25 20:29:31 浏览: 88
Python实现矩阵置换优化算法
以下是使用Python实现矩阵运算的几个常见操作的示例:
1. 创建矩阵:
```python
import numpy as np
# 创建一个3x3的矩阵
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(matrix)
```
2. 矩阵相乘:
```python
import numpy as np
# 创建两个矩阵
matrix1 = np.array([[1, 2, 3], [4, 5, 6]])
matrix2 = np.array([[7, 8], [9, 10], [11, 12]])
# 矩阵相乘
result = np.dot(matrix1, matrix2)
print(result)
```
3. 求逆矩阵:
```python
import numpy as np
# 创建一个2x2的矩阵
matrix = np.array([[1, 2], [3, 4]])
# 求逆矩阵
inverse_matrix = np.linalg.inv(matrix)
print(inverse_matrix)
```
4. 矩阵转置:
```python
import numpy as np
# 创建一个3x2的矩阵
matrix = np.array([[1, 2], [3, 4], [5, 6]])
# 矩阵转置
transpose_matrix = np.transpose(matrix)
print(transpose_matrix)
```
阅读全文