|
Stratax 0.3.1
|
Version: v0.2.0
Status: Complete
Header: include/stratax/containers/Matrix.hpp
stratax::container::Matrix<T> is a two-dimensional owning array for types that satisfy the Numeric concept. It derives from core::ArrayBase<T> and stores elements contiguously in row-major order.
A normally constructed matrix always has rank two. Empty matrices retain that rank and may have shape {0, 0}, {0, n}, or {n, 0}.
Matrix<T> is responsible for:
operator()(row, col) accessat(row, col) accessArrayBase<T>swapMatrix<T> does not directly implement broadcasting, reshaping, row/column views, or high-level numerical algorithms.
For every normally constructed matrix:
rank() == 2shape() == Shape{rows(), cols()}size() == rows() * cols(){cols(), 1} for nonzero column countsoperator[](row * cols() + col) addresses the same element as operator()(row, col) for valid indicesElement-count and stride multiplication are checked during construction. A moved-from matrix remains destructible and assignable, but its previous contents and layout must not be relied upon.
Matrix<T> republishes the complete container alias set from core::ArrayBase<T>:
Constructs an empty rank-two matrix with shape {0, 0}.
Complexity: O(1).
Constructs a matrix containing rows * cols value-initialized elements. For arithmetic types, value initialization produces zero.
Complexity: O(rows * cols).
Throws:
Exceptions::DimensionError if the element count or a stride overflowsstd::bad_alloc if allocation failsvalue_type constructionConstructs a value-initialized matrix from a rank-two shape. Shapes containing a zero dimension are valid.
Complexity: O(shape.elements()).
Throws:
Exceptions::ShapeError when shape.rank() != 2Exceptions::DimensionError if the element count or a stride overflowsstd::bad_alloc if allocation failsvalue_type constructionConstructs a matrix containing rows * cols copies of value.
Complexity: O(rows * cols).
It has the same overflow and allocation failure conditions as the dimensions constructor and may propagate exceptions from the value_type copy constructor.
Copies rows into contiguous row-major storage. Every row must have the same length. An empty outer list creates shape {0, 0}; a non-empty collection of empty rows creates shape {rows, 0}.
Complexity: O(rows * cols), with an additional O(rows) validation pass.
Throws:
Exceptions::ShapeError if row lengths differExceptions::DimensionError if the element count or a stride overflowsstd::bad_alloc if allocation failsvalue_type construction or assignmentThe compiler-generated special members use ArrayBase<T> semantics:
Copying duplicates element storage and metadata. Moving transfers their ownership. Copy operations are O(n), while move construction is O(1).
rows() returns shape()[0], and cols() returns shape()[1]. Both operations are O(1).
The inherited metadata interface is also available:
Computes the row-major offset row * cols() + col without checking either index.
Preconditions:
row < rows()col < cols()Complexity: O(1).
Checks each component independently. Valid row indices are [-rows(), rows()), and valid column indices are [-cols(), cols()). Negative components count backward from the corresponding dimension.
Complexity: O(1).
Throws Exceptions::IndexError if either component is invalid.
operator[] is unchecked. The inherited one-argument at(index) overload is explicitly retained with a using-declaration and supports negative flat indices.
The inherited front(), back(), and data() accessors are also available. front() and back() throw Exceptions::IndexError when the matrix is empty.
All individual element-access operations are O(1).
begin(), end(), their const variants, and all reverse iterator variants traverse the flat row-major sequence. Acquiring an iterator is O(1), and traversing all elements is O(size()).
fill(const_reference value) assigns value to every element in O(size()).
Both overloads exchange the buffer, shape, and strides in O(1). The non-member overload supports argument-dependent lookup:
| Operation | Complexity |
|---|---|
| Default construction | O(1) |
| Dimension, fill, shape, or list construction | O(rows * cols) |
| Copy construction or assignment | O(n) |
| Move construction | O(1) |
| Metadata query | O(1) |
| Flat or two-dimensional element access | O(1) |
| Iterator acquisition | O(1) |
| Complete traversal | O(n) |
fill() | O(n) |
swap() | O(1) |
The loop visits 10.0, 20.0, 35.0, then 40.0.
Keeping ownership and layout behavior in ArrayBase<T> gives Vector, Matrix, and Tensor consistent flat access and iterator semantics. Matrix<T> adds only rank-two validation, dimension queries, rectangular initializer handling, and two-dimensional indexing.