-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConjugate_Gradient.java
More file actions
168 lines (116 loc) · 4.14 KB
/
Copy pathConjugate_Gradient.java
File metadata and controls
168 lines (116 loc) · 4.14 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package MIS3_Assignment;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import org.jblas.DoubleMatrix;
public class MIS3_ConjGrad_A3 {
public List<Double> arr_with_interval(double s,double interval,double e){
List<Double> fin_lis=new ArrayList<>();
double tot_count=(Math.abs(s)+Math.abs(e))/Math.abs(interval);
for(int i=0;i<tot_count;i++) {
fin_lis.add(s+interval*i);
}
return fin_lis;
}
public void print_mat(DoubleMatrix mat) {
//For traversing through rows
for(int i =0;i<mat.rows;i++) {
//For traversing through colums
for(int j=0;j<mat.columns;j++) {
System.out.print("\t"+mat.get(i,j)+"\t");
}
System.out.println();
}
}
public DoubleMatrix mat_mulScal(DoubleMatrix S , double scalar) {
double[][] ret_mat = new double[S.rows][S.columns];
for (int i=0;i<S.rows;i++) {
for(int j=0;j<S.columns;j++) {
ret_mat[i][j] = S.get(i,j)*scalar;
}
}
return new DoubleMatrix(ret_mat);
}
public DoubleMatrix rand_matrix(int[] range,int m,int n) {
double[][] ret_mat = new double[m][n];
for (int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
ret_mat[i][j] = ThreadLocalRandom.current().nextInt(range[0],range[1]+1);
}
}
//new DoubleMatrix(ret_mat).print();
return new DoubleMatrix(ret_mat);
}
public three_mat conjugate_gradient(DoubleMatrix A,DoubleMatrix b,DoubleMatrix x){
double[][] x_mat = new double[A.rows][b.length];
double[][] p_mat = new double[A.rows][b.length];
double[][] r_mat = new double[A.rows][b.length];
DoubleMatrix r = b.sub(A.mmul(x));
DoubleMatrix d = r;
DoubleMatrix rold = r.transpose().mmul(r);
double rsold = rold.get(0,0);
for(int i=1;i<=b.length;i++) {
DoubleMatrix Ap = A.mmul(d);
double alpha = rsold/d.transpose().mmul(Ap).get(0,0);
x = x.add(mat_mulScal(d,alpha));
r = r.sub(mat_mulScal(Ap,alpha));
double rnew = r.transpose().mmul(r).get(0,0);
for (int j = 0;j<x.rows;j++) {
x_mat[j][i-1] = x.get(j,0);
p_mat[j][i-1] = d.get(j,0);
r_mat[j][i-1] = r.get(j,0);
}
if (Math.sqrt(rnew)<=1e-10) {
break;
}
d = r.add(mat_mulScal(d,(rnew/rsold)));
rsold = rnew;
}
return new three_mat(x_mat,p_mat,r_mat);
}
public void prove_perpendicular(DoubleMatrix residuals,DoubleMatrix directions) {
int rn = residuals.rows;
for (int i=1;i<rn;i++) {
for(int j=0;j<i;j++) {
System.out.println("Dot product of r"+i+ " and d"+j+" is "+residuals.getColumn(i).transpose().mmul(directions.getColumn(j)) );
}
}
}
public static void main(String[] args){
MIS3_ConjGrad_A3 m = new MIS3_ConjGrad_A3();
int n = 4;
int[] range1 = {-1,2};
DoubleMatrix A =m.rand_matrix(range1,n,n);
A = A.transpose().mmul(A);
int[] range2 = {-9,9};
DoubleMatrix b = m.rand_matrix(range2, n,1);
DoubleMatrix xo = m.rand_matrix(range2, n,1);
three_mat xpr = m.conjugate_gradient(A, b, xo);
DoubleMatrix x_co = new DoubleMatrix(xpr.x);
DoubleMatrix D_co = new DoubleMatrix(xpr.p);
DoubleMatrix r_co = new DoubleMatrix(xpr.r);
System.out.println("A*x is");
m.print_mat(A.mmul((x_co.getColumn(n-1))));
System.out.println();
System.out.println("b is");
m.print_mat(b);
System.out.println();
System.out.println("Dt*A*D matrix is");
m.print_mat(D_co.transpose().mmul(A.mmul(D_co)));
System.out.println();
System.out.println("Resvt*Resv matrix is");
m.print_mat(r_co.transpose().mmul(r_co));
m.prove_perpendicular(r_co, D_co);//checking whether residuals and directions are perpendicular.
}
}
class three_mat {
public double[][] x;
public double[][] p;
public double[][] r;
public three_mat(double[][] x,double [][]y,double [][]z) {
this.x = x;
this.p = y;
this.r = z;
}
}