bugGNU Octave - Bugs: bug #59890, inf+1i*inf gives nan+1i*inf

 
 

bug #59890: inf+1i*inf gives nan+1i*inf

Submitted by:  Marco Caliari <caliari>
Submitted on:  Sat 16 Jan 2021 03:01:27 PM UTC  
 
Category:  None Severity:  3 - Normal
Priority:  3 - Low Item Group:  Matlab Compatibility
Status:  Confirmed Assigned to:  None
Originator Name:  Open/Closed:  Open
Release:  dev Operating System:  GNU/Linux

Add a New Comment (Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

( Jump to the original submission)

Thu 04 Feb 2021 06:40:41 PM UTC, comment #9: 

The decision comes down to whether it is worth the programmer time to implement an exception for scalar multiplication of complex numbers.  I believe (and jwe wrote this in comment #1) that we just forward scalar multiplication to the C++ math library.  There, the rules for scalar multiplication say that 0 x Inf = NaN so a complex scalar value with a 0 real becomes NaN.  Matlab, by contrast, seems to not perform multiplications when one of the values is zero.

For vectors and matrices, Matlab is equivalent to Octave and just forwards to a BLAS library.  Taking jwe's example,

x = [i,2,3], y = Inf(3,1), x*y
Matlab: NaN + NaNi
Octave: NaN - NaNi

Aside from a difference in the sign of the complex value, this is the same result.  However, now change to using scalar multiplication which because of the orientation of the vectors also results in broadcasting.

x = [i,2,3], y = Inf(3,1), x.*y
Matlab:
[ 0 + Infi, Inf + 0i, Inf + 0i
  0 + Infi, Inf + 0i, Inf + 0i
  0 + Infi, Inf + 0i, Inf + 0i ]

Octave:
[ NaN + Infi, Inf - NaNi, Inf - NaNi
[ NaN + Infi, Inf - NaNi, Inf - NaNi
[ NaN + Infi, Inf - NaNi, Inf - NaNi ]

At the core of the difference are these four multiplications which in Matlab result in

complex (0,0) * Inf
ans = NaN
complex (0,1) * Inf
ans = 0 + Infi
complex (1,0) * Inf
ans = Inf
complex (1,1) * Inf
ans = Inf + Infi

In Octave the results are

complex (0,0) * Inf
ans = NaN - NaNi
complex (0,1) * Inf
ans = NaN + Infi
complex (1,0) * Inf
ans = Inf - NaNi
complex (1,1) * Inf
ans = Inf + Infi

I'm changing the Item Group to Matlab Compatibility since we are different in this regard, and marking as Confirmed.  But I'm lowering the priority since I think there are more interesting bugs to fix ahead of this one.

Rik <rik5>
Project Administrator
Thu 04 Feb 2021 05:07:38 PM UTC, comment #8: 

To create -Inf+InfI in Octave, you can use

x = complex (-Inf, Inf)

Then exp (x) will produce 0.  That is what the Fortran program is doing.

The following version is more like what Octave is doing:

      program tstexp
      double precision zero, one, inf
      complex* 16 im, cval
      zero = 0.0d0
      one = 1.0d0
      inf = one / zero
      im = cmplx (zero, one)
      cval = -inf + im*inf;
      write(6,*) cval, exp(cval)
      stop
      end

We could fix the specific case of scalar multiplication of Inf and a complex value with zero real (or imaginary) part.  And we could use our own complex class internally in Octave to fix more possible occurrences of these multiplications for operations that are done directly in Octave code.  But that wouldn't fix things like

x = [i,2,3], y = Inf(3,1), x*y

where the multiplication is handled by a BLAS function and won't use our overloaded class definition.

John W. Eaton <jwe>
Project Administrator
Sun 17 Jan 2021 12:33:01 PM UTC, comment #7: 

This looks very similar to bug #31974.
Is it a duplicate?

Markus M├╝tzel <mmuetzel>
Project Member
Sun 17 Jan 2021 09:14:13 AM UTC, comment #6: 

I explain you how I met this case: briefly, I noticed that

octave:1> exp(-realmax+1i*realmax)
ans = -0

and then tried

octave:1> exp(-inf+1i*inf)
ans =  NaN + NaNi

Matlab gives 0 in this second case and also this simple fortran77 program

      program tstexp
      double precision d
      complex* 16 a
      d = 0.0d0
      a = cmplx(-1.0/d,1.0d0/d)
      write(6,*) a,exp(a)
      stop
      end

Marco Caliari <caliari>
Project Member
Sat 16 Jan 2021 07:34:51 PM UTC, comment #5: 

The core piece is this statement

1i*Inf

In Octave this results in NaN + Infi, while in Matlab it is 0 + Infi.  It would seem that

(x + yi) * C = C*x + C*yi

except when x or y is zero in which case Matlab does not bother with the multiplication.

Octave ordinarily just passes a multiplication like this down to BLAS.  Either Matlab is checking for zeros and not performing multiplications on those values as is done for sparse matrices.  Or maybe they have their own customized version of BLAS to do this action, or maybe there is a flag to pass to BLAS that determines how the non-finite values NaN, +Inf, -Inf are handled.

Rik <rik5>
Project Administrator
Sat 16 Jan 2021 07:11:04 PM UTC, comment #4: 

Is this a duplicate, or at least fundamentally related, to bug #31974 from 10 years ago?

Rik <rik5>
Project Administrator
Sat 16 Jan 2021 06:33:55 PM UTC, comment #3: 

Most likely it is not by (Matlab's) design , but a result of using a different underlying c library.

Dmitri.
--

Dmitri A. Sergatskov <dasergatskov>
Sat 16 Jan 2021 06:13:57 PM UTC, comment #2: 

I was expecting Inf+Infi, although I understand your reasoning. The same is true for

octave:1> 1+1i*inf
ans =  NaN + Infi

while Matlab gives 1+1i*inf. So, at least, it is a Matlab incompatibility.

Marco Caliari <caliari>
Project Member
Sat 16 Jan 2021 04:56:41 PM UTC, comment #1: 

Are you expecting NaN+NaNi?

There is no pure imaginary type and we just use C++ complex arithmetic operations.  So you are seeing

Inf + (0,1)*Inf  =>
Inf + (NaN,Inf)  =>
(NaN,Inf)

Isn't that what C++ <complex> always does or is that behavior unspecified by the standard?

Even if we fixed this for Octave (by introducing special cases or our own special complex class, it would not fix it for external libraries that perform complex arithmetic.

John W. Eaton <jwe>
Project Administrator
Sat 16 Jan 2021 03:01:27 PM UTC, original submission:  

I think this is not the desired behaviour

octave:1> inf+1i*inf
ans =  NaN + Infi

I see it on 6.0.0 and 7.0.0.

Marco Caliari <caliari>
Project Member

 

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

Attach Files:
   
   
Comment:
   

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 mmuetzel (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by dasergatskov (Posted a comment)
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by caliari (Submitted the item)
  •  

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.

     

     

     

    Follow 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2021-02-04 rik5 Priority5 - Normal => 3 - Low
        Item GroupIncorrect Result => Matlab Compatibility
        StatusNone => Confirmed

    Back to the top


    Powered by Savane 3.5