-
Notifications
You must be signed in to change notification settings - Fork 2
/
jacobi.java
123 lines (99 loc) · 3.54 KB
/
jacobi.java
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Class for the Jacobi iteration method.
*
* @author Katherine Cabezas
* @version 2.0
*/
public class jacobi {
private static final int MAX_ITER = 100;
private static int iterations;
/**
* Method for Jacobi iteration. Returns x vector reached
* after the statement ( || x.n - x.n-1 || < tolerance ) is true.
* @param a Matrix n x n
* @param y Vector n x 1
* @param x Initial guess vector
* @param tol Error tolerance number
* @return x vector approximation
* @throws RuntimeException if iterations > MAX_ITER
*/
public static Vector jacobi(Matrix a, Vector y, Vector x, double tol) {
double difference = tol + 1;
Vector xVector = new Vector(x.rows());
Vector oldXVector = new Vector(x.rows());
double sum;
for (int i = 0; i < oldXVector.rows(); i++) {
oldXVector.set(i, x.get(i));
}
while(!(difference < tol)) {
iterations++;
if (iterations > MAX_ITER) {
throw new RuntimeException("Doesn't converge after 100 iterations.");
}
for (int i = 0; i < oldXVector.rows(); i++) {
oldXVector.set(i, xVector.get(i));
}
for (int i = 0; i < a.rows(); i++) {
sum = y.get(i);
for (int j = 0; j < a.columns(); j++) {
if (i != j) {
sum -= a.get(i, j) * oldXVector.get(j);
}
}
sum = sum / a.get(i, i);
xVector.set(i, sum);
}
if (iterations > 1) {
difference = xVector.get(0) - oldXVector.get(0);
difference = Math.abs(difference);
}
}
System.out.println(iterations + " iterations made.");
return xVector;
}
/**
* Method for Jacobi iteration for binary streams. Returns x vector
* reached after the statement ( || x.n - x.n-1 || < tolerance ) is true.
* @param a Matrix n x n
* @param y Vector n x 1
* @param x Initial guess vector
* @param tol Error tolerance number
* @return x vector approximation
* @throws RuntimeException if iterations > MAX_ITER
*/
public static Vector j_decode(Matrix a, Vector y, Vector x, double tol) {
double difference = tol + 1;
Vector xVector = new Vector(x.rows());
Vector oldXVector = new Vector(x.rows());
double sum;
for (int i = 0; i < oldXVector.rows(); i++) {
oldXVector.set(i, x.get(i));
}
while(!(difference < tol)) {
iterations++;
if (iterations > MAX_ITER) {
throw new RuntimeException("Doesn't converge after 100 iterations.");
}
for (int i = 0; i < oldXVector.rows(); i++) {
oldXVector.set(i, xVector.get(i));
}
for (int i = 0; i < a.rows(); i++) {
sum = y.get(i);
for (int j = 0; j < a.columns(); j++) {
if (i != j) {
sum -= a.get(i, j) * oldXVector.get(j);
}
}
sum = sum / a.get(i, i);
sum = sum % 2;
xVector.set(i, sum);
}
if (iterations > 1) {
difference = xVector.get(0) - oldXVector.get(0);
difference = Math.abs(difference);
}
}
System.out.println(iterations + " iterations made.");
return xVector;
}
}