KickJava   Java API By Example, From Geeks To Geeks.

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


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

19 public abstract class AbstractDoubleMatrix extends Matrix {
20         /**
21         * Constructs a matrix.
22         */

23         protected AbstractDoubleMatrix(final int rows,final int cols) {
24                 super(rows, cols);
25         }
26         /**
27         * Compares two ${nativeTyp} matrices for equality.
28         * @param obj a double matrix
29         */

30         public final boolean equals(Object JavaDoc obj) {
31                 if(obj instanceof AbstractDoubleMatrix) {
32                         return equals((AbstractDoubleMatrix)obj);
33                 } else {
34                         return false;
35                 }
36         }
37         /**
38         * Compares two ${nativeTyp} matrices for equality.
39         * Two matrices are considered to be equal if the Frobenius norm of their difference is within the zero tolerance.
40         * @param m a double matrix
41         */

42         public final boolean equals(AbstractDoubleMatrix m) {
43         return equals(m, GlobalSettings.ZERO_TOL);
44         }
45     public boolean equals(AbstractDoubleMatrix m, double tol) {
46                 if(m != null && numRows == m.rows() && numCols == m.columns()) {
47             double sumSqr = 0;
48                         for(int i=0;i<numRows;i++) {
49                                 for(int j=0;j<numCols;j++) {
50                     double delta = getElement(i,j)-m.getElement(i,j);
51                     sumSqr += delta*delta;
52                                 }
53                         }
54                         return (sumSqr <= tol*tol);
55                 } else {
56                         return false;
57                 }
58         }
59         /**
60         * Returns a string representing this matrix.
61         */

62         public String JavaDoc toString() {
63                 final StringBuffer JavaDoc buf=new StringBuffer JavaDoc(5*numRows*numCols);
64                 for(int i=0;i<numRows;i++) {
65                         for(int j=0;j<numCols;j++) {
66                                 buf.append(getElement(i,j));
67                                 buf.append(' ');
68                         }
69                         buf.append('\n');
70                 }
71                 return buf.toString();
72         }
73         /**
74         * Returns a hashcode for this matrix.
75         */

76         public int hashCode() {
77                 return (int)Math.exp(infNorm());
78         }
79         /**
80         * Converts this matrix to an integer matrix.
81         * @return an integer matrix
82         */

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

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

109         public abstract double getElement(int i, int j);
110         /**
111         * Sets the value of an element of the matrix.
112         * Should only be used to initialise this matrix.
113         * @param i row index of the element
114         * @param j column index of the element
115         * @param x a number
116         * @exception MatrixDimensionException If attempting to access an invalid element.
117         */

118         public abstract void setElement(int i, int j, double x);
119     public final Object JavaDoc getSet() {
120         return DoubleMatrixAlgebra.get(numRows, numCols);
121     }
122         /**
123         * Returns the l<sup><img border=0 alt="infinity" SRC="doc-files/infinity.gif"></sup>-norm.
124         * @author Taber Smith
125         */

126         public double infNorm() {
127                 double result=0,tmpResult;
128                 for(int i=0;i<numRows;i++) {
129                         tmpResult=0;
130                         for(int j=0;j<numCols;j++)
131                                 tmpResult+=Math.abs(getElement(i,j));
132                         if(tmpResult>result)
133                                 result=tmpResult;
134                 }
135                 return result;
136         }
137         /**
138         * Returns the Frobenius or Hilbert-Schmidt (l<sup>2</sup>) norm.
139         * @jsci.planetmath FrobeniusMatrixNorm
140         */

141         public double frobeniusNorm() {
142                 double result=0.0;
143                 for(int j,i=0;i<numRows;i++) {
144                         for(j=0;j<numCols;j++)
145                                 result=ExtraMath.hypot(result, getElement(i,j));
146                 }
147                 return result;
148         }
149
150 //============
151
// OPERATIONS
152
//============
153

154         /**
155         * Returns the negative of this matrix.
156         */

157         public AbelianGroup.Member negate() {
158                 final double array[][]=new double[numRows][numCols];
159                 for(int i=0;i<numRows;i++) {
160                         array[i][0] = -getElement(i,0);
161                         for(int j=1;j<numCols;j++)
162                                 array[i][j] = -getElement(i,j);
163                 }
164                 return new DoubleMatrix(array);
165         }
166
167 // ADDITION
168

169         /**
170         * Returns the addition of this matrix and another.
171         */

172         public final AbelianGroup.Member add(final AbelianGroup.Member m) {
173                 if(m instanceof AbstractDoubleMatrix)
174                         return add((AbstractDoubleMatrix)m);
175                 else
176                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
177         }
178         /**
179         * Returns the addition of this matrix and another.
180         * @param m a double matrix
181         * @exception MatrixDimensionException If the matrices are different sizes.
182         */

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

199         /**
200         * Returns the subtraction of this matrix by another.
201         */

202         public final AbelianGroup.Member subtract(final AbelianGroup.Member m) {
203                 if(m instanceof AbstractDoubleMatrix)
204                         return subtract((AbstractDoubleMatrix)m);
205                 else
206                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
207         }
208         /**
209         * Returns the subtraction of this matrix by another.
210         * @param m a double matrix
211         * @exception MatrixDimensionException If the matrices are different sizes.
212         */

213         public AbstractDoubleMatrix subtract(final AbstractDoubleMatrix m) {
214                 if(numRows==m.rows() && numCols==m.columns()) {
215                         final double array[][]=new double[numRows][numCols];
216                         for(int i=0;i<numRows;i++) {
217                                 array[i][0] = getElement(i,0)-m.getElement(i,0);
218                                 for(int j=1;j<numCols;j++)
219                                         array[i][j] = getElement(i,j)-m.getElement(i,j);
220                         }
221                         return new DoubleMatrix(array);
222                 } else {
223                         throw new MatrixDimensionException("Matrices are different sizes.");
224                 }
225         }
226
227 // SCALAR MULTIPLICATION
228

229         /**
230         * Returns the multiplication of this matrix by a scalar.
231         */

232         public final Module.Member scalarMultiply(Ring.Member x) {
233                 if(x instanceof Number JavaDoc) {
234                         return scalarMultiply(((Number JavaDoc)x).doubleValue());
235                 } else {
236                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
237                 }
238         }
239         /**
240         * Returns the multiplication of this matrix by a scalar.
241         * @param x a double.
242         * @return a double matrix.
243         */

244         public AbstractDoubleMatrix scalarMultiply(final double x) {
245                 final double array[][]=new double[numRows][numCols];
246                 for(int i=0;i<numRows;i++) {
247                         array[i][0] = x*getElement(i,0);
248                         for(int j=1;j<numCols;j++)
249                                 array[i][j] = x*getElement(i,j);
250                 }
251                 return new DoubleMatrix(array);
252         }
253
254 // SCALAR DIVISON
255

256         /**
257         * Returns the division of this matrix by a scalar.
258         * Always throws an exception.
259         */

260         public final VectorSpace.Member scalarDivide(Field.Member x) {
261                 if(x instanceof Number JavaDoc) {
262                         return scalarDivide(((Number JavaDoc)x).doubleValue());
263                 } else {
264                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
265                 }
266         }
267         /**
268         * Returns the division of this matrix by a scalar.
269         * @param x a double.
270         * @return a double matrix.
271         */

272         public AbstractDoubleMatrix scalarDivide(final double x) {
273                 final double array[][]=new double[numRows][numCols];
274                 for(int i=0;i<numRows;i++) {
275                         array[i][0] = getElement(i,0)/x;
276                         for(int j=1;j<numCols;j++)
277                                 array[i][j] = getElement(i,j)/x;
278                 }
279                 return new DoubleMatrix(array);
280         }
281
282 // SCALAR PRODUCT
283

284         /**
285         * Returns the scalar product of this matrix and another.
286         * @param m a double matrix.
287         * @exception MatrixDimensionException If the matrices are different sizes.
288         */

289         public double scalarProduct(final AbstractDoubleMatrix m) {
290                 if(numRows==m.rows() && numCols==m.columns()) {
291                         double ans = 0;
292                         for(int i=0; i<numRows; i++) {
293                                 ans += getElement(i,0)*m.getElement(i,0);
294                                 for(int j=1; j<numCols; j++)
295                                         ans += getElement(i,j)*m.getElement(i,j);
296                         }
297                         return ans;
298                 } else {
299                        throw new MatrixDimensionException("Matrices are different sizes.");
300                 }
301         }
302
303 // MATRIX MULTIPLICATION
304

305         /**
306         * Returns the multiplication of a vector by this matrix.
307         * @param v a double vector.
308         * @exception DimensionException If the matrix and vector are incompatible.
309         */

310         public AbstractDoubleVector multiply(final AbstractDoubleVector v) {
311                 if(numCols==v.dimension()) {
312                         final double array[]=new double[numRows];
313                         for(int i=0;i<numRows;i++) {
314                                 array[i]=getElement(i,0)*v.getComponent(0);
315                                 for(int j=1;j<numCols;j++)
316                                         array[i]+=getElement(i,j)*v.getComponent(j);
317                         }
318                         return new DoubleVector(array);
319                 } else {
320                         throw new DimensionException("Matrix and vector are incompatible.");
321                 }
322         }
323         /**
324         * Returns the multiplication of this matrix and another.
325         */

326         public final Ring.Member multiply(final Ring.Member m) {
327                 if(m instanceof AbstractDoubleMatrix)
328                         return multiply((AbstractDoubleMatrix)m);
329                 else
330                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
331         }
332         /**
333         * Returns the multiplication of this matrix and another.
334         * @param m a double matrix
335         * @return a AbstractDoubleMatrix or a AbstractDoubleSquareMatrix as appropriate
336         * @exception MatrixDimensionException If the matrices are incompatible.
337         */

338         public AbstractDoubleMatrix multiply(final AbstractDoubleMatrix m) {
339                 if(numCols==m.rows()) {
340                         final int mColumns = m.columns();
341                         final double array[][]=new double[numRows][mColumns];
342                         for(int j=0; j<numRows; j++) {
343                                 for(int k=0; k<mColumns; k++) {
344                                         array[j][k] = getElement(j,0)*m.getElement(0,k);
345                                         for(int n=1; n<numCols; n++)
346                                                 array[j][k] += getElement(j,n)*m.getElement(n,k);
347                                 }
348                         }
349                         if(numRows == mColumns)
350                                 return new DoubleSquareMatrix(array);
351                         else
352                                 return new DoubleMatrix(array);
353                 } else {
354                         throw new MatrixDimensionException("Incompatible matrices.");
355                 }
356         }
357
358 // DIRECT SUM
359

360         /**
361         * Returns the direct sum of this matrix and another.
362         */

363         public AbstractDoubleMatrix directSum(final AbstractDoubleMatrix m) {
364                 final double array[][]=new double[numRows+m.numRows][numCols+m.numCols];
365                 for(int i=0;i<numRows;i++) {
366                         for(int j=0;j<numCols;j++)
367                                 array[i][j] = getElement(i,j);
368                 }
369                 for(int i=0;i<m.numRows;i++) {
370                         for(int j=0;j<m.numCols;j++)
371                                 array[i+numRows][j+numCols] = m.getElement(i,j);
372                 }
373                 return new DoubleMatrix(array);
374         }
375
376 // TENSOR PRODUCT
377

378         /**
379         * Returns the tensor product of this matrix and another.
380         */

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

396         /**
397         * Returns the transpose of this matrix.
398         * @return a double matrix
399         */

400         public Matrix transpose() {
401                 final double array[][]=new double[numCols][numRows];
402                 for(int i=0;i<numRows;i++) {
403                         array[0][i] = getElement(i,0);
404                         for(int j=1;j<numCols;j++)
405                                 array[j][i] = getElement(i,j);
406                 }
407                 return new DoubleMatrix(array);
408         }
409
410 // MAP ELEMENTS
411

412         /**
413         * Applies a function on all the matrix elements.
414         * @param f a user-defined function
415         * @return a double matrix
416         */

417         public AbstractDoubleMatrix mapElements(final Mapping f) {
418                 final double array[][]=new double[numRows][numCols];
419                 for(int i=0;i<numRows;i++) {
420                         array[i][0] = f.map(getElement(i,0));
421                         for(int j=1;j<numCols;j++)
422                                 array[i][j] = f.map(getElement(i,j));
423                 }
424                 return new DoubleMatrix(array);
425         }
426 }
427
Popular Tags