Example usage for org.apache.commons.math3.linear SparseRealMatrix getRowDimension

List of usage examples for org.apache.commons.math3.linear SparseRealMatrix getRowDimension

Introduction

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

Prototype

int getRowDimension();

Source Link

Document

Returns the number of rows in the matrix.

Usage

From source file:edu.byu.nlp.util.asserts.MoreAsserts.java

public static void assertSparseMatricesEqual(final SparseRealMatrix actual, final SparseRealMatrix expected) {
    Preconditions.checkNotNull(actual);//from   w  w w . j av a 2s. c  om
    Preconditions.checkNotNull(expected);
    Preconditions.checkArgument(actual.getRowDimension() == expected.getRowDimension());

    // check densely--this doesn't need to be fast
    for (int r = 0; r < actual.getRowDimension(); r++) {
        for (int c = 0; c < actual.getColumnDimension(); c++) {
            double act = actual.getEntry(r, c);
            double exp = expected.getEntry(r, c);
            if (act != exp) {
                System.err.println("actual[" + r + "][" + c + "]=" + act + " does not match expected[" + r
                        + "][" + c + "]=" + exp);
            }
            Assertions.assertThat(act).isEqualTo(exp);
        }
    }
}

From source file:simrankapp.util.ReaderWriterUtil.java

public static void writeSimRankInDB(SparseRealMatrix simRankMatrix) {
    Connection con = null;/*w  w w .  j a v a  2 s.com*/
    PreparedStatement stmt = null;
    try {
        Class.forName(TheCrawlersConstants.JDBC_DRIVER);
        System.out.println("Connecting to a selected database...");
        con = DriverManager.getConnection(TheCrawlersConstants.DB_URL, TheCrawlersConstants.DB_USERNAME,
                TheCrawlersConstants.DB_PASSWORD);
        System.out.println("Connected database successfully...");
        stmt = con.prepareStatement(TheCrawlersConstants.INSERT_SIMRANK_SQL);
        int numNodes = simRankMatrix.getRowDimension();
        for (int row = 0; row < numNodes; row++) {
            for (int col = 0; col < numNodes; col++) {
                double value = simRankMatrix.getEntry(row, col);
                if (value > 0) {
                    stmt.setInt(1, row);
                    stmt.setInt(2, col);
                    stmt.setDouble(3, value);
                    stmt.addBatch();
                }
            }
        }
        stmt.executeBatch();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            stmt.close();
            con.close();
        } catch (Exception e) {
        }
    }
}

From source file:simrankapp.util.ReaderWriterUtil.java

public static void writeSimRankToFile(SparseRealMatrix simRankMatrix, String simRankPath) {
    int numNodes = simRankMatrix.getRowDimension();
    try {//from   w  w  w.j av a 2s.  c o m
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File(simRankPath)));
        // row == col is always 1
        // row > col is symmetric and is equal to row < col
        for (int col = 0; col < numNodes; col++) {
            for (int row = 0; row < numNodes; row++) {
                double value = simRankMatrix.getEntry(row, col);
                bw.write(row + " " + col + " " + value);
                bw.newLine();
            }
        }
        bw.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:simrankapp.util.ReaderWriterUtil.java

public static void writeSparseMatrix(SparseRealMatrix matrix, String Path) {
    int numNodes = matrix.getRowDimension();
    try {/* w  w  w.ja  va  2  s. c o  m*/
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File(Path)));
        // row == col is always 1
        // row > col is symmetric and is equal to row < col
        for (int col = 0; col < numNodes; col++)
            for (int row = 0; row < numNodes; row++) {
                double value = matrix.getEntry(row, col);
                bw.write(row + " " + col + " " + value);
                bw.newLine();
            }
        bw.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}