I have the following code in Python using Numpy:

```
p = np.diag(1.0 / np.array(x))
```

How can I transform it to get the sparse matrix `p2`

with the same values as `p`

without creating `p`

first?

Use `scipy.sparse.spdiags`

(which does a lot, and so may be confusing, at first), `scipy.sparse.dia_matrix`

and/or `scipy.sparse.lil_diags`

. (depending on the format you want the sparse matrix in...)

E.g. using `spdiags`

:

```
import numpy as np
import scipy as sp
import scipy.sparse
x = np.arange(10)
# "0" here indicates the main diagonal...
# "y" will be a dia_matrix type of sparse array, by default
y = sp.sparse.spdiags(x, 0, x.size, x.size)
```

Using the scipy.sparse module,

```
p = sparse.dia_matrix(1.0 / np.array(x), shape=(len(x), len(x)));
```

Similar Questions

when I print the values of the second row in a sparse matrix, I noticed that the first index is 0 instead 1. See my example below. Why is that? >>> from scipy.sparse import * >>> a=l

Does anyone know how to construct following matrix in Java? I can see a transpose pattern but I might be on the wrong track with that. This is what I've got so far... don't laugh :-) import java.io.*

I'm currently working with sparse matrices, and I have to compare the computation time of sparse matrix-matrix multiplication with full matrix-matrix multiplication. The issue is that sparse matrix co

I want to pass a sparse matrix to a shared library from MATLAB, do some operation there (written in C), and then return it. I can pass a dense matrix and use, pretty easy. But, I have no idea how to p

I have googled around but havnt found an answer that suits me for OpenGL. I want to construct a sparse matrix with a single diagonal and around 9 off-diagonals. These diagonals arent necessarily next

