Boost logo

Ublas :

Subject: [ublas] Problem with nested products
From: Michael Wand (michael.wand_at_[hidden])
Date: 2010-12-20 11:49:09


Good evening,

this may be a beginner's question, but nonetheless I'd be grateful for a
little guidance, since I feel I haven't understood at all how UBlas works.

My problem is as follows: I wanted to compute a product of the form (vector *
matrix * vector'), yielding a scalar. I use the ublas::matrix class for both
matrix and vectors, and I'd like to write something like prod(a,prod(b,c)). A
little sample program is:

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>

namespace ublas = boost::numeric::ublas;

// Take a matrix A and a row vector B
void UBlasStuff(ublas::matrix<double> A,ublas::matrix<double> B) {
        ublas::matrix<double> BTrans = ublas::trans(B);
        
// the next two lines work
        ublas::matrix<double> intermediate_result = ublas::prod(B,A);
        ublas::matrix<double> result = ublas::prod(intermediate_result,BTrans);

// the next two lines do not compile if uncommented
/// ublas::matrix<double> result_direct =
/// ublas::prod(B,ublas::prod(A,BTrans));
        
}

int main(int argc,char** argv) {
        ublas::matrix<double> A(3,3); // uninitialized since we're looking for a
        ublas::matrix<double> B(1,3); // compilation error, not a runtime error
        UBlasStuff(A,B);
        return 0;
}

The problem, as noted in the source code, is that the nested product
ublas::prod(B,ublas::prod(A,BTrans));
does not compile. Since I don't understand the error message at all, I've
reproduced it verbatim here:

g++ -I/usr/include/python -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -
MF"src/POC8.d" -MT"src/POC8.d" -o"src/POC8.o" "../src/POC8.cpp"
/usr/local/include/boost/numeric/ublas/matrix_expression.hpp: In function
‘typename boost::numeric::ublas::matrix_matrix_binary_traits<typename
E1::value_type, E1, typename E2::value_type, E2>::result_type
boost::numeric::ublas::prod(const
boost::numeric::ublas::matrix_expression<E>&, const
boost::numeric::ublas::matrix_expression<E2>&) [with E1 =
boost::numeric::ublas::matrix<double,
boost::numeric::ublas::basic_row_major<unsigned int, int>,
boost::numeric::ublas::unbounded_array<double, std::allocator<double> > >, E2
=
boost::numeric::ublas::matrix_matrix_binary<boost::numeric::ublas::matrix<double,
boost::numeric::ublas::basic_row_major<unsigned int, int>,
boost::numeric::ublas::unbounded_array<double, std::allocator<double> > >,
boost::numeric::ublas::matrix<double,
boost::numeric::ublas::basic_row_major<unsigned int, int>,
boost::numeric::ublas::unbounded_array<double, std::allocator<double> > >,
boost::numeric::ublas::matrix_matrix_prod<boost::numeric::ublas::matrix<double,
boost::numeric::ublas::basic_row_major<unsigned int, int>,
boost::numeric::ublas::unbounded_array<double, std::allocator<double> > >,
boost::numeric::ublas::matrix<double,
boost::numeric::ublas::basic_row_major<unsigned int, int>,
boost::numeric::ublas::unbounded_array<double, std::allocator<double> > >,
double> >]’:
../src/POC8.cpp:42: instantiated from here
/usr/local/include/boost/numeric/ublas/matrix_expression.hpp:4815: error:
invalid application of ‘sizeof’ to incomplete type
‘boost::STATIC_ASSERTION_FAILURE<false>’
../src/POC8.cpp:42: instantiated from here
/usr/local/include/boost/numeric/ublas/matrix_expression.hpp:4815: error:
invalid application of ‘sizeof’ to incomplete type
‘boost::STATIC_ASSERTION_FAILURE<false>’
make: *** [src/POC8.o] Fehler 1

Am I missing any header here? What didn't I see?

Thanks for any help,

- Michael