List of usage examples for org.apache.commons.math3.linear RealMatrix getColumnDimension
int getColumnDimension();
From source file:com.analog.lyric.math.LyricSingularValueDecomposition.java
private static RealMatrix checkMatrix(RealMatrix m) { for (int i = 0; i < m.getRowDimension(); i++) { for (int j = 0; j < m.getColumnDimension(); j++) { if (Double.isNaN(m.getEntry(i, j)) || Double.isInfinite(m.getEntry(i, j))) { throw new DimpleException("cannot do SVD on matrix that contains NaN or infinite"); }//from ww w . j a v a 2 s . c om } } return m; }
From source file:lsafunctions.LSA.java
private static int calcDf(int nRow, RealMatrix M) { int df = 0;//from www .j a v a 2 s .c om for (int j = 0; j < M.getColumnDimension(); j++) { if (M.getEntry(nRow, j) != 0) { df++; } } return df; }
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);/*from w ww. ja v a 2 s . c om*/ } else if (lambda < 0.0) { throw new NonPositiveDefiniteMatrixException(lambda, i, eps); } else { 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. * //from ww w . ja v 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:edu.cudenver.bios.matrix.MatrixUtilities.java
/** * Force a square RealMatrix to be symmetric. * * @param rm The RealMatrix.//from w ww . j av a 2s. co m * * @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.HilbertSpace.java
public static double inner_prod(final double[] x, final RealMatrix M, final double[] y) { double s = 0.0; for (int i = 0; i < M.getRowDimension(); ++i) { for (int j = 0; j < M.getColumnDimension(); ++j) { s += x[i] * M.getEntry(i, j) * y[j]; }/*from www .ja v a2 s . com*/ } return s; }
From source file:com.itemanalysis.psychometrics.factoranalysis.MatrixUtils.java
public static double sumMatrix(RealMatrix X) { double sum = 0.0; for (int i = 0; i < X.getRowDimension(); i++) { for (int j = 0; j < X.getColumnDimension(); j++) { sum += X.getEntry(i, j);/*from ww w .j a va2s . c o m*/ } } return sum; }
From source file:com.itemanalysis.psychometrics.factoranalysis.MatrixUtils.java
/** * Elementwise multiplication of two matrices. * * @param A a matrix that is multiplied by the elements of B * @param B another matrix//from w w w . j a v a2s . c o m * @throws DimensionMismatchException */ public static void multiplyElementsBy(RealMatrix A, RealMatrix B) throws DimensionMismatchException { int nrow = A.getRowDimension(); int ncol = A.getColumnDimension(); if (nrow != B.getRowDimension()) { throw new DimensionMismatchException(nrow, B.getRowDimension()); } if (ncol != B.getColumnDimension()) { throw new DimensionMismatchException(ncol, B.getColumnDimension()); } RealMatrix M = new Array2DRowRealMatrix(nrow, ncol); for (int i = 0; i < nrow; i++) { for (int j = 0; j < ncol; j++) { A.multiplyEntry(i, j, B.getEntry(i, j)); } } }
From source file:com.itemanalysis.psychometrics.factoranalysis.MatrixUtils.java
/** * Elementwise multiplication of elements in two arrays. This is equivalent to * using A*B in R when both A and B are matrices. * * @param A a matrix//from w ww . ja v a 2s . c om * @param B a matrix of the same dimension as A * @return a matrix with elements that are the produce of elements in A and B. * @throws org.apache.commons.math3.exception.DimensionMismatchException */ public static RealMatrix multiplyElements(RealMatrix A, RealMatrix B) throws DimensionMismatchException { int nrow = A.getRowDimension(); int ncol = A.getColumnDimension(); if (nrow != B.getRowDimension()) { throw new DimensionMismatchException(nrow, B.getRowDimension()); } if (ncol != B.getColumnDimension()) { throw new DimensionMismatchException(ncol, B.getColumnDimension()); } RealMatrix M = new Array2DRowRealMatrix(nrow, ncol); for (int i = 0; i < nrow; i++) { for (int j = 0; j < ncol; j++) { M.setEntry(i, j, A.getEntry(i, j) * B.getEntry(i, j)); } } return M; }
From source file:edu.byu.nlp.stats.GammaDistribution.java
/** * Samples a new Gamma distributed random variate for each parameter setting specified as elements * of the shapes matrix./*from ww w .j a va2 s. com*/ */ public static double[][] sample(RealMatrix shapes, RandomGenerator rnd) { double[][] gammas = new double[shapes.getRowDimension()][shapes.getColumnDimension()]; for (int i = 0; i < shapes.getRowDimension(); i++) { for (int j = 0; j < shapes.getColumnDimension(); j++) { gammas[i][j] = sample(shapes.getEntry(i, j), rnd); } } return gammas; }