List of usage examples for org.apache.commons.math3.linear RealMatrix createMatrix
RealMatrix createMatrix(int rowDimension, int columnDimension) throws NotStrictlyPositiveException;
From source file:edu.cmu.tetrad.util.MatrixUtils.java
public static RealMatrix transposeWithoutCopy(final RealMatrix apacheData) { return new AbstractRealMatrix(apacheData.getColumnDimension(), apacheData.getRowDimension()) { @Override/*from w ww . ja v a 2s . co m*/ public int getRowDimension() { return apacheData.getColumnDimension(); } @Override public int getColumnDimension() { return apacheData.getRowDimension(); } @Override public RealMatrix createMatrix(int rowDimension, int columnDimension) throws NotStrictlyPositiveException { return apacheData.createMatrix(rowDimension, columnDimension); } @Override public RealMatrix copy() { throw new IllegalArgumentException("Can't copy"); } @Override public double getEntry(int i, int j) throws OutOfRangeException { // throw new UnsupportedOperationException(); return apacheData.getEntry(j, i); } @Override public void setEntry(int i, int j, double v) throws OutOfRangeException { throw new UnsupportedOperationException(); } }; }
From source file:edu.dfci.cccb.mev.domain.Heatmap.java
private RealMatrix transpose(final RealMatrix original) { return new AbstractRealMatrix() { @Override/*www . jav a 2 s.co m*/ public void setEntry(int row, int column, double value) throws OutOfRangeException { original.setEntry(column, row, value); } @Override public int getRowDimension() { return original.getColumnDimension(); } @Override public double getEntry(int row, int column) throws OutOfRangeException { return original.getEntry(column, row); } @Override public int getColumnDimension() { return original.getRowDimension(); } @Override public RealMatrix createMatrix(int rowDimension, int columnDimension) throws NotStrictlyPositiveException { return original.createMatrix(rowDimension, columnDimension); } @Override public RealMatrix copy() { final RealMatrix result = createMatrix(getRowDimension(), getColumnDimension()); walkInOptimizedOrder(new RealMatrixPreservingVisitor() { @Override public void visit(int row, int column, double value) { result.setEntry(row, column, value); } @Override public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) { } @Override public double end() { return NaN; } }); return result; } }; }
From source file:org.knime.al.util.noveltydetection.knfst.KNFST.java
public static RealMatrix projection(final RealMatrix kernelMatrix, final String[] labels) throws KNFSTException { final ClassWrapper[] classes = ClassWrapper.classes(labels); // check labels if (classes.length == 1) { throw new IllegalArgumentException( "not able to calculate a nullspace from data of a single class using KNFST (input variable \"labels\" only contains a single value)"); }/* w w w . java 2s.c o m*/ // check kernel matrix if (!kernelMatrix.isSquare()) { throw new IllegalArgumentException("The KernelMatrix must be quadratic!"); } // calculate weights of orthonormal basis in kernel space final RealMatrix centeredK = centerKernelMatrix(kernelMatrix); final EigenDecomposition eig = new EigenDecomposition(centeredK); final double[] eigVals = eig.getRealEigenvalues(); final ArrayList<Integer> nonZeroEigValIndices = new ArrayList<Integer>(); for (int i = 0; i < eigVals.length; i++) { if (eigVals[i] > 1e-12) { nonZeroEigValIndices.add(i); } } int eigIterator = 0; final RealMatrix eigVecs = eig.getV(); RealMatrix basisvecs = null; try { basisvecs = MatrixUtils.createRealMatrix(eigVecs.getRowDimension(), nonZeroEigValIndices.size()); } catch (final Exception e) { throw new KNFSTException("Something went wrong. Try different parameters or a different kernel."); } for (final Integer index : nonZeroEigValIndices) { final double normalizer = 1 / Math.sqrt(eigVals[index]); final RealVector basisVec = eigVecs.getColumnVector(eigIterator).mapMultiply(normalizer); basisvecs.setColumnVector(eigIterator++, basisVec); } // calculate transformation T of within class scatter Sw: // T= B'*K*(I-L) and L a block matrix final RealMatrix L = kernelMatrix.createMatrix(kernelMatrix.getRowDimension(), kernelMatrix.getColumnDimension()); int start = 0; for (final ClassWrapper cl : classes) { final int count = cl.getCount(); L.setSubMatrix(MatrixFunctions.ones(count, count).scalarMultiply(1.0 / count).getData(), start, start); start += count; } // need Matrix M with all entries 1/m to modify basisvecs which allows // usage of // uncentered kernel values (eye(size(M)).M)*basisvecs final RealMatrix M = MatrixFunctions .ones(kernelMatrix.getColumnDimension(), kernelMatrix.getColumnDimension()) .scalarMultiply(1.0 / kernelMatrix.getColumnDimension()); final RealMatrix I = MatrixUtils.createRealIdentityMatrix(M.getColumnDimension()); // compute helper matrix H final RealMatrix H = ((I.subtract(M)).multiply(basisvecs)).transpose().multiply(kernelMatrix) .multiply(I.subtract(L)); // T = H*H' = B'*Sw*B with B=basisvecs final RealMatrix T = H.multiply(H.transpose()); // calculate weights for null space RealMatrix eigenvecs = MatrixFunctions.nullspace(T); if (eigenvecs == null) { final EigenDecomposition eigenComp = new EigenDecomposition(T); final double[] eigenvals = eigenComp.getRealEigenvalues(); eigenvecs = eigenComp.getV(); final int minId = MatrixFunctions.argmin(MatrixFunctions.abs(eigenvals)); final double[] eigenvecsData = eigenvecs.getColumn(minId); eigenvecs = MatrixUtils.createColumnRealMatrix(eigenvecsData); } // System.out.println("eigenvecs:"); // test.printMatrix(eigenvecs); // calculate null space projection final RealMatrix proj = ((I.subtract(M)).multiply(basisvecs)).multiply(eigenvecs); return proj; }
From source file:org.knime.al.util.noveltydetection.knfst.MatrixFunctions.java
public static RealMatrix multiplyElementWise(final RealMatrix matrix1, final RealMatrix matrix2) { if (matrix1.getRowDimension() != matrix2.getRowDimension() || matrix1.getColumnDimension() != matrix2.getColumnDimension()) { throw new IllegalArgumentException("The matrices must be of the same dimensions!"); }//from www.ja va2s .c om final RealMatrix result = matrix1.createMatrix(matrix1.getRowDimension(), matrix1.getColumnDimension()); for (int r = 0; r < matrix1.getRowDimension(); r++) { for (int c = 0; c < matrix1.getColumnDimension(); c++) { result.setEntry(r, c, matrix1.getEntry(r, c) * matrix2.getEntry(r, c)); } } return result; }
From source file:org.knime.al.util.noveltydetection.knfst.MatrixFunctions.java
public static RealMatrix pow(final RealMatrix matrix, final double power) { final RealMatrix result = matrix.createMatrix(matrix.getRowDimension(), matrix.getColumnDimension()); for (int r = 0; r < result.getRowDimension(); r++) { for (int c = 0; c < result.getColumnDimension(); c++) { result.setEntry(r, c, Math.pow(matrix.getEntry(r, c), power)); }/*from w w w . j av a 2 s .c o m*/ } return result; }