akreon-linalg
Fixed-size linear algebra and numerical methods in C++26.
Loading...
Searching...
No Matches
affine_map.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
8#include "affine_traits.hpp"
11
12namespace affine {
13
15template <linalg::Field T>
17
19template <linalg::Field T>
21
27template <linalg::SquareMatrix Mat>
33
41template <linalg::MatrixContainer Mat>
42 requires(linalg::mat_rows_v<Mat> == 1)
43constexpr AffineMap<Mat> make_selector_map(std::size_t idx) {
45 "make_selector_map index must be within [0, mat_cols)");
46
48 Mat A{};
49 A(0, idx) = T(1);
51}
52
62template <linalg::MatrixContainer Mat>
63class AffineMap {
64public:
67
69 static constexpr std::size_t M = linalg::mat_rows_v<Mat>;
70 static constexpr std::size_t N = linalg::mat_cols_v<Mat>;
71
74
77
83 constexpr explicit AffineMap() {}
84
90 constexpr explicit AffineMap(const Mat& U_in) : U(U_in) {}
91
98 constexpr explicit AffineMap(const Mat& U_in, const AmbientVec& c_in)
99 : U(U_in), c(c_in) {}
100
106 constexpr explicit AffineMap(const linalg::Matrix<T, M + 1, N + 1>& H)
107 : U(get_block<0, M, 0, N>(H)),
108 c(as_vector(get_block<0, M, N, N + 1>(H))) {}
109
124 template <linalg::MatrixContainer OtherMat>
125 requires(std::same_as<T, linalg::matrix_scalar_t<OtherMat>> &&
127 constexpr auto compose(const AffineMap<OtherMat>& other) const {
128 constexpr std::size_t P = linalg::mat_cols_v<OtherMat>;
129 const auto U_comp = U * other.linear_part();
130 const auto c_comp = U * other.translation() + c;
131 return AffineMap<linalg::Matrix<T, M, P>>(U_comp, c_comp);
132 }
133
148 constexpr auto inverse() const {
149 static_assert(M >= N, "short-fat TBD");
150 const auto [U_inv, event] = linalg::MatrixInverse<decltype(U)>()(U);
151 static_cast<void>(event);
152 return AffineMap(U_inv, T(-1) * U_inv * c);
153 }
154
160 constexpr auto linear_part() const { return U; }
161
167 constexpr auto translation() const { return c; }
168
176 constexpr bool operator==(const AffineMap<Mat>& other) const {
177 return is_equal(other);
178 }
179
187 constexpr bool operator!=(const AffineMap<Mat>& other) const {
188 return !is_equal(other);
189 }
190
198 constexpr auto operator()(const DomVec& x) const { return U * x + c; }
199
200private:
202 Mat U; //< valid linear transformation mapping
203 AmbientVec c; //< c bias vector that translates the linear part to
204 // a valid affine subspace
205
213 constexpr bool is_equal(const AffineMap<Mat>& other) const {
214 const auto linear_equal = all_equal(other.linear_part(), linear_part());
215 const auto translation_equal =
216 all_equal(other.translation(), translation());
217 return linear_equal && translation_equal;
218 }
219};
220
221} // namespace affine
#define AK_ASSERT(x)
Definition assert.hpp:13
AffineMap class definition.
constexpr auto linear_part() const
linear part
constexpr bool operator==(const AffineMap< Mat > &other) const
equality comparison overload
linalg::Vector< T, M > AmbientVec
alias for underlying vector spanning R^m (codomain)
constexpr AffineMap()
CTOR.
linalg::matrix_scalar_t< Mat > T
alias to extract underlying type of affine set
constexpr AffineMap(const Mat &U_in)
CTOR.
static constexpr std::size_t M
helpers to extracting M x N matrix
constexpr auto operator()(const DomVec &x) const
transform point to coords
constexpr AffineMap(const Mat &U_in, const AmbientVec &c_in)
CTOR.
constexpr auto translation() const
translation
static constexpr std::size_t N
constexpr AffineMap(const linalg::Matrix< T, M+1, N+1 > &H)
CTOR.
constexpr auto inverse() const
inverse mapping
constexpr auto compose(const AffineMap< OtherMat > &other) const
compose function
constexpr bool operator!=(const AffineMap< Mat > &other) const
inequality comparison overload
constexpr AffineMap< Mat > make_map_id()
make map ID
constexpr AffineMap< Mat > make_selector_map(std::size_t idx)
make map selector
constexpr std::size_t mat_rows_v
Registered compile-time row count after cv/ref normalization.
typename mat_traits< remove_cvref_t< Mat > >::value_type matrix_scalar_t
Registered matrix scalar type after cv/ref normalization.
Owning, fixed-size, row-major matrix container over a scalar field.
Definition matrix.hpp:44
Owning, fixed-size vector container.
Definition vector.hpp:36