My favorites | Sign in
Logo
Project hosting will be READ-ONLY Wednesday at 8am PST due to brief network maintenance.
                
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
package Annas.Math;

import java.text.DecimalFormat;
import java.util.concurrent.Semaphore;

public class Matrix {

private boolean DEBUG = false;

private boolean INFO = false;

public boolean useParallel = false;

public int MAXTHREADS = 10;

private int iDF = 0;

private float[][] matrix;

public Matrix(Matrix m) {
this.matrix = m.getMatrix();
}

public Matrix(int size) {
matrix = new float[size][size];
}

public Matrix(int sizeX, int sizeY) {
matrix = new float[sizeY][sizeX];
}

public Matrix(float[][] m) {
matrix = m;
}

public void SetMatrix(float[][] m) {
if (m.length == matrix.length && m[0].length == matrix[0].length) {
this.matrix = m;
}
}

public float[][] getMatrix() {
return this.matrix;
}

public static Matrix createIdentity(int i) {
float[][] g = new float[i][i];
for (int j = 0; j < i; j++) {
for (int k = 0; k < i; k++) {
if (j == k) {

g[j][k] = 1;
} else {
g[j][k] = 0;
}
}
}
return new Matrix(g);
}

public void print() {
for (int i = 0; i < matrix.length; i++) {
System.out.print("{");
for (int j = 0; j < matrix[0].length; j++) {
if (j < matrix[0].length - 1) {
System.out.print(" " + matrix[i][j] + ",");
} else {
System.out.print(" " + matrix[i][j]);
}

}
System.out.println(" }");

}

}

public void printformatted1(String Pattern) {
DecimalFormat Formatter = new DecimalFormat(Pattern);

for (int i = 0; i < matrix.length; i++) {
System.out.print("{");
for (int j = 0; j < matrix[0].length; j++) {
if (j < matrix[0].length - 1) {
System.out
.print(" " + Formatter.format(matrix[i][j]) + ",");
} else {
System.out.print(" " + Formatter.format(matrix[i][j]));
}

}
System.out.println(" }");

}

}

public Matrix MultiplyMatrix(Matrix mb) throws Exception {
if (this.useParallel) {
return this.ParMultiplyMatrix(mb, 100, 100);
} else {
return this.SeqMultiplyMatrix(mb);
}
}

private Matrix ParMultiplyMatrix(Matrix Mb, int granX, int granY)
throws Exception {
float[][] b = Mb.getMatrix();
float[][] a = this.matrix;
if (a[0].length != b.length)
throw new Exception("Matrices incompatible for multiplication");
float matrix[][] = new float[a.length][b[0].length];

for (int i = 0; i < a.length; i = i + granX)
for (int j = 0; j < b[i].length; j = j + granY)
matrix[i][j] = 0;

// cycle through answer matrix
Matrix res = new Matrix(matrix);
Semaphore s = new Semaphore(matrix.length);
for (int i = 0; i < matrix.length / granX; i++) {
for (int j = 0; j < matrix[0].length / granY; j++) {
// matrix[i][j] = calculateRowColumnProduct(a,i,b,j);
Thread f = new Product(a, i * granX, b, j * granY, res, s,
granX, granY);
f.start();
}
}
s.acquire(matrix.length);
return new Matrix(matrix);
}

private Matrix SeqMultiplyMatrix(Matrix Mb) throws Exception {
float[][] b = Mb.getMatrix();
float[][] a = this.matrix;
if (a[0].length != b.length)
throw new Exception("Matrices incompatible for multiplication");
float matrix[][] = new float[a.length][b[0].length];

for (int i = 0; i < a.length; i++)
for (int j = 0; j < b[i].length; j++)
matrix[i][j] = 0;

// cycle through answer matrix
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = calculateRowColumnProduct(a, i, b, j);

}
}

return new Matrix(matrix);
}

public float calculateRowColumnProduct(float[][] A, int row, float[][] B,
int col) {
float product = 0;
for (int i = 0; i < A[row].length; i++)
product += A[row][i] * B[i][col];
return product;
}

// --------------------------------------------------------------

public float[][] Transpose() {
float[][] a = this.matrix;

if (INFO) {
System.out.println("Performing Transpose...");
}

float m[][] = new float[a[0].length][a.length];

for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[i].length; j++)
m[j][i] = a[i][j];
return m;
}

private float[][] Transpose(float[][] a) {

if (INFO) {
System.out.println("Performing Transpose...");
}

float m[][] = new float[a[0].length][a.length];

for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[i].length; j++)
m[j][i] = a[i][j];
return m;
}

