List of usage examples for org.apache.commons.math3.linear RealMatrix setEntry
void setEntry(int row, int column, double value) throws OutOfRangeException;
From source file:lsafunctions.LSA.java
private static RealMatrix normalizeMatrix(RealMatrix M) { double sumColumn = 0; // Matrix row = new Matrix(1, M.getColumnDimension()); RealMatrix row = MatrixUtils.createRealMatrix(1, M.getColumnDimension()); for (int j = 0; j < M.getColumnDimension(); j++) { sumColumn = 0;//from ww w . ja v a2s . com for (int k = 0; k < M.getRowDimension(); k++) { sumColumn += Math.pow(M.getEntry(k, j), 2); } sumColumn = Math.sqrt(sumColumn); row.setEntry(0, j, sumColumn); } for (int j = 0; j < M.getColumnDimension(); j++) { for (int k = 0; k < M.getRowDimension(); k++) { M.setEntry(k, j, M.getEntry(k, j) / row.getEntry(0, j)); } } return M; }
From source file:edu.washington.gs.skyline.model.quantification.LinearModel.java
private static RealMatrix computeMatrixCrossproduct(RealMatrix matrix, int[] columnIndexes) { RealMatrix result = MatrixUtils.createRealMatrix(columnIndexes.length, columnIndexes.length); for (int iRow = 0; iRow < columnIndexes.length; iRow++) { for (int iCol = 0; iCol < columnIndexes.length; iCol++) { double dotProduct = matrix.getColumnVector(iRow).dotProduct(matrix.getColumnVector(iCol)); result.setEntry(iRow, iCol, dotProduct); }// ww w . ja v a 2s .co m } return result; }
From source file:com.yahoo.egads.utilities.SpectralMethods.java
public static TimeSeries.DataSequence mFilter(TimeSeries.DataSequence data, int windowSize, FilteringMethod method, double methodParameter) { TimeSeries.DataSequence result = new TimeSeries.DataSequence(); RealMatrix dataMat = MatrixUtils.createRealMatrix(data.size(), 1); int i = 0;/* ww w . j a va 2s. co m*/ for (TimeSeries.Entry e : data) { dataMat.setEntry(i, 0, e.value); i++; TimeSeries.Entry eCopy = new TimeSeries.Entry(e); result.add(eCopy); } RealMatrix resultMat = SpectralMethods.mFilter(dataMat, windowSize, method, methodParameter); i = 0; for (TimeSeries.Entry e : result) { e.value = (float) resultMat.getEntry(i, 0); i++; } return result; }
From source file:net.myrrix.common.math.MatrixUtils.java
public static RealMatrix multiplyXYT(FastByIDMap<float[]> X, FastByIDMap<float[]> Y) { int Ysize = Y.size(); int Xsize = X.size(); RealMatrix result = new Array2DRowRealMatrix(Xsize, Ysize); for (int row = 0; row < Xsize; row++) { for (int col = 0; col < Ysize; col++) { result.setEntry(row, col, SimpleVectorMath.dot(X.get(row), Y.get(col))); }/*from w ww. j a v a2 s . c om*/ } return result; }
From source file:edu.cudenver.bios.matrix.MatrixUtilities.java
/** * Force a square RealMatrix to be symmetric. * * @param rm The RealMatrix./*w ww. ja v a 2 s. com*/ * * @return The same RealMatrix, modified if necessary * to be symmetric. * * @throws NonSquareMatrixException if the RealMatrix is * not square. */ public static RealMatrix forceSymmetric(RealMatrix rm) { int m = rm.getRowDimension(); int n = rm.getColumnDimension(); if (m != n) { throw new NonSquareMatrixException(m, n); } for (int i = 0; i < m; ++i) { for (int j = i + 1; j < n; ++j) { double value = (rm.getEntry(i, j) + rm.getEntry(j, i)) / 2; rm.setEntry(i, j, value); rm.setEntry(j, i, value); } } return rm; }
From source file:edu.oregonstate.eecs.mcplan.ml.MatrixAlgorithms.java
public static RealMatrix makePositiveDefinite(final RealMatrix M, final double eps) { assert (eps > 0.0); final SingularValueDecomposition svd = new SingularValueDecomposition(M); final RealMatrix Sigma = svd.getS().copy(); final int N = Math.min(Sigma.getColumnDimension(), Sigma.getRowDimension()); for (int i = 0; i < N; ++i) { final double lambda = Sigma.getEntry(i, i); System.out.println("lambda_" + i + " = " + lambda); if (Math.abs(lambda) < eps) { System.out.println("Corrected " + i); Sigma.setEntry(i, i, eps); } else if (lambda < 0.0) { throw new NonPositiveDefiniteMatrixException(lambda, i, eps); } else {/*from w w w .j ava 2 s .c o m*/ Sigma.setEntry(i, i, lambda); } } return svd.getU().multiply(Sigma).multiply(svd.getVT()); }
From source file:edu.oregonstate.eecs.mcplan.ml.MatrixAlgorithms.java
/** * Computes the inverse of a matrix using the singular value decomposition. * //ww w.j av a 2s . c o m * The input matrix M is assumed to be positive definite up to numerical * precision 'eps'. That is, for all eigenvalues lambda of M, it must be * the case that lambda + eps > 0. For eigenvalues with |lambda| < eps, the * eigenvalue is set to 'eps' before inverting. Throws an exception if * any lambda < -eps. * @param M * @param eps * @return */ public static RealMatrix robustInversePSD(final RealMatrix M, final double eps) { assert (eps > 0.0); final SingularValueDecomposition svd = new SingularValueDecomposition(M); final RealMatrix Sigma = svd.getS().copy(); final int N = Math.min(Sigma.getColumnDimension(), Sigma.getRowDimension()); for (int i = 0; i < N; ++i) { final double lambda = Sigma.getEntry(i, i); System.out.println("lambda_" + i + " = " + lambda); if (Math.abs(lambda) < eps) { System.out.println("Corrected " + i); Sigma.setEntry(i, i, 1.0 / eps); } else if (lambda < 0.0) { throw new IllegalArgumentException("Negative eigenvalue " + lambda); } else { Sigma.setEntry(i, i, 1.0 / lambda); } } return svd.getV().multiply(Sigma.transpose()).multiply(svd.getUT()); }
From source file:ch.zhaw.iamp.rct.weights.Weights.java
private static RealMatrix addNoise(final RealMatrix A) { RealMatrix buffer = A.copy();//from w w w. ja va2 s .c o m RealMatrix noise = MatrixUtils.createRealMatrix(A.getRowDimension(), A.getColumnDimension()); for (int i = 0; i < A.getRowDimension(); ++i) { for (int j = 0; j < A.getColumnDimension(); ++j) { double noiseSign = Math.random() > 0.5 ? 1 : -1; double noiseAmplitude = 1; noise.setEntry(i, j, noiseSign * Math.random() * noiseAmplitude); } } buffer = buffer.add(noise); return buffer; }
From source file:lsafunctions.LSA.java
public static RealMatrix calculateTfIdf(RealMatrix M) { int tf;//from w w w .ja va2s . c o m double idf; int df; double ndf; for (int j = 0; j < M.getRowDimension(); j++) { df = calcDf(j, M); // System.out.println("J:"+j+" df:"+df); for (int k = 0; k < M.getColumnDimension(); k++) { tf = (int) M.getEntry(j, k); ndf = M.getColumnDimension() / df; idf = Math.log(ndf) / Math.log(2); M.setEntry(j, k, idf * tf); } } //M.print(NumberFormat.INTEGER_FIELD, M.getColumnDimension()); M = normalizeMatrix(M); return M; }
From source file:ch.unil.genescore.vegas.LinkageDisequilibrium.java
/** Compute rectangular 'cross'-correlation (LD) r values for the two given snp lists( */ static public RealMatrix computeCrossCorrelationMatrix(ArrayList<Snp> geneSnps1, ArrayList<Snp> geneSnps2) { // The correlation matrix for the given set of snps RealMatrix corr = MatrixUtils.createRealMatrix(geneSnps1.size(), geneSnps2.size()); // For each snp pair for (int i = 0; i < geneSnps1.size(); i++) { for (int j = 0; j < geneSnps2.size(); j++) { Snp snp_i = geneSnps1.get(i); Snp snp_j = geneSnps2.get(j); double r = computeCorrelation(snp_i, snp_j); corr.setEntry(i, j, r); }/*from w w w. j a v a 2 s. c o m*/ } return corr; }