matrix.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_CLASS_HPP
28 #define CRYPTO3_ALGEBRA_MATRIX_CLASS_HPP
29 
30 #include <array>
31 #include <tuple>
32 
34 
37 
38 namespace nil {
39  namespace crypto3 {
40  namespace algebra {
41 
51  template<typename T, std::size_t N, std::size_t M>
52  struct matrix {
53  static_assert(N != 0 && M != 0, "matrix must have have positive dimensions");
54 
55  // CRYPTO3_DETAIL_ASSERT_ARITHMETIC(T)
56 
57  using value_type = T;
58  using size_type = std::size_t;
59  static constexpr size_type column_size = N;
60  static constexpr size_type row_size = M;
61 
64 
70  constexpr vector<T, M> row(std::size_t i) const {
71  if (i >= N)
72  throw "index out of range";
73  return generate<M>([i, this](std::size_t j) { return arrays[i][j]; });
74  }
75 
82  constexpr vector<T, N> column(std::size_t i) const {
83  if (i >= M)
84  throw "index out of range";
85  return generate<N>([i, this](std::size_t j) { return arrays[j][i]; });
86  }
87 
97  constexpr T *operator[](std::size_t i) {
98  return arrays[i];
99  }
100 
102  constexpr T const *operator[](std::size_t i) const {
103  return arrays[i];
104  }
106 
107  private:
108  T arrays[N][M];
109  };
110 
117 
126  template<typename T, std::size_t M, std::size_t N>
127  matrix(const T (&)[M][N]) -> matrix<T, M, N>;
128 
130 
133  } // namespace algebra
134  } // namespace crypto3
135 } // namespace nil
136 
137 #endif // CRYPTO3_ALGEBRA_MATRIX_CLASS_HPP
matrix(const T(&)[M][N]) -> matrix< T, M, N >
deduction guide for aggregate initialization
Definition: pair.hpp:31
A container representing a matrix.
Definition: matrix.hpp:52
constexpr T * operator[](std::size_t i)
access specified element
Definition: matrix.hpp:97
constexpr vector< T, N > column(std::size_t i) const
access specified column
Definition: matrix.hpp:82
std::size_t size_type
Definition: matrix.hpp:58
constexpr T const * operator[](std::size_t i) const
access specified element
Definition: matrix.hpp:102
static constexpr size_type column_size
Number of rows.
Definition: matrix.hpp:59
constexpr vector< T, M > row(std::size_t i) const
access specified row
Definition: matrix.hpp:70
static constexpr size_type row_size
Number of columns.
Definition: matrix.hpp:60
T value_type
Definition: matrix.hpp:57
A container representing a vector.
Definition: vector.hpp:50