Boost logo

Ublas :

Subject: Re: [ublas] function with ublas types
From: Denis Taniguchi (taniguchi_at_[hidden])
Date: 2009-03-16 14:54:24


Hi Gunter,

Thanks for your reply, it was very clarifying.
Actually I solved my particular problem by making the vector class a
template, so I end up with a more generic code, that would probably
(didn't test it myself) work with stl containers or even boost::array.
Some day in the future I will probably have face vector_expressions, but
I will have to understand the concept a little more deeply (for
implementing fixed_sized_expression).
Best regards,

Denis Taniguchi

    template <class T>
    T vec3(typename T::value_type x,
           typename T::value_type y,
           typename T::value_type z)
    {
      T ret(3);
      ret[0] = x;
      ret[1] = y;
      ret[2] = z;
      return ret;
    }

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

On Sat, 2009-03-14 at 22:41 +0100, Gunter Winkler wrote:
> 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
>
>
>
>
>
> _______________________________________________
> ublas mailing list
> ublas_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/ublas