-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearAlgebra.java
130 lines (124 loc) · 2.82 KB
/
LinearAlgebra.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
import java.util.Random;
public class LinearAlgebra {
final static double EPS = 1e-9;
/**
* Modifies a and b!
*/
public static double[] gauss(double[][] a, double[] b) {
int m = a.length;
int n = a[0].length;
int[] pos = new int[m];
for (int i = 0; i < m; i++) {
int s = 0;
for (int j = 1; j < n; j++) {
if (Math.abs(a[i][j]) > Math.abs(a[i][s])) {
s = j;
}
}
if (Math.abs(a[i][s]) < EPS) {
pos[i] = -1;
continue;
}
pos[i] = s;
for (int k = 0; k < m; k++) {
if (k == i) {
continue;
}
double c = - a[k][s] / a[i][s];
for (int j = 0; j < n; j++) {
a[k][j] += c * a[i][j];
}
b[k] += c * b[i];
}
}
double[] ans = new double[n];
for (int i = 0; i < m; i++) {
if (pos[i] == -1) {
continue;
}
ans[pos[i]] = b[i] / a[i][pos[i]];
}
return ans;
}
public static int[][] matrixPower(int[][] a, long power, final int M) {
if (power == 0) {
int[][] res = new int[a.length][a.length];
for (int i = 0; i < a.length; i++) {
res[i][i] = 1;
}
return res;
}
if (power == 1) {
return a;
}
if (power % 2 == 0) {
int[][] b = matrixPower(a, power / 2, M);
return matrixMultiply(b, b, M);
}
int[][] b = matrixPower(a, power - 1, M);
return matrixMultiply(a, b, M);
}
public static int[][] matrixMultiply(int[][] a, int[][] b, final int M) {
int[][] c = new int[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 < b.length; k++) {
c[i][j] = (int) ((c[i][j] + (long) a[i][k] * b[k][j]) % M);
}
}
}
return c;
}
static double[][] pca(double[][] x, int number, int iters, Random rnd) {
int hei = x.length;
int wid = x[0].length;
double[][] coord = new double[number][hei];
for (int c = 0; c < number; c++) {
for (int j = 0; j < wid; j++) {
double av = 0;
for (int i = 0; i < hei; i++) {
av += x[i][j];
}
av /= hei;
for (int i = 0; i < hei; i++) {
x[i][j] -= av;
}
}
double[] r = new double[wid];
for (int j = 0; j < wid; j++) {
r[j] = rnd.nextGaussian();
}
for (int iter = 0; iter < iters; iter++) {
double[] s = new double[wid];
for (int i = 0; i < hei; i++) {
double scal = 0;
for (int j = 0; j < wid; j++) {
scal += r[j] * x[i][j];
}
for (int j = 0; j < wid; j++) {
s[j] += scal * x[i][j];
}
}
double norm = 0;
for (int j = 0; j < wid; j++) {
norm += s[j] * s[j];
}
norm = Math.sqrt(norm);
for (int j = 0; j < wid; j++) {
r[j] = s[j] / norm;
}
}
for (int i = 0; i < hei; i++) {
double scal = 0;
for (int j = 0; j < wid; j++) {
scal += r[j] * x[i][j];
}
coord[c][i] = scal;
for (int j = 0; j < wid; j++) {
x[i][j] -= r[j] * scal;
}
}
}
return coord;
}
}