matrix/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_MATRIX_OPERATORS_HPP
28 #define CRYPTO3_ALGEBRA_MATRIX_OPERATORS_HPP
29 
31 
32 namespace nil {
33  namespace crypto3 {
34  namespace algebra {
35 
47  template<typename T, std::size_t N, std::size_t M>
48  constexpr bool operator==(const matrix<T, N, M> &a, const matrix<T, N, M> &b) {
49  for (std::size_t i = 0; i < N; ++i) {
50  for (std::size_t j = 0; j < M; ++j) {
51  if (a[i][j] != b[i][j])
52  return false;
53  }
54  }
55  return true;
56  }
57 
65  template<typename T, std::size_t N, std::size_t M>
66  constexpr bool operator!=(const matrix<T, N, M> &a, const matrix<T, N, M> &b) {
67  return !(a == b);
68  }
69 
77  template<typename T, std::size_t N, std::size_t M>
78  constexpr matrix<T, N, M> operator+(const matrix<T, N, M> &m, T a) {
79  return elementwise([a](T x) { return x + a; }, m);
80  }
81 
89  template<typename T, std::size_t N, std::size_t M>
90  constexpr matrix<T, N, M> operator+(T a, const matrix<T, N, M> &m) {
91  return elementwise([a](T x) { return a + x; }, m);
92  }
93 
102  template<typename T, std::size_t N, std::size_t M>
104  return elementwise(std::plus<T>(), a, b);
105  }
106 
114  template<typename T, std::size_t N, std::size_t M>
115  constexpr matrix<T, N, M> operator*(const matrix<T, N, M> &m, T a) {
116  return elementwise([a](T x) { return x * a; }, m);
117  }
118 
126  template<typename T, std::size_t N, std::size_t M>
127  constexpr matrix<T, N, M> operator*(T a, const matrix<T, N, M> &m) {
128  return elementwise([a](T x) { return a * x; }, m);
129  }
130 
139  template<typename T, std::size_t N, std::size_t M>
141  return elementwise(std::multiplies<T>(), a, b);
142  }
143 
152  template<typename T, std::size_t N, std::size_t M>
153  constexpr matrix<T, N, M> operator/(T a, const matrix<T, N, M> &m) {
154  return elementwise([a](T x) { return a / x; }, m);
155  }
156 
167  template<typename T, std::size_t N, std::size_t M>
169  return elementwise(std::divides<T>(), a, b);
170  }
171 
174  } // namespace algebra
175  } // namespace crypto3
176 } // namespace nil
177 
178 #endif // CRYPTO3_ALGEBRA_MATRIX_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 matrix.
Definition: matrix.hpp:52