use of cern.colt.matrix.tdouble.DoubleMatrix2D in project clusterMaker2 by RBVI.
the class ColtOps method subtractElement.
/**
* subtract a matrix from this matrix
*
* @param subtrahend the matrix to subtract from our matrix
*/
public void subtractElement(Matrix subtrahend) {
DoubleMatrix2D data = getData();
data.forEachNonZero(new IntIntDoubleFunction() {
public double apply(int row, int column, double v) {
double subValue = subtrahend.doubleValue(row, column);
if (!Double.isNaN(subValue))
return v - subValue;
else
return v;
}
});
}
use of cern.colt.matrix.tdouble.DoubleMatrix2D in project clusterMaker2 by RBVI.
the class ColtOps method eigenVectors.
public double[][] eigenVectors() {
if (decomp == null)
decomp = new DenseDoubleEigenvalueDecomposition(getData());
DoubleMatrix2D eigv = decomp.getV();
System.out.println("Found " + eigv.columns() + " eigenvectors");
return eigv.toArray();
}
use of cern.colt.matrix.tdouble.DoubleMatrix2D in project clusterMaker2 by RBVI.
the class ColtOps method multiplyScalar.
/**
* multiple all cells in the matrix by a value
*
* @param matrix our matrix
* @param value to multiply each cell by
*/
public void multiplyScalar(double value) {
DoubleMatrix2D data = getData();
data.forEachNonZero(new IntIntDoubleFunction() {
public double apply(int row, int column, double v) {
return v * value;
}
});
}
use of cern.colt.matrix.tdouble.DoubleMatrix2D in project clusterMaker2 by RBVI.
the class CyColtMatrix method sortByRowLabels.
public void sortByRowLabels(boolean isNumeric) {
Integer[] index;
if (isNumeric) {
double[] labels = new double[rowLabels.length];
for (int i = 0; i < labels.length; i++) {
if (rowLabels[i] != null)
labels[i] = Double.parseDouble(rowLabels[i]);
}
index = MatrixUtils.indexSort(labels, labels.length);
} else {
index = MatrixUtils.indexSort(rowLabels, rowLabels.length);
}
String[] newRowLabels = new String[nRows];
CyNode[] newRowNodes = new CyNode[nRows];
DoubleMatrix2D newData = DoubleFactory2D.sparse.make(nRows, nColumns);
for (int row = 0; row < nRows; row++) {
newRowLabels[index[row]] = rowLabels[row];
newRowNodes[index[row]] = rowNodes[row];
for (int col = 0; col < nColumns; col++) {
double val = doubleValue(row, col);
newData.setQuick((int) index[row], col, val);
}
}
rowLabels = newRowLabels;
rowNodes = newRowNodes;
data = newData;
}
use of cern.colt.matrix.tdouble.DoubleMatrix2D in project clusterMaker2 by RBVI.
the class FastGreedyAlgorithm method execute.
/*Still use the sparse matrix implementation, but with improved structure*/
public void execute(GSimpleGraphData g, TaskMonitor monitor) {
/*Initialize*/
double[] ai = new double[g.nodeCount];
double qInitial = 0;
membership = new int[g.nodeCount];
// int counter = 0;
for (int i = 0; i < g.nodeCount; i++) {
// graphIndex[counter] = index;
membership[i] = i;
ai[i] = g.degree[i] / (2.0 * g.edgeCount);
qInitial -= ai[i] * ai[i];
// nodeDegree[i] = network.getDegree(index);
// counter++;
}
// for(int i=0; i<nodeIndexes.length-1; i++){
// for(int j=i+1; j<nodeIndexes.length; j++){
// //If there are more than one edge connecting two nodes, reduce the degree
// if(network.getEdgeCount(network.getNode(graphIndex[i]), network.getNode(graphIndex[j]), false)>1){
//
// }
// }
// }
DoubleMatrix2D deltaQMx = DoubleFactory2D.sparse.make(g.nodeCount, g.nodeCount);
// if(this.mode == 0){
MaxFun func = new MaxFun();
// }
double maxDeltaQ = 0;
double q = 0;
int maxI = 0;
int maxJ = 0;
double deltaQ = 0;
int itojDirectedEdgecount = 0;
int jtoiDirectedEdgecount = 0;
int ijUndirectedEdgeCount = 0;
int ijTotalEdgeCount = 0;
for (int i = 0; i < g.nodeCount; i++) {
for (int j = 0; j < g.nodeCount; j++) {
if (i == j) {
continue;
}
if (g.hasEdge(i, j) && i < j) {
// System.out.println("Looking at "+i+","+j+": degree[i]="+g.degree[i]+", g.degree[j]="+g.degree[j]);
deltaQ = (1.0d / (2 * g.edgeCount) - (g.degree[i] * g.degree[j]) / (4.0 * Math.pow(g.edgeCount, 2.0))) * 2;
deltaQMx.setQuick(i, j, deltaQ);
if (maxDeltaQ <= deltaQ) {
maxDeltaQ = deltaQ;
maxI = i;
maxJ = j;
}
} else if (g.hasEdge(i, j) && i > j) {
deltaQMx.setQuick(i, j, deltaQMx.getQuick(j, i));
}
}
}
// make sure i,j are ordered
if (maxI > maxJ) {
// swap
int temp = maxI;
maxI = maxJ;
maxJ = temp;
}
q = qInitial;
int counter = 0;
this.progress = (double) counter / (double) g.nodeCount;
if (monitor != null)
monitor.setProgress(this.progress);
if (halt)
return;
// System.out.println("maxDeltaQ:" + maxDeltaQ);
while (maxDeltaQ > 0) {
counter++;
// this.progress = (int)(100.0*counter/nodeCount);
q += maxDeltaQ;
for (int k = 0; k < g.nodeCount; k++) {
if (halt)
return;
if (k == maxJ || k == maxI) {
continue;
} else {
if (deltaQMx.getQuick(maxI, k) != 0 && deltaQMx.getQuick(maxJ, k) != 0) {
deltaQMx.setQuick(maxJ, k, deltaQMx.getQuick(maxJ, k) + deltaQMx.getQuick(maxI, k));
} else if (deltaQMx.getQuick(maxI, k) == 0 && deltaQMx.getQuick(maxJ, k) != 0) {
deltaQMx.setQuick(maxJ, k, deltaQMx.getQuick(maxJ, k) - 2 * ai[maxI] * ai[k]);
} else if (deltaQMx.getQuick(maxJ, k) == 0 && deltaQMx.getQuick(maxI, k) != 0) {
deltaQMx.setQuick(maxJ, k, deltaQMx.getQuick(maxI, k) - 2 * ai[maxJ] * ai[k]);
} else {
// both are zero
// nothing is done
// it seems that nothing is wrong.
// but the result is not quite correct
// both are zero, no need to update
// System.out.println("This is wrong!");
// System.out.println("Do nothing.");
}
}
}
// end update jth row
int membershipI = membership[maxI];
int membershipJ = membership[maxJ];
for (int k = 0; k < g.nodeCount; k++) {
deltaQMx.setQuick(k, maxJ, deltaQMx.getQuick(maxJ, k));
deltaQMx.setQuick(maxI, k, 0.0);
deltaQMx.setQuick(k, maxI, 0.0);
if (membership[k] == membershipI) {
membership[k] = membershipJ;
}
}
ai[maxJ] = ai[maxI] + ai[maxJ];
ai[maxI] = 0;
deltaQMx.trimToSize();
maxDeltaQ = 0;
maxI = 0;
maxJ = 0;
func.reset();
deltaQMx.forEachNonZero(func);
maxDeltaQ = func.max;
maxI = func.row;
maxJ = func.column;
if (maxI > maxJ) {
// swap
int temp = maxI;
maxI = maxJ;
maxJ = temp;
}
// System.out.println("maxDeltaQ" + maxDeltaQ);
}
// end of while loop
// these all can be done in linear time
HashMap<Integer, Integer> membershipMapping = new HashMap<Integer, Integer>();
int index = 0;
for (int i = 0; i < membership.length; i++) {
if (membershipMapping.containsKey(new Integer(membership[i]))) {
} else {
membershipMapping.put(new Integer(membership[i]), new Integer(index));
index++;
}
}
// System.out.println("NumOfClusters:" + membershipMapping.keySet().size());
this.clusterNumber = membershipMapping.keySet().size();
for (int i = 0; i < membership.length; i++) {
membership[i] = membershipMapping.get(new Integer(membership[i])).intValue();
}
// assignMembership(network, graphIndex, membership, membershipMapping);
// System.out.println(q);
this.modularity = q;
this.progress = 100.0;
if (monitor != null)
monitor.setProgress(this.progress);
// This notifies that the current thread is over
// this.progress = 100;
// System.out.println("The result modularity is:" + q);
// for(int i=0; i<membership.length; i++){
// System.out.println(membership[i]);
// }
}
Aggregations