Auxiliary Arithmetic Algorithms
maximum_space.hpp
1 #pragma once
2 
3 #include <cmath>
4 #include <numeric>
5 
6 #include "traits.hpp"
7 
8 namespace aaa {
9 namespace maximum {
10 
31 template<typename InputIterator, typename T = value_type_i<InputIterator>>
32 T norm(InputIterator first, InputIterator last, T init = T{})
33 {
34  const auto max_abs = [](const T left, const T right) -> T
35  {
36  return std::max(left, std::abs(right));
37  };
38  return std::accumulate(first, last, init, max_abs);
39 }
40 
44 template<typename Container, typename T = value_type<Container>>
45 T norm(const Container& a, T init = T{})
46 {
47  using std::begin;
48  using std::end;
49  return norm(begin(a), end(a), init);
50 }
51 
55 template<typename InputIterator, typename T = value_type_i<InputIterator>>
56 T squared_norm(InputIterator first, InputIterator last, T init = T{})
57 {
58  const auto n = norm(first, last, init);
59  return n * n;
60 }
61 
65 template<typename Container, typename T = value_type<Container>>
66 T squared_norm(const Container& a, T init = T{})
67 {
68  using std::begin;
69  using std::end;
70  return squared_norm(begin(a), end(a), init);
71 }
72 
76 template<typename InputIterator1, typename InputIterator2, typename T = value_type_i<InputIterator1>>
77 T distance(InputIterator1 first_left, InputIterator1 last_left, InputIterator2 first_right, T init = T{})
78 {
79  auto op1 = [](const T left, const T right) -> T
80  {
81  return std::max(left, right);
82  };
83  auto op2 = [](const auto left, const auto right) -> T
84  {
85  return std::abs(left - right);
86  };
87  return std::inner_product(first_left, last_left, first_right, init, op1, op2);
88 }
89 
94 template<typename Container1, typename Container2, typename T = value_type<Container1>>
95 T distance(const Container1& left, const Container2& right, T init = T{})
96 {
97  assert(left.size() == right.size());
98  using std::begin;
99  using std::end;
100  return distance(begin(left), end(left), begin(right), init);
101 }
102 
106 template<typename InputIterator1, typename InputIterator2, typename T = value_type_i<InputIterator1>>
107 T squared_distance(InputIterator1 first_left, InputIterator1 last_left, InputIterator2 first_right, T init = T{})
108 {
109  const auto d = distance(first_left, last_left, first_right, init);
110  return d * d;
111 }
112 
117 template<typename Container1, typename Container2, typename T = value_type<Container1>>
118 T squared_distance(const Container1& left, const Container2& right, T init = T{})
119 {
120  assert(left.size() == right.size());
121  using std::begin;
122  using std::end;
123  return squared_distance(begin(left), end(left), begin(right), init);
124 }
125 
128 } // namespace maximum
129 } // namespace aaa
T squared_norm(const Container &a, T init=T{})
Definition: maximum_space.hpp:66
T norm(const Container &a, T init=T{})
Definition: maximum_space.hpp:45
T squared_distance(const Container1 &left, const Container2 &right, T init=T{})
Definition: maximum_space.hpp:118
Definition: add.hpp:7
T distance(const Container1 &left, const Container2 &right, T init=T{})
Definition: maximum_space.hpp:95