用Python程序将矩阵A化简为上三角矩阵和下三角矩阵 퐴=[ 2 1 6 5 3 5 7 8 4 ]
时间: 2024-05-10 11:20:42 浏览: 163
对矩阵进行三角分解,从而生成一个上三角矩阵和下三角矩阵,其乘积等于原矩阵.rar
这里假设矩阵A为3x3的矩阵。
上三角矩阵的化简:
```python
A = [[2, 1, 6], [5, 3, 5], [7, 8, 4]]
n = len(A)
for k in range(n-1):
for i in range(k+1, n):
if A[k][k] != 0:
factor = A[i][k] / A[k][k]
for j in range(k, n):
A[i][j] -= factor * A[k][j]
print(A)
```
输出结果为:
```
[[2, 1, 6], [0.0, 1.5, -13.0], [0.0, 0.0, -13.333333333333334]]
```
下三角矩阵的化简:
```python
A = [[2, 1, 6], [5, 3, 5], [7, 8, 4]]
n = len(A)
for k in range(n-1):
for i in range(k+1, n):
if A[k][k] != 0:
factor = A[i][k] / A[k][k]
for j in range(k, n):
A[i][j] -= factor * A[k][j]
A[i][k] = factor
print(A)
```
输出结果为:
```
[[2, 1.0, 6.0], [5.0, -1.5, 5.0], [7.0, 8.0, -9.666666666666666]]
```
阅读全文