Auxiliary Arithmetic Algorithms
manhattan_space.hpp
1 #pragma once
2 
3 #include <cassert>
4 #include <cmath>
5 #include <numeric>
6 
7 #include "traits.hpp"
8 
9 namespace aaa {
10 namespace manhattan {
11 
32 template<typename InputIterator, typename T = value_type_i<InputIterator>>
33 T norm(InputIterator first, InputIterator last, T init = T{})
34 {
35  const auto add_abs = [](const auto left, const auto right) -> T
36  {
37  return left + std::abs(right);
38  };
39  return std::accumulate(first, last, init, add_abs);
40 }
41 
45 template<typename Container, typename T = value_type<Container>>
46 T norm(const Container& a, T init = T{})
47 {
48  using std::begin;
49  using std::end;
50  return norm(begin(a), end(a), init);
51 }
52 
56 template<typename InputIterator, typename T = value_type_i<InputIterator>>
57 T squared_norm(InputIterator first, InputIterator last, T init = T{})
58 {
59  const auto n = norm(first, last, init);
60  return n * n;
61 }
62 
66 template<typename Container, typename T = value_type<Container>>
67 T squared_norm(const Container& a, T init = T{})
68 {
69  using std::begin;
70  using std::end;
71  return squared_norm(begin(a), end(a), init);
72 }
73 
78 template<typename InputIterator1, typename InputIterator2, typename T = value_type_i<InputIterator1>>
79 T distance(InputIterator1 first_left, InputIterator1 last_left, InputIterator2 first_right, T init = T{})
80 {
81  auto op1 = [](const T left, const T right) -> T
82  {
83  return left + right;
84  };
85  auto op2 = [](const auto left, const auto right) -> T
86  {
87  return std::abs(left - right);
88  };
89  return std::inner_product(first_left, last_left, first_right, init, op1, op2);
90 }
91 
96 template<typename Container1, typename Container2, typename T = value_type<Container1>>
97 T distance(const Container1& left, const Container2& right, T init = T{})
98 {
99  assert(left.size() == right.size());
100  using std::begin;
101  using std::end;
102  return distance(begin(left), end(left), begin(right), init);
103 }
104 
108 template<typename InputIterator1, typename InputIterator2, typename T = value_type_i<InputIterator1>>
109 T squared_distance(InputIterator1 first_left, InputIterator1 last_left, InputIterator2 first_right, T init = T{})
110 {
111  const auto d = distance(first_left, last_left, first_right, init);
112  return d * d;
113 }
114 
119 template<typename Container1, typename Container2, typename T = value_type<Container1>>
120 T squared_distance(const Container1& left, const Container2& right, T init = T{})
121 {
122  assert(left.size() == right.size());
123  using std::begin;
124  using std::end;
125  return squared_distance(begin(left), end(left), begin(right), init);
126 }
127 
130 } // namespace manhattan
131 } // namespace aaa
T norm(const Container &a, T init=T{})
Definition: manhattan_space.hpp:46
T squared_norm(const Container &a, T init=T{})
Definition: manhattan_space.hpp:67
T squared_distance(const Container1 &left, const Container2 &right, T init=T{})
Definition: manhattan_space.hpp:120
Definition: add.hpp:7
T distance(const Container1 &left, const Container2 &right, T init=T{})
Definition: manhattan_space.hpp:97