akreon-linalg
Fixed-size linear algebra and numerical methods in C++26.
Loading...
Searching...
No Matches
matrix.hpp
Go to the documentation of this file.
1/*
2 * Copyright © 2025-2026 Akreon LLC
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6#pragma once
7
9
10#include <array>
11#include <cstddef>
12#include <span>
13#include <type_traits>
14
17
18namespace linalg {
19
43template <Field T, std::size_t Rows, std::size_t Cols>
44struct Matrix {
46 constexpr Matrix() = default;
47
57 template <typename... RowVectors>
58 requires(sizeof...(RowVectors) == Rows && Rows > 0 &&
59 (std::same_as<remove_cvref_t<RowVectors>, Vector<T, Cols>> && ...))
60 constexpr explicit Matrix(RowVectors&&... rows) {
61 std::size_t row = 0;
62 const auto copy_row = [this, &row](const Vector<T, Cols>& values) {
63 for (std::size_t col = 0; col < Cols; ++col)
64 (*this)(row, col) = values(col);
65 ++row;
66 };
67 (copy_row(rows), ...);
68 }
69
74 constexpr const T& operator()(std::size_t row, std::size_t col) const {
75 AK_ASSERT(row < Rows && col < Cols &&
76 "Matrix const access index out of bounds");
77 return elements[row * Cols + col];
78 }
79
84 constexpr T& operator()(std::size_t row, std::size_t col) {
85 AK_ASSERT(row < Rows && col < Cols &&
86 "Matrix mutable access index out of bounds");
87 return elements[row * Cols + col];
88 }
89
96 constexpr auto row(std::size_t index) const {
97 AK_ASSERT(index < Rows && "Matrix row index out of bounds");
99 for (std::size_t col = 0; col < Cols; ++col)
100 result(col) = (*this)(index, col);
101 return result;
102 }
103
105 constexpr auto begin() { return elements.begin(); }
107 constexpr auto begin() const { return elements.begin(); }
109 constexpr auto end() { return elements.end(); }
111 constexpr auto end() const { return elements.end(); }
113 constexpr auto view() { return std::span<T, Rows * Cols>{elements}; }
115 constexpr auto view() const {
116 return std::span<const T, Rows * Cols>{elements};
117 }
119 constexpr auto size() const { return Rows * Cols; }
120
122 constexpr bool operator==(const Matrix&) const = default;
123
124private:
125 std::array<T, Rows * Cols> elements{};
126};
127
133template <Field T, std::size_t Cols, typename... Rows>
134Matrix(Vector<T, Cols>, Rows...) -> Matrix<T, 1 + sizeof...(Rows), Cols>;
135
137template <Field T, std::size_t Dim>
138constexpr auto as_row_matrix(const Vector<T, Dim>& vector) {
140}
141
143template <Field T, std::size_t Dim>
144constexpr auto as_col_matrix(const Vector<T, Dim>& vector) {
146 for (std::size_t i = 0; i < Dim; ++i) result(i, 0) = vector(i);
147 return result;
148}
149
156template <Field T, std::size_t Rows, std::size_t Cols>
157 requires(Rows == 1 || Cols == 1)
158constexpr auto as_vector(const Matrix<T, Rows, Cols>& matrix) {
159 constexpr auto Dim = Rows == 1 ? Cols : Rows;
161 for (std::size_t i = 0; i < Dim; ++i)
162 result(i) = Rows == 1 ? matrix(0, i) : matrix(i, 0);
163 return result;
164}
165
166} // namespace linalg
#define AK_ASSERT(x)
Definition assert.hpp:13
std::remove_cvref_t< T > remove_cvref_t
Remove top-level const, volatile, and reference qualification.
constexpr auto as_vector(const ColVector< T, N > &A)
express col vector as vector
constexpr std::size_t mat_rows_v
Registered compile-time row count after cv/ref normalization.
constexpr auto as_row_matrix(const Vector< T, Dim > &vector)
Represent a vector as a 1 x Dim matrix.
Definition matrix.hpp:138
constexpr auto as_col_matrix(const Vector< T, Dim > &vector)
Represent a vector as a Dim x 1 matrix.
Definition matrix.hpp:144
Owning, fixed-size, row-major matrix container over a scalar field.
Definition matrix.hpp:44
constexpr auto end() const
Const iterator one past the final flattened element.
Definition matrix.hpp:111
constexpr auto end()
Iterator one past the final flattened element.
Definition matrix.hpp:109
constexpr bool operator==(const Matrix &) const =default
Compare shape-compatible matrices element by element.
constexpr Matrix()=default
Construct a value-initialized matrix.
constexpr auto size() const
Number of stored scalar elements (Rows * Cols).
Definition matrix.hpp:119
constexpr auto begin()
Iterator to the first element in flattened row-major order.
Definition matrix.hpp:105
constexpr const T & operator()(std::size_t row, std::size_t col) const
Read an element at a zero-based row and column.
Definition matrix.hpp:74
constexpr auto begin() const
Const iterator to the first element in row-major order.
Definition matrix.hpp:107
constexpr auto view()
Mutable, fixed-extent view of contiguous row-major storage.
Definition matrix.hpp:113
constexpr T & operator()(std::size_t row, std::size_t col)
Read or modify an element at a zero-based row and column.
Definition matrix.hpp:84
constexpr Matrix(RowVectors &&... rows)
Construct by copying one fixed-size vector per matrix row.
Definition matrix.hpp:60
constexpr auto row(std::size_t index) const
Copy one matrix row into a fixed-size vector.
Definition matrix.hpp:96
constexpr auto view() const
Const, fixed-extent view of contiguous row-major storage.
Definition matrix.hpp:115
Owning, fixed-size vector container.
Definition vector.hpp:36