FougTools  0.7.0dev-046fb6a
Handy tools for C++, Qt and OpenCascade
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
manhattan_norm.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** FougTools
3 ** Copyright Fougue (30 Mar. 2015)
4 ** contact@fougue.pro
5 **
6 ** This software is a computer program whose purpose is to provide utility
7 ** tools for the C++ language and the Qt toolkit.
8 **
9 ** This software is governed by the CeCILL-C license under French law and
10 ** abiding by the rules of distribution of free software. You can use,
11 ** modify and/ or redistribute the software under the terms of the CeCILL-C
12 ** license as circulated by CEA, CNRS and INRIA at the following URL
13 ** "http://www.cecill.info".
14 ****************************************************************************/
15 
16 #ifndef MATHTOOLS_MANHATTAN_NORM_H
17 #define MATHTOOLS_MANHATTAN_NORM_H
18 
19 #include "norm.h"
20 #include <cmath>
21 
22 namespace math {
23 namespace internal {
24 
25 template<typename COORD_ITERATOR>
26 auto ManhattanFunc_value(COORD_ITERATOR begin, COORD_ITERATOR end) -> decltype(typeHelper(*begin))
27 {
28  decltype(typeHelper(*begin)) result = 0;
29  while (begin != end) {
30  result += std::fabs(*begin);
31  ++begin;
32  }
33  return result;
34 }
35 
36 template<std::size_t N, typename COORD_TYPE>
37 struct ManhattanFuncArity
38 {
39  static typename NumTraits<COORD_TYPE>::Real value(const COORD_TYPE* coordPtr)
40  {
41  return ManhattanFuncArity<N - 1, COORD_TYPE>::value(coordPtr) + std::fabs(*(coordPtr + N - 1));
42  }
43 };
44 
45 template<typename COORD_TYPE>
46 struct ManhattanFuncArity<1, COORD_TYPE>
47 {
48  static typename NumTraits<COORD_TYPE>::Real value(const COORD_TYPE* coordPtr)
49  { return std::fabs(*coordPtr); }
50 };
51 
52 struct ManhattanFunc
53 {
54  template<typename COORD_ITERATOR>
55  static auto fromRange(COORD_ITERATOR begin, COORD_ITERATOR end) -> decltype(typeHelper(*begin))
56  { return ManhattanFunc_value(begin, end); }
57 
58  template<std::size_t N, typename COORD_TYPE>
59  static typename NumTraits<COORD_TYPE>::Real fromPtr(const COORD_TYPE* coordPtr)
60  { return ManhattanFuncArity<N, COORD_TYPE>::value(coordPtr); }
61 };
62 
63 template<> struct NormTraits<internal::ManhattanFunc>
64 {
65  typedef ArityNormSpecializationTag NormCategory;
66 };
67 
68 } // namespace internal
69 
78 
79 } // namespace math
80 
81 #endif // MATHTOOLS_NORMS_MANHATTAN_H
Computation of norms in K-vector space.
Definition: norm.h:60
Norm< internal::ManhattanFunc > ManhattanNorm
Provides computation of the manhattan(or taxicab) norm
Definition: manhattan_norm.h:77
Definition: consts.h:18