Boost logo

Ublas :

Subject: Re: [ublas] vector function
From: Gunter Winkler (guwi17_at_[hidden])
Date: 2009-03-15 16:27:18


Heiko Bauke schrieb:
> Hi,
>
> I have a question similar to the question by Denis Taniguchi.
>
> I am working on a project where special (sparse) matrix vector
> multiplications are required. The matrices are fixed 4x4 matrices (the
> Dirac matrices). At the moment matrix vector multiplications are
> implemented as functions, e.g.
>
> template<typename T>
> blas::vector<T> mult_alpha_x(const blas::vector<T> &v) {
> blas::vector<T> v_new(4);
> v_new(0)=v(3);
> v_new(1)=v(2);
> v_new(2)=v(1);
> v_new(3)=v(0);
> return v_new;
> }
>
> I would like to take advantage of expression templates and implement
> these functions such that they take a vector expression as argument
> and return a vector expression as result. How can I implement such
> functions?
>
The general idea is to return a expression type that only holds
references to the arguments:

template<class T>
mult_alpha_expression< vector<T> > mult_alpha_x(const vector<T> &v) {
  return mult_alpha_expression< vector<T> >(v);
}

and to defer the computation until the result is assigned or used:

template<class T>
class mult_alpha_expression< vector<T> > : public vector_expression< T > {
  // ...
public:
  T operator(size_type i) const {
    return v(3-i);
  }
private:
  const vector<T> & v;
}

The only time consuming task is to implement all the methods required by
the vector_expression concept.

mfg
Gunter

(BTW: do you know http://tvmet.sourceforge.net/ ?)