Stratax 0.4.0rc1
Loading...
Searching...
No Matches
Matrix.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include <array>
7#include <cstddef>
8#include <initializer_list>
9
14
15namespace stratax::container {
16
46template<typename T>
47requires DType<T>
48class Matrix : public core::ArrayBase<T>
49{
50public:
73
74private:
82 static core::Shape initializer_shape(
83 std::initializer_list<std::initializer_list<value_type>> list)
84 {
85 const size_type rows = list.size();
86 const size_type cols = rows == 0 ? 0 : list.begin()->size();
87
88 for (const auto& row : list)
89 {
90 if (row.size() != cols)
91 {
93 "Matrix initializer rows must have equal lengths.");
94 }
95 }
96
97 return core::Shape{rows, cols};
98 }
99
100protected:
103
104public:
106 using core::ArrayBase<T>::at;
107
112 Matrix() : Matrix(0, 0) {}
113
115 Matrix(const Matrix&) = default;
117 Matrix& operator=(const Matrix&) = default;
118
126 Matrix(Matrix&& other) : Matrix() { swap(other); }
127
136 {
137 if (this != &other)
138 {
139 Matrix moved(std::move(other));
140 swap(moved);
141 }
142 return *this;
143 }
144
155 : core::ArrayBase<T>(core::Shape{rows, cols})
156 {}
157
169 : core::ArrayBase<T>(core::Shape{rows, cols}, value)
170 {}
171
181 explicit Matrix(const core::Shape& shape)
182 : core::ArrayBase<T>(shape)
183 {
184 if (shape.rank() != 2)
185 {
186 throw Exceptions::RankError("Matrix requires a rank-2 shape.");
187 }
188 }
189
204 Matrix(std::initializer_list<std::initializer_list<value_type>> list)
205 : core::ArrayBase<T>(initializer_shape(list))
206 {
207 size_type index = 0;
208
209 for (const auto& row : list)
210 {
211 for (const auto& value : row)
212 {
213 (*this)[index++] = value;
214 }
215 }
216 }
217
219 [[nodiscard]] size_type rows() const noexcept {return this->shape()[0];}
221 [[nodiscard]] size_type cols() const noexcept {return this->shape()[1];}
222
230 reference operator()(size_type row, size_type col) {return (*this)[row * cols() + col];}
238 const_reference operator()(size_type row, size_type col) const {return (*this)[row * cols() + col];}
239
247 reference at(difference_type row, difference_type col) {return (*this)[normalized_flat_offset(std::array<difference_type, 2>{row, col})];}
255 const_reference at(difference_type row, difference_type col) const {return (*this)[normalized_flat_offset(std::array<difference_type, 2>{row, col})];}
256
262 void swap(Matrix& other) noexcept {core::ArrayBase<T>::swap(other);}
269 friend void swap(Matrix& lhs, Matrix& rhs) noexcept {lhs.swap(rhs);}
270};
271
272} // namespace stratax::container
Shared owning storage and row-major metadata for containers.
Dtype and array interface concepts.
Stratax exception types derived from std::runtime_error.
Immutable dimension metadata and checked row-major strides.
Number of axes or supplied multidimensional indices does not match the required rank.
Shape incompatible with the requested operation or container.
Two-dimensional owning array of supported dtype values.
Definition Matrix.hpp:49
Matrix(size_type rows, size_type cols)
Constructs a value-initialized matrix with the requested dimensions.
Definition Matrix.hpp:154
typename core::ArrayBase< T >::pointer pointer
Mutable element pointer type.
Definition Matrix.hpp:62
friend void swap(Matrix &lhs, Matrix &rhs) noexcept
Exchanges two matrices using argument-dependent lookup.
Definition Matrix.hpp:269
typename core::ArrayBase< T >::const_pointer const_pointer
Read-only element pointer type.
Definition Matrix.hpp:64
typename core::ArrayBase< T >::const_iterator const_iterator
Read-only contiguous random-access iterator type.
Definition Matrix.hpp:68
typename core::ArrayBase< T >::difference_type difference_type
Signed type used for checked indices and iterator distances.
Definition Matrix.hpp:56
Matrix()
Constructs an empty matrix with shape {0, 0}.
Definition Matrix.hpp:112
typename core::ArrayBase< T >::const_reverse_iterator const_reverse_iterator
Read-only reverse iterator type.
Definition Matrix.hpp:72
typename core::ArrayBase< T >::value_type value_type
Stored element type inherited from ArrayBase.
Definition Matrix.hpp:52
Matrix & operator=(const Matrix &)=default
Replaces values and metadata with a deep copy; allocation failure leaves this object unchanged.
typename core::ArrayBase< T >::iterator iterator
Mutable contiguous random-access iterator type.
Definition Matrix.hpp:66
Matrix(Matrix &&other)
Transfers storage and leaves the source with shape {0, 0}.
Definition Matrix.hpp:126
typename core::ArrayBase< T >::reference reference
Mutable element reference type.
Definition Matrix.hpp:58
Matrix(std::initializer_list< std::initializer_list< value_type > > list)
Constructs a matrix by copying a rectangular nested initializer.
Definition Matrix.hpp:204
size_type rows() const noexcept
Returns the number of rows.
Definition Matrix.hpp:219
size_type cols() const noexcept
Returns the number of columns.
Definition Matrix.hpp:221
Matrix(const core::Shape &shape)
Constructs a value-initialized matrix from a rank-two shape.
Definition Matrix.hpp:181
typename core::ArrayBase< T >::size_type size_type
Unsigned type used for element counts and indices.
Definition Matrix.hpp:54
void swap(Matrix &other) noexcept
Exchanges storage and layout metadata with other.
Definition Matrix.hpp:262
Matrix(const Matrix &)=default
Copies values and metadata into independent storage.
const_reference operator()(size_type row, size_type col) const
Returns an element using unchecked row and column indices.
Definition Matrix.hpp:238
Matrix & operator=(Matrix &&other)
Transfers storage and preserves both matrices' rank invariants.
Definition Matrix.hpp:135
const_reference at(difference_type row, difference_type col) const
Returns an element using checked, Python-style indices.
Definition Matrix.hpp:255
typename core::ArrayBase< T >::reverse_iterator reverse_iterator
Mutable reverse iterator type.
Definition Matrix.hpp:70
typename core::ArrayBase< T >::const_reference const_reference
Read-only element reference type.
Definition Matrix.hpp:60
reference at(difference_type row, difference_type col)
Returns an element using checked, Python-style indices.
Definition Matrix.hpp:247
Matrix(size_type rows, size_type cols, const_reference value)
Constructs a matrix filled with copies of value.
Definition Matrix.hpp:168
reference operator()(size_type row, size_type col)
Returns an element using unchecked row and column indices.
Definition Matrix.hpp:230
Shared owning storage and layout base for Stratax array containers.
Definition ArrayBase.hpp:41
typename Buffer< T >::value_type value_type
Stored element type.
Definition ArrayBase.hpp:44
typename Buffer< value_type >::pointer pointer
Mutable element pointer type.
Definition ArrayBase.hpp:54
typename Buffer< value_type >::const_reference const_reference
Read-only element reference type.
Definition ArrayBase.hpp:52
const Shape & shape() const noexcept
Returns the logical shape metadata.
Definition ArrayBase.hpp:82
typename Buffer< value_type >::reverse_iterator reverse_iterator
Mutable reverse iterator type.
Definition ArrayBase.hpp:62
typename Buffer< value_type >::const_reverse_iterator const_reverse_iterator
Read-only reverse iterator type.
Definition ArrayBase.hpp:64
typename Buffer< value_type >::const_pointer const_pointer
Read-only element pointer type.
Definition ArrayBase.hpp:56
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:48
typename Buffer< value_type >::iterator iterator
Mutable contiguous random-access iterator type.
Definition ArrayBase.hpp:58
typename Buffer< value_type >::const_iterator const_iterator
Read-only contiguous random-access iterator type.
Definition ArrayBase.hpp:60
typename Buffer< value_type >::reference reference
Mutable element reference type.
Definition ArrayBase.hpp:50
ArrayBase(const ArrayBase &)=default
Copies storage and layout into independent buffers.
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:46
Stores the dimensions of a multidimensional array.
Definition Shape.hpp:36
size_type rank() const noexcept
Returns the number of dimensions.
Definition Shape.hpp:124
const_iterator begin() const noexcept
Returns a const iterator to the first dimension.
Definition Shape.hpp:227
Supported numeric or boolean element dtype.
Definition Concepts.hpp:74