Boost logo

Ublas :

Subject: Re: [ublas] function with ublas types
From: Gunter Winkler (guwi17_at_[hidden])
Date: 2009-03-14 17:41:17


Am Donnerstag, 12. März 2009 23:01 schrieb Denis Taniguchi:
> Hi all,
>
> I was trying to define a function that uses ublas vector type:
>
> template <class T>
> quaternion<T> rot_quat(const c_vector<T, 3> &vec, T angle)
> {
> T tmp = sin(0.5*angle);
> return quaternion<T>(cos(0.5*angle),
> vec(0) * tmp,
> vec(1) * tmp,
> vec(2) * tmp);
> }
>
> I can successfully use the function with the following line of code:
>
> quaternion<double> q = rot_quat(vec3(0.0, 0.0, 1.0), M_PI/4.0);
>
> But this fails to compile with gcc 4.3.2 and boost 1.34.1:
>
> quaternion<double> q = rot_quat(vec3(0.0, 0.0, 0.5) + vec3(0.0,
> 0.0, 0.5), M_PI/4.0);
>
> src/main.cpp:21: error: no matching function for call to
> 'rot_quat(boost::numeric::ublas::vector_binary<boost::numeric::ublas:
>:c_vector<double, 3ul>, boost::numeric::ublas::c_vector<double, 3ul>,
> boost::numeric::ublas::scalar_plus<double, double> >, double)'
>
> I think that the problem is that vec3(0.0, 0.0, 0.5) + vec3(0.0, 0.0,
> 0.5) is an vector_expression and it can't automatically cast to
> c_vector<double, 3>. If I wanted something generic that would work in
> any type of vector I would implement through vector_expressions but
> this function has only meaning for bounded vectors of size 3.

You are right, the problem is the vector expression. Maybe it is an
option to provide an overload of the above function

// untested
    template <class V>
    quaternion<typename V::value_type> rot_quat(const
vector_expression<V> &ve, typename V::value_type angle)
    {
      assert( ve().size() == 3 );
      T tmp = sin(0.5*angle);
      return quaternion<T>(cos(0.5*angle),
                           ve()(0) * tmp,
                           ve()(1) * tmp,
                           ve()(2) * tmp);
    }

Of course you loose the compile time type checks but the only
alternatives I see are to introduce a temporary c_vector<T,3> or a new
expression type "fixed_size_expression<T, N>".

mfg
Gunter