Boost logo

Ublas :

From: Gunter Winkler (guwi17_at_[hidden])
Date: 2005-08-20 05:49:30

Am Freitag, 19. August 2005 22:00 schrieb Shawn D. Pautz:
> Is there a way to use inner_prod (or something similar) for vectors
> of more complicated types? I would like to form the inner product of
> a vector<double> and a vector<matrix<double> >, with the following
> semantics:
> vector<double> vec;
> vector<matrix<double> > vecOfMatrices;
> matrix<double> result; // = 0
> // result = inner_product(vec, vecOfMatrices); // won't compile
> for (int i = 0; i < vec.size(); ++i)
> result += vec[i]*vecOfMatrices[i];
> What's the best way to perform the above operation without resorting
> to writing loops?

Currently this is not directly supported. The inner_prod uses the
operator * (x, y) function to compute the products which should work
for scalar times Matrix. However, the return type of the expression can
not be calculated automatically. You have to create a specialization of

  typedef double SCALAR;
  typedef matrix<SCALAR> MATRIX;

  struct promote_traits< SCALAR, MATRIX > {
    typedef MATRIX base_type;
    typedef MATRIX promote_type;

Unfortunatly this only fixes the return type deduction. The second
problem is that the return type has to be constructible from (int) 0.
This can be achieved by using a type derived from MATRIX with an
appropriate constructor.

class my_matrix : public MATRIX {
  public: my_matrix(const size_t zero) :
MATRIX( zero_matrix<double>(5,5) ) { }

I hope you got the idea.


PS: How far is the work for type_traits<MATRIX>::zero ?