Multiplying a matrix and vector ===> vector
Dear all,
My basic requirement is to multiply a matrix and a vector and then to
store it into a vector using Boost's UBLAS.
When I multiply a
boost::numeric::ublas::compressed_matrix with a
boost::numeric::ublas::vector using prod(matrix, vector), the output is a
boost::numeric::ublas::matrix_vector_binary1. E.g.
<PSEUDOCODE>
boost::numeric::ublas::compressed_matrix m;
boost::numeric::ublas::vector x;
boost::numeric::ublas::matrix_vector_binary1 tmp;
tmp = prod(m,x);
<PSEUDOCODE>
The problem is that in the third field --- which expects the operation
type --- in line 3, I do not know what to
enter or how to specify the type of operation; I tried '*' and 'prod' and
'vector' and 'compressed_matrix' but none work. Alternatively, I can avoid the tmp variable altogether and get a
working program to copy the result of matrix-vector multiplication onto
a new vector y but then I would
need two multiplications. E.g.
<CODE>
std::copy(prod(m,x).begin(), prod(m,x).end(), y.begin());
<CODE>
Any suggestions on how to declare a matrix_vector_binary1 correctly or
how to
get the finaly result efficiently onto a vector will be appreciated.
Regards,
Vipin
Have you tried: vector z = prod(m,x); On 2012-10-26 13:08, Vipin Varma wrote:
Dear all,
My basic requirement is to multiply a matrix and a vector and then to store it into a vector using Boost's UBLAS.
When I multiply a boost::numeric::ublas::compressed_matrix with a boost::numeric::ublas::vector using prod(matrix, vector), the output is a boost::numeric::ublas::matrix_vector_binary1. E.g. <PSEUDOCODE> boost::numeric::ublas::compressed_matrix m; boost::numeric::ublas::vector x; boost::numeric::ublas::matrix_vector_binary1
tmp; tmp = prod(m,x); <PSEUDOCODE> The problem is that in the third field --- which expects the operation type --- in line 3, I do not know what to enter or how to specify the type of operation; I tried '*' and 'prod' and 'vector' and 'compressed_matrix' but none work.
Alternatively, I can avoid the tmp variable altogether and get a working program to copy the result of matrix-vector multiplication onto a new vector y but then I would need two multiplications. E.g. <CODE> std::copy(prod(m,x).begin(), prod(m,x).end(), y.begin()); <CODE>
Any suggestions on how to declare a matrix_vector_binary1 correctly or how to get the finaly result efficiently onto a vector will be appreciated.
Regards, Vipin _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Yes, in fact I just realised that I can do all the usual algebra for standard data types like double, float etc. But when I try to use CLN rational numbers - which is what I want - the below step fails with the following compile error: <ERROR> /usr/include/boost/numeric/ublas/traits.hpp:97: error: no matching function for call to 'sqrt(const cln::cl_RA&)' /usr/include/bits/mathcalls.h:157: note: candidates are: double sqrt(double) /usr/include/c++/4.3/cmath:434: note: long double std::sqrt(long double) /usr/include/c++/4.3/cmath:430: note: float std::sqrt(float) <ERROR> However I can still multiply a scalar CLN number with a CLN matrix and assign it to a new CLN matrix; it's multiplication of a CLN vector with a CLN matrix that gives the problem. Thanks, Vipin On Fri, 26 Oct 2012, Oswin Krause wrote:
Have you tried:
vector z = prod(m,x);
On 2012-10-26 13:08, Vipin Varma wrote:
Dear all,
My basic requirement is to multiply a matrix and a vector and then to store it into a vector using Boost's UBLAS.
When I multiply a boost::numeric::ublas::compressed_matrix with a boost::numeric::ublas::vector using prod(matrix, vector), the output is a boost::numeric::ublas::matrix_vector_binary1. E.g. <PSEUDOCODE> boost::numeric::ublas::compressed_matrix m; boost::numeric::ublas::vector x; boost::numeric::ublas::matrix_vector_binary1
tmp; tmp = prod(m,x); <PSEUDOCODE> The problem is that in the third field --- which expects the operation type --- in line 3, I do not know what to enter or how to specify the type of operation; I tried '*' and 'prod' and 'vector' and 'compressed_matrix' but none work.
Alternatively, I can avoid the tmp variable altogether and get a working program to copy the result of matrix-vector multiplication onto a new vector y but then I would need two multiplications. E.g. <CODE> std::copy(prod(m,x).begin(), prod(m,x).end(), y.begin()); <CODE>
Any suggestions on how to declare a matrix_vector_binary1 correctly or how to get the finaly result efficiently onto a vector will be appreciated.
Regards, Vipin _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hi, without knowing what CLN should be (would have been nice to know beforehand, that you are using an unusual value_typ for vectors/matrics...) I suspect you are compiling in debug mode and the error goes away in release. This is because the debug test for numeric stability fails. Try to define this before including anything from ublas (or set as compile flag): BOOST_UBLAS_TYPE_CHECK=0 On 2012-10-27 11:47, Vipin Varma wrote:
Yes, in fact I just realised that I can do all the usual algebra for standard data types like double, float etc. But when I try to use CLN rational numbers - which is what I want - the below step fails with the following compile error: <ERROR> /usr/include/boost/numeric/ublas/traits.hpp:97: error: no matching function for call to 'sqrt(const cln::cl_RA&)' /usr/include/bits/mathcalls.h:157: note: candidates are: double sqrt(double) /usr/include/c++/4.3/cmath:434: note: long double std::sqrt(long double) /usr/include/c++/4.3/cmath:430: note: float std::sqrt(float) <ERROR>
However I can still multiply a scalar CLN number with a CLN matrix and assign it to a new CLN matrix; it's multiplication of a CLN vector with a CLN matrix that gives the problem.
Thanks, Vipin
On Fri, 26 Oct 2012, Oswin Krause wrote:
Have you tried:
vector z = prod(m,x);
On 2012-10-26 13:08, Vipin Varma wrote:
Dear all, My basic requirement is to multiply a matrix and a vector and then to store it into a vector using Boost's UBLAS. When I multiply a boost::numeric::ublas::compressed_matrix with a boost::numeric::ublas::vector using prod(matrix, vector), the output is a boost::numeric::ublas::matrix_vector_binary1. E.g. <PSEUDOCODE> boost::numeric::ublas::compressed_matrix m; boost::numeric::ublas::vector x; boost::numeric::ublas::matrix_vector_binary1
tmp; tmp = prod(m,x); <PSEUDOCODE> The problem is that in the third field --- which expects the operation type --- in line 3, I do not know what to enter or how to specify the type of operation; I tried '*' and 'prod' and 'vector' and 'compressed_matrix' but none work. Alternatively, I can avoid the tmp variable altogether and get a working program to copy the result of matrix-vector multiplication onto a new vector y but then I would need two multiplications. E.g. <CODE> std::copy(prod(m,x).begin(), prod(m,x).end(), y.begin()); <CODE> Any suggestions on how to declare a matrix_vector_binary1 correctly or how to get the finaly result efficiently onto a vector will be appreciated. Regards, Vipin
Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
That worked but only with a boost::ublas::numeric::compressed_vector and not a simple boost::ublas::numeric::vector. Thanks, Vipin On Sun, 28 Oct 2012, Oswin Krause wrote:
Hi,
without knowing what CLN should be (would have been nice to know beforehand, that you are using an unusual value_typ for vectors/matrics...)
I suspect you are compiling in debug mode and the error goes away in release. This is because the debug test for numeric stability fails.
Try to define this before including anything from ublas (or set as compile flag): BOOST_UBLAS_TYPE_CHECK=0
On 2012-10-27 11:47, Vipin Varma wrote:
Yes, in fact I just realised that I can do all the usual algebra for standard data types like double, float etc. But when I try to use CLN rational numbers - which is what I want - the below step fails with the following compile error: <ERROR> /usr/include/boost/numeric/ublas/traits.hpp:97: error: no matching function for call to 'sqrt(const cln::cl_RA&)' /usr/include/bits/mathcalls.h:157: note: candidates are: double sqrt(double) /usr/include/c++/4.3/cmath:434: note: long double std::sqrt(long double) /usr/include/c++/4.3/cmath:430: note: float std::sqrt(float) <ERROR>
However I can still multiply a scalar CLN number with a CLN matrix and assign it to a new CLN matrix; it's multiplication of a CLN vector with a CLN matrix that gives the problem.
Thanks, Vipin
On Fri, 26 Oct 2012, Oswin Krause wrote:
Have you tried:
vector z = prod(m,x);
On 2012-10-26 13:08, Vipin Varma wrote:
Dear all, My basic requirement is to multiply a matrix and a vector and then to store it into a vector using Boost's UBLAS. When I multiply a boost::numeric::ublas::compressed_matrix with a boost::numeric::ublas::vector using prod(matrix, vector), the output is a boost::numeric::ublas::matrix_vector_binary1. E.g. <PSEUDOCODE> boost::numeric::ublas::compressed_matrix m; boost::numeric::ublas::vector x; boost::numeric::ublas::matrix_vector_binary1
tmp; tmp = prod(m,x); <PSEUDOCODE> The problem is that in the third field --- which expects the operation type --- in line 3, I do not know what to enter or how to specify the type of operation; I tried '*' and 'prod' and 'vector' and 'compressed_matrix' but none work. Alternatively, I can avoid the tmp variable altogether and get a working program to copy the result of matrix-vector multiplication onto a new vector y but then I would need two multiplications. E.g. <CODE> std::copy(prod(m,x).begin(), prod(m,x).end(), y.begin()); <CODE> Any suggestions on how to declare a matrix_vector_binary1 correctly or how to get the finaly result efficiently onto a vector will be appreciated. Regards, Vipin
Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
doesn't work is a perfect error description. On 2012-10-29 11:30, Vipin Varma wrote:
That worked but only with a boost::ublas::numeric::compressed_vector and not a simple boost::ublas::numeric::vector.
Thanks, Vipin
On Sun, 28 Oct 2012, Oswin Krause wrote:
Hi,
without knowing what CLN should be (would have been nice to know beforehand, that you are using an unusual value_typ for vectors/matrics...)
I suspect you are compiling in debug mode and the error goes away in release. This is because the debug test for numeric stability fails.
Try to define this before including anything from ublas (or set as compile flag): BOOST_UBLAS_TYPE_CHECK=0
On 2012-10-27 11:47, Vipin Varma wrote:
Yes, in fact I just realised that I can do all the usual algebra for standard data types like double, float etc. But when I try to use CLN rational numbers - which is what I want - the below step fails with the following compile error: <ERROR> /usr/include/boost/numeric/ublas/traits.hpp:97: error: no matching function for call to 'sqrt(const cln::cl_RA&)' /usr/include/bits/mathcalls.h:157: note: candidates are: double sqrt(double) /usr/include/c++/4.3/cmath:434: note: long double std::sqrt(long double) /usr/include/c++/4.3/cmath:430: note: float std::sqrt(float) <ERROR> However I can still multiply a scalar CLN number with a CLN matrix and assign it to a new CLN matrix; it's multiplication of a CLN vector with a CLN matrix that gives the problem. Thanks, Vipin On Fri, 26 Oct 2012, Oswin Krause wrote:
Have you tried: vector z = prod(m,x); On 2012-10-26 13:08, Vipin Varma wrote:
Dear all, My basic requirement is to multiply a matrix and a vector and then to store it into a vector using Boost's UBLAS. When I multiply a boost::numeric::ublas::compressed_matrix with a boost::numeric::ublas::vector using prod(matrix, vector), the output is a boost::numeric::ublas::matrix_vector_binary1. E.g. <PSEUDOCODE> boost::numeric::ublas::compressed_matrix m; boost::numeric::ublas::vector x; boost::numeric::ublas::matrix_vector_binary1
tmp; tmp = prod(m,x); <PSEUDOCODE> The problem is that in the third field --- which expects the operation type --- in line 3, I do not know what to enter or how to specify the type of operation; I tried '*' and 'prod' and 'vector' and 'compressed_matrix' but none work. Alternatively, I can avoid the tmp variable altogether and get a working program to copy the result of matrix-vector multiplication onto a new vector y but then I would need two multiplications. E.g. <CODE> std::copy(prod(m,x).begin(), prod(m,x).end(), y.begin()); <CODE> Any suggestions on how to declare a matrix_vector_binary1 correctly or how to get the finaly result efficiently onto a vector will be appreciated. Regards, Vipin
Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Alternatively, I can avoid the tmp variable altogether and get a working program to copy the result of matrix-vector multiplication onto a new vector y but then I would need two multiplications. E.g. <CODE> std::copy(prod(m,x).begin(), prod(m,x).end(), y.begin()); <CODE>
This doesn't answer your question, but I just wanted to point out that the above line is undefined behaviour, since the begin and end iterators refer to two different ranges (two different temporary variables). This problem can be avoided by using boost::copy (from Boost.Range) instead, which allows you to mention the range just once: boost::copy(prod(m, x), y.begin()); Regards, Nate
Unfortunately I'm unable to get the below suggestion to work. Could you please mention if this was what you were referring to: http://www.boost.org/doc/libs/1_49_0/libs/range/doc/html/range/reference/ada... If so, the closest file I have in my Boost Version 1.0 is boost/range/iterator_range.hpp but that doesn't have the below copy function. I'll probably need to get the 1.49.0 version then. Thanks, Vipin On Sat, 27 Oct 2012, Nathan Ridge wrote:
Alternatively, I can avoid the tmp variable altogether and get a working program to copy the result of matrix-vector multiplication onto a new vector y but then I would need two multiplications. E.g. <CODE> std::copy(prod(m,x).begin(), prod(m,x).end(), y.begin()); <CODE>
This doesn't answer your question, but I just wanted to point out that the above line is undefined behaviour, since the begin and end iterators refer to two different ranges (two different temporary variables). This problem can be avoided by using boost::copy (from Boost.Range) instead, which allows you to mention the range just once:
boost::copy(prod(m, x), y.begin());
Regards, Nate
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Alternatively, I can avoid the tmp variable altogether and get a working program to copy the result of matrix-vector multiplication onto a new vector y but then I would need two multiplications. E.g.
std::copy(prod(m,x).begin(), prod(m,x).end(), y.begin());
This doesn't answer your question, but I just wanted to point out that the above line is undefined behaviour, since the begin and end iterators refer to two different ranges (two different temporary variables). This problem can be avoided by using boost::copy (from Boost.Range) instead, which allows you to mention the range just once:
boost::copy(prod(m, x), y.begin());
Unfortunately I'm unable to get the below suggestion to work. Could you please mention if this was what you were referring to: http://www.boost.org/doc/libs/1_49_0/libs/range/doc/html/range/reference/ada...
If so, the closest file I have in my Boost Version 1.0 is boost/range/iterator_range.hpp but that doesn't have the below copy function. I'll probably need to get the 1.49.0 version then.
Boost.Range's copy function is documented here: http://www.boost.org/doc/libs/1_49_0/libs/range/doc/html/range/reference/alg... I believe it was first added in version 1.43. However, it's not difficult to write your own version. The following will work for any range type that has begin() and end() member functions: template void copy(const Range& input, OutputIterator output) { std::copy(input.begin(), input.end(), output); } Regards, Nate
participants (3)
-
Nathan Ridge
-
Oswin Krause
-
Vipin Varma