From 136fcddcd38d0b8f3b40faf7c1cb7365d9b2a753 Mon Sep 17 00:00:00 2001 From: Jaron Kent-Dobias Date: Fri, 15 Jan 2021 14:45:19 +0100 Subject: Converted more of library to templates accepting generic Scalar types and p --- stereographic.hpp | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'stereographic.hpp') diff --git a/stereographic.hpp b/stereographic.hpp index 638923f..2dfa735 100644 --- a/stereographic.hpp +++ b/stereographic.hpp @@ -4,9 +4,10 @@ #include "p-spin.hpp" -Vector stereographicToEuclidean(const Vector& ζ) { +template +Vector stereographicToEuclidean(const Vector& ζ) { unsigned N = ζ.size() + 1; - Vector z(N); + Vector z(N); Scalar a = ζ.transpose() * ζ; Scalar b = 2 * sqrt(N) / (1.0 + a); @@ -20,9 +21,10 @@ Vector stereographicToEuclidean(const Vector& ζ) { return b * z; } -Vector euclideanToStereographic(const Vector& z) { +template +Vector euclideanToStereographic(const Vector& z) { unsigned N = z.size(); - Vector ζ(N - 1); + Vector ζ(N - 1); for (unsigned i = 0; i < N - 1; i++) { ζ(i) = z(i); @@ -31,9 +33,10 @@ Vector euclideanToStereographic(const Vector& z) { return ζ / (sqrt(N) - z(N - 1)); } -Matrix stereographicJacobian(const Vector& ζ) { +template +Matrix stereographicJacobian(const Vector& ζ) { unsigned N = ζ.size(); - Matrix J(N, N + 1); + Matrix J(N, N + 1); Scalar b = 1.0 + (Scalar)(ζ.transpose() * ζ); @@ -52,15 +55,16 @@ Matrix stereographicJacobian(const Vector& ζ) { return 4 * sqrt(N + 1) * J / pow(b, 2); } -std::tuple stereographicHamGradHess(const Tensor& J, const Vector& ζ, const Vector& z) { +template +std::tuple, Matrix> stereographicHamGradHess(const Tensor& J, const Vector& ζ, const Vector& z) { auto [hamiltonian, gradZ, hessZ] = hamGradHess(J, z); - Matrix jacobian = stereographicJacobian(ζ); + Matrix jacobian = stereographicJacobian(ζ); - Matrix metric = jacobian * jacobian.adjoint(); + Matrix metric = jacobian * jacobian.adjoint(); // The metric is Hermitian and positive definite, so a Cholesky decomposition can be used. - Vector grad = metric.llt().solve(jacobian) * gradZ; - Matrix hess = jacobian * hessZ * jacobian.transpose(); + Vector grad = metric.llt().solve(jacobian) * gradZ; + Matrix hess = jacobian * hessZ * jacobian.transpose(); return {hamiltonian, grad, hess}; } -- cgit v1.2.3-54-g00ecf