-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMatrixTest.java
348 lines (262 loc) · 7.36 KB
/
MatrixTest.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import ijaux.Util;
import ijaux.datatype.UnsupportedTypeException;
import ijaux.datatype.access.Access;
import ijaux.hypergeom.index.BaseIndex;
import ijaux.hypergeom.index.BaseIndex2D;
import ijaux.hypergeom.index.GridIndex;
import ijaux.hypergeom.index.Indexing;
import Jama.Matrix;
public class MatrixTest {
public MatrixTest () throws UnsupportedTypeException {
System.gc();
int[] dima={200, 200};
int n=Util.cumprod(dima);
double [] am=Util.rampDouble(n, n);
double [] bm=Util.rampDouble(n, n);
// BaseIndex2D ind2d=new BaseIndex2D(dima);
// AuxIndex ind2d=new AuxIndex(dima);
// final Access<Double> ac=Access.rawAccess(am, ind2d);
// final Access<Double> bc=Access.rawAccess(bm, ind2d);
long start=System.currentTimeMillis();
//int [] ik=new int[2];
//int [] kj=new int[2];
//int [] ij=new int[2];
mult(dima, am, bm);
start=System.currentTimeMillis()- start;
System.out.println("matrix multiplication raw "+ start);
double[][] vals =new double[dima[0]][dima[1]];
double[][] vals1 =new double[dima[0]][dima[1]];
Matrix A = new Matrix(vals);
Matrix B = new Matrix(vals1);
start=System.currentTimeMillis();
Matrix C=A.times(B);
start=System.currentTimeMillis()- start;
System.out.println("matrix multiplication JAMA "+ start);
}
/**
* @param dima
* @param n
* @param am
* @param bm
*/
private double[] mult(int[] dima, double[] am, double[] bm) {
final int n=dima[0]*dima[1];
double [] ret=new double[n];
int ai=0;
for (int i=0; i<dima[0]; i++) {
for (int j=0; j<dima[1]; j++) {
int aint=ai;
int bind=j;
double value=0;
while (bind<n) {
value+=am[aint++]*bm[bind];
bind+=dima[1];
} // end for
ret[ai +j]=value;
} // end for
ai+=dima[0];
} // end for
return ret;
}
/**
* @param args
*/
public static void main(String[] args) {
System.gc();
int[] dims={100, 120, 120};
GridIndex gind=GridIndex.create(dims);
int[] pnt={9, 3, 5};
gind.translateTo(pnt);
int imax=gind.max();
System.out.println("n-D case "+ imax);
System.out.println(gind);
System.out.println(gind.getType());
long time1=System.currentTimeMillis();
for (int i=0; i<imax; i++) {
gind.setIndex(i);
gind.isValid();
}
time1=System.currentTimeMillis() -time1;
System.out.println("time " +time1);
dims=new int[]{1000, 1440};
gind=GridIndex.create(dims);
pnt=new int[]{9, 5};
gind.translateTo(pnt);
imax=gind.max();
System.out.println("2D case " +imax);
System.out.println(gind);
System.out.println(gind.getType());
time1=System.currentTimeMillis();
for (int i=0; i<imax; i++) {
gind.setIndex(i);
gind.isValid();
}
time1=System.currentTimeMillis() -time1;
System.out.println("time " +time1);
gind=new BaseIndex(dims);
pnt=new int[]{9, 5};
gind.translateTo(pnt);
imax=gind.max();
System.out.println("2D case " +imax);
System.out.println(gind.getType());
time1=System.currentTimeMillis();
for (int i=0; i<imax; i++) {
gind.setIndex(i);
gind.isValid();
}
time1=System.currentTimeMillis() -time1;
System.out.println("time " +time1);
dims=new int[]{1440000};
gind=GridIndex.create(dims);
pnt=new int[]{9};
gind.translateTo(pnt);
imax=gind.max();
System.out.println("1D case "+ imax);
System.out.println(gind);
System.out.println(gind.getType());
time1=System.currentTimeMillis();
for (int i=0; i<imax; i++) {
gind.setIndex(i);
gind.isValid();
}
time1=System.currentTimeMillis() -time1;
System.out.println("time " +time1);
gind=new BaseIndex(dims);
pnt=new int[]{9};
gind.translateTo(pnt);
imax=gind.max();
System.out.println("1D case "+ imax);
System.out.println(gind.getType());
time1=System.currentTimeMillis();
for (int i=0; i<imax; i++) {
gind.setIndex(i);
gind.isValid();
}
time1=System.currentTimeMillis() -time1;
System.out.println("time " +time1);
try {
new MatrixTest ();
} catch (UnsupportedTypeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class AuxIndex implements Indexing<int[]> {
private int[] coords;
private int index=0;
private int[] dim;
public AuxIndex(int[] dims ) {
final int n=dims.length;
if (n!=2) throw new IllegalArgumentException("dimensions != 2");
coords=new int[n];
dim=dims;
}
@Override
public int[] getCoordinates() {
return coords;
}
@Override
public int index() {
return indexOf(coords);
}
@Override
public int indexOf(int[] x) {
return x[1]*dim[0]+x[0];
}
@Override
public boolean isValid() {
// TODO Auto-generated method stub
return false;
}
@Override
public void reshape(int[] dims) {
// TODO Auto-generated method stub
}
@Override
public void setIndex(int idx) {
index=idx;
}
@Override
public int translate(int[] x) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int translateTo(int[] x) {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean contains(int[] vect) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean contains(int[] vect, int[] set) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean geq(int[] vect, int[] set) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean in(int[] inner, int[] outer, int[] set) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean leq(int[] vect, int[] set) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean out(int[] inner, int[] outer, int[] set) {
// TODO Auto-generated method stub
return false;
}
@Override
public int getNDim() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setIndexAndUpdate(int idx) {
// TODO Auto-generated method stub
}
@Override
public int[] getDim() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setCoordinates(int[] x) {
// TODO Auto-generated method stub
}
}
/*
public Matrix times (Matrix B) {
if (B.m != n) {
throw new IllegalArgumentException("Matrix inner dimensions must agree.");
}
Matrix X = new Matrix(m,B.n);
double[][] C = X.getArray();
double[] Bcolj = new double[n];
for (int j = 0; j < B.n; j++) {
for (int k = 0; k < n; k++) {
Bcolj[k] = B.A[k][j];
}
for (int i = 0; i < m; i++) {
double[] Arowi = A[i];
double s = 0;
for (int k = 0; k < n; k++) {
s += Arowi[k]*Bcolj[k];
}
C[i][j] = s;
}
}
return X;
}
*/
}