vector/operators.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_OPERATORS_HPP
28 #define CRYPTO3_ALGEBRA_VECTOR_OPERATORS_HPP
29 
31 
32 namespace nil {
33  namespace crypto3 {
34  namespace algebra {
35 
47  template<typename T, std::size_t N>
48  constexpr bool operator==(const vector<T, N> &a, const vector<T, N> &b) {
49  for (std::size_t i = 0; i < vector<T, N>::size; ++i) {
50  if (a[i] != b[i])
51  return false;
52  }
53  return true;
54  }
55 
63  template<typename T, std::size_t N>
64  constexpr bool operator!=(const vector<T, N> &a, const vector<T, N> &b) {
65  return !(a == b);
66  }
67 
75  template<typename T, std::size_t N>
76  constexpr vector<T, N> operator+(const vector<T, N> &v, T a) {
77  return elementwise([a](T x) { return x + a; }, v);
78  }
79 
87  template<typename T, std::size_t N>
88  constexpr vector<T, N> operator+(T a, const vector<T, N> &v) {
89  return elementwise([a](T x) { return a + x; }, v);
90  }
91 
101  template<typename T, std::size_t N>
102  constexpr vector<T, N> operator+(const vector<T, N> &a, const vector<T, N> &b) {
103  return elementwise(std::plus<T>(), a, b);
104  }
105 
113  template<typename T, std::size_t N>
114  constexpr vector<T, N> operator*(const vector<T, N> &v, T a) {
115  return elementwise([a](T x) { return x * a; }, v);
116  }
117 
125  template<typename T, std::size_t N>
126  constexpr vector<T, N> operator*(T a, const vector<T, N> &v) {
127  return elementwise([a](T x) { return a * x; }, v);
128  }
129 
138  template<typename T, std::size_t N>
139  constexpr vector<T, N> operator*(const vector<T, N> &a, const vector<T, N> &b) {
140  return elementwise(std::multiplies<T>(), a, b);
141  }
142 
150  template<typename T, std::size_t N>
151  constexpr vector<T, N> operator/(T a, const vector<T, N> &v) {
152  return elementwise([a](T x) { return a / x; }, v);
153  }
154 
164  template<typename T, std::size_t N>
165  constexpr vector<T, N> operator/(const vector<T, N> &a, const vector<T, N> &b) {
166  return elementwise(std::divides<T>(), a, b);
167  }
168 
171  } // namespace algebra
172  } // namespace crypto3
173 } // namespace nil
174 #endif // CRYPTO3_ALGEBRA_VECTOR_OPERATORS_HPP
constexpr bool operator!=(const matrix< T, N, M > &a, const matrix< T, N, M > &b)
checks inequality of two matrices
Definition: matrix/operators.hpp:66
constexpr matrix< T, N, M > operator/(T a, const matrix< T, N, M > &m)
computes the quotient between a matrix and a scalar
Definition: matrix/operators.hpp:153
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< T, N, M > operator+(const matrix< T, N, M > &m, T a)
computes the sum of a matrix and a scalar
Definition: matrix/operators.hpp:78
constexpr bool operator==(const matrix< T, N, M > &a, const matrix< T, N, M > &b)
checks equality of two matrices
Definition: matrix/operators.hpp:48
constexpr matrix< T, N, M > operator*(const matrix< T, N, M > &m, T a)
computes the product of a matrix and a scalar
Definition: matrix/operators.hpp:115
Definition: pair.hpp:31
A container representing a vector.
Definition: vector.hpp:50