HLR  0.0.1
Matrix.hpp
1 #ifndef KINEMATICS_MATRIX_HPP
2 #define KINEMATICS_MATRIX_HPP
3 
4 #include <array>
5 #include <initializer_list>
6 #include <iostream>
7 #include <optional>
8 #include <type_traits>
9 
10 namespace HLR
11 {
12 namespace Kinematics
13 {
21 template<typename T, std::size_t m, std::size_t n>
22 class Matrix
23 {
24  static_assert(std::is_arithmetic<T>::value,
25  "Matrix can only be declared with a type where "
26  "std::is_arithmetic is true.");
27 
28 public:
34  static constexpr Matrix<T, m, n> get_identity() noexcept;
35 
40  Matrix() = default;
41 
46  ~Matrix() = default;
47 
53  Matrix(const Matrix<T, m, n>& mat) = default;
54 
60  Matrix(Matrix<T, m, n>&& mat) = default;
61 
68  Matrix<T, m, n>& operator=(const Matrix<T, m, n>& mat) = default;
69 
76  Matrix<T, m, n>& operator=(Matrix<T, m, n>&& mat) = default;
77 
84  constexpr Matrix(
85  std::initializer_list<std::initializer_list<T>> matrix_data);
86 
92  constexpr std::size_t get_m() const noexcept;
93 
99  constexpr std::size_t get_n() const noexcept;
100 
107  constexpr std::array<T, n>& operator[](std::size_t pos) noexcept;
108 
115  constexpr const std::array<T, n>& operator[](std::size_t pos) const
116  noexcept;
117 
125  constexpr std::array<T, n>& at(std::size_t pos);
126 
134  constexpr const std::array<T, n>& at(std::size_t pos) const;
135 
144  constexpr bool fuzzy_equal(const Matrix<T, m, n>& mat, T fuzz) const
145  noexcept;
146 
154  constexpr bool operator==(const Matrix<T, m, n>& mat) const noexcept;
155 
163  constexpr bool operator!=(const Matrix<T, m, n>& mat) const noexcept;
164 
171  constexpr void operator+=(T scalar) noexcept;
172 
178  constexpr void operator-=(T scalar) noexcept;
179 
185  constexpr void operator*=(T scalar) noexcept;
191  constexpr void operator/=(T scalar) noexcept;
192 
198  constexpr void operator+=(const Matrix<T, m, n>& mat) noexcept;
199 
205  constexpr void operator-=(const Matrix<T, m, n>& mat) noexcept;
206 
213  constexpr Matrix<T, m, n> operator+(const Matrix<T, m, n>& mat) const
214  noexcept;
215 
222  constexpr Matrix<T, m, n> operator-(const Matrix<T, m, n>& mat) const
223  noexcept;
224 
233  template<std::size_t p>
234  constexpr Matrix<T, m, p> operator*(const Matrix<T, n, p>& mat) const
235  noexcept;
236 
245  template<std::size_t p>
247  const Matrix<T, m, p>& mat) const noexcept;
248 
257  template<std::size_t p>
259  const Matrix<T, p, n>& mat) const noexcept;
260 
273  template<std::size_t y, std::size_t x, std::size_t p, std::size_t q>
274  constexpr Matrix<T, p, q> slice() const noexcept;
275 
281  constexpr Matrix<T, n, m> transpose() const noexcept;
282 
289  constexpr std::optional<Matrix<T, m, n>> inverse() const noexcept;
290 
297  constexpr T get_magnitude() const noexcept;
298 
304  std::string str() const;
305 
306 private:
311  std::array<std::array<T, n>, m> data{};
312 };
313 
322 template<typename T, std::size_t m, std::size_t n>
323 constexpr Matrix<T, m, n> operator+(const Matrix<T, m, n>& lhs, T rhs) noexcept;
324 
333 template<typename T, std::size_t m, std::size_t n>
334 constexpr Matrix<T, m, n> operator+(T lhs, const Matrix<T, m, n>& rhs) noexcept;
335 
344 template<typename T, std::size_t m, std::size_t n>
345 constexpr Matrix<T, m, n> operator-(const Matrix<T, m, n>& lhs, T rhs) noexcept;
346 
355 template<typename T, std::size_t m, std::size_t n>
356 constexpr Matrix<T, m, n> operator-(T lhs, const Matrix<T, m, n>& rhs) noexcept;
357 
366 template<typename T, std::size_t m, std::size_t n>
367 constexpr Matrix<T, m, n> operator*(const Matrix<T, m, n>& lhs, T rhs) noexcept;
368 
377 template<typename T, std::size_t m, std::size_t n>
378 constexpr Matrix<T, m, n> operator*(T lhs, const Matrix<T, m, n>& rhs) noexcept;
379 
388 template<typename T, std::size_t m, std::size_t n>
389 constexpr Matrix<T, m, n> operator/(const Matrix<T, m, n>& lhs, T rhs) noexcept;
390 
399 template<typename T, std::size_t m, std::size_t n>
400 constexpr Matrix<T, m, n> operator/(T lhs, const Matrix<T, m, n>& rhs) noexcept;
401 
409 template<typename T, std::size_t m, std::size_t n>
410 std::ostream& operator<<(std::ostream& os, const Matrix<T, m, n>& mat);
411 
412 } // namespace Kinematics
413 } // namespace HLR
414 
415 #include "Matrix.ipp"
416 
417 #endif
constexpr Matrix< T, m, n > operator+(const Matrix< T, m, n > &mat) const noexcept
Add a matrix to another matrix. Returns a new matrix.
constexpr Matrix< T, m, n > operator/(const Matrix< T, m, n > &lhs, T rhs) noexcept
Divide every element of the matrix with a scalar. Doesn&#39;t modify original matrix. ...
constexpr bool operator==(const Matrix< T, m, n > &mat) const noexcept
Compare two matrices.
std::string str() const
Get a string representation.
constexpr Matrix< T, m, n > operator-(const Matrix< T, m, n > &mat) const noexcept
Subtract a matrix from another matrix. Returns a new matrix.
constexpr Matrix< T, m, n+p > horizontal_augment(const Matrix< T, m, p > &mat) const noexcept
Right augment the current matrix with the provided matrix.
constexpr T get_magnitude() const noexcept
Get the magnitude of the matrix. This is equivalent to the euclidean distance.
constexpr void operator/=(T scalar) noexcept
Divide every element of the matrix with a scalar.
constexpr Matrix< T, m+p, n > vertical_augment(const Matrix< T, p, n > &mat) const noexcept
Bottom augment the current matrix with the provided matrix.
constexpr std::array< T, n > & at(std::size_t pos)
Get row of matrix at pos. Throw out of range exception if out of range.
constexpr void operator-=(T scalar) noexcept
Subtract a scalar from every element of the matrix.
constexpr bool fuzzy_equal(const Matrix< T, m, n > &mat, T fuzz) const noexcept
Fuzzy compare 2 matrices.
constexpr std::array< T, n > & operator[](std::size_t pos) noexcept
Get row of matrix at pos. No bounds checkin.
constexpr std::optional< Matrix< T, m, n > > inverse() const noexcept
Get the inverse of the matrix, implemented by using gauss-jordan. This operation is only available fo...
constexpr bool operator!=(const Matrix< T, m, n > &mat) const noexcept
Compare two matrices.
Matrix< T, m, n > & operator=(const Matrix< T, m, n > &mat)=default
Default assignment operator.
constexpr std::size_t get_n() const noexcept
Get the amount of cols of the matrix.
constexpr Matrix< T, n, m > transpose() const noexcept
Transpose the matrix.
~Matrix()=default
Default destructor.
constexpr void operator*=(T scalar) noexcept
Multiply every element of the matrix with a scalar.
Matrix()=default
Create matrix with all elements set to 0.
static constexpr Matrix< T, m, n > get_identity() noexcept
Get identity matrix.
constexpr Matrix< T, p, q > slice() const noexcept
Create a new matrix from the current one where some columns and rows are sliced of.
constexpr std::size_t get_m() const noexcept
Get the amount of rows of the matrix.
Create a matrix with numerical values.
Definition: Matrix.hpp:22
constexpr Matrix< T, m, p > operator*(const Matrix< T, n, p > &mat) const noexcept
Multiply a matrix with the current matrix.
constexpr void operator+=(T scalar) noexcept
Add a scalar from every element of the matrix.