SigUtil  0.95
Utility modules for modern C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
norm.hpp
Go to the documentation of this file.
1 /*
2 Copyright© 2014 Akihiro Nishimura
3 
4 This software is released under the MIT License.
5 http://opensource.org/licenses/mit-license.php
6 */
7 
8 #ifndef SIG_UTIL_NORM_HPP
9 #define SIG_UTIL_NORM_HPP
10 
11 #include "comparable_check.hpp"
12 #include "../calculation/binary_operation.hpp"
13 
14 #include <numeric>
15 #undef max
16 #undef min
17 
18 namespace sig
19 {
21 
24 template <size_t P>
25 struct Norm
26 {
27  template <class C>
28  double operator()(C const& vec) const
29  {
30  using T = typename impl::container_traits<C>::value_type;
31 
32  return std::pow(
33  std::accumulate(std::begin(vec), std::end(vec), static_cast<T>(0), [&](T sum, T val){ return sum + std::pow(std::abs(val), P); }),
34  1.0 / P
35  );
36  }
37 
38  template <class C1, class C2>
39  double operator()(C1 const& vec1, C2 const& vec2) const
40  {
41  assert(is_comparable(vec1, vec2, impl::NumericVectorTag()));
42  return this->operator()(minus(vec1, vec2));
43  }
44 };
45 
47 
64 
66 
83 
84 
86 struct MaxNorm
87 {
88  template <class C>
89  double operator()(C const& vec) const
90  {
91  using T = typename impl::container_traits<C>::value_type;
92 
93  T max = *std::begin(vec);
94  for(auto e : vec){
95  if(std::abs(e) > max) max = std::abs(e);
96  }
97  return max;
98  }
99 
100  template <class C1, class C2>
101  double operator()(C1 const& vec1, C2 const& vec2) const
102  {
103  assert(is_comparable(vec1, vec2, impl::NumericVectorTag()));
104  return this->operator()(minus(vec1, vec2));
105  }
106 };
107 
109 
126 }
127 #endif
const Norm< 2 > norm_L2
L2ノルムを求める関数(関数オブジェクト)
Definition: norm.hpp:82
auto end(C &&c) -> std::move_iterator< typename RC::iterator >
bool is_comparable(C1 const &v1, C2 const &v2, impl::NumericVectorTag)
2つのベクトルが比較可能か確認
const Norm< 1 > norm_L1
L1ノルムを求める関数(関数オブジェクト)
Definition: norm.hpp:63
double operator()(C const &vec) const
Definition: norm.hpp:89
double operator()(C1 const &vec1, C2 const &vec2) const
Definition: norm.hpp:39
最大ノルム
Definition: norm.hpp:86
auto minus(T1 &&v1, T2 &&v2) -> decltype(binary_operation(minus_t(), std::forward< T1 >(v1), std::forward< T2 >(v2)))
double operator()(C1 const &vec1, C2 const &vec2) const
Definition: norm.hpp:101
auto max(T v) -> T
const MaxNorm norm_max
最大ノルムを求める関数(関数オブジェクト)
Definition: norm.hpp:125
Pノルム
Definition: norm.hpp:25
double operator()(C const &vec) const
Definition: norm.hpp:28
Definition: array.hpp:15
auto sum(C const &data) -> typename impl::SameIf< R, void, typename impl::container_traits< C >::value_type, R >::type
総和
auto begin(C &&c) -> std::move_iterator< typename RC::iterator >