27 #ifndef MLN_METAL_VEC_HH
28 # define MLN_METAL_VEC_HH
37 # include <mln/core/concept/object.hh>
38 # include <mln/algebra/h_vec.hh>
40 # include <mln/trait/all.hh>
41 # include <mln/trait/value_.hh>
42 # include <mln/fun/i2v/all_to.hh>
43 # include <mln/debug/format.hh>
45 # include <mln/value/ops.hh>
56 template <
unsigned n,
typename T>
class vec;
61 template <
unsigned d,
typename C>
class h_vec;
68 template <
unsigned n,
typename T>
69 struct value_< mln::metal::vec<n,T> >
71 typedef trait::value::nature::vectorial
nature;
72 typedef trait::value::kind::data
kind;
76 card = n * mln_card(
T)
78 typedef mln_value_quant_from_(card) quant;
80 typedef metal::vec<n, mln_sum(
T)>
sum;
93 template <
unsigned n,
typename T>
94 class vec_base_ :
public Object< vec<n,T> >
100 template <
typename T>
101 class vec_base_ <1,
T> :
public Object< vec<1,T> >
104 void set(
const T& val0)
112 template <
typename T>
113 class vec_base_ <2,
T> :
public Object< vec<2,T> >
116 void set(
const T& val0,
const T& val1)
125 template <
typename T>
126 class vec_base_ <3,
T> :
public Object< vec<3,T> >
129 void set(
const T& val0,
const T& val1,
const T& val2)
139 template <
typename T>
140 class vec_base_ <4,
T> :
public Object< vec<4,T> >
143 void set(
const T& val0,
const T& val1,
const T& val2,
const T& val3)
159 template <
unsigned n,
typename T>
160 class vec :
public internal::vec_base_<n, T>
162 typedef internal::vec_base_<n, T> super_;
178 vec(
const literal::zero_t&);
179 vec& operator=(
const literal::zero_t&);
182 vec(
const vec<n, T>&
rhs);
184 template <
typename U>
185 vec(
const vec<n, U>&
rhs);
187 template <
typename U>
188 vec& operator=(
const vec<n, U>&
rhs);
192 algebra::h_vec<n, T> to_h_vec()
const;
195 const T& operator[](
unsigned i)
const;
197 T& operator[](
unsigned i);
199 void set_all(
const T& val);
201 unsigned size()
const;
203 const vec<n, T>& normalize();
206 template <
typename F>
207 vec(
const Function_v2v<F>& f);
210 static const vec<n, T>
zero;
213 static const vec<n, T>
origin;
224 template <
template <
class>
class Name,
225 unsigned n,
typename T >
226 struct set_precise_unary_< Name, metal::
vec<n, T> >
228 typedef mln_trait_unary(Name,
T) V;
229 typedef metal::vec<n, V>
ret;
234 template < template <class, class> class Name,
235 unsigned n, typename
T,
237 struct set_precise_binary_< Name,
238 metal::vec<n, T>, metal::vec<n, U> >
240 typedef mln_trait_binary(Name, T, U) V;
241 typedef metal::vec<n, V> ret;
244 template <
unsigned n, typename T,
246 struct set_precise_binary_< op::times,
247 metal::vec<n, T>, metal::vec<n, U> >
249 typedef mln_sum_product(T,U) ret;
252 template < template <class, class> class Name,
253 unsigned n, typename T,
255 struct set_precise_binary_< Name,
256 metal::vec<n, T>, mln::
value::scalar_<S> >
258 typedef mln_trait_binary(Name, T, S) V;
259 typedef metal::vec<n, V> ret;
262 template < template<class, class> class Name,
263 unsigned n, typename T,
265 struct set_binary_< Name,
266 mln::Object, metal::vec<n, T>,
267 mln::
value::Scalar, S >
269 typedef mln_trait_binary(Name, T, S) V;
270 typedef metal::vec<n, V> ret;
282 template <
unsigned n,
typename T,
typename U>
287 template <
unsigned n,
typename T,
typename U>
288 vec<n, mln_trait_op_plus(T,U)>
293 template <
unsigned n,
typename T,
typename U>
294 vec<n, mln_trait_op_minus(T,U)>
299 template <
unsigned n,
typename T,
typename U>
301 operator*(const vec<n,T>& lhs, const vec<n,U>&
rhs);
305 template <
unsigned n, typename T, typename S>
306 vec<n, mln_trait_op_times(T, S)>
307 operator*(const vec<n,T>& lhs, const mln::
value::scalar_<S>& s);
311 template <
unsigned n, typename T, typename S>
312 vec<n, mln_trait_op_div(T, S)>
313 operator/(const vec<n,T>& lhs, const mln::
value::scalar_<S>& s);
317 template <
unsigned n, typename T>
319 operator<<(std::ostream& ostr, const vec<n,T>& v);
323 template <typename T, typename U>
324 vec<3, mln_trait_op_times(T,U)>
325 vprod(const vec<3, T>& lhs, const vec<3, U>& rhs);
328 # ifndef MLN_INCLUDE_ONLY
330 template <
unsigned n,
typename T>
336 template <
unsigned n,
typename T>
338 vec<n,T>::vec(
const literal::zero_t&)
343 template <
unsigned n,
typename T>
346 vec<n,T>::operator=(
const literal::zero_t&)
352 template <
unsigned n,
typename T>
354 vec<n,T>::vec(
const vec<n,T>& rhs)
357 for (
unsigned i = 0; i < n; ++i)
361 template <
unsigned n,
typename T>
362 template <
typename U>
364 vec<n,T>::vec(
const vec<n, U>& rhs)
367 for (
unsigned i = 0; i < n; ++i)
371 template <
unsigned n,
typename T>
372 template <
typename U>
376 for (
unsigned i = 0; i < n; ++i)
381 template <
unsigned n,
typename T>
383 const T& vec<n,T>::operator[](
unsigned i)
const
385 mln_precondition(i < dim);
389 template <
unsigned n,
typename T>
391 T& vec<n,T>::operator[](
unsigned i)
393 mln_precondition(i < dim);
397 template <
unsigned n,
typename T>
399 void vec<n,T>::set_all(
const T& val)
401 for (
unsigned i = 0; i < n; ++i)
405 template <
unsigned n,
typename T>
407 unsigned vec<n,T>::size()
const
412 template <
unsigned n,
typename T>
414 const vec<n, T>& vec<n, T>::normalize()
417 for (
unsigned i = 0; i < n; ++i)
418 n_l2 += data_[i] * data_[i];
420 for (
unsigned i = 0; i < n; ++i)
421 data_[i] = T(data_[i] / n_l2);
425 template <
unsigned n,
typename T>
426 template <
typename F>
428 vec<n, T>::vec(
const Function_v2v<F>& f_)
430 mlc_converts_to(mln_result(F), T)::check();
431 const F& f =
exact(f_);
432 for (
unsigned i = 0; i < n; ++i)
437 template <
unsigned n,
typename T>
438 const vec<n, T> vec<n, T>::zero =
all_to(0);
440 template <
unsigned n,
typename T>
441 const vec<n, T> vec<n, T>::origin =
all_to(0);
447 template <
unsigned n,
typename T,
typename U>
449 bool operator==(
const vec<n,T>& lhs,
const vec<n,U>& rhs)
451 for (
unsigned i = 0; i < n; ++i)
452 if (lhs[i] != rhs[i])
458 template <
unsigned n,
typename T,
typename U>
460 vec<n, mln_trait_op_plus(T,U)>
461 operator+(
const vec<n,T>& lhs,
const vec<n,U>& rhs)
463 vec<n, mln_trait_op_plus(T,U)> tmp;
464 for (
unsigned i = 0; i < n; ++i)
465 tmp[i] = lhs[i] + rhs[i];
469 template <
unsigned n,
typename T,
typename U>
471 vec<n, mln_trait_op_minus(T,U)>
472 operator-(
const vec<n,T>& lhs,
const vec<n,U>& rhs)
474 vec<n, mln_trait_op_minus(T,U)> tmp;
475 for (
unsigned i = 0; i < n; ++i)
476 tmp[i] = lhs[i] - rhs[i];
480 template <
unsigned n,
typename T,
typename U>
483 operator*(const vec<n,T>& lhs, const vec<n,U>& rhs)
485 mln_sum_product(T,U) tmp(literal::zero);
486 for (
unsigned i = 0; i < n; ++i)
487 tmp += lhs[i] * rhs[i];
491 template <
unsigned n, typename T, typename S>
493 vec<n, mln_trait_op_times(T, S)>
494 operator*(const vec<n,T>& lhs, const mln::
value::scalar_<S>& s)
506 vec<n, mln_trait_op_times(T, S)> tmp;
507 for (
unsigned i = 0; i < n; ++i)
508 tmp[i] = lhs[i] * s.to_equiv();
512 template <
unsigned n,
typename T,
typename S>
514 vec<n, mln_trait_op_div(T, S)>
518 vec<n, mln_trait_op_div(T, S)> tmp;
519 for (
unsigned i = 0; i < n; ++i)
525 template <
unsigned n,
typename T>
528 operator<<(std::ostream& ostr, const vec<n,T>& v)
531 for (
unsigned i = 0; i < n; ++i)
538 template <
typename T,
typename U>
540 vec<3, mln_trait_op_times(T,U)>
541 vprod(
const vec<3, T>& lhs,
const vec<3, U>& rhs)
543 vec<3, mln_trait_op_times(T,U)> tmp;
544 tmp[0] = lhs[1] * rhs[2] - lhs[2] * rhs[1];
545 tmp[1] = lhs[2] * rhs[0] - lhs[0] * rhs[2];
546 tmp[2] = lhs[0] * rhs[1] - lhs[1] * rhs[0];
550 # endif // MLN_INCLUDE_ONLY
557 # include <mln/make/vec.hh>
560 #endif // ! MLN_METAL_VEC_HH