00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef OLENA_MATH_MACROS_HH
00029 # define OLENA_MATH_MACROS_HH
00030
00031 # include <ntg/basics.hh>
00032 # include <oln/basics.hh>
00033
00034 namespace oln {
00035
00045 namespace math {
00046
00048 template<class T>
00049 struct f_sqr
00050 {
00051 typedef T output_t;
00052 const T operator()(const T& val) const
00053 {
00054 return val * val;
00055 }
00056 };
00057
00059 template<class T>
00060 const T sqr(const T& val)
00061 {
00062 f_sqr<T> f;
00063 return f(val);
00064 }
00065
00067 template<class T>
00068 struct f_abs
00069 {
00070 typedef T output_t;
00071
00072 const T
00073 operator()(const T& val) const
00074 {
00075 return ::abs(val);
00076 }
00077 };
00078
00080 template<class T>
00081 const T
00082 abs(const T& val)
00083 {
00084 f_abs<T> f;
00085 return f(val);
00086 }
00087
00089 namespace internal {
00091 template<typename DestValue, typename I, typename J>
00092 struct f_dot_product_nv
00093 {
00094 typedef typename ntg_is_a(I, ntg::non_vectorial)::ensure_type t;
00095
00096 static DestValue
00097 product(const I &i, const J &j)
00098 {
00099 return i * j;
00100 }
00101 };
00102
00104 template<typename DestValue, typename I, typename J>
00105 struct f_dot_product_v
00106 {
00107 typedef typename ntg_is_a(I, ntg::vectorial)::ensure_type t;
00108
00109
00110 static DestValue
00111 product(const I &i, const J &j)
00112 {
00113 DestValue d(ntg_zero_val(DestValue));
00114
00115 for (unsigned k = 0; k < ntg_nb_comp(I); ++k)
00116 d += i[k] * j[k];
00117 return d;
00118 }
00119 };
00120 }
00121
00140 template<typename DestValue, typename I, typename J>
00141 DestValue
00142 dot_product(const I &i, const J &j)
00143 {
00144 typedef typename mlc::if_<ntg_is_a(I, ntg::vectorial)::ret,
00145 internal::f_dot_product_v<DestValue, I, J>,
00146 internal::f_dot_product_nv<DestValue, I, J> >::ret fctor;
00147 return fctor::product(i, j);
00148 }
00149
00150 }
00151
00152 }
00153
00154 #endif // ! OLENA_MATH_MACROS_HH