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