We have an application that stores a sparse matrix. This matrix has entries that mostly exist around the main diagonal of the matrix. I was wondering if there were any efficient algorithms (or existin

I currently want to multiply a large sparse matrix(~1M x 200k) with its transpose. The values of the resulting matrix would be in float. I tried loading the matrix in scipy's sparse matrix and by mul

I have a matrix K of dimensions n x n. I want to create a new block diagonal matrix M of dimensions N x N, such that it contains d blocks of matrix K as its diagonal. I would have directly used M = bl

To my understanding, numpy.sparse.csr_sparse.dot(other) does multiply other to my sparse matrix from the right: A = numpy.sparse.csr_sparse(something) B = numpy.matrix(something) C = A.dot(B) # C = A*

I have an RDD as such: byUserHour: org.apache.spark.rdd.RDD[(String, String, Int)] I would like to create a sparse matrix of the data for calculations like median, mean, etc. The RDD contains the row_

Suppose I have this kind of matrix: 1 2 3 3 2 3 4 5 6 7 8 9 3 2 3 How do I add a diagonal of ones into this? Is there an easy way? To show what I mean: 1 2 3 3 1 3 4 5 1 1 8 9 3 1 3

Given a large sparse matrix (say 10k+ by 1M+) I need to find a subset, not necessarily continuous, of the rows and columns that form a dense matrix (all non-zero elements). I want this sub matrix to b

I have a large scipy sparse matrix, which is taking up >90% of my total system memory. I would like to save it to disk, as it takes hours to build the matrix... I tried cPickle, but that leads to a

Hello I know there are a lot of questions on sparse matrix multiplication, but many of the answers say to just use libraries. I want to do it without using library functions. So far I've done the easy

I am trying to compute nearest neighbour clustering on a Scipy sparse matrix returned from scikit-learn's DictVectorizer. However, when I try to compute the distance matrix with scikit-learn I get an

I see with new Eigen 3.2, you can get row, column or even block from a sparse matrix, is there a way to set any of these to 0? Eigen::SparseMatrix<float, Eigen::RowMajor> A( 5, 5 ); A.block(1, 1

I need to overload [] operator in class Sparse Matrix. This operator must work like in 2D table access. For example tab[1][1], return reference. The problem is I have a vector of elements(struct). tem

I have to invert a large sparse matrix. I cannot escape from the matrix inversion, the only shortcut would be to just get an idea of the main diagonal elements, and ignore the off-diagonal elements (I

Bash can create a sparse array in one line names=([0]=Bob [1]=Peter [20]=$USER [21]=Big Bad John) § Creating Arrays Can JavaScript create a sparse array like this?

Im trying to put -5 in the diagonal of my matrix. I am using if statements. But my if statment is wrong. Can anyone give me some help? X= rbind(c(1.2,3.4,10.3),c(2,8,78),c(45,-36,8)) array(X,dim=c(3,3

I have never use R ,but now I need import a sparse matrix to do association rule in R My import data is a sparse matrix like this: i j x 1 2 3 1 2 3 5 1 3 3 1

I have a list of sparse vectors (in R). I need to convert this list to a sparse matrix. Doing it via a for-loop takes a long time. sm<-spMatrix(length(tc2),n.col) for(i in 1:length(tc2)){ sm[i,]<

(There are some questions about time-efficient sparse arrays but I am looking for memory efficiency.) I need the equivalent of a List<T> or Map<Integer,T> which Can grow on demand just by

I'm trying to cluster some data with python and scipy but the following code does not work for reason I do not understand: from scipy.sparse import * matrix = dok_matrix((en,en), int) for pub in pubs:

I am trying to create a large sparse matrix, 10^5 by 10^5 in R, but am running into memory issues. > Matrix(nrow=1e5,ncol=1e5,sparse=TRUE) Error in Matrix(nrow = 1e+05, ncol = 1e+05, sparse = TRUE)

I find many similar questions but no answer. For simple array there is multiprocessing.Array. For sparse matrix or any other arbitrary object I find manager.namespace. So I tried the code below: from

I have this huge sparse matrix A of size 2 million by 10 thousand. I want to index particular 1000 rows (index) from this matrix. If I do B = A(index,:); it takes some time. Is there a better quick

Sorry for rookie question. I'm learning to work with scipy.sparse and I'm out of ideas why this code does not work. The dimensions are correct but the subtraction can not be computed: c=(count_mat[i])

In scipy, we can construct a sparse matrix using scipy.sparse.lil_matrix() etc. But the matrix is in 2d. I am wondering if there is an existing data structure for sparse 3d matrix / array (tensor) in

Say I would like to remove the diagonal from a scipy.sparse.csr_matrix. Is there an efficient way of doing so? I saw that in the sparsetools module there are C functions to return the diagonal. Based

I have a CSV file containing an n * n distance matrix, and here's a part of view, I want to use python to edit this CSV, making elements in the diagonal of the matrix replaced by 1, how should I do？

What is the readable and efficient way to compute the dot product between two columns or rows of a sparse matrix using scipy? Let's say that we want to take the dot product of two vectors x and y, two

There are at least two sparse matrix packages for R. I'm looking into these because I'm working with datasets that are too big and sparse to fit in memory with a dense representation. I want basic lin

How can you add a 1-column matrix to a sparse matrix, or add a sparse matix to a column matrix (either way)? It shouldn't replace the data, just join it into one data type. The sparse matrix: >>

Given a square matrix M, how can you find the sum of the elements on the diagonal? There must be an easier method than this: sum(sum(diag(diag(M), 0)))

void add(sparseMatrix<T> &b, sparseMatrix<T> &c); // c is output sparseMatrix<T> operator+(sparseMatrix<T> &b); I'm creating a sparse matrix which is made up of an

I have a sparse matrix that is not symmetric I.E. the sparsity is somewhat random, and I can't count on all the values being a set distance away from the diagonal. However, it is still sparse, and I w

I have a large sparse matrix A, and I would like to create a sparse matrix of the 3X3 block diagonals of A. How would I do this? keep in mind that A is very large and sparse, so any methods that use i

I am trying to find an efficient way that lets me increase the top k values of a sparse matrix by some constant value. I am currently using the following code, which is quite slow for very large matri

I want to make a sparse matrix in python. I have the index and value of non-zero elements as a dictionary i.e.: {((1,3),0.0001),(10,4),0.0212)...} which means that value of element (1,3) is 0.0001, (

I am converting from a scipy sparse matrix to a dense matrix and adding that to an ndarray using a += operator and I am getting a broadcast error. The ndarray has a shape (M,) while the dense matrix h

I know my question is not really a programming question but it came out of programming need. Does anyone happen to know the convolution matrix for diagonal motion blur. 3x3, 4x4 or 5x5 are all good. T

I want to make a diagonal matrix with the C++ linear algebra library eigen, but I do not know the size of the matrix during compile time. Hence, DiagonalMatrix<Scalar, SizeAtCompileTime> diag1(s

I'm migrating from Matlab to C + GSL and I would like to know what's the most efficient way to calculate the matrix B for which: B[i][j] = exp(A[i][j]) where i in [0, Ny] and j in [0, Nx]. Notice tha

This question already has an answer here: MATLAB: Create a block diagonal matrix with same repeating block 2 answers Matlab syntax question : Given some n x d matrix A(say [1 2 3; 4 5 6]), and

When computing the inverse for some square matrix A in MATLAB, using Ai = inv(A) % should be the same as: Ai = A^-1 MATLAB usually notifies me that this is not the most efficient way of inverting. So

Given a Matrix 3x3 How can I traverse through the longest diagonal only? Take this example: 1 2 3 4 5 6 7 8 9 I want to traverse through 1,5,9

I am looking for the best package for sparse matrix multiplication on single core solution. I am not looking for CUDA, MPI or OpenMP solutions. My preference for languages in decreasing order : Matlab

In R I would like to set to zero all entries of matrix a few entries above (and below) matrix diagonal. Example below for N x N matrix with N = 5 and we delete k=3 lines of entries parallel to matrix

Edit: The huge difference in performance is due to a bug in the test, when set up properly Eigen is 2 to 3 times faster. I noticed that sparse matrix multiplication using C++ Eigen library is much slo