List of usage examples for org.apache.commons.math.linear BlockRealMatrix multiply
public BlockRealMatrix multiply(BlockRealMatrix m) throws IllegalArgumentException
From source file:org.mitre.math.linear.RandomMatrixExample.java
public int run(String args[]) throws Exception { int m = 100;/*from w ww. j a v a2s . c o m*/ int n = 60; boolean generate = false; ArrayList<String> other_args = new ArrayList<String>(); for (int i = 0; i < args.length; ++i) { try { if ("-r".equals(args[i])) { m = Integer.parseInt(args[++i]); } else if ("-c".equals(args[i])) { n = Integer.parseInt(args[++i]); } else if ("-g".equals(args[i])) { generate = true; } else { other_args.add(args[i]); } } catch (NumberFormatException ex) { System.out.println("ERROR: Integer expected instead of " + args[i]); return -1; } catch (ArrayIndexOutOfBoundsException except) { System.out.println("ERROR: Required parameter missing from " + args[i - 1]); return -1; } } if (generate) { LOG.info(String.format("Generating random %dx%d matrix '%s'", m, n, other_args.get(0))); return this.generateRandomMatrix(other_args.get(1), m, n); } LOG.info(String.format("Making random matrix %d}x%d", m, n)); double[][] M = this.randomMatrixVariables(m, n); LOG.info(String.format("Creating Array2DRowRealMatrix from random matrix %dx%d", m, n)); Array2DRowRealMatrix arm = new Array2DRowRealMatrix(M); LOG.info(String.format("Creating BlockRealMatrix from random matrix %dx%d", m, n)); BlockRealMatrix brm = new BlockRealMatrix(M); LOG.info(String.format("Checking Array2DRow and BlockRealMatrix")); if (!this.compareMatrices(arm, brm)) { LOG.fatal("One of these are not like the other!"); } LOG.info(String.format("Creating BufferRealMatrix %dx%d", m, n)); BufferRealMatrix mbrm = new BufferRealMatrix(m, n, null); LOG.info(String.format("Populating BufferRealMatrix %dx%d from random matrix", m, n)); //mbrm.walkInOptimizedOrder(new PopulateMatrixChangingVisitor(brm)); /**/ // not the speedest use walkInOptimizedOrder! for (int i = 0; i < brm.getRowDimension(); i++) { for (int j = 0; j < brm.getColumnDimension(); j++) { mbrm.setEntry(i, j, brm.getEntry(i, j)); } } // System.err.println(this.matrix2String(mbrm, n, 8)); LOG.info("Checking block matrix verse buffer matrix..."); mbrm.walkInOptimizedOrder(new ComparingPreservingVisitor(brm)); LOG.info("Checking matrix addition..."); BlockRealMatrix brm2 = brm.add(new BlockRealMatrix(M)); BufferRealMatrix mbrm_tmp = new BufferRealMatrix(m, n, null); mbrm_tmp.walkInOptimizedOrder(new PopulateMatrixChangingVisitor(brm)); BufferRealMatrix mbrm2 = mbrm.add(mbrm_tmp); mbrm2.walkInOptimizedOrder(new ComparingPreservingVisitor(brm2)); LOG.info("Checking matrix multiplication (none are numerically stable to the final decimal places)..."); BlockRealMatrix brm_tmp = new BlockRealMatrix(n, m); brm_tmp.walkInOptimizedOrder(new RandomRealMatrixChangingVisitor()); LOG.info("Starting BlockRealMatrix Multiplication"); brm2 = brm.multiply(brm_tmp); LOG.info("Finished BlockRealMatrix Multiplication"); mbrm_tmp = new BufferRealMatrix(n, m, null); mbrm_tmp.walkInOptimizedOrder(new PopulateMatrixChangingVisitor(brm_tmp)); LOG.info("Starting BufferRealMatrix Multiplication"); mbrm2 = mbrm.multiply(mbrm_tmp); LOG.info("Finished BufferRealMatrix Multiplication"); LOG.info("Checking BlockRealMatrix to BufferRealMatrix"); mbrm2.walkInOptimizedOrder(new ComparingPreservingVisitor(brm2)); final int bufferBlockLength = BufferRealMatrix.BLOCK_SIZE; int startRow = m / 2; int endRow = m - 1; int startColumn = n / 2; int endColumn = n - 1; LOG.debug(String.format("Checking getting buffer submatrix (%d,%d) to (%d,%d)", startRow, startColumn, endRow, endColumn)); double[][] sub = mbrm.getSubMatrixData(startRow, endRow, startColumn, endColumn); LOG.info(String.format("Found %f at submatrix 0,0 and %f for %d,%d", sub[0][0], mbrm.getEntry(startRow, startColumn), startRow, startColumn)); LOG.info("Allocation testing, doubling size each iteration"); int am = m; int an = n; LOG.info("Starting allocating BlockRealMatrices...."); try { while (true) { am *= 2; an *= 2; BlockRealMatrix t = new BlockRealMatrix(am, an); } } catch (OutOfMemoryError e) { LOG.info(String.format("Ran out of memory generating %dx%d matrix", am, an)); } LOG.info("Creating BufferRealMatrix at last attempted in size..."); File file = new File(this.localTmpFile()); BufferRealMatrix t = new BufferRealMatrix(am, an, file); // slow way need to optimize for blocks t.walkInOptimizedOrder(new RandomRealMatrixChangingVisitor()); LOG.info(String.format("(0,2) = %f", t.getEntry(0, 2))); t = null; LOG.info("Attempting to reload matrix"); BufferRealMatrix rt = BufferRealMatrix.loadMatrix(file); LOG.info(String.format("reloaded (0,2) = %f", rt.getEntry(0, 2))); return 0; }