Matrix
Version: v0.2.0
Status: Complete
Header: include/stratax/core/containers/Matrix.hpp
Overview
Matrix<T> is Stratax's rank-2 owning array container for numeric types.
It stores values contiguously in row-major order and pairs storage with Shape and Strides metadata for consistent container behavior across the library.
Responsibilities
The Matrix class is responsible for:
- Owning contiguous rank-2 element storage
- Exposing row/column shape and stride metadata
- Providing checked and unchecked element access
- Providing iterator access over contiguous row-major storage
- Supporting copy/move ownership semantics
The Matrix class is not responsible for:
- Rank-1 or rank-N container semantics
- Broadcasting policy decisions
- High-level numerical algorithms
- Tensor reshaping outside matrix-specific APIs
Relationships
Matrix<T>
│
├── shape_ : core::Shape
├── strides_ : core::Strides
└── buffer_ : core::Buffer<T>
Depends on:
Used by:
- Tensor conversion and interop paths
- Generic Stratax container utilities
Related classes:
- Shape
- Strides
- Buffer
- Vector
- Tensor
Internal Data
| Member | Description |
| core::Shape shape_ | Matrix shape metadata (rows, cols) |
| core::Strides strides_ | Row-major stride metadata |
| core::Buffer<T> buffer_ | Contiguous row-major element storage |
Invariants
The following conditions are always true:
- rank() is 2 for constructed matrices.
- size() equals rows() * cols() (with overflow guarded at construction).
- data() points to row-major contiguous storage when non-empty.
- at(row, col) validates bounds and supports negative indexing.
- operator[](flat) is unchecked flat access.
Public Interface
Iterator Aliases
using iterator = typename core::Buffer<T>::iterator;
using const_iterator = typename core::Buffer<T>::const_iterator;
using reverse_iterator = typename core::Buffer<T>::reverse_iterator;
using const_reverse_iterator = typename core::Buffer<T>::const_reverse_iterator;
Matrix<T> mirrors Buffer<T> iterator aliases for flat row-major traversal.
Constructors
Default Constructor
Constructs an empty matrix with shape (0, 0).
Complexity
Throws
Rows/Cols Constructor
Matrix(std::size_t rows, std::size_t cols);
Constructs a matrix with default-initialized elements.
Complexity
Throws
Shape Constructor
explicit Matrix(const core::Shape& shape);
Constructs from a validated rank-2 shape.
Complexity
Throws
Fill Constructor
Matrix(std::size_t rows, std::size_t cols, const T& value);
Constructs and fills every element with value.
Complexity
Throws
Nested Initializer List Constructor
Matrix(std::initializer_list<std::initializer_list<T>> list);
Constructs from nested row lists in row-major order.
Complexity
Throws
Copy Constructor
Matrix(const Matrix&) = default;
Complexity
Move Constructor
Matrix(Matrix&&) noexcept = default;
Complexity
Destructor
Complexity
Assignment Operators
Copy Assignment
Matrix& operator=(const Matrix&) = default;
Complexity
Move Assignment
Matrix& operator=(Matrix&&) noexcept = default;
Complexity
Methods
size()
[[nodiscard]] std::size_t size() const noexcept;
Returns total number of stored elements.
Complexity
empty()
[[nodiscard]] bool empty() const noexcept;
Returns whether no elements are stored.
Complexity
rows() / cols()
[[nodiscard]] std::size_t rows() const noexcept;
[[nodiscard]] std::size_t cols() const noexcept;
Returns matrix dimensions.
Complexity
shape() / strides() / rank()
const stratax::core::Strides& strides() const noexcept;
[[nodiscard]] std::size_t rank() const noexcept;
Stores a list of dimension lengths for an array shape.
Returns matrix metadata.
Complexity
at()
T& at(std::ptrdiff_t row, std::ptrdiff_t col);
const T& at(std::ptrdiff_t row, std::ptrdiff_t col) const;
Returns element with bounds checking and negative-index normalization.
Complexity
Throws
front() / back()
T& front();
const T& front() const;
T& back();
const T& back() const;
Returns first/last flat element.
Preconditions
- Matrix must not be empty.
Complexity
data()
[[nodiscard]] T* data() noexcept;
[[nodiscard]] const T* data() const noexcept;
Returns pointer to contiguous row-major storage.
Complexity
Iterators
[[nodiscard]] iterator begin() noexcept;
[[nodiscard]] const_iterator begin() const noexcept;
[[nodiscard]] const_iterator cbegin() const noexcept;
[[nodiscard]] iterator end() noexcept;
[[nodiscard]] const_iterator end() const noexcept;
[[nodiscard]] const_iterator cend() const noexcept;
[[nodiscard]] reverse_iterator rbegin() noexcept;
[[nodiscard]] const_reverse_iterator rbegin() const noexcept;
[[nodiscard]] const_reverse_iterator crbegin() const noexcept;
[[nodiscard]] reverse_iterator rend() noexcept;
[[nodiscard]] const_reverse_iterator rend() const noexcept;
[[nodiscard]] const_reverse_iterator crend() const noexcept;
Provides forward and reverse iteration over flat row-major storage.
Complexity
fill()
void fill(const T& value);
Assigns value to every element.
Complexity
swap()
void swap(Matrix& other) noexcept;
Exchanges metadata and storage with another matrix.
Complexity
Operators
operator()(row, col)
T& operator()(std::size_t row, std::size_t col);
const T& operator()(std::size_t row, std::size_t col) const;
Returns element by row/column with bounds checking.
Complexity
Throws
operator[]
T& operator[](std::size_t index) noexcept;
const T& operator[](std::size_t index) const noexcept;
Unchecked flat indexing in row-major order.
Complexity
See Also
T& at(std::ptrdiff_t row, std::ptrdiff_t col);
const T& at(std::ptrdiff_t row, std::ptrdiff_t col) const;
Complexity Summary
| Operation | Complexity |
| Default construction | O(1) |
| Rows/cols/shape/fill/list construction | O(n) |
| Copy construction | O(n) |
| Move construction | O(1) |
| Copy assignment | O(n) |
| Move assignment | O(1) |
| Destruction | O(n) |
| size() / rows() / cols() / rank() / empty() | O(1) |
| shape() / strides() | O(1) |
| operator() / operator[] / at() | O(1) |
| front() / back() | O(1) |
| Iteration | O(n) |
| fill() | O(n) |
| swap() | O(1) |
Examples
Creating Matrices
Stores a rank-2 Stratax array in row-major order.
Accessing Elements
auto x = m(1, 0);
T & at(std::ptrdiff_t row, std::ptrdiff_t col)
Returns an element by row and column.
Iteration
for (const auto& value : m)
{
std::cout << value << '\n';
}
Design Notes
Matrix<T> keeps shape/stride metadata explicit while storing values in one contiguous row-major Buffer<T>. This mirrors Vector and Tensor internals and simplifies shared indexing and conversion logic.
Checked APIs (operator()(row, col), at) coexist with unchecked flat access (operator[]) to balance safety and performance.
Future Improvements
- Add row/column view APIs
- Add slicing/submatrix view support
- Add specialized SIMD kernels for common element-wise operations
See Also