Boost logo

Boost Users :

From: jhr.walter_at_[hidden]
Date: 2003-02-27 01:28:52


Hi Roland,

you wrote:

> > OK, I think I understand now what you're aiming at: you want ublas to
give
> > you the characteristics of (ordinary ;-) runtime polymorphism. But
would you
> > be willing to pay the performance penalty for using an virtual
operator()
> > then, for example?
>
> I think generally not.
> Hmmm, but is there a way to do without? After a lot of reading and
> thinking and with your help :-) I came out with the following. May I
> show you a code snippet from my spline function to show you my actual
> problem?
>===============================================================
> template<class T>
> class vector_proxy_base {
> public:
> virtual T operator[] (unsigned int idx) = 0;
> };
>
> template<class E>
> class vector_proxy : public vector_proxy_base<E::value_type>
> {
> public:
> vector_proxy(vector_expression<E> e) : m_e(e) {}
> typename E::value_type operator[] (unsigned int idx)
> { return m_e().operator[](idx); }
> private:
> vector_expression<E> m_e;
> };
>
> template<class T>
> class spline : public std::unary_function<T, T> {
> public:
> template<class E>
> spline(vector_expression<E> e) : ex(new vector_proxy<E>(e)) {}
> ~ppi0() { delete ex; }
> T operator() (const T& x){
> // the spline interpolation algorithm goes here ...
> return ex->[0]; // do something useful ;-)
> // ugly, indirection and virtual function call
> }
> private:
> vector_proxy_base<T>* ex;
> };
>===============================================================
>
> I can not see a way to do this differently, since otherwise I would need
to
> template<class T, class E>
> class spline {
> ....
> }
>
> where E is the expression type.

Yep. Here it goes:
----------
#include <boost/numeric/ublas/vector.hpp>
using namespace boost::numeric::ublas;
template<class T, class E>
class spline : public std::unary_function<T, T> {
public:
    spline(const vector_expression<E> &e) : e_ (e) {}
    T operator() (const T& x){
        // the spline interpolation algorithm goes here ...
        return e_ (0); // do something useful ;-)
    }
private:
    const vector_expression<E> &e_;
};
int main() {
    vector<double> v(3);
    spline<double, vector<double> > s1(v);
    // With GCC's extensions the following works :-)
    spline<double, typeof (v) > s2(v);
}

----------

> But instantiating the functor then would be infeasible since I do not
> know the complete expression hierarchy, or at least it is too
> cumbersome to specify.

This is a limitation of the language ;-/

> The automatic template parameter deduction
> does not work for class templates as it does for funtion templates,
> does it?
>
> What yet is missing of course are all the other member functions that
> make a vector or matrix.
>
> Perhaps there is a simpler solution I do not see?

Not one that I know of.

> > vector_reference<> is mainly needed to describe the signatures of
> > operators returning expression templates.
>
> I was trying to
> template<class E>
> void foo(vector_reference<E> e)
> {
> e.size(); // some use of e
> }
>
> and call it
> foo(m.column(0));
>
> But my compiler chokes. So am I right, that vector_reference is not
> intended for this kind of usage?

Correct.

> > groups.yahoo.com/group/ublas-dev
>
> Oh, I may read this as an ordinary user?

Maybe as an ordinary user you simply should copy the vector expression into
another vector ;-)

Regards,

Joerg


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net