List of usage examples for org.apache.commons.math.linear BlockRealMatrix BlockRealMatrix
public BlockRealMatrix(final double[][] rawData) throws IllegalArgumentException
From source file:Covariance.java
/** * Create a Covariance matrix from a rectangular array * whose columns represent covariates. // w w w . j a v a 2s .c o m * * <p>The <code>biasCorrected</code> parameter determines whether or not * covariance estimates are bias-corrected.</p> * * <p>The input array must be rectangular with at least two columns * and two rows.</p> * * @param data rectangular array with columns representing covariates * @param biasCorrected true means covariances are bias-corrected * @throws IllegalArgumentException if the input data array is not * rectangular with at least two rows and two columns. */ public Covariance(double[][] data, boolean biasCorrected, double mu[]) { this(new BlockRealMatrix(data), biasCorrected, mu); }
From source file:Covariance.java
/** * Compute a covariance matrix from a rectangular array whose columns represent * covariates. /*from www . j a v a 2 s . co m*/ * @param data input array (must have at least two columns and two rows) * @param biasCorrected determines whether or not covariance estimates are bias-corrected * @return covariance matrix */ protected RealMatrix computeCovarianceMatrix(double[][] data, boolean biasCorrected) { return computeCovarianceMatrix(new BlockRealMatrix(data), biasCorrected); }
From source file:guineu.modules.dataanalysis.correlations.CorrelationTask.java
private RealMatrix getCorrelation(PeakListRow row, PeakListRow row2, List<String> sampleNames) { List<Double[]> data = new ArrayList<Double[]>(); for (int i = 0; i < sampleNames.size(); i++) { try {/*from w w w. jav a 2 s. c o m*/ if ((Double) row.getPeak(sampleNames.get(i)) != Double.NaN && (Double) row2.getPeak(sampleNames.get(i)) != Double.NaN && (Double) row.getPeak(sampleNames.get(i)) != 0.0 && (Double) row2.getPeak(sampleNames.get(i)) != 0.0) { Double[] dat = new Double[2]; dat[0] = (Double) row.getPeak(sampleNames.get(i)); dat[1] = (Double) row2.getPeak(sampleNames.get(i)); data.add(dat); // System.out.println(sampleNames.elementAt(i) + " - " + row.getPeak(sampleNames.elementAt(i)) + " - " + row2.getPeak(sampleNames.elementAt(i))); } } catch (Exception e) { //System.out.println(row.getPeak(sampleNames.elementAt(i)) + " - " + row2.getPeak(sampleNames.elementAt(i))); //e.printStackTrace(); } } double[][] dataMatrix = new double[data.size()][2]; int count = 0; for (Double[] dat : data) { dataMatrix[count][0] = dat[0]; dataMatrix[count++][1] = dat[1]; } RealMatrix matrix = new BlockRealMatrix(dataMatrix); return matrix; }
From source file:guineu.modules.filter.Alignment.SerumHuNormalization.SerumHuNormalizationTask.java
private RealMatrix getMatrix(PeakListRow row, PeakListRow row2, List<String> sampleNames) { List<Double[]> data = new ArrayList<Double[]>(); for (int i = 0; i < sampleNames.size(); i++) { try {// w w w. ja v a2 s .co m if ((Double) row.getPeak(sampleNames.get(i)) != Double.NaN && (Double) row2.getPeak(sampleNames.get(i)) != Double.NaN && (Double) row.getPeak(sampleNames.get(i)) != 0.0 && (Double) row2.getPeak(sampleNames.get(i)) != 0.0) { Double[] dat = new Double[2]; dat[0] = (Double) row.getPeak(sampleNames.get(i)); dat[1] = (Double) row2.getPeak(sampleNames.get(i)); data.add(dat); } } catch (Exception e) { } } if (data.size() > 0) { double[][] dataMatrix = new double[data.size()][2]; int count = 0; for (Double[] dat : data) { dataMatrix[count][0] = dat[0]; dataMatrix[count++][1] = dat[1]; } RealMatrix matrix = new BlockRealMatrix(dataMatrix); return matrix; } else { return null; } }
From source file:name.mjw.cytospade.fcsFile.java
/** * getCompensatedEventList ---//from ww w .j a va2 s .c o m * <p> * Returns the event list compensated by the SPILL matrix. * <p> * * @return array of double arrays containing the events. */ public double[][] getCompensatedEventList() { double[][] events = this.getEventList(); if (events.length != this.getNumChannels()) return events; // Unable to extract the underlying events // Convert the SPILL string to a compensation matrix String compString = this.getSpillString(); if (compString == null) return events; // No compensation, just return the events // Split the compensation string into its values // // The basic structure for SPILL* is: // $SPILLOVER/n,string1,string2,...,f1,f2,f3,f4,.../ String[] compValues = compString.split(","); String[] compNames = null; String[] compData = null; int compDataStart = 0; int n = 0; try { // Try to parse the number of acquisition parameters n = Integer.parseInt(compValues[0]); if (n <= 0 || n > this.parameters) throw new NumberFormatException(); } catch (NumberFormatException nfe) { CyLogger.getLogger().error("Failed to parse parameter count in spill string", nfe); return events; } compNames = Arrays.copyOfRange(compValues, 1, n + 1); // Match names in spill string to columns in parameter lists compDataStart = Arrays.asList(this.channelShortname).indexOf(compNames[0]); if (compDataStart < 0) { CyLogger.getLogger().error("Failed to match channel " + compNames[0] + " to parameter in file"); return events; // Failure match spill string names to channels } for (int i = 0; i < n; i++) { if (!compNames[i].equals(this.channelShortname[compDataStart + i])) { CyLogger.getLogger().error("Spill channel are not continguous parameters in file"); return events; // Spill string columns not in order } } // Extract actual compensation data compData = Arrays.copyOfRange(compValues, n + 1, compValues.length); if (compData.length != (n * n)) return events; /** * Populate the compensation matrix --- The values are stored in * row-major order, i.e., the elements in the first row appear * first. */ double[][] matrix = new double[n][n]; // Loop through the array of compensation values for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { try { matrix[i][j] = Double.parseDouble(compData[i * n + j]); } catch (NumberFormatException nfe) { // Set default value If a NumberFormatException occurred matrix[i][j] = 0.0d; } } } // Compute the inverse of the compensation data and then apply // to data matrix (which is column major). Specifically compute // transpose(inverse(<SPILL MATRIX>)) * data RealMatrix comp = (new LUDecompositionImpl(new Array2DRowRealMatrix(matrix))).getSolver().getInverse(); RealMatrix data = new BlockRealMatrix(events); data.setSubMatrix( // Update compensated portion of data matrix comp.transpose() .multiply(data.getSubMatrix(compDataStart, compDataStart + n - 1, 0, this.getEventCount() - 1)) .getData(), compDataStart, 0); return data.getData(); }
From source file:gephi.spade.panel.fcsFile.java
/** * getCompensatedEventList ---//from w ww . j a v a2s. c o m * <p> * Returns the event list compensated by the SPILL matrix. * <p> * * @return array of double arrays containing the events. */ public double[][] getCompensatedEventList() { double[][] events = this.getEventList(); if (events.length != this.getNumChannels()) return events; // Unable to extract the underlying events // Convert the SPILL string to a compensation matrix String compString = this.getSpillString(); if (compString == null) return events; // No compensation, just return the events // Split the compensation string into its values // // The basic structure for SPILL* is: // $SPILLOVER/n,string1,string2,...,f1,f2,f3,f4,.../ String[] compValues = compString.split(","); String[] compNames = null; String[] compData = null; int compDataStart = 0; int n = 0; try { // Try to parse the number of acquisition parameters n = Integer.parseInt(compValues[0]); if (n <= 0 || n > this.parameters) throw new NumberFormatException(); } catch (NumberFormatException nfe) { //CyLogger.getLogger().error("Failed to parse parameter count in spill string",nfe); return events; } compNames = Arrays.copyOfRange(compValues, 1, n + 1); // Match names in spill string to columns in parameter lists compDataStart = Arrays.asList(this.channelShortname).indexOf(compNames[0]); if (compDataStart < 0) { //CyLogger.getLogger().error("Failed to match channel "+compNames[0]+" to parameter in file"); return events; // Failure match spill string names to channels } for (int i = 0; i < n; i++) { if (!compNames[i].equals(this.channelShortname[compDataStart + i])) { //CyLogger.getLogger().error("Spill channel are not continguous parameters in file"); return events; // Spill string columns not in order } } // Extract actual compensation data compData = Arrays.copyOfRange(compValues, n + 1, compValues.length); if (compData.length != (n * n)) return events; /** * Populate the compensation matrix --- The values are stored in * row-major order, i.e., the elements in the first row appear * first. */ double[][] matrix = new double[n][n]; // Loop through the array of compensation values for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { try { matrix[i][j] = Double.parseDouble(compData[i * n + j]); } catch (NumberFormatException nfe) { // Set default value If a NumberFormatException occurred matrix[i][j] = 0.0d; } } } // Compute the inverse of the compensation data and then apply // to data matrix (which is column major). Specifically compute // transpose(inverse(<SPILL MATRIX>)) * data RealMatrix comp = (new LUDecompositionImpl(new Array2DRowRealMatrix(matrix))).getSolver().getInverse(); RealMatrix data = new BlockRealMatrix(events); data.setSubMatrix( // Update compensated portion of data matrix comp.transpose() .multiply(data.getSubMatrix(compDataStart, compDataStart + n - 1, 0, this.getEventCount() - 1)) .getData(), compDataStart, 0); return data.getData(); }
From source file:org.mitre.math.linear.RandomMatrixExample.java
public int run(String args[]) throws Exception { int m = 100;//from ww w.j a v a 2 s . co 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; }