Example usage for org.apache.commons.math3.linear MatrixUtils createRealMatrix

List of usage examples for org.apache.commons.math3.linear MatrixUtils createRealMatrix

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear MatrixUtils createRealMatrix.

Prototype

public static RealMatrix createRealMatrix(final int rows, final int columns) 

Source Link

Document

Returns a RealMatrix with specified dimensions.

Usage

From source file:cooccurrence.emf.java

public static void main(String args[]) {
    String path = "";
    String writePath = "";
    BufferedReader br = null;//from w  ww  . j  a va2 s  . c  o  m
    ArrayList<String> files = new ArrayList<>();
    listFilesForFolder(new File(path), files);
    for (String filePath : files) {
        System.out.println(filePath);
        String fileName = new File(filePath).getName();

        //data structure to store the PPMI matrix in the file
        HashMap<String, HashMap<String, Double>> cooccur = new HashMap<>();
        readFileContents(filePath, cooccur); //reading the file and storing the content in the hashmap
        //Because Matrices are identified by row and col id, the following 
        //lists maps id to corresponding string. Note that matrix is symmetric. 
        ArrayList<String> rowStrings = new ArrayList<>(cooccur.keySet());
        ArrayList<String> colStrings = new ArrayList<>(cooccur.keySet());

        //creating matrix with given dimensions and initializing it to 0
        RealMatrix matrixR = MatrixUtils.createRealMatrix(rowStrings.size(), colStrings.size());
        //populating the matrices based on the co-occur hashmap
        populateMatrixR(matrixR, cooccur, rowStrings, colStrings);

    }
}

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;// ww w. j av a  2s . 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:cooccurrence.Omer_Levy.java

public static void main(String args[]) {
    String path = "";
    String writePath = "";
    BufferedReader br = null;/*from ww  w.j a  v a  2  s .  c  o  m*/
    ArrayList<String> files = new ArrayList<>();
    //reading all the files in the directory
    //each file is PPMI matrix for an year
    listFilesForFolder(new File(path), files);
    for (String filePath : files) {
        System.out.println(filePath);
        String fileName = new File(filePath).getName();

        //data structure to store the PPMI matrix in the file
        HashMap<String, HashMap<String, Double>> cooccur = new HashMap<>();
        readFileContents(filePath, cooccur); //reading the file and storing the content in the hashmap
        //Because Matrices are identified by row and col id, the following 
        //lists maps id to corresponding string. Note that matrix is symmetric. 
        ArrayList<String> rowStrings = new ArrayList<>(cooccur.keySet());
        ArrayList<String> colStrings = new ArrayList<>(cooccur.keySet());

        //creating matrix with given dimensions and initializing it to 0
        RealMatrix matrixR = MatrixUtils.createRealMatrix(rowStrings.size(), colStrings.size());

        //creating the matrices for storing top rank-d matrices of SVD 
        RealMatrix matrixUd = MatrixUtils.createRealMatrix(D, D);
        RealMatrix matrixVd = MatrixUtils.createRealMatrix(D, D);
        RealMatrix coVarD = MatrixUtils.createRealMatrix(D, D);

        //populating the matrices based on the co-occur hashmap
        populateMatrixR(matrixR, cooccur, rowStrings, colStrings);

        //computing the svd
        SingularValueDecomposition svd = new SingularValueDecomposition(matrixR);

        //extracting the components of SVD factorization
        RealMatrix U = svd.getU();
        RealMatrix V = svd.getV();
        RealMatrix coVariance = svd.getCovariance(-1);

        //list to store indices of top-D singular values of coVar. 
        //Use this with rowsString (colStrings) to get the corresponding word and context
        ArrayList<Integer> indicesD = new ArrayList<>();
        //Extract topD singular value from covariance to store in coVarD and
        //extract corresponding columns from U and V to store in Ud and Vd
        getTopD(U, V, coVariance, matrixUd, matrixVd, coVarD, indicesD);
        //calulate the squareRoot of coVarD
        RealMatrix squareRootCoVarD = squareRoot(coVarD);
        RealMatrix W_svd = matrixUd.multiply(squareRootCoVarD);
        RealMatrix C_svd = matrixVd.multiply(squareRootCoVarD);
    }
}

From source file:edu.washington.gs.skyline.model.quantification.WeightedRegression.java

