From 39e04d971229f3ca0d1e080d55bf7ac2f7894562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Mon, 19 Jun 2023 09:35:48 -0300 Subject: [PATCH] lib: matrix: SquareMatrix: Deal with the special case of M=1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/lib/matrix/matrix/SquareMatrix.hpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/lib/matrix/matrix/SquareMatrix.hpp b/src/lib/matrix/matrix/SquareMatrix.hpp index 20ecd67f2c..4a1d3d0a81 100644 --- a/src/lib/matrix/matrix/SquareMatrix.hpp +++ b/src/lib/matrix/matrix/SquareMatrix.hpp @@ -322,6 +322,19 @@ SquareMatrix expm(const Matrix &A, size_t order = 5) return res; } +/** + * Deal with the special case where the square matrix is 1 + */ +template +bool inv(const SquareMatrix &A, SquareMatrix &inv, size_t rank = 1) +{ + if (std::fabs(A(0, 0)) < Type(FLT_EPSILON)) { + return false; + } + + inv(0, 0) = Type(1) / A(0, 0); + return true; +} /** * inverse based on LU factorization with partial pivotting