sandbox
Loading...
Searching...
No Matches
matrix.hpp
1#ifndef LIBSBX_MATH_MATRIX_HPP_
2#define LIBSBX_MATH_MATRIX_HPP_
3
4#include <array>
5
6#include <libsbx/math/concepts.hpp>
7#include <libsbx/math/vector.hpp>
8
9namespace sbx::math {
10
11template<std::size_t Columns, std::size_t Rows, scalar Type>
12requires (Columns > 1u && Rows > 1u)
14
15public:
16
17 enum class direction : std::uint8_t {
18 column = 0u,
19 row = 1u
20 }; // enum class direction
21
22 inline static constexpr auto columns = Columns;
23 inline static constexpr auto rows = Rows;
24
25 using value_type = Type;
26 using reference = value_type&;
27 using const_reference = const value_type&;
28 using size_type = std::size_t;
30
31 constexpr basic_matrix() noexcept;
32
33 template<scalar Other = value_type>
34 constexpr basic_matrix(const Other value) noexcept;
35
36 template<scalar Other = value_type>
37 constexpr basic_matrix(const basic_matrix<Columns, Rows, Other>& other) noexcept;
38
39 constexpr basic_matrix(const basic_matrix& other) noexcept = default;
40
41 constexpr basic_matrix(basic_matrix&& other) noexcept = default;
42
43 auto operator=(const basic_matrix& other) noexcept -> basic_matrix& = default;
44
45 auto operator=(basic_matrix&& other) noexcept -> basic_matrix& = default;
46
47 [[nodiscard]] constexpr auto operator[](size_type index) noexcept -> column_type&;
48
49 [[nodiscard]] constexpr auto operator[](size_type index) const noexcept -> const column_type&;
50
51 template<scalar Other>
52 constexpr auto operator+=(const basic_matrix<Columns, Rows, Other>& other) noexcept -> basic_matrix&;
53
54 template<scalar Other>
55 constexpr auto operator-=(const basic_matrix<Columns, Rows, Other>& other) noexcept -> basic_matrix&;
56
57 template<scalar Other>
58 constexpr auto operator*=(Other scalar) noexcept -> basic_matrix&;
59
60 template<scalar Other>
61 constexpr auto operator/=(Other scalar) noexcept -> basic_matrix&;
62
63 constexpr auto row(const size_type row) const noexcept -> basic_vector<Columns, value_type>;
64
65 constexpr auto data() noexcept -> value_type* {
66 return _columns[0].data();
67 }
68
69 constexpr auto data() const noexcept -> const value_type* {
70 return _columns[0].data();
71 }
72
73protected:
74
75 template<scalar Other>
77
78 template<typename... Args>
79 constexpr basic_matrix(Args&&... args) noexcept;
80
81 constexpr static auto identity(const value_type value = static_cast<value_type>(1)) noexcept -> basic_matrix;
82
83private:
84
85 std::array<column_type, Columns> _columns;
86
87}; // class basic_matrix
88
89template<std::size_t Columns, std::size_t Rows, scalar Lhs, scalar Rhs>
90[[nodiscard]] constexpr auto operator==(const basic_matrix<Columns, Rows, Lhs>& lhs, const basic_matrix<Columns, Rows, Rhs>& rhs) noexcept -> bool;
91
92template<std::size_t Columns, std::size_t Rows, scalar Lhs, scalar Rhs>
93[[nodiscard]] constexpr auto operator+(basic_matrix<Columns, Rows, Lhs> lhs, const basic_matrix<Columns, Rows, Rhs>& rhs) noexcept -> basic_matrix<Columns, Rows, Lhs>;
94
95template<std::size_t Columns, std::size_t Rows, scalar Lhs, scalar Rhs>
96[[nodiscard]] constexpr auto operator-(basic_matrix<Columns, Rows, Lhs> lhs, const basic_matrix<Columns, Rows, Rhs>& rhs) noexcept -> basic_matrix<Columns, Rows, Lhs>;
97
98template<std::size_t Columns, std::size_t Rows, scalar Lhs, scalar Rhs>
99[[nodiscard]] constexpr auto operator*(basic_matrix<Columns, Rows, Lhs> lhs, Rhs rhs) noexcept -> basic_matrix<Columns, Rows, Lhs>;
100
101template<std::size_t Columns, std::size_t Rows, scalar Lhs, scalar Rhs>
102[[nodiscard]] constexpr auto operator*(Lhs lhs, basic_matrix<Columns, Rows, Rhs> rhs) noexcept -> basic_matrix<Columns, Rows, Rhs>;
103
104template<typename Matrix>
105[[nodiscard]] constexpr auto from_array(std::span<typename Matrix::value_type, Matrix::columns * Matrix::rows> array) -> Matrix;
106
107} // namespace sbx::math
108
109#include <libsbx/math/matrix.ipp>
110
111#endif // LIBSBX_MATH_MATRIX_HPP_
Definition: matrix.hpp:13
Definition: vector.hpp:22