1 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 19 public abstract class AbstractIntegerMatrix extends Matrix { 20 23 protected AbstractIntegerMatrix(final int rows,final int cols) { 24 super(rows, cols); 25 } 26 30 public final boolean equals(Object obj) { 31 if(obj instanceof AbstractIntegerMatrix) { 32 return equals((AbstractIntegerMatrix)obj); 33 } else { 34 return false; 35 } 36 } 37 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 62 public String toString() { 63 final StringBuffer buf=new StringBuffer (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 76 public int hashCode() { 77 return (int)Math.exp(infNorm()); 78 } 79 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 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 109 public abstract int getElement(int i, int j); 110 118 public abstract void setElement(int i, int j, int x); 119 public final Object getSet() { 120 return IntegerMatrixAlgebra.get(numRows, numCols); 121 } 122 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 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 154 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 169 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 ("Member class not recognised by this method."); 177 } 178 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 199 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 ("Member class not recognised by this method."); 207 } 208 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 229 232 public final Module.Member scalarMultiply(Ring.Member x) { 233 if(x instanceof Number ) { 234 return scalarMultiply(((Number )x).intValue()); 235 } else { 236 throw new IllegalArgumentException ("Member class not recognised by this method."); 237 } 238 } 239 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 256 260 public final VectorSpace.Member scalarDivide(Field.Member x) { 261 throw new UnsupportedOperationException ("Not an algebra"); 262 } 263 264 266 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 287 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 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 ("Member class not recognised by this method."); 313 } 314 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 342 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 360 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 378 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 |