bugGNU Octave - Bugs: bug #63277, isfinite returns full array in...


bug #63277: isfinite returns full array in Matlab for sparse input

Submitter:  Rik <rik5>
Submitted:  Thu 27 Oct 2022 12:17:46 AM UTC
Category:  Octave Function Severity:  2 - Minor
Priority:  5 - Normal Item Group:  WTF, Matlab?!?
Status:  Wont Fix Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup

Tue 15 Nov 2022 05:39:10 PM UTC, comment #5: 

I checked in a final changeset to avoid "! isfinite" in assert.m.  See http://hg.savannah.gnu.org/hgweb/octave/rev/84e8f0bf83f7.

I'm going to mark this bug as "Won't Fix" since we disagree with Matlab in always converting isfinite output to a full matrix. 

Closing report.

Rik <rik5>
Group administrator
Thu 27 Oct 2022 10:00:02 PM UTC, comment #4: 

I used grep to find a list of isfinite instances where the output is negated (and hence could be large if the input were sparse).  The instances are

+matlab/+lang/MemoizedFunction.m:134:                  || ! isfinite (val))
+matlab/+lang/MemoizedFunction.m:140:              if (! isscalar (val) || ! isnumeric (val) || ! isfinite (val) ...
general/int2str.m:80:  nan_inf = ! isfinite (n(:));
general/quadgk.m:374:      if (any (! isfinite (q_subs)))
general/quadgk.m:450:      if (any (! isfinite (q_subs)(:)))
general/quadl.m:112:  elseif (any (! isfinite (q(:))))
general/quadv.m:154:  elseif (any (! isfinite (q(:))))
linear-algebra/cond.m:67:    elseif (any (! isfinite (A(:))))
miscellaneous/mustBeInteger.m:53:  elseif (any (! isfinite (x)))
optimization/glpk.m:526:  if (any (! isfinite (b(:))))
optimization/glpk.m:593:            || any (! isfinite (sense)))
polynomial/roots.m:89:  elseif (any (! isfinite (c)))
set/uniquetol.m:207:      eq_rows(i,1) = eq_rows(i,1) || any (! isfinite (Arow_i), 2);
specfun/primes.m:55:  if (! isfinite (n) && n != -Inf)
testfun/assert.m:351:                      | ! isfinite (A_null_real) & ! isfinite (B_null_real);
testfun/assert.m:362:                        | ! isfinite (A_null_imag) & ! isfinite (B_null_imag);

Most of these can be ignored because the input can be guaranteed to be small.  I changed cond.m and mustBeInteger.m in this changeset: http://hg.savannah.gnu.org/hgweb/octave/rev/303bbdc3c536.

The remaining question is whether to change the two instances in assert.m  It will require introducing two new temporary variables, making their calculation dependent on whether the input is full or sparse, and then clearing those variables at the end of the block doing this calculation.

Rik <rik5>
Group administrator
Thu 27 Oct 2022 09:12:35 PM UTC, comment #3: 

I posted this bug report because I wanted other developer opinions.  My own opinion matches that of jwe: Matlab made an odd choice to have isfinite return a full matrix.  Not only is this inconsistent with similar isXXX functions, but it presumes to know more than the programmer.  In this case, the intermediate "isfinite (x)" will be dense but "! isfinite (x)" will be back to being pretty sparse.  I think the programmer should make the decision here.  If they want a full matrix they can use full(), otherwise it stays as it's original type.  As another example, would you expect a function to convert a single data type to a double data type without the consent of the programmer?

Rik <rik5>
Group administrator
Thu 27 Oct 2022 03:16:28 AM UTC, comment #2: 

the fix was related to this problem identified through the help mailing list:


I'm guessing your input to `glpk` is sparse. Line 507 of `glpk.m` uses:
  if (any (! isfinite (A(:))))
    error ("glpk: The values in A must be finite");

`isfinite (A(:))` creates a sparse Boolean matrix. However, it is likely not
sparsely populated which might be the cause for the memory issue.

Does it work if you edit that file and change the above lines to the following?
  if (any (isinf (A(:))) || any (isnan(A(:))))
    error ("glpk: The values in A must be finite");

that turned out to solve the problem for that user, and so was pushed as a potential fix to prevent reoccurrence.

Nicholas Jankowski <nrjank>
Group Member
Thu 27 Oct 2022 02:58:49 AM UTC, comment #1: 

I can understand converting to full for any function that will return true for an input of 0 since that is guaranteed to turn a very sparse matrix into something that is mostly filled with non-zero entries.  But the same would happen for "sparse_matrix == 0", and I don't think that operator gets the same special treatment.  If I'm right about that, then maybe this is a candidate for tagging as "WTF, Matlab".

John W. Eaton <jwe>
Group administrator
Thu 27 Oct 2022 12:17:46 AM UTC, original submission:  

In changeset 800eb86438cc a modification was made to avoid using isfinite() on sparse input.  I'm not sure what problem that was addressing, but it did lead me to find a difference between Octave and Matlab.

Sample code:


x = sparse ([Inf, 1; 2 NaN]);
isfinite (x)
ans = 2x2 logical array
1 0
0 1


ans =

Compressed Column Sparse (rows = 2, cols = 2, nnz = 2 [50%])

  (2, 1) -> 1
  (1, 2) -> 1

I don't know if we want to follow Matlab or not.  It seems like this is inconsistent with isinf() and isnan() functions which do return a sparse output for sparse input in Matlab.  Octave is arguably being correct here by keeping that pattern.

Rik <rik5>
Group administrator


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

No files currently attached


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by nrjank (Posted a comment)
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by rik5 (Submitted the item)

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.


    Follow 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2022-11-15 rik5 StatusConfirmed Wont Fix
        Open/ClosedOpen Closed
    2022-10-27 rik5 Item GroupMatlab Compatibility WTF, Matlab?!?

    Back to the top

    Powered by Savane 3.13-4b48.
    Corresponding source code