From 857a8e59c9aac045c1814b6055cb4d64081dce17 Mon Sep 17 00:00:00 2001 From: Antonin Portelli Date: Wed, 16 Feb 2022 18:54:16 +0000 Subject: [PATCH] corr to var and SVD dynamic range --- lib/Core/Math.cpp | 25 ++++++++++++++++++++++++- lib/Core/Math.hpp | 5 +++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/Core/Math.cpp b/lib/Core/Math.cpp index df9e8ba..5278412 100644 --- a/lib/Core/Math.cpp +++ b/lib/Core/Math.cpp @@ -29,7 +29,8 @@ using namespace Latan; ******************************************************************************/ DMat MATH_NAMESPACE::varToCorr(const DMat &var) { - DMat res = var, invDiag = res.diagonal(); + DMat res = var; + DVec invDiag = res.diagonal(); invDiag = invDiag.cwiseInverse().cwiseSqrt(); res = (invDiag*invDiag.transpose()).cwiseProduct(res); @@ -37,6 +38,28 @@ DMat MATH_NAMESPACE::varToCorr(const DMat &var) return res; } +DMat MATH_NAMESPACE::corrToVar(const DMat &corr, const DVec &varDiag) +{ + DMat res = corr; + DVec varSqrtDiag = varDiag.cwiseSqrt(); + + res = (varSqrtDiag*varSqrtDiag.transpose()).cwiseProduct(res); + + return res; +} + +double MATH_NAMESPACE::svdDynamicRange(const DMat &mat) +{ + DVec s = mat.singularValues(); + + return s.maxCoeff()/s.minCoeff(); +} + +double MATH_NAMESPACE::svdDynamicRangeDb(const DMat &mat) +{ + return 10.*log10(svdDynamicRange(mat)); +} + /****************************************************************************** * Standard C functions * ******************************************************************************/ diff --git a/lib/Core/Math.hpp b/lib/Core/Math.hpp index 3802ceb..0ece91a 100644 --- a/lib/Core/Math.hpp +++ b/lib/Core/Math.hpp @@ -70,6 +70,11 @@ namespace MATH_NAMESPACE // convert variance matrix to correlation matrix DMat varToCorr(const DMat &var); + DMat corrToVar(const DMat &corr, const DVec &varDiag); + + // matrix SVD dynamic range + double svdDynamicRange(const DMat &mat); + double svdDynamicRangeDb(const DMat &mat); // Constants constexpr double pi = 3.1415926535897932384626433832795028841970;