-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixTest.java
More file actions
386 lines (337 loc) · 11.4 KB
/
MatrixTest.java
File metadata and controls
386 lines (337 loc) · 11.4 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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package matrix;
import java.util.Arrays;
import org.junit.*;
import static org.junit.Assert.*;
public class MatrixTest
{
/**
* Test of plus method, of class Matrix.
*/
@Test
public void testPlus()
{
System.out.println("plus");
double[][] array1 = {{1, 1, 2}, {6, 24, 120}};
double[][] array2 = {{-1, 0, -1}, {-4, -21, -115}};
double[][] result = {{0, 1, 1}, {2, 3, 5}};
Matrix x = new Matrix(array1);
Matrix instance = new Matrix(array2);
assertEquals(true, instance.plus(x).equals(new Matrix(result)));
//fail("The test case is a prototype.");
}
/**
* Test of minus method, of class Matrix.
*/
@Test
public void testMinus()
{
System.out.println("minus");
double[][] array1 = {{0, 1, 1}, {2, 3, 5}};
double[][] array2 = {{8, 13, 21}, {34, 55, 89}};
double[][] result = {{-8, -12, -20}, {-32, -52, -84}};
Matrix x = new Matrix(array1);
Matrix instance = new Matrix(array2);
assertEquals(true, x.minus(instance).equals(new Matrix(result)));
//fail("The test case is a prototype.");
}
/**
* Test of multiply method, of class Matrix.
*/
@Test
public void testMultiply()
{
System.out.println("multiply");
double[][] array1 = {{0, 2, 4, 6, 8}, {10, 12, 14, 16, 18},
{20, 22, 24, 26, 28}, {30, 32, 34, 36, 38}};
double[][] array2 = {{1, 3, 5, 7}, {9, 11, 13, 15}, {17, 19, 21, 23},
{25, 27, 29, 31}, {33, 35, 37, 39}};
double[][] result = {{500, 540, 580, 620}, {1350, 1490, 1630, 1770},
{2200, 2440, 2680, 2920}, {3050, 3390, 3730, 4070}};
Matrix x = new Matrix(array1);
Matrix instance = new Matrix(array2);
Matrix expression_result = x.multiply(instance);
assertEquals(true , expression_result.equals(new Matrix(result)));
double[][] double_result = {{1000, 1080, 1160, 1240}, {2700, 2980, 3260, 3540},
{4400, 4880, 5360, 5840}, {6100, 6780, 7460, 8140}};
assertEquals(true, expression_result.multiply(2).equals(new Matrix(double_result)));
//fail("The test case is a prototype.");
}
/**
* Test of negatate method, of class Matrix.
*/
@Test
public void testNegatate()
{
System.out.println("negatate");
double[][] array = {{1, 10, -7}, {-21, -15, 9}};
double[][] result = {{-1, -10, 7}, {21, 15, -9}};
Matrix instance = new Matrix(array);
instance.negatate();
assertEquals(true, instance.equals(new Matrix(result)));
//fail("The test case is a prototype.");
}
/**
* Test of setZero method, of class Matrix.
*/
@Test
public void testSetZero()
{
System.out.println("setZero");
double[][] array = {{1, 0, -7}, {111, -15, 9}};
double[][] result = {{0, 0, 0}, {0, 0, 0}};
Matrix instance = new Matrix(array);
instance.setZero();
assertEquals(true, instance.equals(new Matrix(result)));
//fail("The test case is a prototype.");
}
/**
* Test of toString method, of class Matrix.
*/
@Test
public void testToString()
{
System.out.println("toString");
double[][] array = {{1, 1, 2}, {1, 0, 3}, {5, -2, 3}};
String result = "1.0 1.0 2.0 1.0 0.0 3.0 5.0 -2.0 3.0";
Matrix instance = new Matrix(array);
assertEquals(true, instance.toString().equals(result));
//fail("The test case is a prototype.");
}
/**
* Test of transpose method, of class Matrix.
*/
@Test
public void testTranspose()
{
System.out.println("transpose");
double[][] array1 = {{1, 0, 5}, {2, -1, 3}};
double[][] result = {{1, 2}, {0, -1}, {5, 3}};
Matrix instance = new Matrix(array1);
assertEquals(true, instance.transpose().equals(new Matrix(result)));
double[][] array2 = {{1, 0}, {2, -1}};
Matrix x = new Matrix(array2);
assertEquals(false, x.transpose().equals(new Matrix(array2)));
//fail("The test case is a prototype.");
}
/**
* Test of getElement method, of class Matrix.
*/
@Test
public void testGetElement()
{
System.out.println("getElement");
double[][] array = {{111, 222, 333}, {444, 555, 666}, {777, 888, 999}};
Matrix instance = new Matrix(array);
assertEquals(666, instance.getElement(1, 2), 0.0);
//fail("The test case is a prototype.");
}
/**
* Test of getRowsNumber method, of class Matrix.
*/
@Test
public void testGetRowsNumber()
{
System.out.println("getRowsNumber");
Matrix instance = new Matrix(32, 64);
assertEquals(32, instance.getRowsNumber());
//fail("The test case is a prototype.");
}
/**
* Test of getColumnNumber method, of class Matrix.
*/
@Test
public void testGetColumnNumber()
{
System.out.println("getColumnNumber");
Matrix instance = new Matrix(666);
assertEquals(666, instance.getColumnNumber());
//fail("The test case is a prototype.");
}
/**
* Test of getRow method, of class Matrix.
*/
@Test
public void testGetRow()
{
System.out.println("getRow");
double[][] array = {{1, 0, 1}, {1, 5, 0}, {20, 3, 1}};
Matrix instance = new Matrix(array);
assertEquals(true, Arrays.equals(array[2], instance.getRow(2)));
//fail("The test case is a prototype.");
}
/**
* Test of getColumn method, of class Matrix.
*/
@Test
public void testGetColumn()
{
System.out.println("getColumn");
double[][] array = {{2, 6, 10}, {-1, -7, -13}, {0, 0, 0}};
double[] result = {6, -7, 0};
Matrix instance = new Matrix(array);
assertEquals(true, Arrays.equals(result, instance.getColumn(1)));
//fail("The test case is a prototype.");
}
/**
* Test of setElement method, of class Matrix.
*/
@Test
public void testSetElement()
{
System.out.println("setElement");
double[][] array = {{4, 0, 15}, {16, 23, 0}};
double[][] result = {{4, 8, 15}, {16, 23, 42}};
Matrix instance = new Matrix(array);
instance.setElement(0, 1, 8);
instance.setElement(1, 2, 42);
assertEquals(true, instance.equals(new Matrix(result)));
//fail("The test case is a prototype.");
}
/**
* Test of setRow method, of class Matrix.
*/
@Test
public void testSetRow()
{
System.out.println("setRow");
double[][] array = {{0, 0, 0}, {1, 1, 1}, {1, 1, 1}};
double[] row1 = {2, -2, 2};
double[] row2 = {5, -7, 2};
double[] row3 = {34, -56, 78};
double[][] result = {{2, -2, 2}, {5, -7, 2}, {34, -56, 78}};
Matrix instance = new Matrix(array);
instance.setRow(0, row1);
instance.setRow(1, row2);
instance.setRow(2, row3);
assertEquals(true, instance.equals(new Matrix(result)));
//fail("The test case is a prototype.");
}
/**
* Test of setColumn method, of class Matrix.
*/
@Test
public void testSetColumn()
{
System.out.println("setColumn");
double[][] array = {{0, 1, 1}, {0, 1, 1}, {0, 1, 1}};
double[] column1 = {2, -2, 2};
double[] column2 = {5, 0, 3};
double[] column3 = {6, 8, 12};
double[][] result = {{2, 5, 6}, {-2, 0, 8}, {2, 3, 12}};
Matrix instance = new Matrix(array);
instance.setColumn(0, column1);
instance.setColumn(1, column2);
instance.setColumn(2, column3);
assertEquals(true, instance.equals(new Matrix(result)));
//fail("The test case is a prototype.");
}
/**
* Test of random method, of class Matrix.
*/
@Test
public void testRandom()
{
System.out.println("random");
Matrix instance = new Matrix(2, 3);
instance.random().printf("%1.1f ");
//fail("The test case is a prototype.");
}
/**
* Test of print method, of class Matrix.
*/
@Test
public void testPrint()
{
System.out.println("print");
double[][] array = {{2, 4, 8, 16, 32 }, {64, 128, 256, 512, 1024}};
Matrix instance = new Matrix(array);
instance.print();
//fail("The test case is a prototype.");
}
/**
* Test of printf method, of class Matrix.
*/
@Test
public void testPrintf()
{
System.out.println("printf");
double[][] array = {{2.22, 4, 8, 16.1616, 32 }, {64, 128, 256.256, 512, 1024.1024}};
Matrix instance = new Matrix(array);
instance.printf("%1.0f ");
//fail("The test case is a prototype.");
}
/**
* Test of determinant method, of class Matrix.
*/
@Test
public void testDeterminant()
{
System.out.println("determinant");
double[][] array = {{1, 1, 2}, {1, 0, 3}, {5, 2, 3}};
Matrix instance = new Matrix(array);
assertEquals(10, instance.determinant(), 0);
//fail("The test case is a prototype.");
}
/**
* Test of inverse method, of class Matrix.
*/
@Test
public void testInverse()
{
System.out.println("inverse");
double[][] array1 = {{1, -2, 0, 4, 5}, {-6, -7, 8, 9, 10},
{666, 777, 666, 777, 1024}, {777, 666, 777, 666, 777},
{1024, 777, 666, 777, 666}};
double[][] array2 = {{134, 256, 378, 490, 245}, {12, 13, 14, 15, 16},
{333, 666, 777, 888, 0}, {-123, -234, -345, -456, -567},
{1024, 2048, 5000, 7000, 1000000000}};
Matrix x = new Matrix(array1);
Matrix instance = new Matrix(array2);
Matrix expression_result = x.multiply(instance);
Matrix y = x.inverse();
assertEquals(true, y.multiply(expression_result).equals(instance));
//fail("The test case is a prototype.");
}
/**
* Test of fastMultiply method, of class Matrix.
* @throws InterruptedException
*/
@Test
public void testFastMultiply() throws InterruptedException
{
System.out.println("fastMultiply");
double[][] array1 = {{0, 2, 4, 6, 8}, {10, 12, 14, 16, 18},
{20, 22, 24, 26, 28}, {30, 32, 34, 36, 38}};
double[][] array2 = {{1, 3, 5, 7}, {9, 11, 13, 15}, {17, 19, 21, 23},
{25, 27, 29, 31}, {33, 35, 37, 39}};
double[][] result = {{500, 540, 580, 620}, {1350, 1490, 1630, 1770},
{2200, 2440, 2680, 2920}, {3050, 3390, 3730, 4070}};
Matrix x = new Matrix(array1);
Matrix instance = new Matrix(array2);
Matrix expression_result;
expression_result = x.fastMultiply(instance, 1);
assertEquals(true , expression_result.equals(new Matrix(result)));
long s1, s2, s3, s4, s5, s6;
x = new Matrix(400);
instance = new Matrix(400);
x.random();
instance.random();
s1 = System.currentTimeMillis();
expression_result = x.fastMultiply(instance, 1);
s2 = System.currentTimeMillis();
expression_result = x.fastMultiply(instance, 2);
s3 = System.currentTimeMillis();
expression_result = x.fastMultiply(instance, 3);
s4 = System.currentTimeMillis();
expression_result = x.fastMultiply(instance, 4);
s5 = System.currentTimeMillis();
expression_result = x.fastMultiply(instance, 100);
s6 = System.currentTimeMillis();
System.out.println("1 thread: " + (s2 - s1));
System.out.println("2_threads: " + (s3 - s2));
System.out.println("3_threads: " + (s4 - s3));
System.out.println("4_threads: " + (s5 - s4));
System.out.println("100_threads: " + (s6 - s5));
//fail("The test case is a prototype.");
}
}