# Ublas :

Subject: [ublas] How to efficiently copy sparse matrix row to vector?
From: Reich, Darrell (DARRELL.REICH_at_[hidden])
Date: 2013-08-23 22:49:43

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.