KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

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

41         public IntegerMatrix(final AbstractIntegerVector 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 int matrix
51         */

52         public boolean equals(AbstractIntegerMatrix m, double tol) {
53                 if(m != null && numRows == m.rows() && numCols == m.columns()) {
54             int sumSqr = 0;
55                         for(int i=0;i<numRows;i++) {
56                                 for(int j=0;j<numCols;j++) {
57                     int 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 a double matrix.
82         * @return a double matrix
83         */

84         public AbstractDoubleMatrix toDoubleMatrix() {
85                 final double ans[][]=new double[numRows][numCols];
86                 for(int i=0;i<numRows;i++) {
87                         for(int j=0;j<numCols;j++)
88                                 ans[i][j]=matrix[i][j];
89                 }
90                 return new DoubleMatrix(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 int 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, int 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 int infNorm() {
135                 int 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 int array[][]=new int[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 IntegerMatrix(array);
173         }
174
175 // ADDITION
176

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

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

203         public AbstractIntegerMatrix subtract(final AbstractIntegerMatrix m) {
204                 if(numRows==m.rows() && numCols==m.columns()) {
205                         final int array[][]=new int[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 IntegerMatrix(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 int.
222         * @return a int matrix.
223         */

224         public AbstractIntegerMatrix scalarMultiply(final int x) {
225                 final int array[][]=new int[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 IntegerMatrix(array);
232         }
233
234 // SCALAR DIVISON
235

236
237 // SCALAR PRODUCT
238

239         /**
240         * Returns the scalar product of this matrix and another.
241         * @param m a int matrix.
242         * @exception MatrixDimensionException If the matrices are different sizes.
243         */

244         public int scalarProduct(final AbstractIntegerMatrix m) {
245                 if(m instanceof IntegerMatrix)
246                         return scalarProduct((IntegerMatrix)m);
247
248                 if(numRows==m.rows() && numCols==m.columns()) {
249                         int ans=0;
250                         for(int i=0;i<numRows;i++) {
251                                 ans += matrix[i][0]*m.getElement(i,0);
252                                 for(int j=1;j<numCols;j++)
253                                         ans += matrix[i][j]*m.getElement(i,j);
254                         }
255                         return ans;
256                 } else {
257                        throw new MatrixDimensionException("Matrices are different sizes.");
258                 }
259         }
260         public int scalarProduct(final IntegerMatrix m) {
261                 if(numRows==m.numRows && numCols==m.numCols) {
262                         int ans=0;
263                         for(int i=0;i<numRows;i++) {
264                                 ans+=matrix[i][0]*m.matrix[i][0];
265                                 for(int j=1;j<numCols;j++)
266                                         ans+=matrix[i][j]*m.matrix[i][j];
267                         }
268                         return ans;
269                 } else
270                         throw new MatrixDimensionException("Matrices are different sizes.");
271         }
272
273 // MATRIX MULTIPLICATION
274

275         /**
276         * Returns the multiplication of a vector by this matrix.
277         * @param v a int vector.
278         * @exception DimensionException If the matrix and vector are incompatible.
279         */

280         public AbstractIntegerVector multiply(final AbstractIntegerVector v) {
281                 if(numCols==v.dimension()) {
282                         final int array[]=new int[numRows];
283                         for(int i=0;i<numRows;i++) {
284                                 array[i]=matrix[i][0]*v.getComponent(0);
285                                 for(int j=1;j<numCols;j++)
286                                         array[i]+=matrix[i][j]*v.getComponent(j);
287                         }
288                         return new IntegerVector(array);
289                 } else {
290                         throw new DimensionException("Matrix and vector are incompatible.");
291                 }
292         }
293         /**
294         * Returns the multiplication of this matrix and another.
295         * @param m a int matrix
296         * @return a AbstractIntegerMatrix or a AbstractIntegerSquareMatrix as appropriate
297         * @exception MatrixDimensionException If the matrices are incompatible.
298         */

299         public AbstractIntegerMatrix multiply(final AbstractIntegerMatrix m) {
300                 if(m instanceof IntegerMatrix)
301                         return multiply((IntegerMatrix)m);
302
303                 if(numCols==m.rows()) {
304                         final int mColumns = m.columns();
305                         final int array[][]=new int[numRows][mColumns];
306                         for(int j=0; j<numRows; j++) {
307                                 for(int k=0; k<mColumns; k++) {
308                                         array[j][k] = matrix[j][0]*m.getElement(0,k);
309                                         for(int n=1; n<numCols; n++)
310                                                 array[j][k] += matrix[j][n]*m.getElement(n,k);
311                                 }
312                         }
313                         if(numRows == mColumns)
314                                 return new IntegerSquareMatrix(array);
315                         else
316                                 return new IntegerMatrix(array);
317                 } else {
318                         throw new MatrixDimensionException("Incompatible matrices.");
319                 }
320         }
321         public AbstractIntegerMatrix multiply(final IntegerMatrix m) {
322                 if(numCols==m.numRows) {
323                         final int array[][]=new int[numRows][m.numCols];
324                         for(int j=0;j<numRows;j++) {
325                                 for(int k=0;k<m.numCols;k++) {
326                                         array[j][k]=matrix[j][0]*m.matrix[0][k];
327                                         for(int n=1;n<numCols;n++)
328                                                 array[j][k]+=matrix[j][n]*m.matrix[n][k];
329                                 }
330                         }
331                         if(numRows == m.numCols)
332                                 return new IntegerSquareMatrix(array);
333                         else
334                                 return new IntegerMatrix(array);
335                 } else
336                         throw new MatrixDimensionException("Incompatible matrices.");
337         }
338
339 // DIRECT SUM
340

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

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

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

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

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

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