Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
linfty.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_LINFTY_HH
27 # define MLN_NORM_LINFTY_HH
28 
35 
36 # include <mln/math/abs.hh>
37 # include <mln/algebra/vec.hh>
38 
39 
40 namespace mln
41 {
42 
43  namespace norm
44  {
45 
48  template <unsigned n, typename C>
49  C linfty(const C (&vec)[n]);
50 
51  template <unsigned n, typename C>
52  C linfty(const algebra::vec<n,C>& vec);
54 
55 
58  template <unsigned n, typename C>
59  C linfty_distance(const C (&vec1)[n], const C (&vec2)[n]);
60 
61  template <unsigned n, typename C>
62  C linfty_distance(const algebra::vec<n,C>& vec1,
63  const algebra::vec<n,C>& vec2);
65 
66 
67 # ifndef MLN_INCLUDE_ONLY
68 
69  namespace impl
70  {
71 
72  template <unsigned n, typename C, typename V>
73  inline
74  C linfty_(const V& vec)
75  {
76  C m = 0;
77  for (unsigned i = 0; i < n; ++i)
78  {
79  // Compute the maximal component absolute value of the
80  // vector.
81  C mc = mln::math::abs(vec[i]);
82  if (mc > m)
83  m = mc;
84  }
85  return m;
86  }
87 
88  template <unsigned n, typename C, typename V>
89  inline
90  C linfty_distance_(const V& vec1, const V& vec2)
91  {
92  C d = 0;
93  for (unsigned i = 0; i < n; ++i)
94  {
95  // Compute the maximal absolute value of the distance
96  // between components of the same index.
97  C dc = mln::math::abs(vec1[i] - vec2[i]);
98  if (dc > d)
99  d = dc;
100  }
101  return d;
102  }
103 
104  } // end of namespace mln::norm::impl
105 
106 
107  /*----------.
108  | Facades. |
109  `----------*/
110 
111  template <unsigned n, typename C>
112  inline
113  C linfty(const C (&vec)[n])
114  {
115  return impl::linfty_<n, C>(vec);
116  }
117 
118  template <unsigned n, typename C>
119  inline
120  C linfty(const algebra::vec<n,C>& vec)
121  {
122  return impl::linfty_<n, C>(vec);
123  }
124 
125  template <unsigned n, typename C>
126  inline
127  C linfty_distance(const C (&vec1)[n], const C (&vec2)[n])
128  {
129  return impl::linfty_distance_<n, C>(vec1, vec2);
130  }
131 
132  template <unsigned n, typename C>
133  inline
134  C linfty_distance(const algebra::vec<n,C>& vec1,
135  const algebra::vec<n,C>& vec2)
136  {
137  return impl::linfty_distance_<n, C>(vec1, vec2);
138  }
139 
140 # endif // ! MLN_INCLUDE_ONLY
141 
142  } // end of namespace mln::norm
143 
144 } // end of namespace mln
145 
146 
147 #endif // ! MLN_NORM_LINFTY_HH