Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
l2.hh
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_NORM_L2_HH
27 # define MLN_NORM_L2_HH
28 
34 
35 # include <mln/math/sqr.hh>
36 # include <mln/math/sqrt.hh>
37 # include <mln/algebra/vec.hh>
38 # include <mln/value/ops.hh>
39 
40 
41 namespace mln
42 {
43 
44  // Forward declaration.
45  namespace algebra
46  {
47  template <unsigned n, typename T> class vec;
48  }
49 
50 
51  namespace norm
52  {
53 
56  template <unsigned n, typename C>
57  mln_sum_product(C,C) l2(const C (&vec)[n]);
58 
59  template <unsigned n, typename C>
60  mln_sum_product(C,C) l2(const algebra::vec<n,C>& vec);
62 
65  template <unsigned n, typename C>
66  mln_sum_product(C,C) sqr_l2(const C (&vec)[n]);
67 
68  template <unsigned n, typename C>
69  mln_sum_product(C,C) sqr_l2(const algebra::vec<n,C>& vec);
71 
74  template <unsigned n, typename C>
75  mln_sum_product(C,C) l2_distance(const C (&vec1)[n], const C (&vec2)[n]);
76 
77  template <unsigned n, typename C>
78  mln_sum_product(C,C) l2_distance(const algebra::vec<n,C>& vec1,
79  const algebra::vec<n,C>& vec2);
81 
82 
83 # ifndef MLN_INCLUDE_ONLY
84 
85  namespace impl
86  {
87 
88  template <unsigned n, typename C, typename V>
89  inline
90  mln_sum_product(C,C)
91  l2_(const V& vec)
92  {
93  typedef mln_sum_product(C,C) M;
94  M m = 0;
95  for (unsigned i = 0; i < n; ++i)
96  {
97  M sqr_v_i = static_cast<M>(mln::math::sqr(vec[i]));
98  m = static_cast<M>(m + sqr_v_i);
99  }
100  return mln::math::sqrt(m);
101  }
102 
103  template <unsigned n, typename C, typename V>
104  inline
105  mln_sum_product(C,C)
106  sqr_l2_(const V& vec)
107  {
108  mln_sum_product(C,C) m = 0;
109  for (unsigned i = 0; i < n; ++i)
110  m += mln::math::sqr(vec[i]);
111  return m;
112  }
113 
114  template <unsigned n, typename C, typename V>
115  inline
116  mln_sum_product(C,C)
117  l2_distance_(const V& vec1, const V& vec2)
118  {
119  typedef mln_sum_product(C,C) D;
120  D d = 0;
121  for (unsigned i = 0; i < n; ++i)
122  {
123  D sqr_v1_v2 = static_cast<D>(mln::math::sqr(vec1[i] - vec2[i]));
124  d = static_cast<D>(d + sqr_v1_v2);
125  }
126  return mln::math::sqrt(d);
127  }
128 
129  } // end of namespace mln::norm::impl
130 
131 
132  /*----------.
133  | Facades. |
134  `----------*/
135 
136  template <unsigned n, typename C>
137  inline
138  mln_sum_product(C,C)
139  l2(const C (&vec)[n])
140  {
141  return impl::l2_<n, C>(vec);
142  }
143 
144  template <unsigned n, typename C>
145  inline
146  mln_sum_product(C,C)
147  l2(const algebra::vec<n,C>& vec)
148  {
149  return impl::l2_<n, C>(vec);
150  }
151 
152 
153  template <unsigned n, typename C>
154  inline
155  mln_sum_product(C,C)
156  sqr_l2(const C (&vec)[n])
157  {
158  return impl::sqr_l2_<n, C>(vec);
159  }
160 
161  template <unsigned n, typename C>
162  inline
163  mln_sum_product(C,C)
164  sqr_l2(const algebra::vec<n,C>& vec)
165  {
166  return impl::sqr_l2_<n, C>(vec);
167  }
168 
169 
170  template <unsigned n, typename C>
171  inline
172  mln_sum_product(C,C)
173  l2_distance(const C (&vec1)[n], const C (&vec2)[n])
174  {
175  return impl::l2_distance_<n, C>(vec1, vec2);
176  }
177 
178  template <unsigned n, typename C>
179  inline
180  mln_sum_product(C,C)
181  l2_distance(const algebra::vec<n,C>& vec1, const algebra::vec<n,C>& vec2)
182  {
183  return impl::l2_distance_<n, C>(vec1, vec2);
184  }
185 
186 # endif // ! MLN_INCLUDE_ONLY
187 
188  } // end of namespace mln::norm
189 
190 } // end of namespace mln
191 
192 
193 #endif // ! MLN_NORM_L2_HH