KickJava   Java API By Example, From Geeks To Geeks.

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


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.AbstractIntegerVector;
9 import JSci.maths.vectors.IntegerVector;
10 import JSci.maths.groups.AbelianGroup;
11 import JSci.maths.algebras.*;
12 import JSci.maths.fields.*;
13
14 /**
15 * The AbstractIntegerMatrix class provides an object for encapsulating integer matrices.
16 * @version 2.2
17 * @author Mark Hale
18 */

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

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

30         public final boolean equals(Object JavaDoc obj) {
31                 if(obj instanceof AbstractIntegerMatrix) {
32                         return equals((AbstractIntegerMatrix)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 int matrix
41         */

42         public final boolean equals(AbstractIntegerMatrix m) {
43         return equals(m, GlobalSettings.ZERO_TOL);
44         }
45     public boolean equals(AbstractIntegerMatrix m, double tol) {
46                 if(m != null && numRows == m.rows() && numCols == m.columns()) {
47             int sumSqr = 0;
48                         for(int i=0;i<numRows;i++) {
49                                 for(int j=0;j<numCols;j++) {
50                     int 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 a double matrix.
81         * @return a double matrix
82         */

83         public AbstractDoubleMatrix toDoubleMatrix() {
84                 final double ans[][]=new double[numRows][numCols];
85                 for(int i=0;i<numRows;i++) {
86                         for(int j=0;j<numCols;j++)
87                                 ans[i][j]=getElement(i,j);
88                 }
89                 return new DoubleMatrix(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 int 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, int x);
119     public final Object JavaDoc getSet() {
120         return IntegerMatrixAlgebra.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 int infNorm() {
127                 int 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 int array[][]=new int[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 IntegerMatrix(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 AbstractIntegerMatrix)
174                         return add((AbstractIntegerMatrix)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 int matrix
181         * @exception MatrixDimensionException If the matrices are different sizes.
182         */

183         public AbstractIntegerMatrix add(final AbstractIntegerMatrix m) {
184                 if(numRows==m.rows() && numCols==m.columns()) {
185                         final int array[][]=new int[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 IntegerMatrix(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 AbstractIntegerMatrix)
204                         return subtract((AbstractIntegerMatrix)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 int matrix
211         * @exception MatrixDimensionException If the matrices are different sizes.
212         */

213         public AbstractIntegerMatrix subtract(final AbstractIntegerMatrix m) {
214                 if(numRows==m.rows() && numCols==m.columns()) {
215                         final int array[][]=new int[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 IntegerMatrix(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).intValue());
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 int.
242         * @return a int matrix.
243         */

244         public AbstractIntegerMatrix scalarMultiply(final int x) {
245                 final int array[][]=new int[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 IntegerMatrix(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                 throw new UnsupportedOperationException JavaDoc("Not an algebra");
262         }
263
264 // SCALAR PRODUCT
265

266         /**
267         * Returns the scalar product of this matrix and another.
268         * @param m a int matrix.
269         * @exception MatrixDimensionException If the matrices are different sizes.
270         */

271         public int scalarProduct(final AbstractIntegerMatrix m) {
272                 if(numRows==m.rows() && numCols==m.columns()) {
273                         int ans = 0;
274                         for(int i=0; i<numRows; i++) {
275                                 ans += getElement(i,0)*m.getElement(i,0);
276                                 for(int j=1; j<numCols; j++)
277                                         ans += getElement(i,j)*m.getElement(i,j);
278                         }
279                         return ans;
280                 } else {
281                        throw new MatrixDimensionException("Matrices are different sizes.");
282                 }
283         }
284
285 // MATRIX MULTIPLICATION
286

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

292         public AbstractIntegerVector multiply(final AbstractIntegerVector v) {
293                 if(numCols==v.dimension()) {
294                         final int array[]=new int[numRows];
295                         for(int i=0;i<numRows;i++) {
296                                 array[i]=getElement(i,0)*v.getComponent(0);
297                                 for(int j=1;j<numCols;j++)
298                                         array[i]+=getElement(i,j)*v.getComponent(j);
299                         }
300                         return new IntegerVector(array);
301                 } else {
302                         throw new DimensionException("Matrix and vector are incompatible.");
303                 }
304         }
305         /**
306         * Returns the multiplication of this matrix and another.
307         */

308         public final Ring.Member multiply(final Ring.Member m) {
309                 if(m instanceof AbstractIntegerMatrix)
310                         return multiply((AbstractIntegerMatrix)m);
311                 else
312                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
313         }
314         /**
315         * Returns the multiplication of this matrix and another.
316         * @param m a int matrix
317         * @return a AbstractIntegerMatrix or a AbstractIntegerSquareMatrix as appropriate
318         * @exception MatrixDimensionException If the matrices are incompatible.
319         */

320         public AbstractIntegerMatrix multiply(final AbstractIntegerMatrix m) {
321                 if(numCols==m.rows()) {
322                         final int mColumns = m.columns();
323                         final int array[][]=new int[numRows][mColumns];
324                         for(int j=0; j<numRows; j++) {
325                                 for(int k=0; k<mColumns; k++) {
326                                         array[j][k] = getElement(j,0)*m.getElement(0,k);
327                                         for(int n=1; n<numCols; n++)
328                                                 array[j][k] += getElement(j,n)*m.getElement(n,k);
329                                 }
330                         }
331                         if(numRows == mColumns)
332                                 return new IntegerSquareMatrix(array);
333                         else
334                                 return new IntegerMatrix(array);
335                 } else {
336                         throw new MatrixDimensionException("Incompatible matrices.");
337                 }
338         }
339
340 // DIRECT SUM
341

342         /**
343         * Returns the direct sum of this matrix and another.
344         */

345         public AbstractIntegerMatrix directSum(final AbstractIntegerMatrix m) {
346                 final int array[][]=new int[numRows+m.numRows][numCols+m.numCols];
347                 for(int i=0;i<numRows;i++) {
348                         for(int j=0;j<numCols;j++)
349                                 array[i][j] = getElement(i,j);
350                 }
351                 for(int i=0;i<m.numRows;i++) {
352                         for(int j=0;j<m.numCols;j++)
353                                 array[i+numRows][j+numCols] = m.getElement(i,j);
354                 }
355                 return new IntegerMatrix(array);
356         }
357
358 // TENSOR PRODUCT
359

360         /**
361         * Returns the tensor product of this matrix and another.
362         */

363         public AbstractIntegerMatrix tensor(final AbstractIntegerMatrix m) {
364                 final int array[][]=new int[numRows*m.numRows][numCols*m.numCols];
365                 for(int i=0;i<numRows;i++) {
366                         for(int j=0;j<numCols;j++) {
367                                 for(int k=0;k<m.numRows;j++) {
368                                         for(int l=0;l<m.numCols;l++)
369                                                 array[i*m.numRows+k][j*m.numCols+l] = getElement(i,j)*m.getElement(k,l);
370                                 }
371                         }
372                 }
373                 return new IntegerMatrix(array);
374         }
375
376 // TRANSPOSE
377

378         /**
379         * Returns the transpose of this matrix.
380         * @return a int matrix
381         */

382         public Matrix transpose() {
383                 final int array[][]=new int[numCols][numRows];
384                 for(int i=0;i<numRows;i++) {
385                         array[0][i] = getElement(i,0);
386                         for(int j=1;j<numCols;j++)
387                                 array[j][i] = getElement(i,j);
388                 }
389                 return new IntegerMatrix(array);
390         }
391
392 }
393
Popular Tags