高斯消元的優化,為了避免分母過小造成的誤差,在每次選取基準數的時候在每列中選擇最大的那個並於該行做初等行變換,該**的時間複雜度為 o(n
4)o(n^4)
o(n4
)
/*
wang_zibi 列消元 */
#include
#define int long long
using
namespace std;
typedef pair<
int,
int> pii;
const
int n =
1e4+10;
double a[n]
[n];
int n, m;
const
double inf =
0x3f3f3f3f
;void
test()
}void
deal
(int x)}if
(flag.second!=
-inf)
for(
int i =
0; i < m; i++
)swap
(a[flag.second]
[i], a[x]
[i]);}
void
gauss()
}}for(
int j =
2; j >=
0; j--
) a[j]
[j]= a[j][3
]/ a[j]
[j];}}
void
solve()
signed
main()
class
gauss
:def
__init__
(self, m, nn, mm)
: self.matrix = m
self.n = nn
self.m = mm
def__deal
(self, t)
: x =
-9999999.0
y =none
for i in
range
(t+1
,self.n):if
(self.matrix[i]
[t]> x)
: x = self.matrix[i]
[t] y=i
for i in
range(0
,self.m)
: temp = self.matrix[t]
[i] self.matrix[t]
[i]= self.matrix[y]
[i] self.matrix[y]
[i]=temp
defsolve
(self)
:for k in
range(0
, self.n)
: self.__deal(k)
for i in
range
(k +
1, self.n)
: t = self.matrix[i]
[k]/ self.matrix[k]
[k]for j in
range(0
, self.m)
: self.matrix[i]
[j]-= self.matrix[k]
[j]* t
for j in
range
(self.n -1,
-1,-
1):for k in
range
(j +
1, self.n)
: self.matrix[j]
[self.n]
= self.matrix[j]
[self.n]
- self.matrix[j]
[k]* self.matrix[k]
[k] self.matrix[j]
[j]= self.matrix[j]
[self.n]
/ self.matrix[j]
[j]for i in
range(0
, self.m -1)
:print
(self.matrix[i]
[i], end=
' ')
defmain()
: n,m=
map(
int,
input()
.split())
line =[[
0]* m]
* n for i in
range
(n):
line[i]
=input()
.split(
" ")
line[i]
=list
(map
(int
,line[i]))
g = gauss(line,n,m)
g.solve(
)main(
)
列主元的高斯消元法(FORTRAN)
program guass1 real,dimension allocatable arr real,dimension allocatable x real a integer i,j,k,n 輸入 輸出數列 write 請輸入需要計算的係數矩陣的大小n read n allocate arr n...
高斯消元(浮點數主列法消元,有剪枝細節
1 include 2 include 3 include 4 include 5 using namespace std 6const double eps 1e 8 7 8 typedef vectorvec 9 typedef vectormat 1011 vec gauss const ma...
數值分析 線性方程組求解 列主元高斯消元法
include stdafx.h include include stdio.h void matrixprint double arr,const int row,const int col double matrixsolve double arr co in,double arr y in,c...