KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > algebra > Matrix


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.examples.algebra;
32
33 import org.apache.log4j.Logger;
34
35 public class Matrix implements java.io.Serializable JavaDoc {
36     
37     static Logger logger = Logger.getLogger(Matrix.class.getName());
38
39   int m; // height
40
int n; // width
41
double[][] elements; // values
42

43   public Matrix() {
44     super();
45   }
46
47
48   public Matrix(int _m, int _n) {
49     super();
50     this.m = _m;
51     this.n = _n;
52     this.elements = new double[_m][_n];
53   }
54
55
56   public Matrix(int _n) {
57     this(_n, _n);
58   }
59
60
61   public Matrix(Matrix _m) {
62     this(_m.getHeight(), _m.getWidth());
63     logger.info("Matrix: constructor (Matrix _m)");
64     int i;
65     Row r;
66
67     for (i = 0; i < m; i++) {
68       r = _m.getRow(i);
69       this.setRow(i, r);
70     }
71   }
72
73
74   public Matrix(Column c_) {
75     this(c_.getSize(), 1);
76     this.setColumn(0, c_);
77   }
78
79
80   public Matrix(Row r_) {
81     this(1, r_.getSize());
82     this.setRow(0, r_);
83   }
84
85
86   public Matrix identity(int n) {
87     // Returns an I matrix of size n
88
int i;
89     Matrix m;
90
91     m = new Matrix(n);
92
93     for (i = 0; i < n; i++) {
94       m.setElement(i, i, 1.0);
95     }
96     return m;
97   }
98
99
100   public Matrix identity() {
101     Matrix result;
102     if (this.m != this.n)
103       return null;
104     result = identity(this.m);
105     return result;
106   }
107
108
109   public double makeDiagOne(int i) {
110     double a;
111     int j;
112
113     a = this.getElement(i, i);
114     if (a == 0)
115       return a;
116
117     for (j = 0; j < this.n; j++) {
118       this.setElement(i, j, this.getElement(i, j) / a);
119     }
120     return a;
121   }
122
123
124   public /*synchronized*/ Matrix transpose() {
125     Matrix mat;
126     int i,j;
127
128     mat = new Matrix(this.n, this.m); // m and n are swapped
129

130     for (i = 0; i < m; i++) {
131       for (j = 0; j < n; j++) {
132         mat.setElement(j, i, this.getElement(i, j));
133       }
134     }
135
136     return mat;
137   }
138
139
140   public /*synchronized*/ void display() {
141     int i,j;
142
143     for (i = 0; i < this.m; i++) {
144       logger.info(i + " |");
145       for (j = 0; j < this.n; j++) {
146         logger.info(this.getElement(i, j) + " ");
147       }
148       logger.info("|");
149     }
150
151     System.out.println();
152
153     return;
154   }
155
156
157   public void randomizeFillIn() {
158     int i,j;
159
160     for (i = 0; i < this.m; i++) {
161       for (j = 0; j < this.n; j++) {
162         this.setElement(i, j, Math.random());
163       }
164     }
165     return;
166   }
167
168
169   public /*synchronized*/ void setElement(int i, int j, double x) {
170     this.elements[i][j] = x;
171     return;
172   }
173
174
175   public /*synchronized*/ double getElement(int i, int j) {
176     return this.elements[i][j];
177   }
178
179
180   public /*synchronized*/ void setRow(int a, Vector r) {
181     int j;
182
183     for (j = 0; j < this.n; j++) {
184       this.setElement(a, j, r.getElement(j));
185     }
186     return;
187   }
188
189
190   public /*synchronized*/ Row getRow(int i) {
191     Row r;
192     int j;
193
194     r = new Row(this.n);
195     for (j = 0; j < this.n; j++) {
196       r.setElement(j, this.getElement(i, j));
197     }
198
199     return r;
200   }
201
202
203   public /*synchronized*/ void setColumn(int a, Vector c) {
204     int i;
205
206     for (i = 0; i < this.m; i++) {
207       this.setElement(i, a, c.getElement(i));
208     }
209     return;
210   }
211
212
213   public /*synchronized*/ Column getColumn(int j) {
214     Column c;
215     int i;
216
217     c = new Column(this.m);
218     for (i = 0; i < this.m; i++) {
219       c.setElement(i, this.getElement(i, j));
220     }
221
222     return c;
223   }
224
225
226   public Matrix getBlock(int i0, int j0, int i1, int j1) {
227     Matrix result;
228     int i,j;
229
230     result = new Matrix((i1 - i0 + 1), (j1 - j0 + 1));
231
232     for (i = i0; i <= i1; i++) {
233       for (j = j0; j <= j1; j++) {
234         result.setElement((i - i0), (j - j0), this.getElement(i, j));
235       }
236     }
237
238     return result;
239   }
240
241
242   public void setBlock(int i0, int j0, Matrix m) {
243     int i,j;
244
245     for (i = 0; i < m.getHeight(); i++) {
246       for (j = 0; j < m.getWidth(); j++) {
247         this.setElement(i + i0, j + j0, m.getElement(i, j));
248       }
249     }
250     return;
251   }
252
253
254   public void swapRows(int a, int b) {
255     Row firstrow, lastrow;
256
257     firstrow = this.getRow(a);
258     lastrow = this.getRow(b);
259
260     this.setRow(a, lastrow);
261     this.setRow(b, firstrow);
262
263     return;
264   }
265
266
267   public void swapColumns(int a, int b) {
268     Column firstcol, lastcol;
269
270     firstcol = this.getColumn(a);
271     lastcol = this.getColumn(b);
272
273     this.setColumn(a, lastcol);
274     this.setColumn(b, firstcol);
275
276     return;
277   }
278
279
280   public Matrix rightProduct(Matrix m) {
281     // Let's verify dimensions (product is this*m)
282
int i,j,k;
283     Matrix result;
284     double s;
285
286     if (this.getWidth() != m.getHeight())
287       return null;
288
289     result = new Matrix(this.getHeight(), m.getWidth());
290
291     for (i = 0; i < result.getHeight(); i++) {
292       for (j = 0; j < result.getWidth(); j++) {
293         s = 0;
294         for (k = 0; k < m.getHeight(); k++) {
295           s = s + this.getElement(i, k) * m.getElement(k, j);
296         }
297         result.setElement(i, j, s);
298       }
299     }
300
301     return result;
302   }
303
304
305   public Vector rightProduct(Vector v) {
306     Vector result = new Vector(this.getHeight());
307     int i,j;
308     double s;
309
310     for (i = 0; i < this.getHeight(); i++) {
311       s = 0;
312       for (j = 0; j < this.getWidth(); j++) {
313         s = s + this.getElement(i, j) * v.getElement(j);
314       }
315       result.setElement(i, s);
316     }
317
318     return result;
319   }
320
321
322   public int getWidth() {
323     return this.n;
324   }
325
326
327   public int getHeight() {
328     return this.m;
329   }
330
331
332   public /*synchronized*/ void rowLC(int firstrow, int secondrow, double alpha, double beta) {
333     int j;
334
335     for (j = 0; j < this.n; j++) {
336       this.setElement(firstrow, j, alpha * this.getElement(firstrow, j) + beta * this.getElement(secondrow, j));
337     }
338     return;
339   }
340
341
342   public /*synchronized*/ void columnLC(int firstcol, int secondcol, double alpha, double beta) {
343     int i;
344
345     for (i = 0; i < this.m; i++) {
346       this.setElement(i, firstcol, alpha * this.getElement(i, firstcol) + beta * this.getElement(i, secondcol));
347     }
348     return;
349   }
350
351
352   public Matrix getInverse() {
353     Matrix source, result;
354     int i, p, r, s;
355     double alpha,beta, gamma;
356
357     source = new Matrix(this);
358
359     if (source.m != source.n)
360       return null; // is a square matrix ?
361
s = source.m;
362
363     result = identity(s);
364
365     for (r = 0; r < s; r++) {
366       // Choix du pivot
367
p = source.findPivot(r);
368
369       source.swapRows(r, p); // Le pivot est maintenant sur la ligne courante (ligne r)
370
result.swapRows(r, p);
371     
372       // On normalise la ligne du pivot
373
gamma = source.makeDiagOne(r);
374       result.rowLC(r, 0, 1 / gamma, 0); // On multiplie de la meme maniere sur l'autre
375

376       // On elimine les zeros dans la colonne du pivot, en dessous ET en dessus de la
377
// ligne courante
378
for (i = 0; i < r; i++) {
379         alpha = 1;
380         beta = (-1) * source.getElement(i, r) / source.getElement(r, r);
381         source.rowLC(i, r, alpha, beta);
382         result.rowLC(i, r, alpha, beta);
383       }
384       for (i = r + 1; i < s; i++) {
385         alpha = 1;
386         beta = (-1) * source.getElement(i, r) / source.getElement(r, r);
387         source.rowLC(i, r, alpha, beta);
388         result.rowLC(i, r, alpha, beta);
389       }
390
391     }
392     logger.info("Inversion terminee pour n = " + this.m);
393
394     return result;
395   }
396
397
398   private int findPivot(int r) {
399     // Find pivot row for rank r
400
int i, result;
401
402     result = r;
403     for (i = r; i < this.m; i++) {
404       if (Math.abs(this.getElement(i, r)) > result)
405         result = i;
406     }
407     return result;
408   }
409
410
411   public double distance(Matrix mat) {
412     double result = 0;
413     double temp = 0;
414     int i,j;
415
416     if (this.m != mat.getHeight())
417       return -1;
418     if (this.n != mat.getWidth())
419       return -1;
420
421     for (i = 0; i < this.m; i++) {
422       for (j = 0; j < this.n; j++) {
423         temp = Math.abs(this.getElement(i, j) - mat.getElement(i, j));
424         if (temp > result)
425           result = temp;
426       }
427     }
428     return temp;
429   }
430
431
432   public double trace() {
433     double result = 0;
434     int i;
435
436     if (this.m != this.n)
437       return 0;
438     for (i = 0; i < m; i++) {
439       result = result + this.getElement(i, i);
440     }
441     return result;
442   }
443 }
444
445
Popular Tags