forked from NonEuclideanDreamer/CutAndProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
301 lines (280 loc) · 5.94 KB
/
Matrix.java
File metadata and controls
301 lines (280 loc) · 5.94 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
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
//*************************************
// author: NonEuclideanDreamer
// Class for mxn-Matrices, together with methods for handling them
//************************************
public class Matrix
{
public static double ex=0.00000001;//Exactness when checking whether smth is =0)
public int m, n;
public double[][] entry;
public Matrix(double[][] c)
{
m=c.length;
n=c[0].length;
entry=c;
}
public Matrix(int x, int y)
{
m=x;
n=y;
entry=new double[x][y];
}
public static Matrix idmatrix(int n)
{
double[][]out =new double[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)out[i][j]=1;
}
}
return new Matrix(out);
}
public static Matrix project(int n,int i, int j, int k)
{
double[][] entries=new double[n][n];
entries[i][i]=1;entries[j][j]=1;entries[k][k]=1;
return new Matrix(entries);
}
public static Matrix hrotate(double angle, int x, int y,int dim)//Rotation matrix for rotation by angle in ex-ey-plane
{
Matrix matrix=idmatrix(dim);
matrix.entry[x][x]=Math.cos(angle);
matrix.entry[x][y]=-Math.sin(angle);
matrix.entry[y][x]=Math.sin(angle);
matrix.entry[y][y]=Math.cos(angle);
return matrix;
}
//Rotates the ex-(ey+ez)-plane by angle
public static Matrix drotate(double angle, int x, int y, int z, int dim)
{
Matrix matrix=idmatrix(dim);//ToDo
matrix.entry[x][x]=Math.cos(angle);
matrix.entry[x][y]=-Math.sin(angle)/Math.sqrt(2);
matrix.entry[x][z]=matrix.entry[x][y];
matrix.entry[y][x]=-matrix.entry[x][y];
matrix.entry[z][x]=matrix.entry[y][x];
matrix.entry[y][y]=(Math.cos(angle)+1)/2;
matrix.entry[z][z]=matrix.entry[y][y];
matrix.entry[y][z]=matrix.entry[y][y]-1;
matrix.entry[z][y]=matrix.entry[y][z];
return matrix;
}
public void mirror(int j)
{
for(int i=0;i<m;i++)
entry[i][j]*=-1;
}
public Matrix times(Matrix matrix)
{
double[][] out=new double[m][matrix.n];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
for(int k=0;k<matrix.n;k++)
{
out[i][k]=out[i][k]+entry[i][j]*matrix.entry[j][k];
}
}
}
return new Matrix(out);
}
public Rn times(Rn vector)
{
double[] c=new double[m];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
c[i]=c[i]+entry[i][j]*vector.c[j];
}
}
return new Rn(c);
}
public Matrix times(double scalar)
{
double[][] nentry=new double[m][n];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
nentry[i][j]=entry[i][j]*scalar;
}
}
return new Matrix(nentry);
}
public void print()
{
/*System.out.println("Matrix:");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(entry[i][j]+" ");
}
System.out.println();
}
*/
}
public Matrix copy()
{
double[][] out=new double[m][n];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
out[i][j]=entry[i][j];
}
}
return new Matrix(out);
}
public Rn[] nullspace( )//actually left nullspace
{
Rn res=Rn.zero(m);
Matrix out=new Matrix(m,m+n);
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
out.entry[i][j]=entry[i][j];
}
out.entry[i][i+n]=1;
}
int i=0,j=0;
while(i<m&&j<n)
{while(Math.abs(out.entry[i][j])<=ex)
{
int k=i+1;boolean problemsolved=false;
while(k<m&& !problemsolved)
{
if(Math.abs(out.entry[k][j])>ex)problemsolved=true;
else k++;
}
if(problemsolved)
{
double[] s=out.entry[i];
out.entry[i]=out.entry[k];
out.entry[k]=s;
}
else{j++;}
}
double f=out.entry[i][j];
for(int h=0;h<n+m;h++)
{
out.entry[i][h]=out.entry[i][h]/f;
}
for(int l=0;l<m;l++)
{
double a=out.entry[l][j];
if(l!=i)
for(int h=0;h<n+m;h++)
{
out.entry[l][h]-=a*out.entry[i][h];
}
}
{i++;j++;} //out.print();
}
boolean iszero=false;i=-1;
//System.out.print("Hessematrix:");
//out.print();
while(!iszero&&i<m-1)
{
i++;
boolean maybe=true;
for(j=0;j<n;j++)
{
if(Math.abs(out.entry[i][j])>ex)maybe=false;
}
if(maybe)iszero=true;
}
Rn[]nullsp=new Rn[n-i];
for(int k=i;k<n;k++)
{
double[] e=new double[m];
for(int l=0;l<m;l++)
{
e[l]=out.entry[k][n+l];
}
nullsp[k-i]=new Rn(e);
}
return nullsp;
}
public Matrix subtract(Matrix subtractor)
{
return add(subtractor.times(-1));
}
public Matrix add(Matrix summand)
{
double[][]out=new double[m][n];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
out[i][j]=entry[i][j]+summand.entry[i][j];
}
}
return new Matrix(out);
}
//Using the Gram-Schmith Algorithm to orthonormalize the rows
public void orthonormalize()
{
for(int i=0;i<n;i++)
{
double[] dotproducts=new double[i]; Rn column=getColumn(i);
for(int j=0;j<i;j++)
{
dotproducts[j]=getColumn(j).dot(column);
}
for(int j=0;j<i;j++)
{
column=column.substract(getColumn(j).times(dotproducts[j]));
}
setColumn(i,column.normalize());
}
}
//Changing the columns towards new matrix
Matrix linComb(Matrix goal, int[]changing,double[] t )
{
double[][]out=entry.clone();
for(int k=0;k<changing.length;k++)
{
int j=changing[k];
for(int i=0;i<n;i++)
out[i][j]=entry[i][j]*(1-t[k])+goal.entry[i][j]*t[k];
}
return new Matrix(out);
}
public Rn getRow(int i)
{
double[] out=entry[i];
return new Rn(out);
}
public void setRow(int i, Rn r)
{
entry[i]=r.c;
}
public Rn getColumn(int j)
{
double[]out=new double[m];
for(int i=0;i<m;i++)
{
out[i]=entry[i][j];
}
return new Rn(out);
}
public void setColumn(int j,Rn r)
{
for(int i=0;i<m;i++)
entry[i][j]=r.get(i);
}
public Matrix transpone()
{
double[][]out =new double[n][m];
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
out[j][i]=entry[i][j];
return new Matrix(out);
}
}