/** * Function to compute Cholesky decomposition of the given input matrix. The input must be a real * symmetric positive-definite matrix. * * @param in commons-math3 Array2DRowRealMatrix * @return matrix block * @throws DMLRuntimeException if DMLRuntimeException occurs */ private static MatrixBlock computeCholesky(Array2DRowRealMatrix in) throws DMLRuntimeException { if (!in.isSquare()) throw new DMLRuntimeException( "Input to cholesky() must be square matrix -- given: a " + in.getRowDimension() + "x" + in.getColumnDimension() + " matrix."); CholeskyDecomposition cholesky = new CholeskyDecomposition(in); RealMatrix rmL = cholesky.getL(); return DataConverter.convertToMatrixBlock(rmL.getData()); }
/** * Returns the result of postmultiplying {@code this} by {@code m}. * * @param m matrix to postmultiply by * @return {@code this * m} * @throws DimensionMismatchException if {@code columnDimension(this) != rowDimension(m)} */ public Array2DRowRealMatrix multiply(final Array2DRowRealMatrix m) throws DimensionMismatchException { MatrixUtils.checkMultiplicationCompatible(this, m); final int nRows = this.getRowDimension(); final int nCols = m.getColumnDimension(); final int nSum = this.getColumnDimension(); final double[][] outData = new double[nRows][nCols]; // Will hold a column of "m". final double[] mCol = new double[nSum]; final double[][] mData = m.data; // Multiply. for (int col = 0; col < nCols; col++) { // Copy all elements of column "col" of "m" so that // will be in contiguous memory. for (int mRow = 0; mRow < nSum; mRow++) { mCol[mRow] = mData[mRow][col]; } for (int row = 0; row < nRows; row++) { final double[] dataRow = data[row]; double sum = 0; for (int i = 0; i < nSum; i++) { sum += dataRow[i] * mCol[i]; } outData[row][col] = sum; } } return new Array2DRowRealMatrix(outData, false); }
/** * Function to compute matrix inverse via matrix decomposition. * * @param in commons-math3 Array2DRowRealMatrix * @return matrix block * @throws DMLRuntimeException if DMLRuntimeException occurs */ private static MatrixBlock computeMatrixInverse(Array2DRowRealMatrix in) throws DMLRuntimeException { if (!in.isSquare()) throw new DMLRuntimeException( "Input to inv() must be square matrix -- given: a " + in.getRowDimension() + "x" + in.getColumnDimension() + " matrix."); QRDecomposition qrdecompose = new QRDecomposition(in); DecompositionSolver solver = qrdecompose.getSolver(); RealMatrix inverseMatrix = solver.getInverse(); return DataConverter.convertToMatrixBlock(inverseMatrix.getData()); }
public void outputMatrix(String file) throws Exception { int row = lpsolver.getNrows() + 1; int col = lpsolver.getNcolumns() + 1; ArrayRealVector f = new ArrayRealVector(col - 1); for (int j = 1; j < col; j++) { f.setEntry(j - 1, lpsolver.getMat(0, j)); } Array2DRowRealMatrix Aeq = new Array2DRowRealMatrix(numOfBus, col - 1); ArrayRealVector beq = new ArrayRealVector(numOfBus); for (int i = 1; i < numOfBus + 1; i++) { for (int j = 1; j < col; j++) { Aeq.setEntry(i - 1, j - 1, lpsolver.getMat(i, j)); } beq.setEntry(i - 1, lpsolver.getRh(i)); } Array2DRowRealMatrix Aiq = new Array2DRowRealMatrix(row - numOfBus - 1, col - 1); ArrayRealVector biq = new ArrayRealVector(row - numOfBus - 1); int cnt = 0; for (int i = numOfBus + 1; i < row; i++) { for (int j = 1; j < col; j++) { Aiq.setEntry(cnt, j - 1, lpsolver.getMat(i, j)); } biq.setEntry(cnt, lpsolver.getRh(i)); cnt++; } ArrayRealVector lb = new ArrayRealVector(col - 1); for (int i = 1; i < col - 1; i++) { lb.setEntry(i - 1, lpsolver.getLowbo(i)); } ArrayRealVector ub = new ArrayRealVector(col - 1); for (int i = 1; i < col; i++) { ub.setEntry(i - 1, lpsolver.getUpbo(i)); } writeMatlabInputFile(file, f, Aeq, beq, Aiq, biq, ub, lb); }
/* 133: */ /* 134: */ public void updateHighOrderDerivativesPhase2( double[] start, double[] end, Array2DRowRealMatrix highOrder) /* 135: */ { /* 136:330 */ double[][] data = highOrder.getDataRef(); /* 137:331 */ for (int i = 0; i < data.length; i++) /* 138: */ { /* 139:332 */ double[] dataI = data[i]; /* 140:333 */ double c1I = this.c1[i]; /* 141:334 */ for (int j = 0; j < dataI.length; j++) { /* 142:335 */ dataI[j] += c1I * (start[j] - end[j]); /* 143: */ } /* 144: */ } /* 145: */ }
@Override public void process() { // Get image dimensions, store ICA dimensions (number of frames). int[] dim = ip.getDimensions(); dimensions = dim[4]; IJ.showStatus("ICA: reformatting data matrix..."); // Compute number of real voxels to be used int n = 0; for (@SuppressWarnings("unused") Voxel v : ip) { n++; } // Create new array and fill it with the image data double[][] image_data = new double[n][dimensions]; int i = 0; for (Voxel v : ip) { image_data[i++] = v.tac; } // Transpose the data matrix Array2DRowRealMatrix temp = new Array2DRowRealMatrix(image_data, false); image_data = temp.transpose().getData(); temp = null; System.gc(); // Perform the ICA computation IJ.showStatus("ICA: performing source separation " + "(may take some time)..."); FastICA fi = null; try { fi = new FastICA(image_data, ican); } catch (FastICAException e) { System.out.println(e.getLocalizedMessage()); } IJ.showStatus("ICA computed, reformatting results..."); // Get projections on each dimension double[][] vectors = fi.getICVectors(); // Get independent signals (to be offered as additional information) // It is transposed to print the columns. double[][] sep = fi.getSeparatingMatrix(); RealMatrix sources = new Array2DRowRealMatrix(sep); sources = sources.transpose(); Array2DRowRealMatrix result = new Array2DRowRealMatrix(vectors, false); // If the ICA image is to be shown, create a new image with // result.getRowDimension() frames and the original number of // x, y, z dimensions if (showICA) { ImagePlus ICA_image = RealMatrix2IJ(result, dim, this.ip, "ICA image"); ICA_image.show(); } // Please note: this is somehow incorrect. As the clustering model // that we are following needs one voxel -> one cluster, this step // below assigns each voxel to the principal component with the // maximum weight for its particular kinetics. In "real life", the // resulting images would contain the contribution of that component // in all voxels, but for segmentation purposes this approach is // chosen. int column_index = 0; for (Voxel v : ip) { double[] projection = result.getColumn(column_index++); // Every Voxel belongs to the maximum index of its projected TAC int max = getMaxIndex(projection) + 1; addTACtoCluster(v, max); } // Fill in the additionalInfo array. additionalInfo = new String[2]; additionalInfo[0] = "ica_sources"; StringBuilder sb = new StringBuilder(); int rows = sources.getRowDimension(); for (int j = 0; j < rows; j++) { double[] row = sources.getRow(j); sb.append(Arrays.toString(row)); sb.append("\n"); } // Remove brackets String tempstr = sb.toString(); tempstr = tempstr.replace("[", ""); tempstr = tempstr.replace("]", ""); additionalInfo[1] = tempstr; }