algebra/include/nil/crypto3/algebra/vector/math.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2020-2021 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020-2021 Nikita Kaskov <nbering@nil.foundation>
4 // Copyright (c) 2020-2021 Ilias Khairullin <ilias@nil.foundation>
5 //
6 // MIT License
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 //---------------------------------------------------------------------------//
26 
27 #ifndef CRYPTO3_ALGEBRA_VECTOR_MATH_HPP
28 #define CRYPTO3_ALGEBRA_VECTOR_MATH_HPP
29 
33 
34 namespace nil {
35  namespace crypto3 {
36  namespace algebra {
37 
49  template<typename T, std::size_t N>
50  constexpr vector<T, N> conj(const vector<T, N> &v) {
51  return elementwise(algebra::conj<T>, v);
52  }
53 
60  template<typename T, std::size_t N>
61  constexpr vector<T, N> sqrt(const vector<T, N> &v) {
62  return elementwise(static_cast<T (*)(T)>(sqrt), v);
63  }
64 
72  template<typename T, std::size_t N>
74  return elementwise([](auto i) { return std::real(i); }, v);
75  }
76 
84  template<typename T, std::size_t N>
86  return elementwise([](auto i) { return std::imag(i); }, v);
87  }
88 
96  template<typename T, std::size_t N>
98  return elementwise(abs<T>, v);
99  }
100 
109  template<typename T, std::size_t N>
110  constexpr T dot(const vector<T, N> &a, const vector<T, N> &b) {
111  T r = 0;
112  for (std::size_t i = 0; i < vector<T, N>::size; ++i)
113  r += a[i] * conj(b[i]);
114  return r;
115  }
116 
123  template<typename T, std::size_t N>
124  constexpr T sum(const vector<T, N> &v) {
125  return accumulate(v, T(0), std::plus<T>());
126  }
127 
134  template<typename T, std::size_t N>
135  constexpr T min(const vector<T, N> &v) {
136  return accumulate(v, v[0], [](T a, T b) { return std::min(a, b); });
137  }
138 
145  template<typename T, std::size_t N>
146  constexpr T max(const vector<T, N> &v) {
147  return accumulate(v, v[0], [](T a, T b) { return std::max(a, b); });
148  }
149 
157  template<typename T, std::size_t N>
158  constexpr std::size_t min_index(const vector<T, N> &v) {
159  T min = v[0];
160  std::size_t index = 0;
161  for (std::size_t i = 0; i < vector<T, N>::size; ++i)
162  if (v[i] < min) {
163  index = i;
164  min = v[i];
165  }
166  return index;
167  }
168 
176  template<typename T, std::size_t N>
177  constexpr std::size_t max_index(const vector<T, N> &v) {
178  T max = v[0];
179  std::size_t index = 0;
180  for (std::size_t i = 0; i < vector<T, N>::size; ++i)
181  if (v[i] > max) {
182  index = i;
183  max = v[i];
184  }
185  return index;
186  }
187 
190  } // namespace algebra
191  } // namespace crypto3
192 } // namespace nil
193 #endif // CRYPTO3_ALGEBRA_VECTOR_MATH_HPP
constexpr matrix< U, N, M > elementwise(F f, const matrix< T, N, M > &m, const Matrices &...matrices)
applies a function elementwise between many matrices
Definition: matrix/utility.hpp:55
constexpr matrix< nil::crypto3::detail::remove_complex_t< T >, M, N > imag(const matrix< T, M, N > &m)
computes the elementwise imag
Definition: algebra/include/nil/crypto3/algebra/matrix/math.hpp:81
constexpr matrix< T, M, N > conj(const matrix< T, M, N > &m)
computes the elementwise complex conjugate
Definition: algebra/include/nil/crypto3/algebra/matrix/math.hpp:57
constexpr matrix< nil::crypto3::detail::remove_complex_t< T >, M, N > real(const matrix< T, M, N > &m)
computes the elementwise real
Definition: algebra/include/nil/crypto3/algebra/matrix/math.hpp:69
constexpr nil::crypto3::detail::remove_complex_t< T > abs(T x)
computes the absolute value
Definition: algebra/include/nil/crypto3/algebra/scalar/math.hpp:76
constexpr double sqrt(double x)
computes the square root
Definition: algebra/include/nil/crypto3/algebra/scalar/math.hpp:47
constexpr T min(const vector< T, N > &v)
computes the minimum valued element
Definition: algebra/include/nil/crypto3/algebra/vector/math.hpp:135
constexpr vector< nil::crypto3::detail::remove_complex_t< T >, N > imag(const vector< T, N > &v)
computes the elementwise imag
Definition: algebra/include/nil/crypto3/algebra/vector/math.hpp:85
constexpr T dot(const vector< T, N > &a, const vector< T, N > &b)
computes the dot product
Definition: algebra/include/nil/crypto3/algebra/vector/math.hpp:110
constexpr vector< nil::crypto3::detail::remove_complex_t< T >, N > real(const vector< T, N > &v)
computes the elementwise real
Definition: algebra/include/nil/crypto3/algebra/vector/math.hpp:73
constexpr T sum(const vector< T, N > &v)
computes the sum of elements
Definition: algebra/include/nil/crypto3/algebra/vector/math.hpp:124
constexpr std::size_t max_index(const vector< T, N > &v)
computes the index of the maximum valued element
Definition: algebra/include/nil/crypto3/algebra/vector/math.hpp:177
constexpr std::size_t min_index(const vector< T, N > &v)
computes the index of the minimum valued element
Definition: algebra/include/nil/crypto3/algebra/vector/math.hpp:158
constexpr U accumulate(const vector< T, N > &v, U init, F &&f)
accumulates an operation across a vector
Definition: vector/utility.hpp:70
constexpr T max(const vector< T, N > &v)
computes the maximum valued element
Definition: algebra/include/nil/crypto3/algebra/vector/math.hpp:146
Definition: pair.hpp:31
A container representing a vector.
Definition: vector.hpp:50