List of usage examples for org.apache.commons.math3.linear RealMatrix getRowDimension
int getRowDimension();
From source file:movierecommend.MovieRecommend.java
public static void main(String[] args) throws ClassNotFoundException, SQLException { String url = "jdbc:sqlserver://localhost;databaseName=MovieDB;integratedSecurity=true"; Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection conn = DriverManager.getConnection(url); Statement stm = conn.createStatement(); ResultSet rsRecnik = stm.executeQuery("SELECT Recnik FROM Recnik WHERE (ID_Zanra = 1)"); //citam recnik iz baze za odredjeni zanr String recnik[] = null;//from ww w. ja v a2 s. c o m while (rsRecnik.next()) { recnik = rsRecnik.getString("Recnik").split(","); //delim recnik na reci } ResultSet rsFilmovi = stm.executeQuery( "SELECT TOP (200) Naziv_Filma, LemmaPlots, " + "ID_Filma FROM Film WHERE (ID_Zanra = 1)"); List<Film> listaFilmova = new ArrayList<>(); Film f = null; int rb = 0; while (rsFilmovi.next()) { f = new Film(rb, Integer.parseInt(rsFilmovi.getString("ID_Filma")), rsFilmovi.getString("Naziv_Filma"), rsFilmovi.getString("LemmaPlots")); listaFilmova.add(f); rb++; } //kreiranje vektorskog modela M = MatrixUtils.createRealMatrix(recnik.length, listaFilmova.size()); System.out.println("Prva tezinska matrica"); for (int i = 0; i < recnik.length; i++) { String recBaza = recnik[i]; for (Film film : listaFilmova) { for (String lemmaRec : film.getPlotLema()) { if (recBaza.equals(lemmaRec)) { M.setEntry(i, film.getRb(), M.getEntry(i, film.getRb()) + 1); } } } } //racunanje tf-idf System.out.println("td-idf"); M = LSA.calculateTfIdf(M); System.out.println("SVD"); //SVD SingularValueDecomposition svd = new SingularValueDecomposition(M); RealMatrix V = svd.getV(); RealMatrix Vk = V.getSubMatrix(0, V.getRowDimension() - 1, 0, brojDimenzija - 1); //dimenzija je poslednji argument //kosinusna slicnost System.out.println("Cosin simmilarity"); CallableStatement stmTop = conn.prepareCall("{call Dodaj_TopList(?,?,?)}"); for (int j = 0; j < listaFilmova.size(); j++) { Film fl = listaFilmova.get(j); List<Film> lFilmova1 = new ArrayList<>(); lFilmova1.add(listaFilmova.get(j)); double sim = 0.0; for (int k = 0; k < listaFilmova.size(); k++) { // System.out.println(listaFilmova.size()); sim = LSA.cosinSim(j, k, Vk.transpose()); listaFilmova.get(k).setSimilarity(sim); lFilmova1.add(listaFilmova.get(k)); } Collections.sort(lFilmova1); for (int k = 2; k < 13; k++) { stmTop.setString(1, fl.getID() + ""); stmTop.setString(2, lFilmova1.get(k).getID() + ""); stmTop.setString(3, lFilmova1.get(k).getSimilarity() + ""); stmTop.execute(); } } stm.close(); rsRecnik.close(); rsFilmovi.close(); conn.close(); }
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"); }// w w w . ja va 2 s . c o m } } return m; }
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]; }// w ww . jav a 2 s . co m } return s; }
From source file:edu.cudenver.bios.matrix.MatrixUtilities.java
/** * Force a square RealMatrix to be symmetric. * * @param rm The RealMatrix.// w ww . ja v a2s. c o 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: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 w ww . ja v a 2s . co 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// w w w . ja v a 2s .c om * @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/* w w w . j a v a 2 s . c o m*/ * @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.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 ww w.j a v a 2 s.c o m*/ } 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.HilbertSpace.java
public static double inner_prod(final RealVector x, final RealMatrix M, final RealVector y) { // return x.dotProduct( M.operate( y ) ); double s = 0.0; for (int i = 0; i < M.getRowDimension(); ++i) { for (int j = 0; j < M.getColumnDimension(); ++j) { s += x.getEntry(i) * M.getEntry(i, j) * y.getEntry(j); }//from w ww .j a va 2s .com } return s; }
From source file:edu.oregonstate.eecs.mcplan.ml.MatrixAlgorithms.java
/** * Computes the inverse of a matrix using the singular value decomposition. * //w w w .jav a 2 s .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()); }