Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
l1.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_L1_HH
27 # define MLN_NORM_L1_HH
28 
33 
34 # include <mln/math/abs.hh>
35 # include <mln/algebra/vec.hh>
36 
37 
38 namespace mln
39 {
40 
41  namespace norm
42  {
43 
46  template <unsigned n, typename C>
47  mln_sum_product(C,C) l1(const C (&vec)[n]);
48 
49  template <unsigned n, typename C>
50  mln_sum_product(C,C) l1(const algebra::vec<n,C>& vec);
52 
55  template <unsigned n, typename C>
56  mln_sum_product(C,C) l1_distance(const C (&vec1)[n], const C (&vec2)[n]);
57 
58  template <unsigned n, typename C>
59  mln_sum_product(C,C) l1_distance(const algebra::vec<n,C>& vec1,
60  const algebra::vec<n,C>& vec2);
62 
63 
64 # ifndef MLN_INCLUDE_ONLY
65 
66  namespace impl
67  {
68  template <unsigned n, typename C, typename V>
69  inline
70  mln_sum_product(C,C)
71  l1_(const V& vec)
72  {
73  typedef mln_sum_product(C,C) M;
74  M m = 0;
75  for (unsigned i = 0; i < n; ++i)
76  {
77  M v_i = static_cast<M>(mln::math::abs(vec[i]));
78  m = static_cast<M>(m + v_i);
79  }
80  return m;
81  }
82 
83  template <unsigned n, typename C, typename V>
84  inline
85  mln_sum_product(C,C)
86  l1_distance_(const V& vec1, const V& vec2)
87  {
88  typedef mln_sum_product(C,C) D;
89  D d = 0;
90  for (unsigned i = 0; i < n; ++i)
91  {
92  D v1_v2 = static_cast<D>(mln::math::abs(vec1[i] - vec2[i]));
93  d = static_cast<D>(d + v1_v2);
94  }
95  return d;
96  }
97 
98  } // end of namespace mln::norm::impl
99 
100 
101  /*----------.
102  | Facades. |
103  `----------*/
104 
105  template <unsigned n, typename C>
106  inline
107  mln_sum_product(C,C)
108  l1(const C (&vec)[n])
109  {
110  return impl::l1_<n, C>(vec);
111  }
112 
113  template <unsigned n, typename C>
114  inline
115  mln_sum_product(C,C)
116  l1(const algebra::vec<n,C>& vec)
117  {
118  return impl::l1_<n, C>(vec);
119  }
120 
121  template <unsigned n, typename C>
122  inline
123  mln_sum_product(C,C)
124  l1_distance(const C (&vec1)[n], const C (&vec2)[n])
125  {
126  return impl::l1_distance_<n, C>(vec1, vec2);
127  }
128 
129  template <unsigned n, typename C>
130  inline
131  mln_sum_product(C,C)
132  l1_distance(const algebra::vec<n,C>& vec1, const algebra::vec<n,C>& vec2)
133  {
134  return impl::l1_distance_<n, C>(vec1, vec2);
135  }
136 
137 # endif // ! MLN_INCLUDE_ONLY
138 
139  } // end of namespace mln::norm
140 
141 } // end of namespace mln
142 
143 
144 #endif // ! MLN_NORM_L1_HH