Using the Eigen library in C++, given a sparse matrix `A`

, what is the most efficient way (row-wise operations? how to?) to compute a sparse matrix `B`

such that `B(i, j) = A(i, j) / A(i, i)`

? That is, divide each row `i`

by the corresponding diagonal element `A(i, i)`

. It would be helpful to know how to do it both in-place (replacing entries in `A`

) and out-of-place (creating a new sparse matrix `B`

).

My sparse matrix is defined as:

```
typedef double Real;
typedef Eigen::SparseMatrix<Real> SparseMatrixR;
```

Thank you,

m.

In other words you want to extract the diagonal of A, view it as a diagonal matrix, and apply its inverse to A:

```
A = A.diagonal().asDiagonal().inverse() * A;
```

This operation should be slightly more efficient if A is rowmajor.

Similar Questions

I'm trying to multiply each element in a vector by itself such that it produces a matrix that is symmetric about the diagonal. For example, given this vector:: x <- 1:3 I would like to create this

Define a predicate that determines whether a matrix (square) is symmetric about the main diagonal. The predicate would read about, where Z is a matrix (square) symmetric about the main diagonal symmet

I am trying to find the indices of nonzero entries by row in a sparse matrix: scipy.sparse.csc_matrix. So far, I am looping over each row in the matrix, and using numpy.nonzero() to each row to get t

I have this sparse matrix of size 20 millionx20million in matlab. I want to get around 40000 specific rows from this matrix. If I do new_data = data_original(index,:) where index consists of the rows

I have matrix, suppose A = [1 2 3 1 1 1 2 3] I want to find number of times the number appeared in the matrix. The output matrix for this i/p would be B = [1 1 1 2 3 4 2 2] i.e. 1 appeared 4 times

I process rather large matrices in Python/Scipy. I need to extract rows from large matrix (which is loaded to coo_matrix) and use them as diagonal elements. Currently I do that in the following fashio

How send Eigen matrix to GLSL? For example this: // Set up the model and projection matrix Eigen::Matrix<GLfloat,4,4> projection_matrix; projection_matrix = frustum(-1.0f, 1.0f, -aspect, aspec

I have a large sparse matrix, and I want to permute its rows or columns to turn the original matrix into a block diagonal matrix. Anyone knows which functions in R or MATLAB can do this? Thanks a lot.

