KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > matrices > DoubleMatrix


1 /* AUTO-GENERATED */
2 package JSci.maths.matrices;
3
4 import JSci.maths.ExtraMath;
5 import JSci.maths.Mapping;
6 import JSci.maths.DimensionException;
7 import JSci.maths.vectors.AbstractDoubleVector;
8 import JSci.maths.vectors.DoubleVector;
9 import JSci.maths.groups.AbelianGroup;
10 import JSci.maths.algebras.*;
11 import JSci.maths.fields.*;
12
13 /**
14 * The DoubleMatrix class provides an object for encapsulating double matrices.
15 * @version 2.2
16 * @author Mark Hale
17 */

18 public class DoubleMatrix extends AbstractDoubleMatrix {
19         /**
20         * Array containing the elements of the matrix.
21         */

22         protected final double matrix[][];
23         /**
24         * Constructs a matrix by wrapping an array.
25         * @param array an assigned value
26         */

27         public DoubleMatrix(final double array[][]) {
28                 super(array.length, array[0].length);
29                 matrix=array;
30         }
31         /**
32         * Constructs an empty matrix.
33         */

34         public DoubleMatrix(final int rows,final int cols) {
35                 this(new double[rows][cols]);
36         }
37         /**
38         * Constructs a matrix from an array of vectors (columns).
39         * @param array an assigned value
40         */

41         public DoubleMatrix(final AbstractDoubleVector array[]) {
42                 this(array[0].dimension(), array.length);
43                 for(int i=0;i<numRows;i++) {
44                         for(int j=0;j<numCols;j++)
45                                 matrix[i][j]=array[j].getComponent(i);
46                 }
47         }
48         /**
49         * Compares two ${nativeTyp} matrices for equality.
50         * @param m a double matrix
51         */

52         public boolean equals(AbstractDoubleMatrix m, double tol) {
53                 if(m != null && numRows == m.rows() && numCols == m.columns()) {
54             double sumSqr = 0;
55                         for(int i=0;i<numRows;i++) {
56                                 for(int j=0;j<numCols;j++) {
57                     double delta = matrix[i][j]-m.getElement(i,j);
58                     sumSqr += delta*delta;
59                                 }
60                         }
61                         return (sumSqr <= tol*tol);
62                 } else {
63                         return false;
64                 }
65         }
66         /**
67         * Returns a string representing this matrix.
68         */

69         public String JavaDoc toString() {
70                 final StringBuffer JavaDoc buf=new StringBuffer JavaDoc(5*numRows*numCols);
71                 for(int i=0;i<numRows;i++) {
72                         for(int j=0;j<numCols;j++) {
73                                 buf.append(matrix[i][j]);
74                                 buf.append(' ');
75                         }
76                         buf.append('\n');
77                 }
78                 return buf.toString();
79         }
80         /**
81         * Converts this matrix to an integer matrix.
82         * @return an integer matrix
83         */

84         public AbstractIntegerMatrix toIntegerMatrix() {
85                 final int ans[][]=new int[numRows][numCols];
86                 for(int i=0;i<numRows;i++) {
87                         for(int j=0;j<numCols;j++)
88                                 ans[i][j]=Math.round((float)matrix[i][j]);
89                 }
90                 return new IntegerMatrix(ans);
91         }
92         /**
93         * Converts this matrix to a complex matrix.
94         * @return a complex matrix
95         */

96         public AbstractComplexMatrix toComplexMatrix() {
97                 ComplexMatrix cm = new ComplexMatrix(numRows, numCols);
98                 for(int i=0;i<numRows;i++) {
99                         for(int j=0;j<numCols;j++)
100                                 cm.setElement(i, j, matrix[i][j], 0.0);
101                 }
102                 return cm;
103         }
104         /**
105         * Returns an element of the matrix.
106         * @param i row index of the element
107         * @param j column index of the element
108         * @exception MatrixDimensionException If attempting to access an invalid element.
109         */

110         public double getElement(int i, int j) {
111                 if(i>=0 && i<numRows && j>=0 && j<numCols)
112                         return matrix[i][j];
113                 else
114                         throw new MatrixDimensionException(getInvalidElementMsg(i,j));
115         }
116         /**
117         * Sets the value of an element of the matrix.
118         * Should only be used to initialise this matrix.
119         * @param i row index of the element
120         * @param j column index of the element
121         * @param x a number
122         * @exception MatrixDimensionException If attempting to access an invalid element.
123         */

124         public void setElement(int i, int j, double x) {
125                 if(i>=0 && i<numRows && j>=0 && j<numCols)
126                         matrix[i][j]=x;
127                 else
128                         throw new MatrixDimensionException(getInvalidElementMsg(i,j));
129         }
130         /**
131         * Returns the l<sup><img border=0 alt="infinity" SRC="doc-files/infinity.gif"></sup>-norm.
132         * @author Taber Smith
133         */

134         public double infNorm() {
135                 double result=0,tmpResult;
136                 for(int i=0;i<numRows;i++) {
137                         tmpResult=0;
138                         for(int j=0;j<numCols;j++)
139                                 tmpResult+=Math.abs(matrix[i][j]);
140                         if(tmpResult>result)
141                                 result=tmpResult;
142                 }
143                 return result;
144         }
145         /**
146         * Returns the Frobenius or Hilbert-Schmidt (l<sup>2</sup>) norm.
147         * @jsci.planetmath FrobeniusMatrixNorm
148         */

149         public double frobeniusNorm() {
150                 double result=0.0;
151                 for(int j,i=0;i<numRows;i++) {
152                         for(j=0;j<numCols;j++)
153                                 result=ExtraMath.hypot(result, matrix[i][j]);
154                 }
155                 return result;
156         }
157
158 //============
159
// OPERATIONS
160
//============
161

162         /**
163         * Returns the negative of this matrix.
164         */

165         public AbelianGroup.Member negate() {
166                 final double array[][]=new double[numRows][numCols];
167                 for(int i=0;i<numRows;i++) {
168                         array[i][0] = -matrix[i][0];
169                         for(int j=1;j<numCols;j++)
170                                 array[i][j] = -matrix[i][j];
171                 }
172                 return new DoubleMatrix(array);
173         }
174
175 // ADDITION
176

177         /**
178         * Returns the addition of this matrix and another.
179         * @param m a double matrix
180         * @exception MatrixDimensionException If the matrices are different sizes.
181         */

182         public AbstractDoubleMatrix add(final AbstractDoubleMatrix m) {
183                 if(numRows==m.rows() && numCols==m.columns()) {
184                         final double array[][]=new double[numRows][numCols];
185                         for(int i=0;i<numRows;i++) {
186                                 array[i][0] = matrix[i][0]+m.getElement(i,0);
187                                 for(int j=1;j<numCols;j++)
188                                         array[i][j] = matrix[i][j]+m.getElement(i,j);
189                         }
190                         return new DoubleMatrix(array);
191                 } else {
192                         throw new MatrixDimensionException("Matrices are different sizes.");
193                 }
194         }
195
196 // SUBTRACTION
197

198         /**
199         * Returns the subtraction of this matrix by another.
200         * @param m a double matrix
201         * @exception MatrixDimensionException If the matrices are different sizes.
202         */

203         public AbstractDoubleMatrix subtract(final AbstractDoubleMatrix m) {
204                 if(numRows==m.rows() && numCols==m.columns()) {
205                         final double array[][]=new double[numRows][numCols];
206                         for(int i=0;i<numRows;i++) {
207                                 array[i][0] = matrix[i][0]-m.getElement(i,0);
208                                 for(int j=1;j<numCols;j++)
209                                         array[i][j] = matrix[i][j]-m.getElement(i,j);
210                         }
211                         return new DoubleMatrix(array);
212                 } else {
213                         throw new MatrixDimensionException("Matrices are different sizes.");
214                 }
215         }
216
217 // SCALAR MULTIPLICATION
218

219         /**
220         * Returns the multiplication of this matrix by a scalar.
221         * @param x a double.
222         * @return a double matrix.
223         */

224         public AbstractDoubleMatrix scalarMultiply(final double x) {
225                 final double array[][]=new double[numRows][numCols];
226                 for(int i=0;i<numRows;i++) {
227                         array[i][0] = x*matrix[i][0];
228                         for(int j=1;j<numCols;j++)
229                                 array[i][j] = x*matrix[i][j];
230                 }
231                 return new DoubleMatrix(array);
232         }
233
234 // SCALAR DIVISON
235

236         /**
237         * Returns the division of this matrix by a scalar.
238         * @param x a double.
239         * @return a double matrix.
240         */

241         public AbstractDoubleMatrix scalarDivide(final double x) {
242                 final double array[][]=new double[numRows][numCols];
243                 for(int i=0;i<numRows;i++) {
244                         array[i][0] = matrix[i][0]/x;
245                         for(int j=1;j<numCols;j++)
246                                 array[i][j] = matrix[i][j]/x;
247                 }
248                 return new DoubleMatrix(array);
249         }
250
251 // SCALAR PRODUCT
252

253         /**
254         * Returns the scalar product of this matrix and another.
255         * @param m a double matrix.
256         * @exception MatrixDimensionException If the matrices are different sizes.
257         */

258         public double scalarProduct(final AbstractDoubleMatrix m) {
259                 if(m instanceof DoubleMatrix)
260                         return scalarProduct((DoubleMatrix)m);
261
262                 if(numRows==m.rows() && numCols==m.columns()) {
263                         double ans=0;
264                         for(int i=0;i<numRows;i++) {
265                                 ans += matrix[i][0]*m.getElement(i,0);
266                                 for(int j=1;j<numCols;j++)
267                                         ans += matrix[i][j]*m.getElement(i,j);
268                         }
269                         return ans;
270                 } else {
271                        throw new MatrixDimensionException("Matrices are different sizes.");
272                 }
273         }
274         public double scalarProduct(final DoubleMatrix m) {
275                 if(numRows==m.numRows && numCols==m.numCols) {
276                         double ans=0;
277                         for(int i=0;i<numRows;i++) {
278                                 ans+=matrix[i][0]*m.matrix[i][0];
279                                 for(int j=1;j<numCols;j++)
280                                         ans+=matrix[i][j]*m.matrix[i][j];
281                         }
282                         return ans;
283                 } else
284                         throw new MatrixDimensionException("Matrices are different sizes.");
285         }
286
287 // MATRIX MULTIPLICATION
288

289         /**
290         * Returns the multiplication of a vector by this matrix.
291         * @param v a double vector.
292         * @exception DimensionException If the matrix and vector are incompatible.
293         */

294         public AbstractDoubleVector multiply(final AbstractDoubleVector v) {
295                 if(numCols==v.dimension()) {
296                         final double array[]=new double[numRows];
297                         for(int i=0;i<numRows;i++) {
298                                 array[i]=matrix[i][0]*v.getComponent(0);
299                                 for(int j=1;j<numCols;j++)
300                                         array[i]+=matrix[i][j]*v.getComponent(j);
301                         }
302                         return new DoubleVector(array);
303                 } else {
304                         throw new DimensionException("Matrix and vector are incompatible.");
305                 }
306         }
307         /**
308         * Returns the multiplication of this matrix and another.
309         * @param m a double matrix
310         * @return a AbstractDoubleMatrix or a AbstractDoubleSquareMatrix as appropriate
311         * @exception MatrixDimensionException If the matrices are incompatible.
312         */

313         public AbstractDoubleMatrix multiply(final AbstractDoubleMatrix m) {
314                 if(m instanceof DoubleMatrix)
315                         return multiply((DoubleMatrix)m);
316
317                 if(numCols==m.rows()) {
318                         final int mColumns = m.columns();
319                         final double array[][]=new double[numRows][mColumns];
320                         for(int j=0; j<numRows; j++) {
321                                 for(int k=0; k<mColumns; k++) {
322                                         array[j][k] = matrix[j][0]*m.getElement(0,k);
323                                         for(int n=1; n<numCols; n++)
324                                                 array[j][k] += matrix[j][n]*m.getElement(n,k);
325                                 }
326                         }
327                         if(numRows == mColumns)
328                                 return new DoubleSquareMatrix(array);
329                         else
330                                 return new DoubleMatrix(array);
331                 } else {
332                         throw new MatrixDimensionException("Incompatible matrices.");
333                 }
334         }
335         public AbstractDoubleMatrix multiply(final DoubleMatrix m) {
336                 if(numCols==m.numRows) {
337                         final double array[][]=new double[numRows][m.numCols];
338                         for(int j=0;j<numRows;j++) {
339                                 for(int k=0;k<m.numCols;k++) {
340                                         array[j][k]=matrix[j][0]*m.matrix[0][k];
341                                         for(int n=1;n<numCols;n++)
342                                                 array[j][k]+=matrix[j][n]*m.matrix[n][k];
343                                 }
344                         }
345                         if(numRows == m.numCols)
346                                 return new DoubleSquareMatrix(array);
347                         else
348                                 return new DoubleMatrix(array);
349                 } else
350                         throw new MatrixDimensionException("Incompatible matrices.");
351         }
352
353 // DIRECT SUM
354

355         /**
356         * Returns the direct sum of this matrix and another.
357         */

358         public AbstractDoubleMatrix directSum(final AbstractDoubleMatrix m) {
359                 final double array[][]=new double[numRows+m.numRows][numCols+m.numCols];
360                 for(int i=0;i<numRows;i++) {
361                         for(int j=0;j<numCols;j++)
362                                 array[i][j] = matrix[i][j];
363                 }
364                 for(int i=0;i<m.numRows;i++) {
365                         for(int j=0;j<m.numCols;j++)
366                                 array[i+numRows][j+numCols] = m.getElement(i,j);
367                 }
368                 return new DoubleMatrix(array);
369         }
370
371 // TENSOR PRODUCT
372

373         /**
374         * Returns the tensor product of this matrix and another.
375         */

376         public AbstractDoubleMatrix tensor(final AbstractDoubleMatrix m) {
377                 final double array[][]=new double[numRows*m.numRows][numCols*m.numCols];
378                 for(int i=0;i<numRows;i++) {
379                         for(int j=0;j<numCols;j++) {
380                                 for(int k=0;k<m.numRows;j++) {
381                                         for(int l=0;l<m.numCols;l++)
382                                                 array[i*m.numRows+k][j*m.numCols+l] = matrix[i][j]*m.getElement(k,l);
383                                 }
384                         }
385                 }
386                 return new DoubleMatrix(array);
387         }
388
389 // TRANSPOSE
390

391         /**
392         * Returns the transpose of this matrix.
393         * @return a double matrix
394         */

395         public Matrix transpose() {
396                 final double array[][]=new double[numCols][numRows];
397                 for(int i=0;i<numRows;i++) {
398                         array[0][i] = matrix[i][0];
399                         for(int j=1;j<numCols;j++)
400                                 array[j][i] = matrix[i][j];
401                 }
402                 return new DoubleMatrix(array);
403         }
404
405 // MAP ELEMENTS
406

407         /**
408         * Applies a function on all the matrix elements.
409         * @param f a user-defined function
410         * @return a double matrix
411         */

412         public AbstractDoubleMatrix mapElements(final Mapping f) {
413                 final double array[][]=new double[numRows][numCols];
414                 for(int i=0;i<numRows;i++) {
415                         array[i][0] = f.map(matrix[i][0]);
416                         for(int j=1;j<numCols;j++)
417                                 array[i][j] = f.map(matrix[i][j]);
418                 }
419                 return new DoubleMatrix(array);
420         }
421 }
422
Popular Tags