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.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 19 public abstract class AbstractDoubleMatrix extends Matrix { 20 23 protected AbstractDoubleMatrix(final int rows,final int cols) { 24 super(rows, cols); 25 } 26 30 public final boolean equals(Object obj) { 31 if(obj instanceof AbstractDoubleMatrix) { 32 return equals((AbstractDoubleMatrix)obj); 33 } else { 34 return false; 35 } 36 } 37 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 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 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 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 double getElement(int i, int j); 110 118 public abstract void setElement(int i, int j, double x); 119 public final Object getSet() { 120 return DoubleMatrixAlgebra.get(numRows, numCols); 121 } 122 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 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 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 169 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 ("Member class not recognised by this method."); 177 } 178 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 199 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 ("Member class not recognised by this method."); 207 } 208 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 229 232 public final Module.Member scalarMultiply(Ring.Member x) { 233 if(x instanceof Number ) { 234 return scalarMultiply(((Number )x).doubleValue()); 235 } else { 236 throw new IllegalArgumentException ("Member class not recognised by this method."); 237 } 238 } 239 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 256 260 public final VectorSpace.Member scalarDivide(Field.Member x) { 261 if(x instanceof Number ) { 262 return scalarDivide(((Number )x).doubleValue()); 263 } else { 264 throw new IllegalArgumentException ("Member class not recognised by this method."); 265 } 266 } 267 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 284 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 305 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 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 ("Member class not recognised by this method."); 331 } 332 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 360 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 378 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 396 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 412 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 |