// --------------------------------------------------------------

public Matrix Inverse() throws Exception {
float[][] a = this.matrix;
// Formula used to Calculate Inverse:
// inv(A) = 1/det(A) * adj(A)
if (INFO) {
System.out.println("Performing Inverse...");
}
int tms = a.length;

float m[][] = new float[tms][tms];
float mm[][] = Adjoint(a);

float det = Determinant(a);
float dd = 0;

if (det == 0) {

if (INFO) {
System.out.println("Determinant Equals 0, Not Invertible.");
}
} else {
dd = 1 / det;
}

for (int i = 0; i < tms; i++)
for (int j = 0; j < tms; j++) {
m[i][j] = dd * mm[i][j];
}

return new Matrix(m);
}

// --------------------------------------------------------------

public float[][] Adjoint() throws Exception {
float[][] a = this.matrix;

if (INFO) {
System.out.println("Performing Adjoint...");
}
int tms = a.length;

float m[][] = new float[tms][tms];

int ii, jj, ia, ja;
float det;

for (int i = 0; i < tms; i++)
for (int j = 0; j < tms; j++) {
ia = ja = 0;

float ap[][] = new float[tms - 1][tms - 1];

for (ii = 0; ii < tms; ii++) {
for (jj = 0; jj < tms; jj++) {

if ((ii != i) && (jj != j)) {
ap[ia][ja] = a[ii][jj];
ja++;
}

}
if ((ii != i) && (jj != j)) {
ia++;
}
ja = 0;
}

det = Determinant(ap);
m[i][j] = (float) Math.pow(-1, i + j) * det;
}

m = Transpose(m);

return m;
}

private float[][] Adjoint(float[][] a) throws Exception {

if (INFO) {
System.out.println("Performing Adjoint...");
}
int tms = a.length;

float m[][] = new float[tms][tms];

int ii, jj, ia, ja;
float det;

for (int i = 0; i < tms; i++)
for (int j = 0; j < tms; j++) {
ia = ja = 0;

float ap[][] = new float[tms - 1][tms - 1];

for (ii = 0; ii < tms; ii++) {
for (jj = 0; jj < tms; jj++) {

if ((ii != i) && (jj != j)) {
ap[ia][ja] = a[ii][jj];
ja++;
}

}
if ((ii != i) && (jj != j)) {
ia++;
}
ja = 0;
}

det = Determinant(ap);
m[i][j] = (float) Math.pow(-1, i + j) * det;
}

m = Transpose(m);

return m;
}

// --------------------------------------------------------------

public float[][] UpperTriangle() {
float[][] m = this.matrix;

if (INFO) {
System.out.println("Converting to Upper Triangle...");
}

float f1 = 0;
float temp = 0;
int tms = m.length; // get This Matrix Size (could be smaller than
// global)
int v = 1;

iDF = 1;

for (int col = 0; col < tms - 1; col++) {
for (int row = col + 1; row < tms; row++) {
v = 1;

outahere: while (m[col][col] == 0) // check if 0 in diagonal
{ // if so switch until not
if (col + v >= tms) // check if switched all rows
{
iDF = 0;
break outahere;
} else {
for (int c = 0; c < tms; c++) {
temp = m[col][c];
m[col][c] = m[col + v][c]; // switch rows
m[col + v][c] = temp;
}
v++; // count row switchs
iDF = iDF * -1; // each switch changes determinant
// factor
}
}

if (m[col][col] != 0) {
if (DEBUG) {
System.out.println("tms = " + tms + " col = " + col
+ " row = " + row);
}

try {
f1 = (-1) * m[row][col] / m[col][col];
for (int i = col; i < tms; i++) {
m[row][i] = f1 * m[col][i] + m[row][i];
}
} catch (Exception e) {
System.out.println("Still Here!!!");
}

}

}
}

return m;
}

private float[][] UpperTriangle(float[][] m) {

if (INFO) {
System.out.println("Converting to Upper Triangle...");
}

float f1 = 0;
float temp = 0;
int tms = m.length; // get This Matrix Size (could be smaller than
// global)
int v = 1;

iDF = 1;

for (int col = 0; col < tms - 1; col++) {
for (int row = col + 1; row < tms; row++) {
v = 1;

outahere: while (m[col][col] == 0) // check if 0 in diagonal
{ // if so switch until not
if (col + v >= tms) // check if switched all rows
{
iDF = 0;
break outahere;
} else {
for (int c = 0; c < tms; c++) {
temp = m[col][c];
m[col][c] = m[col + v][c]; // switch rows
m[col + v][c] = temp;
}
v++; // count row switchs
iDF = iDF * -1; // each switch changes determinant
// factor
}
}

if (m[col][col] != 0) {
if (DEBUG) {
System.out.println("tms = " + tms + " col = " + col
+ " row = " + row);
}

try {
f1 = (-1) * m[row][col] / m[col][col];
for (int i = col; i < tms; i++) {
m[row][i] = f1 * m[col][i] + m[row][i];
}
} catch (Exception e) {
System.out.println("Still Here!!!");
}

}

}
}

return m;
}

