Boost logo

Ublas :

Subject: Re: [ublas] Extended/overloading ublas
From: Kim Kuen Tang (kuentang_at_[hidden])
Date: 2009-06-03 15:18:36


Hi kayhman,

below is the working code.
To understand what you are doing, can you tell us more?

//#include <vector>
#include <iostream>
#include <boost/numeric/ublas/vector.hpp>

namespace boost { namespace numeric { namespace ublas {

class twist : public vector<double, ublas::bounded_array<double,3> >
{
    public:
    typedef vector<double, bounded_array<double,3> > Base_vector;
   
    // Default construction
    twist () : Base_vector(3)
    {}
    // Construction and assignment from a uBLAS vector expression or
copy assignment
    template <class R> twist (const vector_expression<R>& r) :
Base_vector(r)
    {}
    template <class R> void operator=(const vector_expression<R>& r)
    {
        Base_vector::operator=(r);
    }
    template <class R> void operator=(const Base_vector& r)
    {
        Base_vector::operator=(r);
    }
};

BOOST_UBLAS_INLINE
vector_binary_traits<twist::Base_vector,
    twist::Base_vector,
                    scalar_plus<double,double> >::result_type
//operator + (const vector_expression<twist > &e1,
// const vector_expression<twist > &e2)
operator + (twist::Base_vector &e1, twist::Base_vector &e2)
{
    typedef vector_binary_traits<twist::Base_vector,
        twist::Base_vector, scalar_plus<double,double>
>::expression_type expression_type;
    std::cout << "twist sum !!!!" << std::endl;
    return expression_type (e1, e2);
}

}
}
}

int main()
{
    using namespace boost::numeric::ublas;
    twist T1;
    twist T2;

    std::cout << "sum Twist : " << std::endl;
    T2+T1;
    //std::cout << "Quat : " << T2+T1 << std::endl;

}

kayhman schrieb:
> Hello !
>
>
> I'd like to extend ublas, i.e. I want to create a new Twist class, and
> overload some of the standard operation like addition, inner_prod, ...
>
> I've define the new class as follow :
>
> class twist : public vector<double, ublas::bounded_array<double,3> >
> {
> typedef vector<double, bounded_array<double,3> > Base_vector;
> public:
> // Default construction
> twist () : Base_vector(3)
> {}
> // Construction and assignment from a uBLAS vector expression or
> copy assignment
> template <class R> twist (const vector_expression<R>& r) :
> Base_vector(r)
> {}
> template <class R> void operator=(const vector_expression<R>& r)
> {
> Base_vector::operator=(r);
> }
> template <class R> void operator=(const Base_vector& r)
> {
> Base_vector::operator=(r);
> }
> };
>
> And I've overloaded the sum specializing the following templated
> function :
>
> template<>
> BOOST_UBLAS_INLINE
> vector_binary_traits<twist,
> twist,
> scalar_plus<double,double> >::result_type
> operator + (const vector_expression<twist > &e1,
> const vector_expression<twist > &e2)
> {
> typedef vector_binary_traits<twist, twist,
> scalar_plus<double,double> >::expression_type expression_type;
> std::cout << "twist sum !!!!" << std::endl;
> return expression_type (e1(), e2());
> }
>
>
>
> And I get the following error (using gcc-4.2):
>
> ../../../boost/numeric/ublas/
> expression_types.hpp: In member function ‘const E&
> boost::numeric::ublas::vector_expression<E>::operator()() const [with
> E = boost::numeric::ublas::twist]’:
> twist.cpp:37: instantiated from here
> ../../../boost/numeric/ublas/expression_types.hpp:184: error: invalid
> static_cast from type ‘const
> boost::numeric::ublas::vector_expression<boost::numeric::ublas::twist>*
> const’ to type ‘const boost::numeric::ublas::twist*’
>
> Do you have any idea of where is my mistake ?
>
> Thanks,
>
> kayhman
>
> P.S. : a small example is enclosed.
> ------------------------------------------------------------------------
>
> _______________________________________________
> ublas mailing list
> ublas_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/ublas
>