sandbox
Loading...
Searching...
No Matches
matrix.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
26#ifndef LIBSBX_MATH_MATRIX_HPP_
27#define LIBSBX_MATH_MATRIX_HPP_
28
29#include <array>
30#include <cstddef>
31#include <cstdint>
32#include <span>
33
36
37namespace sbx::math {
38
46template<std::size_t Columns, std::size_t Rows, scalar Type>
47requires (Columns > 1u && Rows > 1u)
49
50public:
51
55 enum class direction : std::uint8_t {
56 column = 0u,
57 row = 1u
58 }; // enum class direction
59
63 inline static constexpr auto columns = Columns;
64
68 inline static constexpr auto rows = Rows;
69
70 using value_type = Type;
71 using reference = value_type&;
72 using const_reference = const value_type&;
73 using size_type = std::size_t;
75
79 constexpr basic_matrix() noexcept;
80
88 template<scalar Other = value_type>
89 constexpr basic_matrix(const Other value) noexcept;
90
98 template<scalar Other = value_type>
99 constexpr basic_matrix(const basic_matrix<Columns, Rows, Other>& other) noexcept;
100
101 constexpr basic_matrix(const basic_matrix& other) noexcept = default;
102
103 constexpr basic_matrix(basic_matrix&& other) noexcept = default;
104
105 auto operator=(const basic_matrix& other) noexcept -> basic_matrix& = default;
106
107 auto operator=(basic_matrix&& other) noexcept -> basic_matrix& = default;
108
116 [[nodiscard]] constexpr auto operator[](size_type index) noexcept -> column_type&;
117
125 [[nodiscard]] constexpr auto operator[](size_type index) const noexcept -> const column_type&;
126
136 template<scalar Other>
137 constexpr auto operator+=(const basic_matrix<Columns, Rows, Other>& other) noexcept -> basic_matrix&;
138
148 template<scalar Other>
149 constexpr auto operator-=(const basic_matrix<Columns, Rows, Other>& other) noexcept -> basic_matrix&;
150
160 template<scalar Other>
161 constexpr auto operator*=(Other scalar) noexcept -> basic_matrix&;
162
172 template<scalar Other>
173 constexpr auto operator/=(Other scalar) noexcept -> basic_matrix&;
174
182 constexpr auto row(const size_type row) const noexcept -> basic_vector<Columns, value_type>;
183
189 constexpr auto data() noexcept -> value_type*;
190
196 constexpr auto data() const noexcept -> const value_type*;
197
198protected:
199
200 template<scalar Other>
201 using column_type_for = basic_vector<Rows, Other>;
202
210 template<typename... Args>
211 constexpr basic_matrix(Args&&... args) noexcept;
212
220 constexpr static auto identity(const value_type value = static_cast<value_type>(1)) noexcept -> basic_matrix;
221
222private:
223
224 std::array<column_type, Columns> _columns;
225
226}; // class basic_matrix
227
241template<std::size_t Columns, std::size_t Rows, scalar Lhs, scalar Rhs>
242[[nodiscard]] constexpr auto operator==(const basic_matrix<Columns, Rows, Lhs>& lhs, const basic_matrix<Columns, Rows, Rhs>& rhs) noexcept -> bool;
243
257template<std::size_t Columns, std::size_t Rows, scalar Lhs, scalar Rhs>
258[[nodiscard]] constexpr auto operator+(basic_matrix<Columns, Rows, Lhs> lhs, const basic_matrix<Columns, Rows, Rhs>& rhs) noexcept -> basic_matrix<Columns, Rows, Lhs>;
259
273template<std::size_t Columns, std::size_t Rows, scalar Lhs, scalar Rhs>
274[[nodiscard]] constexpr auto operator-(basic_matrix<Columns, Rows, Lhs> lhs, const basic_matrix<Columns, Rows, Rhs>& rhs) noexcept -> basic_matrix<Columns, Rows, Lhs>;
275
289template<std::size_t Columns, std::size_t Rows, scalar Lhs, scalar Rhs>
290[[nodiscard]] constexpr auto operator*(basic_matrix<Columns, Rows, Lhs> lhs, Rhs rhs) noexcept -> basic_matrix<Columns, Rows, Lhs>;
291
305template<std::size_t Columns, std::size_t Rows, scalar Lhs, scalar Rhs>
306[[nodiscard]] constexpr auto operator*(Lhs lhs, basic_matrix<Columns, Rows, Rhs> rhs) noexcept -> basic_matrix<Columns, Rows, Rhs>;
307
317template<typename Matrix>
318[[nodiscard]] constexpr auto from_array(std::span<typename Matrix::value_type, Matrix::columns * Matrix::rows> array) -> Matrix;
319
320} // namespace sbx::math
321
322#include <libsbx/math/matrix.ipp>
323
324#endif // LIBSBX_MATH_MATRIX_HPP_
Fixed-size column-major matrix.
Definition: matrix.hpp:48
direction
Matrix traversal direction.
Definition: matrix.hpp:55
Fixed-size vector type.
Definition: vector.hpp:55
Concept for scalar numeric types.
Definition: concepts.hpp:150
Core numeric concepts and type traits.
Generic fixed-size vector type.