Mercurial > octave
changeset 31357:303bbdc3c536
Avoid out-of-memory errors for sparse matrices and ! isfinite usage.
* cond.m: Replace "! isifinite (A)" input validation with a try/catch block
because svd() already performs this check.
* mustBeInteger.m: Use "! isfinite" for full matrices and
"isinf (x) || isnan (x)" test for sparse matrices.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 27 Oct 2022 14:54:27 -0700 |
parents | 07a3ceab58d6 |
children | 1193e869de98 |
files | scripts/linear-algebra/cond.m scripts/miscellaneous/mustBeInteger.m |
diffstat | 2 files changed, 8 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/linear-algebra/cond.m Thu Oct 27 10:38:47 2022 -0400 +++ b/scripts/linear-algebra/cond.m Thu Oct 27 14:54:27 2022 -0700 @@ -64,10 +64,12 @@ if (p == 2) if (isempty (A)) c = 0.0; - elseif (any (! isfinite (A(:)))) - error ("cond: A must not contain Inf or NaN values"); else - sigma = svd (A); + try + sigma = svd (A); + catch + error ("cond: A must not contain Inf or NaN values"); + end_try_catch sigma_1 = sigma(1); sigma_n = sigma(end); if (sigma_1 == 0 || sigma_n == 0)
--- a/scripts/miscellaneous/mustBeInteger.m Thu Oct 27 10:38:47 2022 -0400 +++ b/scripts/miscellaneous/mustBeInteger.m Thu Oct 27 14:54:27 2022 -0700 @@ -50,7 +50,9 @@ but = sprintf ("it was non-numeric (found a %s)", class (x)); elseif (! isreal (x)) but = "it was complex"; - elseif (any (! isfinite (x))) + elseif (issparse (x) && (any (isinf (x)) || any (isnan (x)))) + but = "there were non-finite values"; + elseif (! issparse (x) && any (! isfinite (x))) but = "there were non-finite values"; elseif (any (x != fix (x))) but = "it had fractional values in some elements";