高斯消除矩阵
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
def pprint(A):
for i in A:
print (i)
print ("")
data = [
[ 1 , 2 , 1 , 2 ],
[ 3 , 8 , 1 , 12 ],
[ 0 , 4 , 1 , 2 ]
]
n = len (data)
print ( "输入数据" )
pprint(data)
for i in range (n):
print ( "第{}次操作" . format (i))
maxE = abs (data[i][i])
# 最大值在第几行
maxRow = i
for k in range (i + 1 , n):
if abs (data[k][i] > maxE):
maxE = abs (data[k][i])
maxRow = k
# 当前行交换最大的行
for k in range (i, n + 1 ):
data[maxRow][k], data[i][k] = data[i][k], data[maxRow][k]
print ( "当前行交换最大的行 交换结果" )
pprint(data)
# 改0
for k in range (i + 1 , n):
c = - data[k][i] / data[i][i]
print ( - data[k][i] , "/" , data[i][i])
for j in range (i, n + 1 ):
if i = = j:
data[k][j] = 0
else :
data[k][j] + = c * data[i][j]
print ( "改0结果" )
pprint(data)
# 化简后结果
print ( "化简结果" )
pprint(data)
# 结果计算
result = [ 0 for i in range (n)]
for i in range (n - 1 , - 1 , - 1 ):
result[i] = data[i][n] / data[i][i]
for k in range (i - 1 , - 1 , - 1 ):
data[k][n] - = data[k][i] * result[i]
print (result)
|
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/staHuri/article/details/83899053