Auxiliary Arithmetic Algorithms
euclidean_space.hpp
1 #pragma once
2 
3 #include <numeric>
4 
5 #include "traits.hpp"
6 
7 namespace aaa {
8 namespace euclidean {
9 
32 template<typename InputIterator1, typename InputIterator2, typename T = value_type_i<InputIterator1>>
33 T dot(InputIterator1 first_left, InputIterator1 last_left, InputIterator2 first_right, T init = T{})
34 {
35  return std::inner_product(first_left, last_left, first_right, init);
36 }
37 
42 template<typename Container1, typename Container2, typename T = value_type<Container1>>
43 T dot(const Container1& a, Container2& b, T init = T{})
44 {
45  assert(a.size() == b.size());
46  using std::begin;
47  using std::end;
48  return dot(begin(a), end(a), begin(b), init);
49 }
50 
54 template<typename InputIterator, typename T = value_type_i<InputIterator>>
55 T squared_norm(InputIterator first, InputIterator last, T init = T{})
56 {
57  return dot(first, last, first, init);
58 }
59 
63 template<typename Container, typename T = value_type<Container>>
64 T squared_norm(const Container& a, T init = T{})
65 {
66  using std::begin;
67  using std::end;
68  return squared_norm(begin(a), end(a), init);
69 }
70 
75 template<typename InputIterator, typename T = value_type_i<InputIterator>>
76 sqrt_type_t<T> norm(InputIterator first, InputIterator last, T init = T{})
77 {
78  return sqrt(squared_norm(first, last, init));
79 }
80 
85 template<typename Container, typename T = value_type<Container>>
86 sqrt_type_t<T> norm(const Container& a, T init = T{})
87 {
88  using std::begin;
89  using std::end;
90  return norm(begin(a), end(a), init);
91 }
92 
96 template<typename InputIterator1, typename InputIterator2, typename T = value_type_i<InputIterator1>>
97 T squared_distance(InputIterator1 first_left, InputIterator1 last_left, InputIterator2 first_right, T init = T{})
98 {
99  auto op1 = [](const T left, const T right) -> T
100  {
101  return left + right;
102  };
103  auto op2 = [](const auto left, const auto right) -> T
104  {
105  return (left - right) * (left - right);
106  };
107  return std::inner_product(first_left, last_left, first_right, init, op1, op2);
108 }
109 
114 template<typename Container1, typename Container2, typename T = value_type<Container1>>
115 T squared_distance(const Container1& left, const Container2& right, T init = T{})
116 {
117  assert(left.size() == right.size());
118  using std::begin;
119  using std::end;
120  return squared_distance(begin(left), end(left), begin(right), init);
121 }
122 
127 template<typename InputIterator1, typename InputIterator2, typename T = value_type_i<InputIterator1>>
128 sqrt_type_t<T> distance(InputIterator1 first_left, InputIterator1 last_left, InputIterator2 first_right, T init = T{})
129 {
130  return sqrt(squared_distance(first_left, last_left, first_right, init));
131 }
132 
138 template<typename Container1, typename Container2, typename T = value_type<Container1>>
139 sqrt_type_t<T> distance(const Container1& left, const Container2& right, T init = T{})
140 {
141  assert(left.size() == right.size());
142  using std::begin;
143  using std::end;
144  return distance(begin(left), end(left), begin(right), init);
145 }
146 
149 } // namespace euclidean
150 } // namespace aaa
T dot(const Container1 &a, Container2 &b, T init=T{})
Definition: euclidean_space.hpp:43
T squared_distance(const Container1 &left, const Container2 &right, T init=T{})
Definition: euclidean_space.hpp:115
sqrt_type_t< T > distance(const Container1 &left, const Container2 &right, T init=T{})
Definition: euclidean_space.hpp:139
Definition: add.hpp:7
T squared_norm(const Container &a, T init=T{})
Definition: euclidean_space.hpp:64
sqrt_type_t< T > norm(const Container &a, T init=T{})
Definition: euclidean_space.hpp:86