bugGNU Octave - Bugs: bug #52285, inv(0) gives 0 (instead of inf)

 
 

bug #52285: inv(0) gives 0 (instead of inf)

Submitter:  Colin Macdonald <cbm>
Submitted:  Thu 26 Oct 2017 08:20:24 AM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Regression
Status:  Fixed Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * dev Operating System:  * GNU/Linux
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Mon 22 Nov 2021 12:55:59 AM UTC, comment #14: 

Finally re-visited this bug report after 4 years.  I checked in a compatibility fix for Matlab here: http://hg.savannah.gnu.org/hgweb/octave/rev/98400baa509f.

This will be part of the 7.1 release of Octave.

Rik <rik5>
Group administrator
Mon 06 Nov 2017 02:19:44 PM UTC, comment #13: 

I wonder if it mostly depends on what version of gcc/glibc MW used to compiled a particular version....

Dmitri.
--

Dmitri A. Sergatskov <dasergatskov>
Mon 06 Nov 2017 08:42:35 AM UTC, comment #12: 

I confirm all NaNs for the example in comment #9


>> inv (repmat (Inf, [1]))
Warning: Matrix is singular to working precision.

ans =

     0

>> inv (repmat (Inf, [2,2]))
Warning: Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN.

ans =

   NaN   NaN
   NaN   NaN

>> inv (repmat (Inf, [3,3]))
Warning: Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN.

ans =

   NaN   NaN   NaN
   NaN   NaN   NaN
   NaN   NaN   NaN

>> version

ans =

    '9.3.0.713579 (R2017b)'


Marco Caliari <caliari>
Group Member
Mon 06 Nov 2017 05:43:05 AM UTC, comment #11: 

I also get the all-nans-with-zero-in-lower-right result on 2008b.  But strangely on 2014a, I get all nans:


>> inv(repmat(inf,[2,2]))
Warning: Matrix is singular, close to singular or badly scaled. Results may be
inaccurate. RCOND = NaN.
ans =
   NaN   NaN
   NaN   NaN
>> inv(repmat(-inf,[3,3]))
Warning: Matrix is singular, close to singular or badly scaled. Results may be
inaccurate. RCOND = NaN.
ans =
   NaN   NaN   NaN
   NaN   NaN   NaN
   NaN   NaN   NaN
>> ver
MATLAB Version: 8.3.0.532 (R2014a)


Colin Macdonald <cbm>
Sat 04 Nov 2017 09:32:18 PM UTC, comment #10: 

Rik,And you thought you could not have it both ways:

>> inv (repmat (Inf, [2,2]))

Warning: Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND =
NaN.

ans =

   NaN   NaN
   NaN     0

>> version


ans =

    '9.3.0.713579 (R2017b)'

>>



Michael Godfrey <godfrey>
Group Member
Sat 04 Nov 2017 08:23:59 PM UTC, comment #9: 

The number of corner cases is just ridiculous.  What does Matlab return for


inv (repmat (Inf, [2,2]))


I'm guessing it is either all zeros or all NaNs.


Rik <rik5>
Group administrator
Sat 04 Nov 2017 10:05:01 AM UTC, comment #8: 


>> inv (complex (0,0))

Warning: Matrix is singular to working precision.

ans =

   Inf

>> version


ans =

    '9.3.0.713579 (R2017b)'

>>


Michael Godfrey <godfrey>
Group Member
Sat 04 Nov 2017 04:20:08 AM UTC, comment #7: 

@Colin: Can you check this corner case?


inv (complex (0,0))


In Octave the result is strange


inv (complex (0,0))
warning: matrix singular to machine precision
ans = Inf - NaNi


I think better thing would be to do automatic narrowing before calling inv() so that the result would be Inf.

Rik <rik5>
Group administrator
Fri 03 Nov 2017 09:32:56 PM UTC, comment #6: 

Yes your guess is correct: Matlab gives the 2x2 matrix of Infs.

Colin Macdonald <cbm>
Fri 03 Nov 2017 03:52:39 PM UTC, comment #5: 

Another test case that needs to be run through Matlab


inv (diag ([0 0]))


In Octave, this produces a special Diagonal matrix with Inf only on the diagonal.


inv (diag ([0 0]))
ans =

Diagonal Matrix

   Inf     0
     0   Inf


On the other hand, my guess is that Matlab will return all Inf as in



 inv (full (diag ([0 0])))
warning: matrix singular to machine precision
ans =

   Inf   Inf
   Inf   Inf



Rik <rik5>
Group administrator
Wed 01 Nov 2017 11:01:08 PM UTC, comment #4: 

I stepped through the code in dMatrix.cc and in fact the MatrixType is set to Diagonal for the case of a scalar.  It looks like the general question is why there isn't an early decision to take care of diagonal matrices which would be very easy


  if (typ == MatrixType::Diagonal)
    ret = 1 / (*this);
  else if (...)


Find attached a quick test (inv.diff).  It seems to pass "make check" with no problems.  To be complete there should also be some BIST tests for the correct behavior of scalars of all types (double, single, double complex, single complex) with respect to the various operations '1 / x', 'inv (x)', x^-1, 'pinv (x)'.


(file #42320)

Rik <rik5>
Group administrator
Mon 30 Oct 2017 10:49:42 AM UTC, comment #3: 

The problem is around line 646 of dMatrix.cc. If the condition number is zero, any result is set Inf, except for the (numel() == 1 ) case. What was the purpose of such an explicit exclusion?
I have a more general question: why a scalar is treated as a full matrix and not (at least) as a triangular matrix?

Marco Caliari <caliari>
Group Member
Thu 26 Oct 2017 03:24:56 PM UTC, comment #2: 

There have been some previous reports on this.

See bug #49690: inv(inf) behaves differently from inf^-1, 1/inf, inf\1

See bug #51246: pinv(0) different from Matlab

As of 10/26/17 and hg id 857b553177a5, I get


octave:2> inv (Inf)
warning: matrix singular to machine precision
ans = 0
octave:3> inv (0)
warning: matrix singular to machine precision
ans = 0
octave:4> 1/0
warning: division by zero
ans = Inf
octave:5> pinv (0)
ans = Inf


The only problem is 'inv (0)'.


Rik <rik5>
Group administrator
Thu 26 Oct 2017 09:22:38 AM UTC, comment #1: 

Another edge case:


>> inv(Inf)
warning: matrix singular to machine precision
ans = Inf


That should be 0.

Ceral Paquet <octavebugs>
Thu 26 Oct 2017 08:20:24 AM UTC, original submission:  


octave:1> inv(0)
warning: matrix singular to machine precision
ans = 0


That should be "Inf" (e.g., in 4.2.1).

I think it changed sometime in the last few months but I have not bisected it yet to be sure.

Colin Macdonald <cbm>

 

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

Attach Files:
   
   
Comment:
   

Attached Files
file #42320:  inv.diff added by rik5 (5KiB - text/x-patch)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by dasergatskov (Posted a comment)
  • -email is unavailable- added by godfrey (Posted a comment)
  • -email is unavailable- added by caliari (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by octavebugs (Posted a comment)
  • -email is unavailable- added by cbm (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 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2021-11-22 rik5 StatusConfirmed Fixed
        Open/ClosedOpen Closed
    2017-11-01 rik5 Attached File- Added inv.diff, #42320
    2017-10-26 rik5 StatusNone Confirmed

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code