E
er
hi,
i'm trying to implement x*=y or x+=y; where x and y are containers and
*= and += are element wise i.e. x*=y is meant as
x[1]*=y[1],...,x[n]*=y[n] and likewise for +=;
i'm wondering if
a) there exists a function already defined for this purpose for the
types that i intend it for (see below)
b) is there a way to make * or + a parameter so that i don't have to
write two (nearly identical) pieces of code for each (see below).
c) more generally, is there a better way to do this?
// possible types for B and D:
// type boost::numeric::ublas::vector<double>
//
//code starts here:
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>:perator()
(double elem){
if(iter<end){
*(iter++)*=elem;
}else{
throw std::runtime_error("Aux::Op_product_term_by_term");
};
};
template<typename D> class Op_sum_term_by_term{
public:
Op_sum_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_sum_term_by_term<D>:perator()(double
elem){
if(iter<end){
*(iter++)+=elem;
}else{
throw std::runtime_error("Aux::Op_sum_term_by_term");
};
};
}
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 std::runtime_error("Aux:roduct_term_by_term");
};
};
template<typename D, typename B> void sum_term_by_term(D& dest,const
B& by){
if(dest.size()==by.size()){
for_each(by.begin(),by.end(),Op_sum_term_by_term<D>(dest));
}else{
throw std::runtime_error("Aux::sum_term_by_term");
};
};
i'm trying to implement x*=y or x+=y; where x and y are containers and
*= and += are element wise i.e. x*=y is meant as
x[1]*=y[1],...,x[n]*=y[n] and likewise for +=;
i'm wondering if
a) there exists a function already defined for this purpose for the
types that i intend it for (see below)
b) is there a way to make * or + a parameter so that i don't have to
write two (nearly identical) pieces of code for each (see below).
c) more generally, is there a better way to do this?
// possible types for B and D:
// type boost::numeric::ublas::vector<double>
//
//code starts here:
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>:perator()
(double elem){
if(iter<end){
*(iter++)*=elem;
}else{
throw std::runtime_error("Aux::Op_product_term_by_term");
};
};
template<typename D> class Op_sum_term_by_term{
public:
Op_sum_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_sum_term_by_term<D>:perator()(double
elem){
if(iter<end){
*(iter++)+=elem;
}else{
throw std::runtime_error("Aux::Op_sum_term_by_term");
};
};
}
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 std::runtime_error("Aux:roduct_term_by_term");
};
};
template<typename D, typename B> void sum_term_by_term(D& dest,const
B& by){
if(dest.size()==by.size()){
for_each(by.begin(),by.end(),Op_sum_term_by_term<D>(dest));
}else{
throw std::runtime_error("Aux::sum_term_by_term");
};
};