-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearSystems.java
330 lines (291 loc) · 8.18 KB
/
LinearSystems.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
* Implementation of base linear operations. Matrices
* are represented as two-dimensional arrays of doubles.
* @author Aubrey Alston
*/
public class LinearSystems {
public static double[][] concatenateColumns(double[][] a, double[][] b){
double[][] result = new double[a.length][a[0].length+b[0].length];
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a[0].length + b[0].length; j++){
if(j < a[0].length)
result[i][j] = a[i][j];
else
result[i][j] = b[i][j-a[0].length];
}
}
return result;
}
/**
* Returns the particular solution for a system in rref.
* @param sys
* @return
*/
public static double[][] getParticularSolution(double[][] sys){
double[][] solution = new double[sys[0].length-1][1];
for(int i = sys.length-1; i >= 0; i--){
double v = sys[i][sys[0].length-1];
for(int j = 0; j < sys[0].length - 1; j++){
v -= solution[j][0] * sys[i][j];
}
solution[i][0] = v / sys[i][i];
}
return solution;
}
/**
* Returns the special solutions for a system in rref.
* @return
*/
public static double[][][] getSpecialSolutions(double[][] sys){
double[][][] solutions =
new double[sys[0].length-sys.length-1][sys[0].length-1][1];
int solIndex = 0;
for(int current = sys.length; current < sys[0].length-1; current++){
double[][] solution = new double[sys[0].length-1][1];
solution[current][0] = 1;
// pls fix
for(int i = sys.length-1; i >= 0; i--){
double v = 0;
for(int j = 0; j < sys[0].length - 1; j++){
if(i != j){
v -= solution[j][0] * sys[i][j];
}
}
solution[i][0] = v / sys[i][i];
}
solutions[solIndex++] = solution;
}
return solutions;
}
public static double[][][] getOtherSolutions(double[][] sys){
double[][][] solutions =
new double[sys[0].length-sys.length-1][sys[0].length-1][1];
int solIndex = 0;
for(int current = sys.length; current < sys[0].length-1; current++){
double[][] solution = new double[sys[0].length-1][1];
solution[current][0] = 1;
// pls fix
for(int i = sys.length-1; i >= 0; i--){
double v = sys[i][sys[0].length-1];
for(int j = 0; j < sys[0].length - 1; j++){
if(i != j){
v -= solution[j][0] * sys[i][j];
}
}
solution[i][0] = v / sys[i][i];
}
solutions[solIndex++] = solution;
}
return solutions;
}
public static double[][] eliminate(double[][] a){
double[][] work = new double[a.length][a[0].length];
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a[0].length; j++){
work[i][j] = a[i][j];
}
}
// Forward elimination (n^3)
for(int i = 0; i < a.length; i++){
for(int j = i+1; j < a.length; j++){
double l_value = work[j][i] / work[i][i];
for(int k = i; k < work[0].length; k++){
work[j][k] -= work[i][k] * l_value;
}
}
}
return work;
}
public static double[][] getOrthogonalizedSpace(double[][] a){
double[][] result = new double[a.length][a[0].length];
double[][][] vectors = getVectors(a);
for(int i = 0; i < vectors.length; i++){
vectors[i] = getUnitVector(vectors[i]);
double[][] proj = getVectorProjectionMatrix(vectors[i]);
for(int j = i +1; j < vectors.length; j++){
vectors[j] = subtract(vectors[j], multiply(proj, vectors[j]));
}
}
return toMatrix(vectors);
}
public static double[][][] getVectors(double[][] a){
double[][][] vectors = new double[a[0].length][a.length][1];
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a[0].length; j++){
vectors[j][i][0] = a[i][j];
}
}
return vectors;
}
public static double[][] toMatrix(double[][][] vectors){
double[][] matrix = new double[vectors[0].length][vectors.length];
for(int i = 0; i < matrix.length; i++){
for(int j = 0; j < matrix[0].length; j++){
matrix[i][j] = vectors[j][i][0];
}
}
return matrix;
}
/**
* Returns the resulting projection of b onto a.
* @param a
* @param b
* @return
*/
public static double[][] project(double[][] a, double[][] b){
return multiply(getMatrixProjectionMatrix(a), b);
}
/**
* Returns the projection matrix for a vector.
*/
public static double[][] getVectorProjectionMatrix(double[][] a){
double[][] transpose = transpose(a);
return divideByNumber(multiply(a,transpose),multiply(transpose,a)[0][0]);
}
/**
* Returns the projection matrix of a matrix.
* @param a
* @return
*/
public static double[][] getMatrixProjectionMatrix(double[][] a){
double[][] transpose = transpose(a);
double[][] ata_inv = inverse(multiply(transpose, a));
return multiply(multiply(a, ata_inv), transpose);
}
/**
* Returns the sum of two matrices.
* @param a
* @param b
* @return
*/
public static double[][] add(double[][] a, double[][] b){
double[][] result = new double[a.length][a[0].length];
for(int i = 0; i < a.length; i++)
for(int j = 0; j < a[0].length; j++)
result[i][j] = a[i][j] + b[i][j];
return result;
}
/**
* Returns the difference between two matrices.
*/
public static double[][] subtract(double[][] a, double[][] b){
double[][] result = new double[a.length][a[0].length];
for(int i = 0; i < a.length; i++)
for(int j = 0; j < a[0].length; j++)
result[i][j] = a[i][j] - b[i][j];
return result;
}
/**
* Returns the inverse of a. (n^3)
* @return
*/
public static double[][] inverse(double[][] a){
double[][] work = new double[a.length][a.length*2];
double[][] result = new double[a.length][a.length];
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a[0].length; j++){
work[i][j] = a[i][j];
}
work[i][i+a[0].length] = 1;
}
// Forward elimination (n^3)
for(int i = 0; i < a.length; i++){
for(int j = i+1; j < a.length; j++){
double l_value = work[j][i] / work[i][i];
for(int k = i; k < work[0].length; k++){
work[j][k] -= work[i][k] * l_value;
}
}
}
// Backward elimination (n^3)
for(int i = a.length-1; i > 0; i--){
for(int j = i-1; j >= 0; j--){
double l_value = work[j][i] / work[i][i];
for(int k = i; k < work[0].length; k++){
work[j][k] -= work[i][k] * l_value;
}
}
}
// Divide by pivots
for(int i = 0; i < a.length; i++){
for(int j = a[0].length; j < work[0].length; j++)
work[i][j] /= work[i][i];
work[i][i] = 1;
}
for(int i = 0; i < result.length; i++){
for(int j = 0; j < a[0].length; j++){
result[i][j] = work[i][j+a[0].length];
}
}
return result;
}
public static double[][] getUnitVector(double[][] a){
return divideByNumber(a, getNorm(a));
}
public static double getNorm(double[][] a){
return Math.sqrt(getSquaredNorm(a));
}
public static double getSquaredNorm(double[][] a){
double n = 0;
for(int i = 0; i < a.length; i++){
n += a[i][0] * a[i][0];
}
return n;
}
/**
* Returns the transpose of a.
*/
public static double[][] transpose(double[][] a){
double[][] result = new double[a[0].length][a.length];
for(int i = 0; i < a.length; i++)
for(int j = 0; j < a[0].length; j++)
result[j][i] = a[i][j];
return result;
}
/**
* Vanilla nmr matrix multiplication implementation
* of A * B.
* @param a
* @param b
*/
public static double[][] multiply(double[][] a, double[][] b){
double[][] result = new double[a.length][b[0].length];
for(int i = 0; i < a.length; i++)
for(int j = 0; j < b[0].length; j++)
for(int k = 0; k < a[0].length; k++)
result[i][j] += a[i][k] * b[k][j];
return result;
}
public static void print2D(double[][] a){
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a[0].length; j++)
System.out.print(a[i][j]+" ");
System.out.println();
}
System.out.println();
}
/**
* Divides every element of the matrix a by a constant.
* NOTE: THIS CHANGES THE PASSED MATRIX
* @param a
* @param n
*/
public static double[][] divideByNumber(double[][] a, double n){
for(int i = 0; i < a.length; i++)
for(int j = 0; j < a[0].length; j++)
a[i][j] /= n;
return a;
}
/**
* Multiplies every element of the matrix by a constant.
* NOTE: THIS CHANGES THE PASSED MATRIX
* @param a
* @param n
*/
public static double[][] multiplyByNumber(double[][] a, double n){
for(int i = 0; i < a.length; i++)
for(int j = 0; j < a[0].length; j++)
a[i][j] *= n;
return a;
}
}