I wish to speed up my machine learning algorithm (written in Python) using Numba (http://numba.pydata.org/). Note that this algorithm takes as its input data a sparse matrix. In my pure Python impleme

I am trying to write a function that takes fixed size matrix using template on the matrix size. I have read http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html but I am not able to make

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: >>

This Creating sparse matrix in MEX has a good example on mxCreateSparse. But this function return a double sparse matrix instead of single. If I want to return a single sparse matrix, what should I do

I have sparse square matrices of dimensions of the order of 10000 to 100000. I am using C++ programming language with armadillo and eigen as libraries for linear algebra. I know how to work with these

I have an array comprising n rows and 4 colums. Each of the four entries on the row is an integer, i.e., X = [ 111 112 432 2 6 9 115 111 112 432 111 2 ]; Each row represents the vertices of a tetrahe

Assume that mat below is of type Eigen::MatrixXd and already contains some data. In an attempt to avoid duplicating memory, I tried to instantiate a flann::Matrix<double> object from the pointer

I'm trying to create a type, using llvm c++ api, for a Eigen::Matrix <complex<double>,Dynamic, 1> parameter. Anyone knows how to do this? I wrote simple sample c++ code and exposed llvm IR

I am attempting to cluster a set of data points that are represented as a sparse scipy matrix, X. That is, >>> print type(X) <class 'scipy.sparse.csr.csr_matrix'> >>> print X.s

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

I'm using Eigen and I've got a matrix: MatrixXi x = MatrixXi::Random(5); I'd like to randomly permute the rows and columns using a randomly drawn permutation (just one permutation for both the rows a

I am persistently getting error messages whenever I try to use the selfadjointView property of any matrix or sparse matrix using the eigen library. Below is a simple code to check that. In my program

I'm working with a very large sparse matrix multiplication (matmul) problem. As an example let's say: A is a binary ( 75 x 200,000 ) matrix. It's sparse, so I'm using csc for storage. I need to do th

I've got a strange bug that I'm hoping a more experience programmer might have some insight into. I'm using the boost ublas sparse matrices, specifically mapped_matrix, and there is an intermittent bu

I need to create a diagonal matrix (8x8) and fill it with random numbers in [-30..45] range After I need to transform array into vector and sort it I tried different ways. At first I create matrix (8x

I'm looking for a matrix / linear algebra library in Java that provides a sparse matrix that can be written to concurrently from different threads. Most of the libraries I've come across either do not

If you have a sparse matrix X: >> print type(X) <class 'scipy.sparse.csr.csr_matrix'> ...How can you sum the squares of each element in each row, and save them into a list? For example: &

I would like to divide each row of a matrix by a fixed vector. For example mat<-matrix(1,ncol=2,nrow=2,TRUE) dev<-c(5,10) Giving mat/dev divides each column by dev. [,1] [,2] [1,] 0.2 0.2 [2

I'm trying to make a simple shift of Eigen's Matrix<int,200,200>, but I can't get Eigen::Translation to work. Since I'm rather new to C++, Eigen`s official documentation isn't of much use to me.

Suppose I have a matrix A = [1, 2, 3; 4, 5, 6; 7, 8, 9; 8, 7, 6]; I want the cross product of each row of A and another matrix B, where B = [1, 0.5, 0.5; 0.5, 1, 0.5; 0.5, 0.5, 1]; A for loop

I am building a sparse linear system with multiple (soft) constraints. I am converting some code which used to build the matrix with boost::ublas, to Eigen. The boost:ublas has a convenient way to cre

I was trying to iterate over the non zero elements of a row major sparse matrix, such as shown below: Eigen::SparseMatrix<double,Eigen::RowMajor> Test(2, 3); Test.insert(0, 1) = 34; Test.insert

I've been wondering about this question for quite a while but cannot find a reference: How does Matlab transpose a sparse matrix so fast, given that it is stored in CSC (compressed sparse column) form

I have an class ClusterNode which contains an Eigen::Matrix4d as a class variable. I also have a function numNodes() which tells me the total number of ClusterNodes, so that I can collect them in some

I am wondering if there is a built-in function in R which applies a function to each element of the matrix (of course, the function should be computed based on matrix indices). The equivalent would be

I want to calculate row/col sums for upper/lower triangle matrix (with diagonal). Example: m <- matrix(1:9, nrow=3) #result: upperRowSums(m) ## [1] 12 13 9 lowerRowSums(m) ## [1] 1 7 18 I know tha

I have a very large and very sparse matrix, composed of only 0s and 1s. I then basically handle (row-column) pairs. I have at most 10k pairs per row/column. My needs are the following: Parallel inser

Given an arbitrary numpy array (ndarray), is there a function or a short way to convert it to a scipy.sparse matrix? I'd like something that works like: A = numpy.array([0,1,0],[0,0,0],[1,0,0]) S = t

I have a large sparse matrix (lil) implemented in Python with scipy, which comprises of Users on one axis, and songs they played on the other. So each row is a linked list of the songs that user has p

I have a large sparse matrix, implemented as a lil sparse matrix from sci-py. I just want a statistic for how sparse the matrix is once populated. Is there a method to find out this?

This Sparse Matrix and its 3-Tuple representation is not getting into my head... Either its bit tricky or my resources from where I am studying are really not that good... here is the URI Sparse Matri

I have a csv file with headers like: Given this test.csv file contains sparse matrix: A,B,C,D,E,F,timestamp 611.88243,0,0,0,0,0,0 0,9089.5601,0,864.07514,0,0,0 0,0,5133.0,0,0,0,0 I simp

I have two sparse matrices, m1 and m2: > m1 <- Matrix(data=0,nrow=2, ncol=1, sparse=TRUE, dimnames=list(c(b,d),NULL)) > m2 <- Matrix(data=0,nrow=2, ncol=1, sparse=TRUE, dimnames=list(c

If I set the value of a SparseMatrix entry in Eigen as follows: sparse_matrix->coeffref(10, 10) = 0; Would this actually shrink the storage required by the matrix or would it try and store a 0 and

If you have a sparse matrix X: >> X = csr_matrix([[0,2,0,2],[0,2,0,1]]) >> print type(X) >> print X.todense() <class 'scipy.sparse.csr.csr_matrix'> [[0 2 0 2] [0 2 0 1]] And a

I have a following code , I want to display all the attributes of first UserData element of each Occurence corresponding to occurenceRefs ? Actually i want to display this in the HTML format and i am

I am working in R language. I want to impose condition that if any of the upper diagonal element of a matrix is greater than 0.5 then it prints it in output. I am using the following code but it print

I'm trying to utilize the ODE integration capabilities of Boost using the Matrix class from Eigen 3 as my state vector, but I'm running into problems deep into Boost that I don't understand how to add

I need a command to check for zero sparse matrix, isempty(..) does not work. Is there some sparse version of isempty(..)? >> mlf2=sparse([],[],[],2^31+1,1) mlf2 = All zero sparse: 2147483649-by-

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)))

I need an algorithm in Matlab which counts how many adjacent and non-overlapping (1,1) I have in each row of a matrix A mx(n*2) without using loops. E.g. A=[1 1 1 0 1 1 0 0 0 1; 1 0 1 1 1 1 0 0 1 1] %

So I have created a list which holds doubles, is it possible to divide every element in this list by an integer variable? List<Double> amount = new List<Double>();