SLIP  1.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
norms.hpp
Go to the documentation of this file.
1 /*
2  * Copyright(c):
3  * Signal Image and Communications (SIC) Department
4  * http://www.sic.sp2mi.univ-poitiers.fr/
5  * - University of Poitiers, France http://www.univ-poitiers.fr
6  * - XLIM Institute UMR CNRS 7252 http://www.xlim.fr/
7  *
8  * and
9  *
10  * D2 Fluid, Thermic and Combustion
11  * - University of Poitiers, France http://www.univ-poitiers.fr
12  * - PPRIME Institute - UPR CNRS 3346 http://www.pprime.fr
13  * - ISAE-ENSMA http://www.ensma.fr
14  *
15  * Contributor(s):
16  * The SLIP team,
17  * Benoit Tremblais <tremblais_AT_sic.univ-poitiers.fr>,
18  * Laurent David <laurent.david_AT_lea.univ-poitiers.fr>,
19  * Ludovic Chatellier <ludovic.chatellier_AT_univ-poitiers.fr>,
20  * Lionel Thomas <lionel.thomas_AT_univ-poitiers.fr>,
21  * Denis Arrivault <arrivault_AT_sic.univ-poitiers.fr>,
22  * Julien Dombre <julien.dombre_AT_univ-poitiers.fr>.
23  *
24  * Description:
25  * The Simple Library of Image Processing (SLIP) is a new image processing
26  * library. It is written in the C++ language following as much as possible
27  * the ISO/ANSI C++ standard. It is consequently compatible with any system
28  * satisfying the ANSI C++ complience. It works on different Unix , Linux ,
29  * Mircrosoft Windows and Mac OS X plateforms. SLIP is a research library that
30  * was created by the Signal, Image and Communications (SIC) departement of
31  * the XLIM, UMR 7252 CNRS Institute in collaboration with the Fluids, Thermic
32  * and Combustion departement of the P', UPR 3346 CNRS Institute of the
33  * University of Poitiers.
34  *
35  * The SLIP Library source code has been registered to the APP (French Agency
36  * for the Protection of Programs) by the University of Poitiers and CNRS,
37  * under registration number IDDN.FR.001.300034.000.S.P.2010.000.21000.
38 
39  * http://www.sic.sp2mi.univ-poitiers.fr/slip/
40  *
41  * This software is governed by the CeCILL-C license under French law and
42  * abiding by the rules of distribution of free software. You can use,
43  * modify and/ or redistribute the software under the terms of the CeCILL-C
44  * license as circulated by CEA, CNRS and INRIA at the following URL
45  * http://www.cecill.info.
46  * As a counterpart to the access to the source code and rights to copy,
47  * modify and redistribute granted by the license, users are provided only
48  * with a limited warranty and the software's author, the holder of the
49  * economic rights, and the successive licensors have only limited
50  * liability.
51  *
52  * In this respect, the user's attention is drawn to the risks associated
53  * with loading, using, modifying and/or developing or reproducing the
54  * software by the user in light of its specific status of free software,
55  * that may mean that it is complicated to manipulate, and that also
56  * therefore means that it is reserved for developers and experienced
57  * professionals having in-depth computer knowledge. Users are therefore
58  * encouraged to load and test the software's suitability as regards their
59  * requirements in conditions enabling the security of their systems and/or
60  * data to be ensured and, more generally, to use and operate it in the
61  * same conditions as regards security.
62  *
63  * The fact that you are presently reading this means that you have had
64  * knowledge of the CeCILL-C license and that you accept its terms.
65  */
66 
67 
68 
75 #ifndef SLIP_NORMS_HPP
76 #define SLIP_NORMS_HPP
77 
78 
79 #include <algorithm>
80 #include <numeric>
81 #include <cassert>
82 #include <cmath>
83 #include "complex_cast.hpp"
84 
85 namespace slip
86 {
87 
110  template<typename Value_T, typename InputIterator>
111  inline
112  Value_T L22_norm(InputIterator first,
113  InputIterator last)
114  {
115  assert(first != last);
116  return std::inner_product(first,last,first,Value_T(0));
117  }
118 
119 
120 
121 
147 template<typename Value_T, typename InputIterator,typename MaskIterator>
148  inline
149  Value_T L22_norm_mask(InputIterator first,
150  InputIterator last,
151  MaskIterator mask_first,
152  typename std::iterator_traits<MaskIterator>::value_type value=typename
153  std::iterator_traits<MaskIterator>::value_type(1))
154 {
155 assert(first != last);
156 
157  Value_T __init = Value_T();
158  for (; first != last; ++first,++mask_first)
159  {
160  if(*mask_first == value)
161  {
162  __init +=(*first)*(*first);
163  }
164  }
165  return __init;
166 }
167 
194 template<typename Value_T,
195  typename InputIterator,
196  typename Predicate>
197 Value_T L22_norm_if(InputIterator first,
198  InputIterator last,
199  Predicate pred)
200 {
201 assert(first != last);
202 
203  Value_T __init = Value_T();
204  for (; first != last; ++first)
205  {
206  if(pred(*first))
207  {
208  __init +=(*first)*(*first);
209  }
210  }
211  return __init;
212 }
213 
214 
240  template<typename Value_T, typename InputIterator>
241  inline
242  Value_T L2_norm(InputIterator first,
243  InputIterator last)
244  {
245  return std::sqrt(slip::L22_norm<Value_T>(first,last));
246  }
247 
272 template<typename Value_T, typename InputIterator,typename MaskIterator>
273  inline
274  Value_T L2_norm_mask(InputIterator first,
275  InputIterator last,
276  MaskIterator mask_first,
277  typename std::iterator_traits<MaskIterator>::value_type value=typename
278  std::iterator_traits<MaskIterator>::value_type(1))
279 {
280 return std::sqrt(slip::L22_norm_mask<Value_T>(first,last,mask_first,value));
281 }
282 
283 
309 template<typename Value_T,
310  typename InputIterator,
311  typename Predicate>
312 Value_T L2_norm_if(InputIterator first,
313  InputIterator last,
314  Predicate pred)
315 
316 {
317 return std::sqrt(slip::L22_norm_if<Value_T>(first,last,pred));
318 }
319 
345  template<typename Value_T, typename InputIterator>
346  inline
347  Value_T Euclidean_norm(InputIterator first,
348  InputIterator last)
349  {
350  return slip::L2_norm<Value_T>(first,last);
351  }
352 
378  template<typename Value_T, typename InputIterator,typename MaskIterator>
379  inline
380  Value_T Euclidean_norm_mask(InputIterator first,
381  InputIterator last,
382  MaskIterator mask_first,
383  typename std::iterator_traits<MaskIterator>::value_type value=typename
384  std::iterator_traits<MaskIterator>::value_type(1))
385  {
386  return slip::L2_norm_mask<Value_T>(first,last,mask_first,value);
387  }
388 
389 
415 template<typename Value_T, typename InputIterator, typename Predicate>
416  inline
417  Value_T Euclidean_norm_if(InputIterator first,
418  InputIterator last,
419  Predicate pred)
420  {
421  return slip::L2_norm_if<Value_T>(first,last,pred);
422 
423  }
424 
444  template<typename Value_T, typename InputIterator>
445  inline
446  Value_T L1_norm(InputIterator first,
447  InputIterator last)
448  {
449  assert(first != last);
450 
451  Value_T __init = Value_T(0);
452  for (; first != last; ++first)
453  __init = __init + std::abs(*first);
454  return __init;
455  }
481 template<typename Value_T, typename InputIterator,typename MaskIterator>
482  inline
483  Value_T L1_norm_mask(InputIterator first,
484  InputIterator last,
485  MaskIterator mask_first,
486  typename std::iterator_traits<MaskIterator>::value_type value=typename
487  std::iterator_traits<MaskIterator>::value_type(1))
488 {
489  assert(first != last);
490 
491  Value_T __init = Value_T(0);
492  for (; first != last; ++first,++mask_first)
493  {
494  if(*mask_first == value)
495  {
496  __init = __init + std::abs(*first);
497  }
498  }
499  return __init;
500  }
501 
527 template<typename Value_T,
528  typename InputIterator,
529  typename Predicate>
530 Value_T L1_norm_if(InputIterator first,
531  InputIterator last,
532  Predicate pred)
533 {
534 assert(first != last);
535 
536  Value_T __init = Value_T(0);
537  for (; first != last; ++first)
538  {
539  if(pred(*first))
540  {
541  __init = __init + std::abs(*first);
542  }
543  }
544  return __init;
545 }
546 
566  template<typename Value_T, typename InputIterator>
567  inline
568  Value_T infinite_norm(InputIterator first,
569  InputIterator last)
570  {
571 
572  if(first == last)
573  return std::abs(*first);
574 
575  InputIterator result = first;
576 
577  while (++first != last)
578  {
579 
580  if (std::abs(*result) < std::abs(*first))
581  result = first;
582  }
583  return std::abs(*result);
584 
585  }
586 
612 template<typename Value_T, typename InputIterator,typename MaskIterator>
613  inline
614  Value_T infinite_norm_mask(InputIterator first,
615  InputIterator last,
616  MaskIterator mask_first,
617  typename std::iterator_traits<MaskIterator>::value_type value=typename
618  std::iterator_traits<MaskIterator>::value_type(1))
619 
620 {
621  if(first == last)
622  return std::abs(*first);
623 
624  InputIterator result = first;
625  ++mask_first;
626 
627  while (++first != last)
628  {
629  if(*mask_first==value)
630  {
631  if (std::abs(*result) < std::abs(*first))
632  {
633  result = first;
634  }
635 
636  }
637  ++mask_first;
638  }
639  return std::abs(*result);
640 
641  }
642 
643 
669 template<typename Value_T,
670  typename InputIterator,
671  typename Predicate>
672 Value_T infinite_norm_if(InputIterator first,
673  InputIterator last,
674  Predicate pred)
675 
676 {
677  if(first == last)
678  return std::abs(*first);
679 
680  InputIterator result = first;
681 
682 
683  while (++first != last)
684  {
685  if(pred(*first))
686  {
687  if (std::abs(*result) < std::abs(*first))
688  {
689  result = first;
690  }
691 
692  }
693 
694  }
695  return std::abs(*result);
696 }
697 
698 
699 
700 }//slip::
701 
702 
703 #endif //SLIP_NORMS_HPP
Value_T Euclidean_norm(InputIterator first, InputIterator last)
Computes the Euclidean norm of a range.
Definition: norms.hpp:347
Value_T Euclidean_norm_if(InputIterator first, InputIterator last, Predicate pred)
Computes the Euclidean norm a range according to a Predicate.
Definition: norms.hpp:417
Value_T L22_norm(InputIterator first, InputIterator last)
Computes the L22 norm ( ) of a range.
Definition: norms.hpp:112
Value_T L22_norm_mask(InputIterator first, InputIterator last, MaskIterator mask_first, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the L22 norm of a range according to a mask sequence.
Definition: norms.hpp:149
Value_T L22_norm_if(InputIterator first, InputIterator last, Predicate pred)
Computes the L22 norm a range according to a Predicate.
Definition: norms.hpp:197
Value_T infinite_norm_if(InputIterator first, InputIterator last, Predicate pred)
Computes the infinite norm a range according to a Predicate.
Definition: norms.hpp:672
HyperVolume< T > abs(const HyperVolume< T > &M)
Value_T L1_norm_mask(InputIterator first, InputIterator last, MaskIterator mask_first, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the L1 norm of a range according to a mask sequence.
Definition: norms.hpp:483
Value_T L2_norm_mask(InputIterator first, InputIterator last, MaskIterator mask_first, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the Euclidean norm of a range according to a mask sequence.
Definition: norms.hpp:274
Value_T Euclidean_norm_mask(InputIterator first, InputIterator last, MaskIterator mask_first, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the Euclidean norm of a range according to a mask sequence.
Definition: norms.hpp:380
Value_T infinite_norm(InputIterator first, InputIterator last)
Computes the infinite norm of a range.
Definition: norms.hpp:568
Value_T L1_norm(InputIterator first, InputIterator last)
Computes the L1 norm of a range.
Definition: norms.hpp:446
Provides some macros which are used for using complex as real.
HyperVolume< T > sqrt(const HyperVolume< T > &M)
Value_T infinite_norm_mask(InputIterator first, InputIterator last, MaskIterator mask_first, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the infinite norm of a range according to a mask sequence.
Definition: norms.hpp:614
Value_T L1_norm_if(InputIterator first, InputIterator last, Predicate pred)
Computes the L1 norm a range according to a Predicate.
Definition: norms.hpp:530
std::iterator_traits< RandomAccessIterator1 >::value_type inner_product(RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, typename std::iterator_traits< RandomAccessIterator1 >::value_type init=typename std::iterator_traits< RandomAccessIterator1 >::value_type())
Computes the inner_product of two ranges X and Y: .
Value_T L2_norm_if(InputIterator first, InputIterator last, Predicate pred)
Computes the Euclidean norm a range according to a Predicate.
Definition: norms.hpp:312
Value_T L2_norm(InputIterator first, InputIterator last)
Computes the L2 norm of a range.
Definition: norms.hpp:242