sparse-matrix

Use of uninitialised value of size 8

允我心安 提交于 2019-12-11 11:46:07
问题 I'm having a quite strange problem with the fwd_iterator that I'm implementing: if I use the iterators in methods defined inside the class, they work, but if I create a method with global scope in which I use iterators, valgrind says that I'm attempting to access to uninitialised memory. It seems like iterators created outside the class cannot read a private attribute of the class (even with public methods created to do this). This is the global-scope method: template<typename T, class Pred>

extract sparse rows from sparse matrix in r

不羁岁月 提交于 2019-12-11 11:43:55
问题 I have a large sparse matrix to analyze in R. For instance: i <- c(1,3:8); j <- c(2,9,6:10); x <- 7 * (1:7) (A <- sparseMatrix(i, j, x = x)) [1,] . 7 . . . . . . . . [2,] . . . . . . . . . . [3,] . . . . . . . . 14 . [4,] . . . . . 21 . . . . [5,] . . . . . . 28 . . . [6,] . . . . . . . 35 . . [7,] . . . . . . . . 42 . [8,] . . . . . . . . . 49 I want to extract the i -th row from this matrix, as a sparse vector. If I write (x=A[1,]) I obtain [1] 0 7 0 0 0 0 0 0 0 0 but what I would like is

How to calculate the generalized inverse of a Sparse Matrix in scipy

我与影子孤独终老i 提交于 2019-12-11 11:37:55
问题 I have a sparse matrix W, when I use linalg.pinv(W) , it throws some errors: Traceback (most recent call last): File "/Users/ad9075/PycharmProjects/bednmf/test.py", line 14, in testNmfRun self.factor = factorization(self.V) File "/Users/ad9075/PycharmProjects/bednmf/nmf.py", line 18, in factorization W_trans = linalg.pinv(W) File "/Library/Python/2.7/site-packages/scipy/linalg/basic.py", line 540, in pinv b = np.identity(a.shape[0], dtype=a.dtype) IndexError: tuple index out of range` But

tile operation to create a csr_matrix from one row of another csr_matrix

狂风中的少年 提交于 2019-12-11 11:14:23
问题 I have a csr_matrix 'a' type of sparse matrix. I want to perform an operation to create a new csr_matrix 'b' where each row of 'b' is same ith row of 'a'. I think for normal numpy arrays it is possible using 'tile' operation. But I am not able to find the same for csr_matrix. Making first a numpy matrix and converting to csr_matrix is not an option as the size of matrix is 10000 x 10000. 回答1: I actually could get to answer which doesn't require creating full numpy matrix and is quite fast for

C++ armadillo sparse matrix type conversion

二次信任 提交于 2019-12-11 10:40:59
问题 I want to add two sparse armadillo matrices of arbitrary (different) type with operator+ , e.g. SpMat<double> M1(2,2); SpMat<cx_double> M2(2,2); // ..fill both matrices cout<<M1 + M2<<endl; Upon compiling, the compiler complains that operator+ is not defined for those types. When doing the same with DENSE matrices, armadillo automatically promotes the double matrix to a complex one, performs the addition and prints a complex result matrix. There is a corresponding template for this operator

NumPy: Importing a Sparse Matrix from R into Python

[亡魂溺海] 提交于 2019-12-11 10:17:29
问题 I have a matrix in R that is very large and sparse, created with the 'Matrix' package, and I want to handle in python + numpy. The R object is in the csc format, and if I export it using the function writeMM in the Matrix package, the output looks something like this: %%MatrixMarket matrix coordinate real general 4589 17366 160441 22 1 5.954510725783322 36 1 29.77255362891661 41 1 23.81804290313329 74 1 5.954510725783322 116 1 59.54510725783322 127 1 11.909021451566645 159 1 17

R Matrix. Set particular elements of sparse matrix to zero.

前提是你 提交于 2019-12-11 09:39:56
问题 I have reasonably large sparse matrix ( dgCMatrix or dgTMatrix , but this is not very important here). And I want to set some elements to zero. For example I have 3e4 * 3e4 matrix, which is upper triangular and it is quite dense: ~23% of elements are not zeros. (actually I have much bigger matrices ~ 1e5 * 1e5 , but they are much more sparser) So in triplet dgTMatrix form it takes about 3.1gb of RAM. Now I want to set to zero all elements which are less some threshold (say, 1 ). Very naive

spdiags() in MATLAB into Python

坚强是说给别人听的谎言 提交于 2019-12-11 08:33:19
问题 I am trying to translate a MATLAB implementation into a Python 3 implementation. I've found a function, spdiags(), that I do not understand, and am also not sure how to translate it into Python 3. The MATLAB documentation on the function is here: http://www.mathworks.com/help/matlab/ref/spdiags.html The Scipy documentation on an identically named function is here: http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.spdiags.html What does the MATLAB function do, and is there a

How to add a sparse row to a sparse matrix in Python?

▼魔方 西西 提交于 2019-12-11 07:36:51
问题 This task is pretty trivial in NumPy like so import numpy as np a= np.array([[1,2,3,0,9],[3,2,6,2,7],[0,0,0,8,0],[1,0,0,0,3]]) a + a[1] Output: array([[ 4, 4, 9, 2, 16], [ 6, 4, 12, 4, 14], [ 3, 2, 6, 10, 7], [ 4, 2, 6, 2, 10]]) See how the vector dimensions are automatically broadcasted to each row of the matrix. But when it comes to sparse matrices, there is a dimension mismatch error. from scipy.sparse import * a= csr_matrix([[1,2,3,0,9],[3,2,6,2,7],[0,0,0,8,0],[1,0,0,0,3]]) a + a[1]

creating sparse matrix of unknown size

為{幸葍}努か 提交于 2019-12-11 07:28:04
问题 I have a text file with each line indicating an edge on a graph, for example 2 5 1 indicates an edge of weight 1 between nodes 2 and 5. I want to create a sparse adjacency matrix using these tuples. Typically, I'd initialize a sparse matrix as G = scipy.sparse.lil_matrix((n,n)) where n is the number of nodes in the graph. But in this case, I do not know what 'n' is. Is there a more efficient way to create the matrix than looping over the lines of the file to find the max node index, creating