akreon-linalg
Fixed-size linear algebra and numerical methods in C++26.
Loading...
Searching...
No Matches
operator_traits.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
11
12namespace linalg {
13
21enum struct SymmetricOperatorType { INV = 0, IND, PSD, PD, NSD, ND };
22
25
27template <SquareMatrix Mat, typename Derived>
28struct Endomorphism;
29
31template <SquareMatrix Mat, typename Derived>
32struct SymmetricOperator;
33
35template <SquareMatrix Mat>
36struct SymmetricMatrix;
37
39template <SquareMatrix Mat>
40struct Hessenberg;
41
43template <SquareMatrix Mat, DifferentialType Type>
44struct Hessian;
45
47template <SquareMatrix Mat>
48struct LDL;
49
51template <SquareMatrix Mat, typename Circ>
52struct QuadraticRep;
53
55template <SquareMatrix Mat>
56struct Cholesky;
57
59template <SquareMatrix Mat>
62 using Point = Mat;
65
66 static constexpr std::size_t N = mat_rows_v<Mat>;
67 static constexpr std::size_t DIM = N * (N + 1) / 2;
69};
70
71template <SquareMatrix Mat>
75 static constexpr std::size_t N = mat_rows_v<Mat>;
76
77 static constexpr auto to_coordinates(const Mat& element) {
79 for (std::size_t row = 0, index = 0; row < N; ++row) {
80 for (std::size_t col = 0; col <= row; ++col, ++index) {
81 coordinate(index) = element(row, col);
82 if (row != col) coordinate(index) *= sqrt2_v<T>;
83 }
84 }
85 return coordinate;
86 }
87
88 static constexpr auto from_coordinates(
90 Mat element;
91 for (std::size_t row = 0, index = 0; row < N; ++row) {
92 for (std::size_t col = 0; col <= row; ++col, ++index) {
93 auto value = coordinate(index);
94 if (row != col) value /= sqrt2_v<T>;
95 element(row, col) = element(col, row) = value;
96 }
97 }
98 return element;
99 }
100};
101
103template <SquareMatrix Mat, typename Derived>
106 static constexpr std::size_t ROWS = mat_rows_v<Mat>;
107 static constexpr std::size_t COLS = mat_cols_v<Mat>;
108 using MatT = Mat;
109};
110
112template <SquareMatrix Mat, typename Derived>
115 static constexpr std::size_t ROWS = mat_rows_v<Mat>;
116 static constexpr std::size_t COLS = mat_cols_v<Mat>;
117 using MatT = Mat;
118};
119
121template <SquareMatrix Mat>
124 static constexpr std::size_t ROWS = mat_rows_v<Mat>;
125 static constexpr std::size_t COLS = mat_cols_v<Mat>;
126 using MatT = Mat;
127};
128
130template <SquareMatrix Mat>
131struct mat_traits<Hessenberg<Mat>> {
133 static constexpr std::size_t ROWS = mat_rows_v<Mat>;
134 static constexpr std::size_t COLS = mat_cols_v<Mat>;
135 using MatT = Mat;
136};
137
139template <SquareMatrix Mat, DifferentialType Type>
140struct mat_traits<Hessian<Mat, Type>> {
142 static constexpr std::size_t ROWS = mat_rows_v<Mat>;
143 static constexpr std::size_t COLS = mat_cols_v<Mat>;
144 using MatT = Mat;
145};
146
148template <SquareMatrix Mat>
149struct mat_traits<LDL<Mat>> {
151 static constexpr std::size_t ROWS = mat_rows_v<Mat>;
152 static constexpr std::size_t COLS = mat_cols_v<Mat>;
153 using MatT = Mat;
154};
155
157template <SquareMatrix Mat, typename Circ>
160 static constexpr std::size_t ROWS = mat_rows_v<Mat>;
161 static constexpr std::size_t COLS = mat_cols_v<Mat>;
162 using MatT = Mat;
163};
164
166template <SquareMatrix Mat>
167struct mat_traits<Cholesky<Mat>> {
169 static constexpr std::size_t ROWS = mat_rows_v<Mat>;
170 static constexpr std::size_t COLS = mat_cols_v<Mat>;
171 using MatT = Mat;
172};
173
175template <SquareMatrix Mat, typename Derived>
178 static constexpr std::size_t N = mat_rows_v<Mat>;
179 static constexpr std::size_t dim = N * N;
181};
182
184template <SquareMatrix Mat, typename Derived>
187 static constexpr std::size_t N = mat_rows_v<Mat>;
188 static constexpr std::size_t dim = N * (N + 1) / 2;
190};
191
193template <SquareMatrix Mat>
196 static constexpr std::size_t N = mat_rows_v<Mat>;
197 static constexpr std::size_t dim = N * (N + 1) / 2;
199};
200
202template <SquareMatrix Mat>
205 static constexpr std::size_t N = mat_rows_v<Mat>;
206 static constexpr std::size_t dim = N * N;
208};
209
211template <SquareMatrix Mat, DifferentialType Type>
214 static constexpr std::size_t N = mat_rows_v<Mat>;
215 static constexpr std::size_t dim = N * N;
217};
218
220template <SquareMatrix Mat>
221struct vector_traits<LDL<Mat>> {
223 static constexpr std::size_t N = mat_rows_v<Mat>;
224 static constexpr std::size_t dim = N * (N + 1) / 2;
226};
227
229template <SquareMatrix Mat, typename Circ>
232 static constexpr std::size_t N = mat_rows_v<Mat>;
233 static constexpr std::size_t dim = N * N;
235};
236
238template <SquareMatrix Mat>
241 static constexpr std::size_t N = mat_rows_v<Mat>;
242 static constexpr std::size_t dim = N * (N + 1) / 2;
244};
245
247template <typename Space>
248struct is_symmetric_matrix_space : std::false_type {};
249
251template <SquareMatrix Mat>
252struct is_symmetric_matrix_space<SymmetricMatrix<Mat>> : std::true_type {};
253
255template <typename Space>
256inline constexpr bool is_symmetric_matrix_space_v =
258
259template <class Operator>
261
262template <typename T, std::size_t Rows, std::size_t Cols>
269
270template <class Operator>
271 requires requires {
272 typename mat_traits<remove_cvref_t<Operator>>::value_type;
274 }
281
282template <class Operator>
284
285template <class Operator>
287
288template <class Operator>
290
291template <class Operator>
293
294template <class Operator>
296
297template <MatrixContainer Mat>
298constexpr auto apply(const Mat& matrix, const operator_domain_t<Mat>& vector)
301 for (std::size_t row = 0; row < mat_rows_v<Mat>; ++row)
302 for (std::size_t col = 0; col < mat_cols_v<Mat>; ++col)
303 result(row) += matrix(row, col) * vector(col);
304 return result;
305}
306
307template <class Operator>
309 requires(const remove_cvref_t<Operator>& op,
311 { apply(op, value) } -> std::same_as<operator_codomain_t<Operator>>;
312 } ||
313 requires(const remove_cvref_t<Operator>& op,
315 { op.apply(value) } -> std::same_as<operator_codomain_t<Operator>>;
316 };
317
318template <class Operator>
321 std::same_as<operator_domain_t<Operator>, operator_codomain_t<Operator>>;
322} // namespace linalg
Trait-registered finite-dimensional mathematical space.
std::remove_cvref_t< T > remove_cvref_t
Remove top-level const, volatile, and reference qualification.
SymmetricOperatorType
simple enum to define symmetric matrix types INV = Invalid IND = Indefinite PSD = Positive Semidefini...
DifferentialType
differential type
typename operator_traits_t< Operator >::scalar_type operator_scalar_t
typename operator_traits_t< Operator >::domain_type operator_domain_t
constexpr std::size_t mat_rows_v
Registered compile-time row count after cv/ref normalization.
typename operator_traits_t< Operator >::codomain_type operator_codomain_t
typename space_traits< remove_cvref_t< Space > >::coordinate_type space_coordinate_t
Finite coordinate representation registered for a space.
constexpr auto apply(const Mat &matrix, const operator_domain_t< Mat > &vector) -> operator_codomain_t< Mat >
constexpr bool is_symmetric_matrix_space_v
symmetric matrix space type trait var
typename space_traits< remove_cvref_t< Space > >::scalar_type space_scalar_t
Scalar field registered for a space.
typename mat_traits< remove_cvref_t< Mat > >::value_type matrix_scalar_t
Registered matrix scalar type after cv/ref normalization.
typename operator_traits_t< Operator >::matrix_type operator_matrix_t
Factors a real positive-definite matrix as A = G G^T.
Definition cholesky.hpp:20
Endomorphism operator CRTP base.
Reduces A to upper Hessenberg form H = U^* A U.
Hessian as a linear operator.
Definition hessian.hpp:25
Factors a real symmetric matrix as A = L D L^T without pivoting.
Definition ldl.hpp:22
Owning, fixed-size, row-major matrix container over a scalar field.
Definition matrix.hpp:44
forward declaration of P(x) / P(x^{-1})
forward declaration for a matrix
forward declaration for symmetric operator
Owning, fixed-size vector container.
Definition vector.hpp:36
static constexpr auto from_coordinates(const space_coordinate_t< Space > &coordinate)
static constexpr auto to_coordinates(const Mat &element)
Customization point mapping space elements to finite coordinates.
symmetric matrix space type trait
Customization point for compile-time matrix metadata.
typename mat_traits< remove_cvref_t< Operator > >::MatT matrix_type
typename vector_traits< remove_cvref_t< Operator > >::Vec domain_type
typename mat_traits< remove_cvref_t< Operator > >::value_type scalar_type
Customization point for finite-dimensional space metadata.
Customization point for fixed-size vector metadata.