Boost logo

Ublas :

From: Vardan Akopian (vakopian_at_[hidden])
Date: 2007-09-04 18:38:49


On 8/9/07, e r <erwann.rogard_at_[hidden]> wrote:
> hi,
> what's the best way to achieve x*=y? where x and y are vectors (or
> proxies) of identical size? although i wrote a function (below) i'm
> hoping there's already some ublas related facility to do so that someone
> would kindly want to share.
>
> template<typename D> class Op_product_term_by_term{
> public:
> Op_product_term_by_term(D& dest):iter(dest.begin()),end(dest.end()){};
> void operator()(double elem);
> private:
> typename D::iterator iter;
> typename D::iterator end;
> };
> template<typename D> void Op_product_term_by_term<D>::operator()(double
> elem){
> if(iter<end){
> *(iter++)*=elem;
> }else{
> throw;
> };
> };
> template<typename D, typename B> void product_term_by_term(D& dest,const
> B& by){
> if(dest.size()==by.size()){
> for_each(by.begin(),by.end(),Op_product_term_by_term<D>(dest));
> }else{
> throw;
> };
> };
>
> ps: should i post this kind of ad in boost.ublas or boost.users?
>

Ublas has a function called element_prod, that you could use to get
the same effect:
x = element_prod(x, y)
The right hand side produces an expression template, which is then
computed and assigned to x. this may or may not be as efficient as the
hand written code.

Regards.
-Vardan