Re: How to efficiently copy sparse matrix row to vector?
"oswin krause" <[email protected]>
| Newsgroups | gmane.comp.lib.boost.ublas |
|---|---|
| Message-ID | <[email protected]> |
Hi, there are several solutions, they all are more or less inefficient. The easiest and cleanest solution is noalias(v) = row(M,i); which was also the right solution at the stackoverflow page after fixing the obvious indexing error. Clearly, this is the intended way to do assigning - using the assignment operator (which is not the same as the equals operator which is ==). This should also be the fastest way, however, it is not the most efficient way to do this. The most efficient way is digging down into the matrix internals, and write a for-loop copying the internal matrix arrays directly into your vector. The other syntax you noticed is for doing initialisation. And i doubt that this is more efficient than the much easier assignment :) std::copy won't work as it won't copy the indices, only the values. On 24.08.2013 04:49, Reich, Darrell wrote: > What is the best way to copy a row from an N x N sparse matrix into a > vector of size N? I see a possible solution using std::copy or the > overloaded equals sign for the opposite question here: > > http://stackoverflow.com/questions/9420486/how-to-copy-a-boostnumericubl > asvector-to-a-matrix > > std::copy(v.begin(), v.end(), m.begin2()); > > ublas::matrix_row<ublas::matrix<double> > r( m, 0 ); > row(m, 0) = v; > > I understand from the documentation about the iterators used in the copy > command above. The overloaded '>' is a surprise. I'm resisting using for > loops. Please clarify the syntax of the most efficient way to do this > for the sparse matrix example code below. > > #include <boost/numeric/ublas/vector_sparse.hpp> > #include <boost/numeric/ublas/matrix_sparse.hpp> > > const int n = 1e6; > boost::numeric::ublas::compressed_matrix<float> M(n,n); // sparse > boost::numeric::ublas::compressed_vector<float> v(n); // sparse > M(0,0) = 1; > M(1,1) = 2; > // TODO: copy row of matrix to vector... > newM = M * v; > > Thank you for your help! So far boost has been a pleasure to use as I > brush up on my linear algebra and C++ syntax--Fortran and java have > turned my mind into mush. All of my frustrations so far have been > successfully blamed on Microsoft Visual C++ for Windows. Google has > successfully matched me to equally frustrated individuals, a few of > which actually posted the correct solution so I've got my sample code to > compile! Could they hide the include path better so it takes a few more > mouse clicks? > > I'm warming up to the idea of the header library idea more and more very > cleaver. > > _______________________________________________ > ublas mailing list > [email protected] > http://lists.boost.org/mailman/listinfo.cgi/ublas > Sent to: [email protected]