Modernize InvertPSDMatrix with if constexpr

Use if constexpr for kSize checks in InvertPSDMatrix to allow for
dead-code elimination at compile time. This ensures that for small
fixed-size matrices, only the inverse() path is compiled, and for
larger or dynamic matrices, only the LLT path is compiled when
assume_full_rank is true.

Change-Id: I4def2faadd080a4defccf1c0527015ae004f20ce
This commit is contained in:
Sameer Agarwal
2026-03-23 11:42:24 -07:00
parent 2f946a582a
commit 87c406d9d0
+3 -2
View File
@@ -60,12 +60,13 @@ typename EigenTypes<kSize, kSize>::Matrix InvertPSDMatrix(
// //
// https://eigen.tuxfamily.org/dox/group__TutorialLinearAlgebra.html#title3 // https://eigen.tuxfamily.org/dox/group__TutorialLinearAlgebra.html#title3
if (assume_full_rank) { if (assume_full_rank) {
if (kSize > 0 && kSize < 5) { if constexpr (kSize > 0 && kSize < 5) {
return m.inverse(); return m.inverse();
} } else {
return m.template selfadjointView<Eigen::Upper>().llt().solve( return m.template selfadjointView<Eigen::Upper>().llt().solve(
MType::Identity(size, size)); MType::Identity(size, size));
} }
}
// For a thin SVD the number of columns of the matrix need to be dynamic. // For a thin SVD the number of columns of the matrix need to be dynamic.
using SVDMType = typename EigenTypes<kSize, Eigen::Dynamic>::Matrix; using SVDMType = typename EigenTypes<kSize, Eigen::Dynamic>::Matrix;