Stratax 0.3.1
Loading...
Searching...
No Matches
Matrix.hpp
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <initializer_list>
6
7#include <stratax/core/dtypes/Concepts.hpp>
8#include <stratax/core/ArrayBase.hpp>
9#include <stratax/core/Shape.hpp>
10#include <stratax/exceptions/Exceptions.hpp>
11
12namespace stratax::container {
13
43template<typename T>
44requires DType<T>
45class Matrix : public core::ArrayBase<T>
46{
47public:
70
71private:
79 static core::Shape initializer_shape(
80 std::initializer_list<std::initializer_list<value_type>> list)
81 {
82 const size_type rows = list.size();
83 const size_type cols = rows == 0 ? 0 : list.begin()->size();
84
85 for (const auto& row : list)
86 {
87 if (row.size() != cols)
88 {
90 "Matrix initializer rows must have equal lengths.");
91 }
92 }
93
94 return core::Shape{rows, cols};
95 }
96
97protected:
100
101public:
103 using core::ArrayBase<T>::at;
104
109 Matrix() : Matrix(0, 0) {}
110
121 : core::ArrayBase<T>(core::Shape{rows, cols})
122 {}
123
135 : core::ArrayBase<T>(core::Shape{rows, cols}, value)
136 {}
137
147 explicit Matrix(const core::Shape& shape)
148 : core::ArrayBase<T>(shape)
149 {
150 if (shape.rank() != 2)
151 {
152 throw Exceptions::RankError("Matrix requires a rank-2 shape.");
153 }
154 }
155
170 Matrix(std::initializer_list<std::initializer_list<value_type>> list)
171 : core::ArrayBase<T>(initializer_shape(list))
172 {
173 size_type index = 0;
174
175 for (const auto& row : list)
176 {
177 for (const auto& value : row)
178 {
179 (*this)[index++] = value;
180 }
181 }
182 }
183
185 [[nodiscard]] size_type rows() const noexcept {return this->shape()[0];}
187 [[nodiscard]] size_type cols() const noexcept {return this->shape()[1];}
188
196 reference operator()(size_type row, size_type col) {return (*this)[row * cols() + col];}
204 const_reference operator()(size_type row, size_type col) const {return (*this)[row * cols() + col];}
205
213 reference at(difference_type row, difference_type col) {return (*this)[normalized_flat_offset(std::array<difference_type, 2>{row, col})];}
221 const_reference at(difference_type row, difference_type col) const {return (*this)[normalized_flat_offset(std::array<difference_type, 2>{row, col})];}
222
228 void swap(Matrix& other) noexcept {core::ArrayBase<T>::swap(other);}
235 friend void swap(Matrix& lhs, Matrix& rhs) noexcept {lhs.swap(rhs);}
236};
237
238} // namespace stratax::container
Two-dimensional owning array of numeric values.
Definition Matrix.hpp:46
Matrix(size_type rows, size_type cols)
Constructs a value-initialized matrix with the requested dimensions.
Definition Matrix.hpp:120
typename core::ArrayBase< T >::pointer pointer
Mutable element pointer type.
Definition Matrix.hpp:59
friend void swap(Matrix &lhs, Matrix &rhs) noexcept
Exchanges two matrices using argument-dependent lookup.
Definition Matrix.hpp:235
typename core::ArrayBase< T >::const_pointer const_pointer
Read-only element pointer type.
Definition Matrix.hpp:61
typename core::ArrayBase< T >::const_iterator const_iterator
Read-only contiguous random-access iterator type.
Definition Matrix.hpp:65
typename core::ArrayBase< T >::difference_type difference_type
Signed type used for checked indices and iterator distances.
Definition Matrix.hpp:53
Matrix()
Constructs an empty matrix with shape {0, 0}.
Definition Matrix.hpp:109
typename core::ArrayBase< T >::const_reverse_iterator const_reverse_iterator
Read-only reverse iterator type.
Definition Matrix.hpp:69
typename core::ArrayBase< T >::value_type value_type
Stored element type inherited from ArrayBase.
Definition Matrix.hpp:49
typename core::ArrayBase< T >::iterator iterator
Mutable contiguous random-access iterator type.
Definition Matrix.hpp:63
typename core::ArrayBase< T >::reference reference
Mutable element reference type.
Definition Matrix.hpp:55
Matrix(std::initializer_list< std::initializer_list< value_type > > list)
Constructs a matrix by copying a rectangular nested initializer.
Definition Matrix.hpp:170
size_type rows() const noexcept
Returns the number of rows.
Definition Matrix.hpp:185
size_type cols() const noexcept
Returns the number of columns.
Definition Matrix.hpp:187
Matrix(const core::Shape &shape)
Constructs a value-initialized matrix from a rank-two shape.
Definition Matrix.hpp:147
typename core::ArrayBase< T >::size_type size_type
Unsigned type used for element counts and indices.
Definition Matrix.hpp:51
void swap(Matrix &other) noexcept
Exchanges storage and layout metadata with other.
Definition Matrix.hpp:228
const_reference operator()(size_type row, size_type col) const
Returns an element using unchecked row and column indices.
Definition Matrix.hpp:204
const_reference at(difference_type row, difference_type col) const
Returns an element using checked, Python-style indices.
Definition Matrix.hpp:221
typename core::ArrayBase< T >::reverse_iterator reverse_iterator
Mutable reverse iterator type.
Definition Matrix.hpp:67
typename core::ArrayBase< T >::const_reference const_reference
Read-only element reference type.
Definition Matrix.hpp:57
reference at(difference_type row, difference_type col)
Returns an element using checked, Python-style indices.
Definition Matrix.hpp:213
Matrix(size_type rows, size_type cols, const_reference value)
Constructs a matrix filled with copies of value.
Definition Matrix.hpp:134
reference operator()(size_type row, size_type col)
Returns an element using unchecked row and column indices.
Definition Matrix.hpp:196
Shared owning storage and layout base for Stratax array containers.
Definition ArrayBase.hpp:38
typename Buffer< T >::value_type value_type
Stored element type.
Definition ArrayBase.hpp:41
typename Buffer< value_type >::pointer pointer
Mutable element pointer type.
Definition ArrayBase.hpp:51
typename Buffer< value_type >::const_reference const_reference
Read-only element reference type.
Definition ArrayBase.hpp:49
ArrayBase(const Shape &shape)
Constructs value-initialized storage for shape.
const Shape & shape() const noexcept
Returns the logical shape metadata.
Definition ArrayBase.hpp:79
typename Buffer< value_type >::reverse_iterator reverse_iterator
Mutable reverse iterator type.
Definition ArrayBase.hpp:59
typename Buffer< value_type >::const_reverse_iterator const_reverse_iterator
Read-only reverse iterator type.
Definition ArrayBase.hpp:61
typename Buffer< value_type >::const_pointer const_pointer
Read-only element pointer type.
Definition ArrayBase.hpp:53
size_type normalized_flat_offset(const IndexContainer &raw_indices) const
Converts checked signed multidimensional indices to a flat offset.
typename Buffer< value_type >::difference_type difference_type
Signed type used for checked indices and iterator distances.
Definition ArrayBase.hpp:45
typename Buffer< value_type >::iterator iterator
Mutable contiguous random-access iterator type.
Definition ArrayBase.hpp:55
typename Buffer< value_type >::const_iterator const_iterator
Read-only contiguous random-access iterator type.
Definition ArrayBase.hpp:57
typename Buffer< value_type >::reference reference
Mutable element reference type.
Definition ArrayBase.hpp:47
void swap(ArrayBase &other) noexcept
Exchanges storage and layout metadata with other.
typename Buffer< value_type >::size_type size_type
Unsigned type used for element counts and indices.
Definition ArrayBase.hpp:43
Stores the dimensions of a multidimensional array.
Definition Shape.hpp:33
size_type rank() const noexcept
Returns the number of dimensions.
Definition Shape.hpp:121
const_iterator begin() const noexcept
Returns a const iterator to the first dimension.
Definition Shape.hpp:223