// --------------------------------------------------------------

public float Determinant() {
float[][] matrix = this.matrix;

if (INFO) {
System.out.println("Getting Determinant...");
}
int tms = matrix.length;

float det = 1;

matrix = UpperTriangle(matrix);

for (int i = 0; i < tms; i++) {
det = det * matrix[i][i];
} // multiply down diagonal

det = det * iDF; // adjust w/ determinant factor

if (INFO) {
System.out.println("Determinant: " + det);
}
return det;
}

private float Determinant(float[][] matrix) {

if (INFO) {
System.out.println("Getting Determinant...");
}
int tms = matrix.length;

float det = 1;

matrix = UpperTriangle(matrix);

for (int i = 0; i < tms; i++) {
det = det * matrix[i][i];
} // multiply down diagonal

det = det * iDF; // adjust w/ determinant factor

if (INFO) {
System.out.println("Determinant: " + det);
}
return det;
}

public Matrix addMatrix(Matrix m) throws Exception {
if ((m.matrix.length == this.matrix.length)
&& (m.matrix[0].length == this.matrix[0].length)) {
Matrix retval = new Matrix(this.matrix.length);
for (int i = 0; i < this.matrix[0].length; i++) {
for (int j = 0; j < this.matrix.length; j++) {
retval.matrix[i][j] = this.matrix[i][j] + m.matrix[i][j];
}
}
return retval;
} else {
throw new Exception();
}

}

public Matrix subtractMatrix(Matrix m) throws Exception {
if ((m.matrix.length == this.matrix.length)
&& (m.matrix[0].length == this.matrix[0].length)) {
Matrix retval = new Matrix(this.matrix.length);
for (int i = 0; i < this.matrix[0].length; i++) {
for (int j = 0; j < this.matrix.length; j++) {
retval.matrix[i][j] = this.matrix[i][j] - m.matrix[i][j];
}
}
return retval;
} else {
throw new Exception();
}

}

public Matrix divide(Matrix j) {
try {
Matrix mi = this.Inverse();
mi = mi.MultiplyMatrix(j);
return mi;
} catch (Exception e) {
throw new RuntimeException("Invalid Matrix operation.");
}
}

public boolean eq(Matrix B) {
Matrix A = this;
if (B.getMatrix().length != getMatrix().length
|| B.getMatrix()[0].length != getMatrix()[0].length)
throw new RuntimeException("Illegal matrix dimensions.");
for (int i = 0; i < this.getMatrix().length; i++)
for (int j = 0; j < this.getMatrix()[0].length; j++)
if (A.getMatrix()[i][j] != B.getMatrix()[i][j])
return false;
return true;
}

public class Product extends Thread {

private float[][] A;
private int row;
private float[][] B;
private int col;
private Matrix res;
private Semaphore s;
private int width;
private int height;

/**
* @param a
* @param row
* @param b
* @param col
* @param res
*/
public Product(float[][] a, int row, float[][] b, int col, Matrix res,
Semaphore s, int width, int height) {
super();
this.A = a;
this.row = row;
this.B = b;
this.col = col;
this.res = res;
this.s = s;
this.width = width;
this.height = height;
try {
this.s.acquire();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void run() {
for (int i = this.row; i < this.row + this.width; i++) {
for (int j = this.col; j < this.col + this.height; j++) {
this.res.matrix[i][j] = this.calculateRowColumnProduct(A,
i, B, j);

}
}
this.s.release();
}

public float calculateRowColumnProduct(float[][] A, int row,
float[][] B, int col) {
float product = 0;
for (int i = 0; i < A[row].length; i++)
product += A[row][i] * B[i][col];
return product;
}
}
}
Show details Hide details

Change log

r164 by samwilson001 on Jul 24, 2009   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

r163 by samwilson001 on Jul 23, 2009   Diff
[No log message]
r142 by samwilson001 on Jun 15, 2009   Diff
[No log message]
r98 by samwilson001 on Apr 04, 2009   Diff
[No log message]
All revisions of this file

File info

Size: 13391 bytes, 595 lines
Hosted by Google Code