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/core/validation/Validation.hpp>
11#include <stratax/exceptions/Exceptions.hpp>
12
13namespace stratax::container {
14
44template<typename T>
45requires DType<T>
46class Matrix : public core::ArrayBase<T>
47{
48public:
71
72private:
80 static core::Shape initializer_shape(
81 std::initializer_list<std::initializer_list<value_type>> list)
82 {
83 const size_type rows = list.size();
84 const size_type cols = rows == 0 ? 0 : list.begin()->size();
85
86 for (const auto& row : list)
87 {
88 if (row.size() != cols)
89 {
91 "Matrix initializer rows must all have the same number of columns.");
92 }
93 }
94
95 return core::Shape{rows, cols};
96 }
97
105 static const core::Shape& validate_shape(const core::Shape& shape)
106 {
107 if (shape.rank() != 2) {
109 "Matrix shape must be rank 2.");
110 }
111
112 return shape;
113 }
114
115protected:
117 using core::ArrayBase<T>::normalized_flat_offset;
118
119public:
121 using core::ArrayBase<T>::at;
122
127 Matrix() : Matrix(0, 0) {}
128
139 : core::ArrayBase<T>(core::Shape{rows, cols})
140 {}
141
153 : core::ArrayBase<T>(core::Shape{rows, cols}, value)
154 {}
155
165 explicit Matrix(const core::Shape& shape)
166 : core::ArrayBase<T>(validate_shape(shape))
167 {}
168
183 Matrix(std::initializer_list<std::initializer_list<value_type>> list)
184 : core::ArrayBase<T>(initializer_shape(list))
185 {
186 size_type index = 0;
187
188 for (const auto& row : list)
189 {
190 for (const auto& value : row)
191 {
192 (*this)[index++] = value;
193 }
194 }
195 }
196
198 [[nodiscard]] size_type rows() const noexcept {return this->shape()[0];}
200 [[nodiscard]] size_type cols() const noexcept {return this->shape()[1];}
201
209 reference operator()(size_type row, size_type col) {return (*this)[row * cols() + col];}
217 const_reference operator()(size_type row, size_type col) const {return (*this)[row * cols() + col];}
218
226 reference at(difference_type row, difference_type col) {return (*this)[normalized_flat_offset(std::array<difference_type, 2>{row, col})];}
234 const_reference at(difference_type row, difference_type col) const {return (*this)[normalized_flat_offset(std::array<difference_type, 2>{row, col})];}
235
241 void swap(Matrix& other) noexcept {core::ArrayBase<T>::swap(other);}
248 friend void swap(Matrix& lhs, Matrix& rhs) noexcept {lhs.swap(rhs);}
249};
250
251} // namespace stratax::container
Matrix(size_type rows, size_type cols)
Constructs a value-initialized matrix with the requested dimensions.
Definition Matrix.hpp:138
typename core::ArrayBase< T >::pointer pointer
Mutable element pointer type.
Definition Matrix.hpp:60
friend void swap(Matrix &lhs, Matrix &rhs) noexcept
Exchanges two matrices using argument-dependent lookup.
Definition Matrix.hpp:248
typename core::ArrayBase< T >::const_pointer const_pointer
Read-only element pointer type.
Definition Matrix.hpp:62
typename core::ArrayBase< T >::const_iterator const_iterator
Read-only contiguous random-access iterator type.
Definition Matrix.hpp:66
typename core::ArrayBase< T >::difference_type difference_type
Signed type used for checked indices and iterator distances.
Definition Matrix.hpp:54
Matrix()
Constructs an empty matrix with shape {0, 0}.
Definition Matrix.hpp:127
typename core::ArrayBase< T >::const_reverse_iterator const_reverse_iterator
Read-only reverse iterator type.
Definition Matrix.hpp:70
typename core::ArrayBase< T >::value_type value_type
Stored element type inherited from ArrayBase.
Definition Matrix.hpp:50
typename core::ArrayBase< T >::iterator iterator
Mutable contiguous random-access iterator type.
Definition Matrix.hpp:64
typename core::ArrayBase< T >::reference reference
Mutable element reference type.
Definition Matrix.hpp:56
Matrix(std::initializer_list< std::initializer_list< value_type > > list)
Constructs a matrix by copying a rectangular nested initializer.
Definition Matrix.hpp:183
size_type rows() const noexcept
Returns the number of rows.
Definition Matrix.hpp:198
size_type cols() const noexcept
Returns the number of columns.
Definition Matrix.hpp:200
Matrix(const core::Shape &shape)
Constructs a value-initialized matrix from a rank-two shape.
Definition Matrix.hpp:165
typename core::ArrayBase< T >::size_type size_type
Unsigned type used for element counts and indices.
Definition Matrix.hpp:52
void swap(Matrix &other) noexcept
Exchanges storage and layout metadata with other.
Definition Matrix.hpp:241
const_reference operator()(size_type row, size_type col) const
Returns an element using unchecked row and column indices.
Definition Matrix.hpp:217
const_reference at(difference_type row, difference_type col) const
Returns an element using checked, Python-style indices.
Definition Matrix.hpp:234
typename core::ArrayBase< T >::reverse_iterator reverse_iterator
Mutable reverse iterator type.
Definition Matrix.hpp:68
typename core::ArrayBase< T >::const_reference const_reference
Read-only element reference type.
Definition Matrix.hpp:58
reference at(difference_type row, difference_type col)
Returns an element using checked, Python-style indices.
Definition Matrix.hpp:226
Matrix(size_type rows, size_type cols, const_reference value)
Constructs a matrix filled with copies of value.
Definition Matrix.hpp:152
reference operator()(size_type row, size_type col)
Returns an element using unchecked row and column indices.
Definition Matrix.hpp:209
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
size_type normalized_flat_offset(const IndexContainer &raw_indices, const char *rank_mismatch_message="Multi-index rank must match array rank.", const char *component_oob_message=nullptr) const
Converts checked signed multidimensional indices to a flat offset.
typename Buffer< value_type >::const_pointer const_pointer
Read-only element pointer type.
Definition ArrayBase.hpp:53
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:122