-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jacobi.cpp
70 lines (58 loc) · 1.27 KB
/
Jacobi.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <bits/stdc++.h>
#define Col 3
#define Row 3
#define FOR(i,a,b) for(long long i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define debug(x) cout << #x << " = " << x << endl;
using namespace std;
double a[Col][Row] = {{2.0,3.0,3.0},{3.0,2.0,-1.0},{5.0,4.0,2.0}};
double b[Col] = {5.0,-4.0,3.0};
double D[Col][Row];
double invD[Col][Row];
double G[Col][Row];
void Decomposition(){
REP(i,Col){
REP(j,Row){
if(i == j){
D[i][j] = a[i][j];
}else{
G[i][j] = a[i][j];
}
}
}
}
void Inversion(){
REP(i,Col){
REP(j,Row){
if(D[i][j] != 0)
invD[i][j] = 1.0 /D[i][j];
}
}
}
void substBack(){
for(int i = Col-1; i>=0; i--){
FOR(j,i+1,Row){
b[i] -= a[i][j] * b[j];
a[i][j] = 0.0;
}
b[i] /= a[i][i];
a[i][i] = 1.0;
}
}
void printMatrix(double array1[][Row],double array2[Col]){
REP(i,Col){
REP(j,Row){
printf("\t%lf ",array1[i][j]);
}
printf("\t%lf\n",array2[i]);
}
printf("\n");
}
int main(){
Decomposition();
Inversion();
printMatrix(D,b);
printMatrix(invD,b);
//gaussElimination();
substBack();
}