public static double[] weighted(double[][] x, double[] y, double[] weights, boolean intercept) {
    RealMatrix predictor;/*from  w  ww  . j  a  v a  2  s . co m*/
    if (intercept) {
        int nRows = x.length;
        int nCols = x[0].length + 1;
        predictor = MatrixUtils.createRealMatrix(nRows, nCols);
        for (int iRow = 0; iRow < nRows; iRow++) {
            predictor.setEntry(iRow, 0, 1.0);
            for (int iCol = 1; iCol < nCols; iCol++) {
                predictor.setEntry(iRow, iCol, x[iRow][iCol - 1]);
            }
        }
    } else {
        predictor = MatrixUtils.createRealMatrix(x);
    }
    RealVector responseVector = MatrixUtils.createRealVector(y);
    RealMatrix weightsMatrix = MatrixUtils.createRealDiagonalMatrix(weights);
    RealMatrix predictorTransposed = predictor.transpose();
    RealMatrix predictorTransposedTimesWeights = predictorTransposed
            .multiply(weightsMatrix.multiply(predictor));
    CholeskyDecomposition choleskyDecomposition = new CholeskyDecomposition(predictorTransposedTimesWeights);
    RealVector vectorToSolve = predictorTransposed.operate(weightsMatrix.operate(responseVector));
    RealVector result = choleskyDecomposition.getSolver().solve(vectorToSolve);
    return result.toArray();
}

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;/*  w ww  .  j  a  va  2 s .c  o m*/
        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:ch.unil.genescore.vegas.LinkageDisequilibrium.java

/** Compute correlation (LD) r values for the given snps (use snpCorrelation_ to avoid recomputing previous values) */
static public RealMatrix computeCorrelationMatrix(ArrayList<Snp> geneSnps) {

    // The correlation matrix for the given set of snps
    RealMatrix corr = MatrixUtils.createRealMatrix(geneSnps.size(), geneSnps.size());

    // For each snp pair
    for (int i = 0; i < geneSnps.size(); i++) {
        for (int j = i + 1; j < geneSnps.size(); j++) {
            Snp snp_i = geneSnps.get(i);
            Snp snp_j = geneSnps.get(j);

            double r = computeCorrelation(snp_i, snp_j);
            corr.setEntry(i, j, r);//from  ww w .  ja v a 2s . c  o  m
            corr.setEntry(j, i, r);
        }
    }

    // Set diagonal to 1
    for (int i = 0; i < geneSnps.size(); i++)
        corr.setEntry(i, i, 1);

    return corr;
}

From source file:io.warp10.script.functions.TOMAT.java

@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {

    Object o = stack.pop();/* w  w  w. java 2  s.co  m*/

    if (o instanceof RealVector) {

        RealMatrix matrix = MatrixUtils.createRealMatrix(((RealVector) o).getDimension(), 1);

        matrix.setColumnVector(0, (RealVector) o);

        stack.push(matrix);

        return stack;
    }

    if (!(o instanceof List)) {
        throw new WarpScriptException(getName() + " expects a 2D array onto the stack.");
    }

    int rows = ((List) o).size();
    int cols = -1;

    for (Object oo : (List) o) {
        if (!(oo instanceof List)) {
            throw new WarpScriptException(getName() + " expects a 2D array onto the stack.");
        }
        if (-1 == cols) {
            cols = ((List) oo).size();
        } else if (cols != ((List) oo).size()) {
            throw new WarpScriptException(
                    getName() + " expects a common number of columns throughout the 2D array.");
        }
    }

    double[][] doubles = new double[rows][cols];

    for (int i = 0; i < rows; i++) {
        List<Object> row = (List<Object>) ((List) o).get(i);
        for (int j = 0; j < cols; j++) {
            Object elt = row.get(j);
            if (!(elt instanceof Number)) {
                throw new WarpScriptException(getName() + " expects a numeric 2D array onto the stack.");
            }
            doubles[i][j] = ((Number) elt).doubleValue();
        }
    }

    RealMatrix mat = MatrixUtils.createRealMatrix(doubles);

    stack.push(mat);

    return stack;
}

From source file:com.caseystella.analytics.outlier.batch.rpca.RPCA.java

private void initMatrices() {
    this.L = MatrixUtils.createRealMatrix(this.X.getRowDimension(), this.X.getColumnDimension());
    this.S = MatrixUtils.createRealMatrix(this.X.getRowDimension(), this.X.getColumnDimension());
    this.E = MatrixUtils.createRealMatrix(this.X.getRowDimension(), this.X.getColumnDimension());
}

From source file:jmatbench.commonsmath.CommonsMathAlgorithmFactory.java

@Override
public BenchmarkMatrix create(int numRows, int numCols) {
    return wrap(MatrixUtils.createRealMatrix(numRows, numCols));
}

From source file:com.yahoo.egads.utilities.SpectralMethods.java

public static RealMatrix createHankelMatrix(RealMatrix data, int windowSize) {

    int n = data.getRowDimension();
    int m = data.getColumnDimension();
    int k = n - windowSize + 1;

    RealMatrix res = MatrixUtils.createRealMatrix(k, m * windowSize);
    double[] buffer = {};

    for (int i = 0; i < n; ++i) {
        double[] row = data.getRow(i);
        buffer = ArrayUtils.addAll(buffer, row);

        if (i >= windowSize - 1) {
            RealMatrix mat = MatrixUtils.createRowRealMatrix(buffer);
            res.setRowMatrix(i - windowSize + 1, mat);
            buffer = ArrayUtils.subarray(buffer, m, buffer.length);
        }//from   w w  w . j  ava  2  s.com
    }

    return res;
}