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

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

Introduction

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

Prototype

void setEntry(int row, int column, double value) throws OutOfRangeException;

Source Link

Document

Set the entry in the specified row and column.

Usage

From source file:simrankapp.SimRankAlgorithm.java

public static <V> SparseRealMatrix computeSimRank(Graph<V, ?> g, int maxIter) {
    System.out.println("Inside compute simrank");
    int numNodes = g.getVertexCount();
    SparseRealMatrix currentR = computeInitialSimRank(g);
    SparseRealMatrix nextR = new OpenMapRealMatrix(numNodes, numNodes);
    int[][] status = new int[numNodes][numNodes];
    nextR = currentR;/*from  w  ww.  ja v  a  2 s  .  co  m*/
    for (int k = 0; k < maxIter && maxIter > 0; k++) {
        for (int l = 0; l < numNodes; l++) {
            for (int m = 0; m < numNodes; m++) {
                if (l == m)
                    status[l][m] = 1;
                status[l][m] = 0; // Unprocessed
            }
        }
        for (V a : g.getVertices()) {
            for (V b : g.getVertices()) {
                int i = Integer.valueOf(String.valueOf(a));
                int j = Integer.valueOf(String.valueOf(b));
                if (status[i][j] == 0) {
                    if (i != j) {
                        float sum = computeSum(g, a, b, currentR);

                        int sia = g.inDegree(a);
                        int sib = g.inDegree(b);

                        //                    if(i==j)
                        //                        continue;
                        System.out.println("Inside compute simrank : computing for " + i + " and " + j);
                        if (sia == 0 || sib == 0) {
                            nextR.setEntry(i, j, 0.0f);
                            nextR.setEntry(j, i, 0.0f);
                        } else {
                            nextR.setEntry(i, j, (DEFAULT_C / (sia * sib)) * sum);
                            nextR.setEntry(j, i, (DEFAULT_C / (sia * sib)) * sum);
                        }
                    }
                }
                status[i][j] = 1;
                status[j][i] = 1;
            }
        }

        //System.out.println("After iteration "+k);
        //print(g, nextR);

        currentR = nextR;
    }
    return currentR;
}

From source file:simrankapp.SimRankAlgorithm.java

/**
 * Compute the initial SimRank for the vertices of the given graph.
 * This initial SimRank for two vertices (a,b) is 0.0f when
 * a != b, and 1.0f when a == b/*ww  w .  ja  v a 2s  .  com*/
 */
private static <V> SparseRealMatrix computeInitialSimRank(Graph<V, ?> g) {
    System.out.println("Inside compute initial simrank");
    int numNodes = g.getVertexCount();
    SparseRealMatrix initialSimRankMatrix = new OpenMapRealMatrix(numNodes, numNodes);
    for (V a : g.getVertices()) {
        for (V b : g.getVertices()) {
            int i = Integer.valueOf(String.valueOf(a));
            int j = Integer.valueOf(String.valueOf(b));

            if (a.equals(b)) {
                initialSimRankMatrix.setEntry(i, j, 1.0f);
            } else {
                initialSimRankMatrix.setEntry(i, j, 0.0f);
            }
        }
    }
    //  ReaderWriterUtil.writeSparseMatrix(initialSimRankMatrix, "InitialSimRank.txt");
    return initialSimRankMatrix;
}