Skip to content

Instantly share code, notes, and snippets.

@DavidAce
Created November 24, 2019 09:43
Show Gist options
  • Save DavidAce/f48afbbc0e82ceedeb4711c71e812f59 to your computer and use it in GitHub Desktop.
Save DavidAce/f48afbbc0e82ceedeb4711c71e812f59 to your computer and use it in GitHub Desktop.
diff --git a/Eigen/src/SVD/BDCSVD.h b/Eigen/src/SVD/BDCSVD.h
index 1134d66e7..bb9944b41 100644
--- a/Eigen/src/SVD/BDCSVD.h
+++ b/Eigen/src/SVD/BDCSVD.h
@@ -921,7 +921,17 @@ void BDCSVD<MatrixType>::perturbCol0
Index i = perm(l);
if(i!=k)
{
- Index j = i<k ? i : perm(l-1);
+ //Sometimes we get i >= k and l == 0, leading to perm(l-1) being out of bounds
+ //Here we make sure that perm isn't accessed out of bounds.
+ //However, when this happens, the resulting U,S and V^T matrices will usually contain
+ //NAN's, but at least we then get a chance to do something about it, instead of segfault.
+
+ //Index j = i<k ? i : perm(l-1);
+ Index j;
+ if (i<k) j = i;
+ else if (l > 0 and l < m) j = perm(l-1);
+ else continue;
+
prod *= ((singVals(j)+dk) / ((diag(i)+dk))) * ((mus(j)+(shifts(j)-dk)) / ((diag(i)-dk)));
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
if(i!=k && std::abs(((singVals(j)+dk)*(mus(j)+(shifts(j)-dk)))/((diag(i)+dk)*(diag(i)-dk)) - 1) > 0.9 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment