1 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.AbstractDoubleVector; 8 import JSci.maths.vectors.DoubleVector; 9 import JSci.maths.groups.AbelianGroup; 10 import JSci.maths.algebras.*; 11 import JSci.maths.fields.*; 12 13 18 public class DoubleMatrix extends AbstractDoubleMatrix { 19 22 protected final double matrix[][]; 23 27 public DoubleMatrix(final double array[][]) { 28 super(array.length, array[0].length); 29 matrix=array; 30 } 31 34 public DoubleMatrix(final int rows,final int cols) { 35 this(new double[rows][cols]); 36 } 37 41 public DoubleMatrix(final AbstractDoubleVector 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 52 public boolean equals(AbstractDoubleMatrix m, double tol) { 53 if(m != null && numRows == m.rows() && numCols == m.columns()) { 54 double sumSqr = 0; 55 for(int i=0;i<numRows;i++) { 56 for(int j=0;j<numCols;j++) { 57 double 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 69 public String toString() { 70 final StringBuffer buf=new StringBuffer (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 84 public AbstractIntegerMatrix toIntegerMatrix() { 85 final int ans[][]=new int[numRows][numCols]; 86 for(int i=0;i<numRows;i++) { 87 for(int j=0;j<numCols;j++) 88 ans[i][j]=Math.round((float)matrix[i][j]); 89 } 90 return new IntegerMatrix(ans); 91 } 92 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 110 public double 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 124 public void setElement(int i, int j, double 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 134 public double infNorm() { 135 double 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 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 162 165 public AbelianGroup.Member negate() { 166 final double array[][]=new double[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 DoubleMatrix(array); 173 } 174 175 177 182 public AbstractDoubleMatrix add(final AbstractDoubleMatrix m) { 183 if(numRows==m.rows() && numCols==m.columns()) { 184 final double array[][]=new double[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 DoubleMatrix(array); 191 } else { 192 throw new MatrixDimensionException("Matrices are different sizes."); 193 } 194 } 195 196 198 203 public AbstractDoubleMatrix subtract(final AbstractDoubleMatrix m) { 204 if(numRows==m.rows() && numCols==m.columns()) { 205 final double array[][]=new double[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 DoubleMatrix(array); 212 } else { 213 throw new MatrixDimensionException("Matrices are different sizes."); 214 } 215 } 216 217 219 224 public AbstractDoubleMatrix scalarMultiply(final double x) { 225 final double array[][]=new double[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 DoubleMatrix(array); 232 } 233 234 236 241 public AbstractDoubleMatrix scalarDivide(final double x) { 242 final double array[][]=new double[numRows][numCols]; 243 for(int i=0;i<numRows;i++) { 244 array[i][0] = matrix[i][0]/x; 245 for(int j=1;j<numCols;j++) 246 array[i][j] = matrix[i][j]/x; 247 } 248 return new DoubleMatrix(array); 249 } 250 251 253 258 public double scalarProduct(final AbstractDoubleMatrix m) { 259 if(m instanceof DoubleMatrix) 260 return scalarProduct((DoubleMatrix)m); 261 262 if(numRows==m.rows() && numCols==m.columns()) { 263 double 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("Matrices are different sizes."); 272 } 273 } 274 public double scalarProduct(final DoubleMatrix m) { 275 if(numRows==m.numRows && numCols==m.numCols) { 276 double ans=0; 277 for(int i=0;i<numRows;i++) { 278 ans+=matrix[i][0]*m.matrix[i][0]; 279 for(int j=1;j<numCols;j++) 280 ans+=matrix[i][j]*m.matrix[i][j]; 281 } 282 return ans; 283 } else 284 throw new MatrixDimensionException("Matrices are different sizes."); 285 } 286 287 289 294 public AbstractDoubleVector multiply(final AbstractDoubleVector v) { 295 if(numCols==v.dimension()) { 296 final double array[]=new double[numRows]; 297 for(int i=0;i<numRows;i++) { 298 array[i]=matrix[i][0]*v.getComponent(0); 299 for(int j=1;j<numCols;j++) 300 array[i]+=matrix[i][j]*v.getComponent(j); 301 } 302 return new DoubleVector(array); 303 } else { 304 throw new DimensionException("Matrix and vector are incompatible."); 305 } 306 } 307 313 public AbstractDoubleMatrix multiply(final AbstractDoubleMatrix m) { 314 if(m instanceof DoubleMatrix) 315 return multiply((DoubleMatrix)m); 316 317 if(numCols==m.rows()) { 318 final int mColumns = m.columns(); 319 final double array[][]=new double[numRows][mColumns]; 320 for(int j=0; j<numRows; j++) { 321 for(int k=0; k<mColumns; k++) { 322 array[j][k] = matrix[j][0]*m.getElement(0,k); 323 for(int n=1; n<numCols; n++) 324 array[j][k] += matrix[j][n]*m.getElement(n,k); 325 } 326 } 327 if(numRows == mColumns) 328 return new DoubleSquareMatrix(array); 329 else 330 return new DoubleMatrix(array); 331 } else { 332 throw new MatrixDimensionException("Incompatible matrices."); 333 } 334 } 335 public AbstractDoubleMatrix multiply(final DoubleMatrix m) { 336 if(numCols==m.numRows) { 337 final double array[][]=new double[numRows][m.numCols]; 338 for(int j=0;j<numRows;j++) { 339 for(int k=0;k<m.numCols;k++) { 340 array[j][k]=matrix[j][0]*m.matrix[0][k]; 341 for(int n=1;n<numCols;n++) 342 array[j][k]+=matrix[j][n]*m.matrix[n][k]; 343 } 344 } 345 if(numRows == m.numCols) 346 return new DoubleSquareMatrix(array); 347 else 348 return new DoubleMatrix(array); 349 } else 350 throw new MatrixDimensionException("Incompatible matrices."); 351 } 352 353 355 358 public AbstractDoubleMatrix directSum(final AbstractDoubleMatrix m) { 359 final double array[][]=new double[numRows+m.numRows][numCols+m.numCols]; 360 for(int i=0;i<numRows;i++) { 361 for(int j=0;j<numCols;j++) 362 array[i][j] = matrix[i][j]; 363 } 364 for(int i=0;i<m.numRows;i++) { 365 for(int j=0;j<m.numCols;j++) 366 array[i+numRows][j+numCols] = m.getElement(i,j); 367 } 368 return new DoubleMatrix(array); 369 } 370 371 373 376 public AbstractDoubleMatrix tensor(final AbstractDoubleMatrix m) { 377 final double array[][]=new double[numRows*m.numRows][numCols*m.numCols]; 378 for(int i=0;i<numRows;i++) { 379 for(int j=0;j<numCols;j++) { 380 for(int k=0;k<m.numRows;j++) { 381 for(int l=0;l<m.numCols;l++) 382 array[i*m.numRows+k][j*m.numCols+l] = matrix[i][j]*m.getElement(k,l); 383 } 384 } 385 } 386 return new DoubleMatrix(array); 387 } 388 389 391 395 public Matrix transpose() { 396 final double array[][]=new double[numCols][numRows]; 397 for(int i=0;i<numRows;i++) { 398 array[0][i] = matrix[i][0]; 399 for(int j=1;j<numCols;j++) 400 array[j][i] = matrix[i][j]; 401 } 402 return new DoubleMatrix(array); 403 } 404 405 407 412 public AbstractDoubleMatrix mapElements(final Mapping f) { 413 final double array[][]=new double[numRows][numCols]; 414 for(int i=0;i<numRows;i++) { 415 array[i][0] = f.map(matrix[i][0]); 416 for(int j=1;j<numCols;j++) 417 array[i][j] = f.map(matrix[i][j]); 418 } 419 return new DoubleMatrix(array); 420 } 421 } 422 | Popular Tags |