001: /* AUTO-GENERATED */
002: package JSci.maths.matrices;
003:
004: import JSci.maths.ExtraMath;
005: import JSci.maths.Mapping;
006: import JSci.maths.DimensionException;
007: import JSci.maths.vectors.AbstractIntegerVector;
008: import JSci.maths.vectors.IntegerVector;
009: import JSci.maths.groups.AbelianGroup;
010: import JSci.maths.algebras.*;
011: import JSci.maths.fields.*;
012:
013: /**
014: * The IntegerMatrix class provides an object for encapsulating integer matrices.
015: * @version 2.2
016: * @author Mark Hale
017: */
018: public class IntegerMatrix extends AbstractIntegerMatrix {
019: /**
020: * Array containing the elements of the matrix.
021: */
022: protected final int matrix[][];
023:
024: /**
025: * Constructs a matrix by wrapping an array.
026: * @param array an assigned value
027: */
028: public IntegerMatrix(final int array[][]) {
029: super (array.length, array[0].length);
030: matrix = array;
031: }
032:
033: /**
034: * Constructs an empty matrix.
035: */
036: public IntegerMatrix(final int rows, final int cols) {
037: this (new int[rows][cols]);
038: }
039:
040: /**
041: * Constructs a matrix from an array of vectors (columns).
042: * @param array an assigned value
043: */
044: public IntegerMatrix(final AbstractIntegerVector array[]) {
045: this (array[0].dimension(), array.length);
046: for (int i = 0; i < numRows; i++) {
047: for (int j = 0; j < numCols; j++)
048: matrix[i][j] = array[j].getComponent(i);
049: }
050: }
051:
052: /**
053: * Compares two ${nativeTyp} matrices for equality.
054: * @param m a int matrix
055: */
056: public boolean equals(AbstractIntegerMatrix m, double tol) {
057: if (m != null && numRows == m.rows() && numCols == m.columns()) {
058: int sumSqr = 0;
059: for (int i = 0; i < numRows; i++) {
060: for (int j = 0; j < numCols; j++) {
061: int delta = matrix[i][j] - m.getElement(i, j);
062: sumSqr += delta * delta;
063: }
064: }
065: return (sumSqr <= tol * tol);
066: } else {
067: return false;
068: }
069: }
070:
071: /**
072: * Returns a string representing this matrix.
073: */
074: public String toString() {
075: final StringBuffer buf = new StringBuffer(5 * numRows * numCols);
076: for (int i = 0; i < numRows; i++) {
077: for (int j = 0; j < numCols; j++) {
078: buf.append(matrix[i][j]);
079: buf.append(' ');
080: }
081: buf.append('\n');
082: }
083: return buf.toString();
084: }
085:
086: /**
087: * Converts this matrix to a double matrix.
088: * @return a double matrix
089: */
090: public AbstractDoubleMatrix toDoubleMatrix() {
091: final double ans[][] = new double[numRows][numCols];
092: for (int i = 0; i < numRows; i++) {
093: for (int j = 0; j < numCols; j++)
094: ans[i][j] = matrix[i][j];
095: }
096: return new DoubleMatrix(ans);
097: }
098:
099: /**
100: * Converts this matrix to a complex matrix.
101: * @return a complex matrix
102: */
103: public AbstractComplexMatrix toComplexMatrix() {
104: ComplexMatrix cm = new ComplexMatrix(numRows, numCols);
105: for (int i = 0; i < numRows; i++) {
106: for (int j = 0; j < numCols; j++)
107: cm.setElement(i, j, matrix[i][j], 0.0);
108: }
109: return cm;
110: }
111:
112: /**
113: * Returns an element of the matrix.
114: * @param i row index of the element
115: * @param j column index of the element
116: * @exception MatrixDimensionException If attempting to access an invalid element.
117: */
118: public int getElement(int i, int j) {
119: if (i >= 0 && i < numRows && j >= 0 && j < numCols)
120: return matrix[i][j];
121: else
122: throw new MatrixDimensionException(getInvalidElementMsg(i,
123: j));
124: }
125:
126: /**
127: * Sets the value of an element of the matrix.
128: * Should only be used to initialise this matrix.
129: * @param i row index of the element
130: * @param j column index of the element
131: * @param x a number
132: * @exception MatrixDimensionException If attempting to access an invalid element.
133: */
134: public void setElement(int i, int j, int x) {
135: if (i >= 0 && i < numRows && j >= 0 && j < numCols)
136: matrix[i][j] = x;
137: else
138: throw new MatrixDimensionException(getInvalidElementMsg(i,
139: j));
140: }
141:
142: /**
143: * Returns the l<sup><img border=0 alt="infinity" src="doc-files/infinity.gif"></sup>-norm.
144: * @author Taber Smith
145: */
146: public int infNorm() {
147: int result = 0, tmpResult;
148: for (int i = 0; i < numRows; i++) {
149: tmpResult = 0;
150: for (int j = 0; j < numCols; j++)
151: tmpResult += Math.abs(matrix[i][j]);
152: if (tmpResult > result)
153: result = tmpResult;
154: }
155: return result;
156: }
157:
158: /**
159: * Returns the Frobenius or Hilbert-Schmidt (l<sup>2</sup>) norm.
160: * @jsci.planetmath FrobeniusMatrixNorm
161: */
162: public double frobeniusNorm() {
163: double result = 0.0;
164: for (int j, i = 0; i < numRows; i++) {
165: for (j = 0; j < numCols; j++)
166: result = ExtraMath.hypot(result, matrix[i][j]);
167: }
168: return result;
169: }
170:
171: //============
172: // OPERATIONS
173: //============
174:
175: /**
176: * Returns the negative of this matrix.
177: */
178: public AbelianGroup.Member negate() {
179: final int array[][] = new int[numRows][numCols];
180: for (int i = 0; i < numRows; i++) {
181: array[i][0] = -matrix[i][0];
182: for (int j = 1; j < numCols; j++)
183: array[i][j] = -matrix[i][j];
184: }
185: return new IntegerMatrix(array);
186: }
187:
188: // ADDITION
189:
190: /**
191: * Returns the addition of this matrix and another.
192: * @param m a int matrix
193: * @exception MatrixDimensionException If the matrices are different sizes.
194: */
195: public AbstractIntegerMatrix add(final AbstractIntegerMatrix m) {
196: if (numRows == m.rows() && numCols == m.columns()) {
197: final int array[][] = new int[numRows][numCols];
198: for (int i = 0; i < numRows; i++) {
199: array[i][0] = matrix[i][0] + m.getElement(i, 0);
200: for (int j = 1; j < numCols; j++)
201: array[i][j] = matrix[i][j] + m.getElement(i, j);
202: }
203: return new IntegerMatrix(array);
204: } else {
205: throw new MatrixDimensionException(
206: "Matrices are different sizes.");
207: }
208: }
209:
210: // SUBTRACTION
211:
212: /**
213: * Returns the subtraction of this matrix by another.
214: * @param m a int matrix
215: * @exception MatrixDimensionException If the matrices are different sizes.
216: */
217: public AbstractIntegerMatrix subtract(final AbstractIntegerMatrix m) {
218: if (numRows == m.rows() && numCols == m.columns()) {
219: final int array[][] = new int[numRows][numCols];
220: for (int i = 0; i < numRows; i++) {
221: array[i][0] = matrix[i][0] - m.getElement(i, 0);
222: for (int j = 1; j < numCols; j++)
223: array[i][j] = matrix[i][j] - m.getElement(i, j);
224: }
225: return new IntegerMatrix(array);
226: } else {
227: throw new MatrixDimensionException(
228: "Matrices are different sizes.");
229: }
230: }
231:
232: // SCALAR MULTIPLICATION
233:
234: /**
235: * Returns the multiplication of this matrix by a scalar.
236: * @param x a int.
237: * @return a int matrix.
238: */
239: public AbstractIntegerMatrix scalarMultiply(final int x) {
240: final int array[][] = new int[numRows][numCols];
241: for (int i = 0; i < numRows; i++) {
242: array[i][0] = x * matrix[i][0];
243: for (int j = 1; j < numCols; j++)
244: array[i][j] = x * matrix[i][j];
245: }
246: return new IntegerMatrix(array);
247: }
248:
249: // SCALAR DIVISON
250:
251: // SCALAR PRODUCT
252:
253: /**
254: * Returns the scalar product of this matrix and another.
255: * @param m a int matrix.
256: * @exception MatrixDimensionException If the matrices are different sizes.
257: */
258: public int scalarProduct(final AbstractIntegerMatrix m) {
259: if (m instanceof IntegerMatrix)
260: return scalarProduct((IntegerMatrix) m);
261:
262: if (numRows == m.rows() && numCols == m.columns()) {
263: int 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(
272: "Matrices are different sizes.");
273: }
274: }
275:
276: public int scalarProduct(final IntegerMatrix m) {
277: if (numRows == m.numRows && numCols == m.numCols) {
278: int ans = 0;
279: for (int i = 0; i < numRows; i++) {
280: ans += matrix[i][0] * m.matrix[i][0];
281: for (int j = 1; j < numCols; j++)
282: ans += matrix[i][j] * m.matrix[i][j];
283: }
284: return ans;
285: } else
286: throw new MatrixDimensionException(
287: "Matrices are different sizes.");
288: }
289:
290: // MATRIX MULTIPLICATION
291:
292: /**
293: * Returns the multiplication of a vector by this matrix.
294: * @param v a int vector.
295: * @exception DimensionException If the matrix and vector are incompatible.
296: */
297: public AbstractIntegerVector multiply(final AbstractIntegerVector v) {
298: if (numCols == v.dimension()) {
299: final int array[] = new int[numRows];
300: for (int i = 0; i < numRows; i++) {
301: array[i] = matrix[i][0] * v.getComponent(0);
302: for (int j = 1; j < numCols; j++)
303: array[i] += matrix[i][j] * v.getComponent(j);
304: }
305: return new IntegerVector(array);
306: } else {
307: throw new DimensionException(
308: "Matrix and vector are incompatible.");
309: }
310: }
311:
312: /**
313: * Returns the multiplication of this matrix and another.
314: * @param m a int matrix
315: * @return a AbstractIntegerMatrix or a AbstractIntegerSquareMatrix as appropriate
316: * @exception MatrixDimensionException If the matrices are incompatible.
317: */
318: public AbstractIntegerMatrix multiply(final AbstractIntegerMatrix m) {
319: if (m instanceof IntegerMatrix)
320: return multiply((IntegerMatrix) m);
321:
322: if (numCols == m.rows()) {
323: final int mColumns = m.columns();
324: final int array[][] = new int[numRows][mColumns];
325: for (int j = 0; j < numRows; j++) {
326: for (int k = 0; k < mColumns; k++) {
327: array[j][k] = matrix[j][0] * m.getElement(0, k);
328: for (int n = 1; n < numCols; n++)
329: array[j][k] += matrix[j][n]
330: * m.getElement(n, k);
331: }
332: }
333: if (numRows == mColumns)
334: return new IntegerSquareMatrix(array);
335: else
336: return new IntegerMatrix(array);
337: } else {
338: throw new MatrixDimensionException("Incompatible matrices.");
339: }
340: }
341:
342: public AbstractIntegerMatrix multiply(final IntegerMatrix m) {
343: if (numCols == m.numRows) {
344: final int array[][] = new int[numRows][m.numCols];
345: for (int j = 0; j < numRows; j++) {
346: for (int k = 0; k < m.numCols; k++) {
347: array[j][k] = matrix[j][0] * m.matrix[0][k];
348: for (int n = 1; n < numCols; n++)
349: array[j][k] += matrix[j][n] * m.matrix[n][k];
350: }
351: }
352: if (numRows == m.numCols)
353: return new IntegerSquareMatrix(array);
354: else
355: return new IntegerMatrix(array);
356: } else
357: throw new MatrixDimensionException("Incompatible matrices.");
358: }
359:
360: // DIRECT SUM
361:
362: /**
363: * Returns the direct sum of this matrix and another.
364: */
365: public AbstractIntegerMatrix directSum(final AbstractIntegerMatrix m) {
366: final int array[][] = new int[numRows + m.numRows][numCols
367: + m.numCols];
368: for (int i = 0; i < numRows; i++) {
369: for (int j = 0; j < numCols; j++)
370: array[i][j] = matrix[i][j];
371: }
372: for (int i = 0; i < m.numRows; i++) {
373: for (int j = 0; j < m.numCols; j++)
374: array[i + numRows][j + numCols] = m.getElement(i, j);
375: }
376: return new IntegerMatrix(array);
377: }
378:
379: // TENSOR PRODUCT
380:
381: /**
382: * Returns the tensor product of this matrix and another.
383: */
384: public AbstractIntegerMatrix tensor(final AbstractIntegerMatrix m) {
385: final int array[][] = new int[numRows * m.numRows][numCols
386: * m.numCols];
387: for (int i = 0; i < numRows; i++) {
388: for (int j = 0; j < numCols; j++) {
389: for (int k = 0; k < m.numRows; j++) {
390: for (int l = 0; l < m.numCols; l++)
391: array[i * m.numRows + k][j * m.numCols + l] = matrix[i][j]
392: * m.getElement(k, l);
393: }
394: }
395: }
396: return new IntegerMatrix(array);
397: }
398:
399: // TRANSPOSE
400:
401: /**
402: * Returns the transpose of this matrix.
403: * @return a int matrix
404: */
405: public Matrix transpose() {
406: final int array[][] = new int[numCols][numRows];
407: for (int i = 0; i < numRows; i++) {
408: array[0][i] = matrix[i][0];
409: for (int j = 1; j < numCols; j++)
410: array[j][i] = matrix[i][j];
411: }
412: return new IntegerMatrix(array);
413: }
414:
415: }
|