27 #ifndef MLN_ALGEBRA_VEC_HH
28 # define MLN_ALGEBRA_VEC_HH
37 # include <mln/core/concept/object.hh>
39 # include <mln/literal/zero.hh>
40 # include <mln/literal/origin.hh>
41 # include <mln/norm/l2.hh>
42 # include <mln/trait/all.hh>
43 # include <mln/trait/value_.hh>
44 # include <mln/fun/i2v/all_to.hh>
45 # include <mln/debug/format.hh>
47 # include <mln/value/ops.hh>
58 template <
unsigned n,
typename T>
class vec;
59 template <
unsigned d,
typename C>
class h_vec;
60 template <
unsigned n,
unsigned m,
typename T>
class mat;
68 template <
unsigned n,
typename C>
69 mln_sum_product(C,C) l2(
const algebra::vec<n,C>& vec);
76 template <
unsigned n,
typename T>
77 struct value_< mln::algebra::vec<n,T> >
79 typedef trait::value::nature::vectorial nature;
80 typedef trait::value::kind::data kind;
84 nbits = n * mln_nbits(T),
85 card = n * mln_card(T)
88 typedef mln_value_quant_from_(card) quant;
89 typedef algebra::vec<n, mln_sum(T)> sum;
92 template <
unsigned n, typename T>
93 struct set_precise_unary_< op::ord, mln::algebra::vec<n,T> >
95 typedef mln::internal::ord_vec< mln::algebra::vec<n,T> > ret;
108 template <
unsigned n,
typename T>
109 class vec_base_ :
public Object< vec<n,T> >
115 template <
typename T>
116 class vec_base_ <1, T> :
public Object< vec<1,T> >
119 void set(
const T& val0)
127 template <
typename T>
128 class vec_base_ <2, T> :
public Object< vec<2,T> >
131 void set(
const T& val0,
const T& val1)
140 template <
typename T>
141 class vec_base_ <3, T> :
public Object< vec<3,T> >
144 void set(
const T& val0,
const T& val1,
const T& val2)
154 template <
typename T>
155 class vec_base_ <4, T> :
public Object< vec<4,T> >
158 void set(
const T& val0,
const T& val1,
const T& val2,
const T& val3)
174 template <
unsigned n,
typename T>
175 class vec :
public internal::vec_base_<n, T>
177 typedef internal::vec_base_<n, T> super_;
193 vec(
const literal::zero_t&);
194 vec& operator=(
const literal::zero_t&);
198 vec(
const literal::origin_t&);
199 vec& operator=(
const literal::origin_t&);
202 vec(
const vec<n, T>& rhs);
204 template <
typename U>
205 vec(
const vec<n, U>& rhs);
207 template <
typename U>
208 vec& operator=(
const vec<n, U>& rhs);
212 algebra::h_vec<n, T> to_h_vec()
const;
215 const T& operator[](
unsigned i)
const;
217 T& operator[](
unsigned i);
219 void set_all(
const T& val);
221 unsigned size()
const;
235 const vec<n, T>& normalize();
238 mat<1, n, T> t()
const;
241 template <
typename F>
242 vec(
const Function_v2v<F>& f);
245 static const vec<n, T>
zero;
248 static const vec<n, T>
origin;
252 template <
typename U>
253 operator mat<n, 1, U>()
const;
256 template <
typename U>
257 vec(
const mat<n, 1, U>& rhs);
260 template <
typename U>
261 vec& operator=(
const mat<n, 1, U>& rhs);
274 template <
template <
class>
class Name,
275 unsigned n,
typename T >
276 struct set_precise_unary_< Name, algebra::
vec<n, T> >
278 typedef mln_trait_unary(Name, T) V;
279 typedef algebra::vec<n, V> ret;
288 template <
unsigned n, typename T,
290 struct set_precise_binary_< op::plus,
291 algebra::vec<n, T>, algebra::vec<n, U> >
293 typedef mln_trait_op_plus(T, U) V;
294 typedef algebra::vec<n, V> ret;
299 template <
unsigned n, typename T >
300 struct set_precise_unary_< op::uminus,
303 typedef mln_trait_op_uminus(T) V;
304 typedef algebra::vec<n, V> ret;
309 template <
unsigned n, typename T,
311 struct set_precise_binary_< op::minus,
312 algebra::vec<n, T>, algebra::vec<n, U> >
314 typedef mln_trait_op_minus(T, U) V;
315 typedef algebra::vec<n, V> ret;
320 template <
unsigned n, typename T,
322 struct set_precise_binary_< op::times,
323 algebra::vec<n, T>, algebra::vec<n, U> >
325 typedef mln_sum_product(T,U) ret;
330 template <
unsigned n, typename T,
332 struct set_precise_binary_< op::times,
333 algebra::vec<n, T>, mln::value::scalar_<S> >
335 typedef mln_trait_op_times(T, S) V;
336 typedef algebra::vec<n, V> ret;
341 template <
unsigned n, typename T,
343 struct set_precise_binary_< op::div,
344 algebra::vec<n, T>, mln::value::scalar_<S> >
346 typedef mln_trait_op_div(T, S) V;
347 typedef algebra::vec<n, V> ret;
370 template <
unsigned n,
typename T,
typename U>
372 operator==(
const vec<n,T>& lhs,
const vec<n,U>& rhs);
376 template <
unsigned n,
typename T,
typename U>
377 vec<n, mln_trait_op_plus(T,U)>
378 operator+(
const vec<n,T>& lhs,
const vec<n,U>& rhs);
382 template <
unsigned n,
typename T>
383 vec<n, mln_trait_op_uminus(T)>
384 operator-(
const vec<n,T>& rhs);
388 template <
unsigned n,
typename T,
typename U>
389 vec<n, mln_trait_op_minus(T,U)>
390 operator-(
const vec<n,T>& lhs,
const vec<n,U>& rhs);
395 template <
unsigned n,
typename T,
typename U>
397 operator*(const vec<n,T>& lhs, const vec<n,U>& rhs);
401 template <
unsigned n, typename T, typename S>
402 vec<n, mln_trait_op_times(T, S)>
403 operator*(const vec<n,T>& lhs, const mln::value::scalar_<S>& s);
407 template <
unsigned n, typename T, typename S>
408 vec<n, mln_trait_op_div(T, S)>
409 operator/(const vec<n,T>& lhs, const mln::value::scalar_<S>& s);
413 template <
unsigned n, typename T>
415 operator<<(std::ostream& ostr, const vec<n,T>& v);
419 template <
unsigned n, typename T>
421 operator>>(std::istream& istr, vec<n,T>& v);
426 template <typename T, typename U>
427 vec<3, mln_trait_op_times(T,U)>
428 vprod(const vec<3, T>& lhs, const vec<3, U>& rhs);
435 # ifndef MLN_INCLUDE_ONLY
440 template <
unsigned n,
typename T>
446 template <
unsigned n,
typename T>
448 vec<n,T>::vec(
const literal::zero_t&)
453 template <
unsigned n,
typename T>
456 vec<n,T>::operator=(
const literal::zero_t&)
462 template <
unsigned n,
typename T>
464 vec<n,T>::vec(
const literal::origin_t&)
469 template <
unsigned n,
typename T>
472 vec<n,T>::operator=(
const literal::origin_t&)
478 template <
unsigned n,
typename T>
480 vec<n,T>::vec(
const vec<n,T>& rhs)
483 for (
unsigned i = 0; i < n; ++i)
487 template <
unsigned n,
typename T>
488 template <
typename U>
490 vec<n,T>::vec(
const vec<n, U>& rhs)
493 mlc_converts_to(U, T)::check();
494 for (
unsigned i = 0; i < n; ++i)
495 data_[i] = static_cast<T>(rhs[i]);
498 template <
unsigned n,
typename T>
499 template <
typename U>
501 vec<n,T>& vec<n,T>::operator=(
const vec<n, U>& rhs)
503 mlc_converts_to(U, T)::check();
504 for (
unsigned i = 0; i < n; ++i)
505 data_[i] = static_cast<T>(rhs[i]);
509 template <
unsigned n,
typename T>
511 const T& vec<n,T>::operator[](
unsigned i)
const
513 mln_precondition(i < dim);
517 template <
unsigned n,
typename T>
519 T& vec<n,T>::operator[](
unsigned i)
521 mln_precondition(i < dim);
525 template <
unsigned n,
typename T>
527 void vec<n,T>::set_all(
const T& val)
529 for (
unsigned i = 0; i < n; ++i)
533 template <
unsigned n,
typename T>
535 unsigned vec<n,T>::size()
const
540 template <
unsigned n,
typename T>
542 const vec<n, T>& vec<n, T>::normalize()
544 mln_sum_product(T,T) l2_norm = norm::l2(*this);
545 mln_assertion(l2_norm > mln_sum_product(T,T)(0));
546 for (
unsigned i = 0; i < n; ++i)
547 data_[i] = static_cast<T>(data_[i] / l2_norm);
551 template <
unsigned n, typename T>
552 template <typename F>
554 vec<n, T>::vec(const Function_v2v<F>& f_)
556 mlc_converts_to(mln_result(F), T)::check();
557 const F& f = exact(f_);
558 for (
unsigned i = 0; i < n; ++i)
559 data_[i] = static_cast<T>(f(i));
563 template <
unsigned n,
typename T>
564 const vec<n, T> vec<n, T>::zero = all_to(0);
566 template <
unsigned n,
typename T>
567 const vec<n, T> vec<n, T>::origin = all_to(0);
576 template <
unsigned n,
typename T,
typename U>
578 bool operator==(
const vec<n,T>& lhs,
const vec<n,U>& rhs)
580 for (
unsigned i = 0; i < n; ++i)
581 if (lhs[i] != rhs[i])
588 template <
unsigned n,
typename T,
typename U>
590 vec<n, mln_trait_op_plus(T,U)>
591 operator+(
const vec<n,T>& lhs,
const vec<n,U>& rhs)
593 typedef mln_trait_op_plus(T,U) R;
595 for (
unsigned i = 0; i < n; ++i)
596 tmp[i] = lhs[i] + rhs[i];
602 template <
unsigned n, typename T>
604 vec<n, mln_trait_op_uminus(T)>
605 operator-(const vec<n,T>& rhs)
607 typedef mln_trait_op_uminus(T) R;
609 for (
unsigned i = 0; i < n; ++i)
616 template <
unsigned n, typename T, typename U>
618 vec<n, mln_trait_op_minus(T,U)>
619 operator-(const vec<n,T>& lhs, const vec<n,U>& rhs)
621 typedef mln_trait_op_minus(T,U) R;
623 for (
unsigned i = 0; i < n; ++i)
624 tmp[i] = lhs[i] - rhs[i];
630 template <
unsigned n, typename T, typename U>
633 operator*(const vec<n,T>& lhs, const vec<n,U>& rhs)
635 typedef mln_sum_product(T,U) R;
637 for (
unsigned i = 0; i < n; ++i)
638 tmp += lhs[i] * rhs[i];
644 template <
unsigned n,
typename T,
typename S>
646 vec<n, mln_trait_op_times(T, S)>
647 operator*(
const vec<n,T>& lhs,
const mln::value::scalar_<S>& s)
659 typedef mln_trait_op_times(T, S) R;
661 for (
unsigned i = 0; i < n; ++i)
662 tmp[i] = lhs[i] * s.to_equiv();
668 template <
unsigned n, typename T, typename S>
670 vec<n, mln_trait_op_div(T, S)>
671 operator/(const vec<n,T>& lhs, const mln::value::scalar_<S>& s)
674 typedef mln_trait_op_div(T, S) R;
676 for (
unsigned i = 0; i < n; ++i)
677 tmp[i] = lhs[i] / s.to_equiv();
684 template <
unsigned n, typename T>
687 operator<<(std::ostream& ostr, const vec<n,T>& v)
690 for (
unsigned i = 0; i < n; ++i)
698 template <
unsigned n,
typename T>
701 operator>>(std::istream& istr, vec<n,T>& v)
703 for (
unsigned i = 0; i < n; ++i)
711 template <
typename T,
typename U>
713 vec<3, mln_trait_op_times(T,U)>
714 vprod(
const vec<3, T>& lhs,
const vec<3, U>& rhs)
716 vec<3, mln_trait_op_times(T,U)> tmp;
717 tmp[0] = lhs[1] * rhs[2] - lhs[2] * rhs[1];
718 tmp[1] = lhs[2] * rhs[0] - lhs[0] * rhs[2];
719 tmp[2] = lhs[0] * rhs[1] - lhs[1] * rhs[0];
724 template <
typename P>
727 to_point(
const vec<P::dim,float>& v)
730 for (
unsigned i = 0; i < P::dim; ++i)
731 tmp[i] = round(v[i]);
739 # endif // MLN_INCLUDE_ONLY
744 # include <mln/make/vec.hh>
745 # include <mln/algebra/mat.hh>
748 #endif // ! MLN_ALGEBRA_VEC_HH