本文整理汇总了Java中cern.colt.matrix.impl.SparseDoubleMatrix2D类的典型用法代码示例。如果您正苦于以下问题:Java SparseDoubleMatrix2D类的具体用法?Java SparseDoubleMatrix2D怎么用?Java SparseDoubleMatrix2D使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SparseDoubleMatrix2D类属于cern.colt.matrix.impl包,在下文中一共展示了SparseDoubleMatrix2D类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: init
import cern.colt.matrix.impl.SparseDoubleMatrix2D; //导入依赖的package包/类
public void init()
{
System.out.println("load matrix");
load_metapath(metaPaths.get(0));
if (doc_num == 0) {
System.exit(0);
}
doc_path = new SparseDoubleMatrix2D(doc_num, metaPaths.size());
for (int i = 0; i < metaPaths.size(); i++)
{
if (i % 20 == 0)
System.err.println("has loaded " + i + " matrix");
double[] hasInstance = load_metapath(metaPaths.get(i));
for (int j = 0; j < doc_num; j ++)
if (hasInstance[j] > 0.01)
doc_path.setQuick(j, i, hasInstance[j]);
}
}
示例2: graphToSparseMatrix
import cern.colt.matrix.impl.SparseDoubleMatrix2D; //导入依赖的package包/类
/**
* Returns a SparseDoubleMatrix2D whose entries represent the edge weights for the
* edges in <code>g</code>, as specified by <code>nev</code>.
*
* <p>The <code>(i,j)</code> entry of the matrix returned will be equal to the sum
* of the weights of the edges connecting the vertex with index <code>i</code> to
* <code>j</code>.
*
* <p>If <code>nev</code> is <code>null</code>, then a constant edge weight of 1 is used.
*
* @param g
* @param nev
*/
public static <V,E> SparseDoubleMatrix2D graphToSparseMatrix(Graph<V,E> g, Map<E,Number> nev)
{
if (nev == null)
nev = new ConstantMap<E,Number>(1);
int numVertices = g.getVertexCount();
SparseDoubleMatrix2D matrix = new SparseDoubleMatrix2D(numVertices,
numVertices);
BidiMap<V,Integer> indexer = Indexer.<V>create(g.getVertices());
int i=0;
for(V v : g.getVertices())
{
for (E e : g.getOutEdges(v))
{
V w = g.getOpposite(v,e);
int j = indexer.get(w);
matrix.set(i, j, matrix.getQuick(i,j) + nev.get(e).doubleValue());
}
i++;
}
return matrix;
}
示例3: computeVoltagePotentialMatrix
import cern.colt.matrix.impl.SparseDoubleMatrix2D; //导入依赖的package包/类
/**
* The idea here is based on the metaphor of an electric circuit. We assume
* that an undirected graph represents the structure of an electrical
* circuit where each edge has unit resistance. One unit of current is
* injected into any arbitrary vertex s and one unit of current is extracted
* from any arbitrary vertex t. The voltage at some vertex i for source
* vertex s and target vertex t can then be measured according to the
* equation: V_i^(s,t) = T_is - T-it where T is the voltage potential matrix
* returned by this method. *
*
* @param graph
* an undirected graph representing an electrical circuit
* @return the voltage potential matrix
* @see "P. Doyle and J. Snell, 'Random walks and electric networks,', 1989"
* @see "M. Newman, 'A measure of betweenness centrality based on random walks', pp. 5-7, 2003"
*/
public static <V,E> DoubleMatrix2D computeVoltagePotentialMatrix(
UndirectedGraph<V,E> graph)
{
int numVertices = graph.getVertexCount();
//create adjacency matrix from graph
DoubleMatrix2D A = GraphMatrixOperations.graphToSparseMatrix(graph,
null);
//create diagonal matrix of vertex degrees
DoubleMatrix2D D = GraphMatrixOperations
.createVertexDegreeDiagonalMatrix(graph);
DoubleMatrix2D temp = new SparseDoubleMatrix2D(numVertices - 1,
numVertices - 1);
//compute D - A except for last row and column
for (int i = 0; i < numVertices - 1; i++)
{
for (int j = 0; j < numVertices - 1; j++)
{
temp.set(i, j, D.get(i, j) - A.get(i, j));
}
}
Algebra algebra = new Algebra();
DoubleMatrix2D tempInverse = algebra.inverse(temp);
DoubleMatrix2D T = new SparseDoubleMatrix2D(numVertices, numVertices);
//compute "voltage" matrix
for (int i = 0; i < numVertices - 1; i++)
{
for (int j = 0; j < numVertices - 1; j++)
{
T.set(i, j, tempInverse.get(i, j));
}
}
return T;
}
示例4: testUpdateScoreInMatrix_forFirstTime
import cern.colt.matrix.impl.SparseDoubleMatrix2D; //导入依赖的package包/类
@Test
public void testUpdateScoreInMatrix_forFirstTime() throws Exception {
SparseDoubleMatrix2D matrix2D = new SparseDoubleMatrix2D(100, 100);
List<Integer> members = Arrays.asList(4, 8, 19, 5, 27);
int recordId = 19;
Block block = PowerMockito.mock(Block.class);
PowerMockito.when(block.getMembers()).thenReturn(members);
PowerMockito.when(block.getMemberScore(Mockito.eq(recordId))).thenReturn((float) 888);
PowerMockito.when(block.getMemberProbability(Mockito.eq(recordId))).thenReturn((float) 0.456);
Whitebox.invokeMethod(classUnderTest, "updateScoreInMatrix", matrix2D, recordId, block, CellType.PROBABILITY);
for (Integer member : members) {
if (recordId != member) {
int rawIndex = Whitebox.invokeMethod(classUnderTest, "getMatrixPosFromRecordID", recordId);
int colIndex = Whitebox.invokeMethod(classUnderTest, "getMatrixPosFromRecordID", member);
MatcherAssert.assertThat(matrix2D.getQuick(rawIndex, colIndex), closeTo(0.456, 0.0001));
}
}
}
示例5: testUpdateScoreInMatrix_cellsNotAffected
import cern.colt.matrix.impl.SparseDoubleMatrix2D; //导入依赖的package包/类
@Test
public void testUpdateScoreInMatrix_cellsNotAffected() throws Exception {
SparseDoubleMatrix2D matrix2D = new SparseDoubleMatrix2D(100, 100);
List<Integer> members = Arrays.asList(4, 8, 19, 5, 27);
int recordId = 19;
Block block = PowerMockito.mock(Block.class);
PowerMockito.when(block.getMembers()).thenReturn(members);
PowerMockito.when(block.getMemberScore(Mockito.eq(recordId))).thenReturn((float) 0.999);
PowerMockito.when(block.getMemberProbability(Mockito.eq(recordId))).thenReturn((float) 0.456);
Whitebox.invokeMethod(classUnderTest, "updateScoreInMatrix", matrix2D, recordId, block, CellType.PROBABILITY);
for (int someMember = 1; someMember <= 100; someMember++) {
if (!members.contains(someMember)) {
int rawIndex = Whitebox.invokeMethod(classUnderTest, "getMatrixPosFromRecordID", recordId);
int colIndex = Whitebox.invokeMethod(classUnderTest, "getMatrixPosFromRecordID", someMember);
MatcherAssert.assertThat(matrix2D.getQuick(rawIndex, colIndex), closeTo(0.0, 0.000001));
}
}
}
示例6: testUpdateScoreInMatrix_forSecondTime
import cern.colt.matrix.impl.SparseDoubleMatrix2D; //导入依赖的package包/类
@Test
public void testUpdateScoreInMatrix_forSecondTime() throws Exception {
SparseDoubleMatrix2D matrix2D = new SparseDoubleMatrix2D(100, 100);
int recordId = 19;
List<Integer> members = Arrays.asList(4, 8, 19, 5, 27);
Block block = PowerMockito.mock(Block.class);
PowerMockito.when(block.getMembers()).thenReturn(members);
PowerMockito.when(block.getMemberScore(Mockito.eq(recordId))).thenReturn((float) 0.92);
PowerMockito.when(block.getMemberProbability(Mockito.eq(recordId))).thenReturn((float) 0.32);
Whitebox.invokeMethod(classUnderTest, "updateScoreInMatrix", matrix2D, recordId, block, CellType.PROBABILITY);
members = Arrays.asList(19, 5, 27);
Whitebox.invokeMethod(classUnderTest, "updateScoreInMatrix", matrix2D, recordId, block, CellType.PROBABILITY);
int rawIndex = Whitebox.invokeMethod(classUnderTest, "getMatrixPosFromRecordID", recordId);
int colIndex = Whitebox.invokeMethod(classUnderTest, "getMatrixPosFromRecordID", 5);
MatcherAssert.assertThat(matrix2D.getQuick(rawIndex, colIndex), closeTo(0.64, 0.0001));
colIndex = Whitebox.invokeMethod(classUnderTest, "getMatrixPosFromRecordID", 27);
MatcherAssert.assertThat(matrix2D.getQuick(rawIndex, colIndex), closeTo(0.64, 0.0001));
}
示例7: graphToSparseMatrix
import cern.colt.matrix.impl.SparseDoubleMatrix2D; //导入依赖的package包/类
/**
* Returns a SparseDoubleMatrix2D whose entries represent the edge weights for the
* edges in <code>g</code>, as specified by <code>nev</code>.
*
* <p>The <code>(i,j)</code> entry of the matrix returned will be equal to the sum
* of the weights of the edges connecting the vertex with index <code>i</code> to
* <code>j</code>.
*
* <p>If <code>nev</code> is <code>null</code>, then a constant edge weight of 1 is used.
*
* @param g
* @param nev
*/
public static SparseDoubleMatrix2D graphToSparseMatrix(Graph g, NumberEdgeValue nev)
{
if (nev == null)
nev = new ConstantEdgeValue(1);
int numVertices = g.getVertices().size();
SparseDoubleMatrix2D matrix = new SparseDoubleMatrix2D(numVertices,
numVertices);
Indexer id = Indexer.getIndexer(g);
for (int i = 0; i < numVertices; i++)
{
Vertex v = (Vertex)id.getVertex(i);
for (Iterator o_iter = v.getOutEdges().iterator(); o_iter.hasNext(); )
{
Edge e = (Edge)o_iter.next();
Vertex w = e.getOpposite(v);
int j = id.getIndex(w);
matrix.set(i, j, matrix.getQuick(i,j) + nev.getNumber(e).doubleValue());
}
}
return matrix;
}
示例8: exportGraph
import cern.colt.matrix.impl.SparseDoubleMatrix2D; //导入依赖的package包/类
@Override
public void exportGraph() {
try {
initialiseOut();
Map<WeightedEdge, Number> map = new HashMap<WeightedEdge, Number>();
Graph<Agent, WeightedEdge> graph = tradeNetworkReport.getGraph();
for(WeightedEdge edge : graph.getEdges()) {
map.put(edge, edge.getValue());
}
SparseDoubleMatrix2D matrix = GraphMatrixOperations
.graphToSparseMatrix(this.tradeNetworkReport.getGraph(),
map);
for (int i = 0; i < matrix.rows(); i++) {
for (int j = 0; j < matrix.columns(); j++) {
out.newData(matrix.get(i, j));
}
out.endRecord();
}
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例9: laplacianScore
import cern.colt.matrix.impl.SparseDoubleMatrix2D; //导入依赖的package包/类
public int[] laplacianScore(int numK, String type, double[] weights) {
if (this.fMat == null) {
logger.warning("No feature loaded");
return null;
}
System.out.println("computeLaplacian");
computeLaplacian(numK, type);
DoubleMatrix2D diagSum = new SparseDoubleMatrix2D(fMat.rows(), fMat.rows());
for (int i = 0; i < fMat.rows(); ++i) {
diagSum.setQuick(i, i, sumRow[i]);
}
diagSum.trimToSize();
DenseDoubleMatrix1D onesVector = new DenseDoubleMatrix1D (fMat.rows());
onesVector.assign(1);
DenseDoubleMatrix1D zerosVector = new DenseDoubleMatrix1D (fMat.rows());
zerosVector.assign(0);
// f = f - \frac{f^T D 1}{1^T D 1} 1
System.out.println("compute weight");
// DoubleMatrix1D[] trans_vec = new DoubleMatrix1D[fMat.columns()];
// for (int i = 0; i < fMat.columns(); i++)
// {
// trans_vec[i] = fMat.viewColumn(i).copy();
// }
for (int i = 0; i < fMat.columns(); ++i) {
if (i % 100 == 0)
System.out.println("have handle " + i + " feature");
DoubleMatrix1D colVector = null;
// colVector = trans_vec[i];
colVector = fMat.viewColumn(i).copy();
DoubleMatrix1D tempVector = null;
// potential problem: diagSum.zMult(onesVector, tempVector) will lead tempVector as null!!
tempVector = diagSum.zMult(onesVector, tempVector);
double temp1 = colVector.zDotProduct(tempVector);
double temp2 = onesVector.zDotProduct(onesVector);
for (int j = 0; j < colVector.size(); ++j) {
double temp3 = colVector.getQuick(j);
double temp4 = temp3 - temp1/temp2;
colVector.setQuick(j, temp4);
}
Laplacian.zMult(colVector, tempVector);
temp1 = colVector.zDotProduct(tempVector);
diagSum.zMult(colVector, tempVector);
temp2 = colVector.zDotProduct(tempVector);
lScore.insert(i, temp1/(temp2+Double.MIN_VALUE));
weights[i] = temp1/(temp2+Double.MIN_VALUE);
}
int[] sortedIndices = MaxHeap.heapSort(lScore);
//reverse array
// sortedIndices = lScore.getIndices();
// double[] scores = lScore.getValues();
// for (double score: scores)
// System.out.print(score + " ");
// System.out.println();
return sortedIndices;
}
示例10: inverseSparse
import cern.colt.matrix.impl.SparseDoubleMatrix2D; //导入依赖的package包/类
static public List<DoubleMatrix1D> inverseSparse(List<DoubleMatrix1D> A) {
// TODO: optimize this code
List<DoubleMatrix1D> C = null;
DoubleMatrix2D A1 = new SparseDoubleMatrix2D(A.size(), A.get(0).size());
for (int i = 0; i < A.size(); ++i) {
DoubleMatrix1D vector = A.get(i);
for (int j = 0; j < vector.size(); ++j ) {
A1.set(i, j, vector.get(j));
}
}
double tol = 0.001;
for (int i = 0; i < A.size(); ++i) {
A1.set(i, i, A1.get(i, i) + tol);
}
Algebra algebra = new Algebra();
DoubleMatrix2D C1 = algebra.inverse(A1);
C = new ArrayList<DoubleMatrix1D>();
for (int i = 0; i < A.size(); ++i) {
C.add(new ColtSparseVector(A.get(0).size()));
}
for (int i = 0; i < C1.rows(); ++i) {
for (int j = 0; j < C1.columns(); ++j ) {
C.get(i).set(j, C1.get(i, j));
}
}
return C;
}
示例11: multiplyMatrix
import cern.colt.matrix.impl.SparseDoubleMatrix2D; //导入依赖的package包/类
static DoubleMatrix2D multiplyMatrix(DoubleMatrix2D A, DoubleMatrix2D B) {
DoubleMatrix2D C = new SparseDoubleMatrix2D(A.rows(), B.columns());
A.zMult(B, C);
return C;
}
示例12: load
import cern.colt.matrix.impl.SparseDoubleMatrix2D; //导入依赖的package包/类
private void load(File similarities, File preferences, int base) throws IOException {
// Load preferences
List<Double> prefs = loadPreferences(preferences);
this.n = prefs.size();
// Similarity matrix
s = new SparseDoubleMatrix2D(n, n);
int c = 0;
for (Double p: prefs) {
s.set(c, c, p);
c++;
}
// Load similarities
int simCount = 0;
String line;
BufferedReader reader = new BufferedReader(new FileReader(similarities));
while ((line = reader.readLine()) != null) {
List<String> tokens = getTokens(line, delimiters);
int i = Integer.parseInt(tokens.get(0)) - base;
int j = Integer.parseInt(tokens.get(1)) - base;
Double v = Double.parseDouble(tokens.get(2));
s.set(i, j, v);
simCount++;
}
reader.close();
double full = 100d * (simCount + n) / (n * n);
Logger.info("[Loader] Loaded %d similarities, matrix is %f%% populated", simCount, full);
}
示例13: getA
import cern.colt.matrix.impl.SparseDoubleMatrix2D; //导入依赖的package包/类
@Override
public DoubleMatrix2D getA() {
SparseDoubleMatrix2D a = new SparseDoubleMatrix2D(n, n);
for (Group group: groups) {
for (int i=0; i<group.size; i++) {
int len = group.lengths[i];
int k;
for (int j=0; j<len; j++) {
k = group.indices[i][j];
a.setQuick(i+group.startIndex, k, group.a[i][j]);
}
}
}
return a;
}
示例14: ColtSparseDoubleMatrix2D
import cern.colt.matrix.impl.SparseDoubleMatrix2D; //导入依赖的package包/类
public ColtSparseDoubleMatrix2D(Matrix source) {
super(source.getRowCount(), source.getColumnCount());
this.matrix = new SparseDoubleMatrix2D((int) source.getRowCount(), (int) source.getColumnCount());
for (long[] c : source.availableCoordinates()) {
setDouble(source.getAsDouble(c), c);
}
if (source.getMetaData() != null) {
setMetaData(source.getMetaData().clone());
}
}
示例15: plus
import cern.colt.matrix.impl.SparseDoubleMatrix2D; //导入依赖的package包/类
public Matrix plus(double value) {
Matrix result = new ColtSparseDoubleMatrix2D((SparseDoubleMatrix2D) matrix.copy().assign(Functions.plus(value)));
MapMatrix<String, Object> a = getMetaData();
if (a != null) {
result.setMetaData(a.clone());
}
return result;
}