akreon-linalg
Fixed-size linear algebra and numerical methods in C++26.
Loading...
Searching...
No Matches
matrix_sqrt.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
19template <SquareMatrix MatIn>
20struct MatrixSqrt {
24 static constexpr std::size_t N = mat_rows_v<Mat>;
25
26 static_assert(Real<T>, "Sqrt assumes reals domain");
27 static_assert(N >= 1, "Sqrt requires matrix dim of at least 1");
28
35 constexpr auto operator()(const Mat& A) const { return apply(A); }
36
43 constexpr auto apply(const Mat& A) const {
44 // Initialize the coupled Denman-Beavers iterates for A^(1/2) and A^(-1/2).
45 Mat Y{A};
47
48 for (std::size_t i = 0; i < config.steps; ++i) {
49 const auto [Y_inv, Y_event] = MatrixInverse<Mat>()(Y);
51 return std::tuple(Mat{}, Mat{}, Y_event);
52
53 const auto [Z_inv, Z_event] = MatrixInverse<Mat>()(Z);
55 return std::tuple(Mat{}, Mat{}, Z_event);
56
57 // Y_{k+1} = (Y_k + Z_k^-1)/2; Z_{k+1} = (Z_k + Y_k^-1)/2.
58 const auto Y_next = T(0.5) * (Y + Z_inv);
59 const auto Z_next = T(0.5) * (Z + Y_inv);
60
61 // Accept when the square-root residual Y^2 - A meets tolerance.
62 if (all_equal(Y_next * Y_next, A, config.tol))
63 return std::tuple(Y_next, Z_next, linalg::NumericalEvent::NONE);
64
65 Y = Y_next;
66 Z = Z_next;
67 }
68
70 }
71
74 .steps = 128, .tol = T(1e-12), .floor_eps = T(1e-14)};
75};
76
77} // namespace linalg
Field classified by scalar_traits as real.
std::remove_cvref_t< T > remove_cvref_t
Remove top-level const, volatile, and reference qualification.
constexpr std::size_t mat_rows_v
Registered compile-time row count after cv/ref normalization.
constexpr bool all_equal(const Vec &X, const Vec &Y, real_type_t< vector_element_t< Vec > > tol=1e-10)
checks if two vectors are equivalent to a given tolerance (different than == operator on vector which...
typename mat_traits< remove_cvref_t< Mat > >::value_type matrix_scalar_t
Registered matrix scalar type after cv/ref normalization.
Configuration for generic numerical factorizations.
Matrix sqrt (X^{1/2})
constexpr auto apply(const Mat &A) const
Compute the matrix square root and inverse square root.
matrix_scalar_t< Mat > T
static constexpr std::size_t N
linalg::remove_cvref_t< MatIn > Mat
alias & types
constexpr auto operator()(const Mat &A) const
Compute the matrix square root and inverse square root.