FougTools  0.7.0dev-046fb6a
Handy tools for C++, Qt and OpenCascade
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sqr_euclidean_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_SQR_EUCLIDEAN_NORM_H
17 #define MATHTOOLS_SQR_EUCLIDEAN_NORM_H
18 
19 #include "norm.h"
20 
21 namespace math {
22 namespace internal {
23 
24 template<typename COORD_ITERATOR>
25 auto SqrEuclideanFunc_value(COORD_ITERATOR begin, COORD_ITERATOR end) -> decltype(typeHelper(*begin))
26 {
27  decltype(typeHelper(*begin)) result = 0;
28  while (begin != end) {
29  result += (*begin) * (*begin);
30  ++begin;
31  }
32  return result;
33 }
34 
35 template<std::size_t N, typename COORD_TYPE>
36 struct SqrEuclideanFuncArity
37 {
38  static typename NumTraits<COORD_TYPE>::Real value(const COORD_TYPE* coordPtr)
39  {
40  const COORD_TYPE& coord = *(coordPtr + N - 1);
41  return SqrEuclideanFuncArity<N - 1, COORD_TYPE>::value(coordPtr) + coord*coord;
42  }
43 };
44 
45 template<typename COORD_TYPE>
46 struct SqrEuclideanFuncArity<1, COORD_TYPE>
47 {
48  static typename NumTraits<COORD_TYPE>::Real value(const COORD_TYPE* coordPtr)
49  { return (*coordPtr) * (*coordPtr); }
50 };
51 
52 struct SqrEuclideanFunc
53 {
54  template<typename COORD_ITERATOR>
55  static auto fromRange(COORD_ITERATOR begin, COORD_ITERATOR end) -> decltype(typeHelper(*begin))
56  { return SqrEuclideanFunc_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 SqrEuclideanFuncArity<N, COORD_TYPE>::value(coordPtr); }
61 };
62 
63 template<> struct NormTraits<internal::SqrEuclideanFunc>
64 {
65  typedef ArityNormSpecializationTag NormCategory;
66 };
67 
68 } // namespace internal
69 
76 
77 } // namespace math
78 
79 #endif // MATHTOOLS_NORMS_SQR_EUCLIDEAN_H
Computation of norms in K-vector space.
Definition: norm.h:60
Definition: consts.h:18
Norm< internal::SqrEuclideanFunc > SqrEuclideanNorm
Provides computation of the squared euclidean norm.
Definition: sqr_euclidean_norm.h:75