26 #include "../algorithm.h"
32 template < std::
size_t N, std::
size_t M,
typename T =
float >
37 static constexpr std::size_t
rows =
N;
38 static constexpr std::size_t
cols = M;
53 [[nodiscard]] constexpr
auto begin()
const
58 [[nodiscard]] constexpr
auto end()
const
76 std::array< row_type, N > data_;
79 template <
typename Matrix >
82 template <
typename Matrix2 >
86 stub( std::size_t i, Matrix2& m )
92 constexpr
auto const&
operator[]( std::size_t j )
const
109 static constexpr std::size_t
rows = Matrix::cols;
110 static constexpr std::size_t
cols = Matrix::rows;
117 constexpr stub< Matrix const >
operator[]( std::size_t i )
const
130 for ( std::size_t
const i :
range(
rows ) )
131 for ( std::size_t
const j :
range(
cols ) )
132 res[i][j] = m_[j][i];
142 template < std::
size_t N,
typename T =
float >
148 stub( std::size_t i )
155 return i_ == j ? 1 : 0;
164 static constexpr std::size_t
rows =
N;
165 static constexpr std::size_t
cols =
N;
177 for ( std::size_t
const i :
range(
rows ) )
178 for ( std::size_t
const j :
range(
cols ) )
179 res[i][j] = i == j ? 1 : 0;
184 template <
typename M >
186 { M::rows } -> std::convertible_to< std::size_t >;
187 { M::cols } -> std::convertible_to< std::size_t >;
188 { m[i][j] } -> std::convertible_to< typename M::value_type >;
191 template < matrix_like Matrix, std::
size_t I, std::
size_t J >
194 template <
typename Matrix2 >
198 stub( std::size_t i, Matrix2& m )
204 constexpr
auto const&
operator[]( std::size_t j )
const
206 return m_[i_][j < J ? j : j + 1];
211 return m_[i_][j < J ? j : j + 1];
221 static constexpr std::size_t
rows = Matrix::rows - 1;
222 static constexpr std::size_t
cols = Matrix::cols - 1;
229 constexpr stub< Matrix const >
operator[]( std::size_t i )
const
231 return { i < I ? i : i + 1, m_ };
236 return { i < I ? i : i + 1, m_ };
243 #ifdef EMLABCPP_USE_OSTREAM
244 template < matrix_like Matrix >
245 std::ostream&
operator<<( std::ostream& os, Matrix
const& m )
247 for ( std::size_t
const i :
range( Matrix::rows ) ) {
248 for ( std::size_t
const j :
range( Matrix::cols ) )
249 os << m[i][j] <<
'\t';
256 template < matrix_like LH, matrix_like RH >
257 requires( LH::rows == RH::rows && LH::cols == RH::cols )
258 constexpr
auto operator==(
const LH& lh,
const RH& rh )
260 for ( std::size_t
const i :
range( LH::rows ) ) {
261 for ( std::size_t
const j :
range( LH::cols ) )
262 if ( lh[i][j] != rh[i][j] )
268 template < matrix_like LH, matrix_like RH,
typename T =
typename LH::value_type >
270 constexpr matrix< LH::rows, RH::cols, T >
operator*(
const LH& lh,
const RH& rh )
272 matrix< LH::rows, RH::cols, T >
res;
274 for ( std::size_t
const i :
range( LH::rows ) ) {
275 for ( std::size_t
const j :
range( RH::cols ) ) {
277 for ( std::size_t
const k :
range( LH::cols ) )
278 v += lh[i][k] * rh[k][j];
286 template < matrix_like LH >
287 constexpr matrix< LH::rows, LH::cols, typename LH::value_type >
291 for ( std::size_t
const i :
range( LH::rows ) )
292 for ( std::size_t
const j :
range( LH::cols ) )
298 template < matrix_like LH >
299 constexpr matrix< LH::rows, LH::cols, typename LH::value_type >
305 template < matrix_like LH, matrix_like RH,
typename T =
typename LH::value_type >
306 requires( LH::cols == RH::cols && LH::rows == RH::rows )
307 constexpr matrix< LH::rows, LH::cols, T >
operator+(
const LH& lh,
const RH& rh )
309 matrix< LH::rows, LH::cols, T >
res{};
310 for ( std::size_t
const i :
range( LH::rows ) )
311 for ( std::size_t
const j :
range( LH::cols ) )
312 res[i][j] = lh[i][j] + rh[i][j];
316 template < matrix_like LH, matrix_like RH,
typename T =
typename LH::value_type >
317 requires( LH::cols == RH::cols && LH::rows == RH::rows )
318 constexpr matrix< LH::rows, LH::cols, T >
operator-(
const LH& lh,
const RH& rh )
320 matrix< LH::rows, LH::cols, T >
res{};
321 for ( std::size_t
const i :
range( LH::rows ) )
322 for ( std::size_t
const j :
range( LH::cols ) )
323 res[i][j] = lh[i][j] - rh[i][j];
327 template < matrix_like M >
328 constexpr transposed_matrix< M > transpose( M& m )
333 template < matrix_like M >
334 constexpr matrix< M::cols, M::rows, typename M::value_type > transpose( M&& m )
336 return transpose( m );
339 template < matrix_like M >
341 constexpr auto determinant( M const& m )
343 return m[0][0] * m[1][1] - m[0][1] * m[1][0];
346 template < matrix_like M >
348 constexpr
auto determinant( M
const& m )
351 constexpr std::size_t
N = M::rows;
353 for_each_index< N >( [&]< std::size_t i > {
355 res += ( i % 2 == 0 ? 1 : -1 ) * m[i][0] * determinant( submatrix );
360 template < matrix_like M >
365 res[0][0] = 1.f / m[0][0];
369 template < matrix_like M >
370 requires( M::rows == 2 && M::cols == 2 )
371 constexpr matrix< M::rows, M::cols, typename M::
value_type >
inverse( M const& m )
373 auto v = 1.f / determinant( m );
375 matrix< M::rows, M::cols, typename M::value_type >
res;
376 res[0] = { m[1][1], -m[0][1] };
377 res[1] = { -m[1][0], m[0][0] };
static constexpr std::size_t cols
Definition: matrix.h:165
T value_type
Definition: matrix.h:163
constexpr identity_matrix()=default
constexpr stub operator[](std::size_t i) const
Definition: matrix.h:169
static constexpr std::size_t rows
Definition: matrix.h:164
constexpr matrix(std::array< row_type, N > data)
Definition: matrix.h:48
constexpr row_type & operator[](std::size_t i)
Definition: matrix.h:68
std::array< value_type, M > row_type
Definition: matrix.h:39
static constexpr std::size_t rows
Definition: matrix.h:37
constexpr auto end() const
Definition: matrix.h:58
static constexpr std::size_t cols
Definition: matrix.h:38
constexpr matrix()
Definition: matrix.h:41
T value_type
Definition: matrix.h:36
constexpr bool operator==(matrix const &other) const =default
constexpr auto begin() const
Definition: matrix.h:53
constexpr row_type const & operator[](std::size_t i) const
Definition: matrix.h:63
static constexpr std::size_t cols
Definition: matrix.h:222
static constexpr std::size_t rows
Definition: matrix.h:221
typename Matrix::value_type value_type
Definition: matrix.h:220
constexpr stub< Matrix > operator[](std::size_t i)
Definition: matrix.h:234
constexpr stub< Matrix const > operator[](std::size_t i) const
Definition: matrix.h:229
constexpr rowcol_submatrix(Matrix &m)
Definition: matrix.h:224
typename Matrix::value_type value_type
Definition: matrix.h:108
static constexpr std::size_t rows
Definition: matrix.h:109
static constexpr std::size_t cols
Definition: matrix.h:110
constexpr bool operator==(transposed_matrix const &other) const =default
constexpr stub< Matrix const > operator[](std::size_t i) const
Definition: matrix.h:117
constexpr transposed_matrix(Matrix &m)
Definition: matrix.h:112
constexpr stub< Matrix > operator[](std::size_t i)
Definition: matrix.h:122
std::variant< int64_t, float, bool, string_buffer > value_type
Definition: base.h:51
MIT License.
Definition: impl.h:31
constexpr pointer data() noexcept
Returns pointer to first item of the storage.
Definition: static_storage.h:108
constexpr point< N > operator+(point< N > a, vector< N > const &b)
Returns a result of addition a to b, viz += operator.
Definition: point.h:93
constexpr point< N > operator*(point< N > a, point< N > const &b)
Multiplication of points multiplies each coordinate of A by coordinate of B on same dimension.
Definition: point.h:74
T value_type
Definition: static_storage.h:100
concept matrix_like
Definition: matrix.h:185
T res
Definition: algorithm.h:505
constexpr pose inverse(pose const &x)
Definition: pose.h:164
requires(!range_container< Container >) const expr std
Returns index of an element in tuple 't', for which call to predicate f(x) holds true,...
Definition: algorithm.h:127
constexpr vector< N > operator-(point< N > a, point< N > const &b)
Returns a result of subtraction of A from B, viz -= operator.
Definition: point.h:84
constexpr view< iterators::numeric_iterator< Numeric > > range(Numeric from, Numeric to)
Builds numeric view over interval [from, to)
Definition: range.h:34
std::ostream & operator<<(std::ostream &os, string_buffer< N > const &sb)
Definition: string_buffer.h:112
N
Definition: static_storage.h:97
constexpr bool operator==(pose const &x, pose const &y)
compares poses on their position and orientation
Definition: pose.h:93