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

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

Introduction

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

Prototype

double getEntry(int row, int column) throws OutOfRangeException;

Source Link

Document

Get the entry in the specified row and column.

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  ww  .  j  a  va 2 s .co m
    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.SimRankAlgorithm.java

/**
     * Compute the sum of all SimRank values of all incoming
     * neighbors of the given vertices//from w w  w.  j av a2s .  co  m
             
     */
private static <V> float computeSum(Graph<V, ?> g, V a, V b, SparseRealMatrix simRank) {
    Collection<V> ia = g.getPredecessors(a);
    Collection<V> ib = g.getPredecessors(b);
    float sum = 0;
    for (V iia : ia) {
        for (V ijb : ib) {
            int i = Integer.valueOf(String.valueOf(iia));
            int j = Integer.valueOf(String.valueOf(ijb));
            Float r = (float) simRank.getEntry(i, j);
            sum += r;
        }
    }
    return sum;
}

From source file:simrankapp.util.ReaderWriterUtil.java

public static void writeSimRankInDB(SparseRealMatrix simRankMatrix) {
    Connection con = null;//from  w w  w . j a v a 2 s.  c  o m
    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 {//  w ww  .  ja v  a2  s  . 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 {/*from  ww  w  .jav  a  2 s  .co 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();
    }
}