Boost logo

Ublas :

Subject: Re: [ublas] Matrix Multiplication with Different Result Type
From: Gunter Winkler (guwi17_at_[hidden])
Date: 2010-05-31 18:42:37


Hello Tynan,

the uBLAS return type deduction only works for simple types and types
that support the operator+() with the limitation that operator+(A,B)
must either return type A or B. If the operator+() returns some
unrelated type, then you have to provide the specialization of
promote_traits, see below.

Additionally you always have to provide the default constructor and the
contructor from int (which is needed for zero-initialization and
one-initialization). It was planned to move the zero and one to scalar
traits but this is a quite tedious task ...

Am Friday 28 May 2010 schrieb Tynan Wilke:
> Hello,
>
> Any hints/suggestions on getting this to compile are greatly
> appreciated.
>
> -------------------------------------------------
> #include<iostream>
> #include<boost/numeric/ublas/matrix.hpp>
> #include<boost/numeric/ublas/io.hpp>
>
> namespace ub = boost::numeric::ublas;
>
> struct A {
> A() {}
> A(int) {}
> };
> struct B {};
> struct C {};
> //typedef A C;

namespace boost { namespace numeric { namespace ublas {

  // one must explicitly pollute the ublas namespace unless
  // (A+B) returns either A or B

template<>
struct promote_traits<A,B> {
  typedef C promote_type;
};

}}}

>
> // For matrix multiply *
> C operator*(const A &a, const B&b) {
> // noop
> }
>
> // For matrix multiply +
> C operator+(const C &c1, const C &c2) {
> // noop
> }
>
> // For matrix multiply +=
> C operator+=(C &c1, const C &c2) {
> // noop
> }
>
> // Needed for promote_traits<A, B> for result_type (I think)
> C operator+(const A &a, const B &b) {
> // noop
> }
>
> int main() {
> ub::matrix<A> mxA(1, 3);
> ub::matrix<B> mxB(3, 3);
> ub::matrix<C> mxC = prod(mxA, mxB);
> };