bugGNU Octave - Bugs: bug #45820, linspace() incompatibility with...

 
 

bug #45820: linspace() incompatibility with Matlab when N < 2

Submitter:  José Luis García Pallero <jgpallero>
Submitted:  Tue 25 Aug 2015 11:42:01 AM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Fixed 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
   

Jump to the original submission

Sat 29 Aug 2015 02:16:21 PM UTC, comment #14: 

I fixed this to be Matlab compatible on the development branch in this changeset (http://hg.savannah.gnu.org/hgweb/octave/rev/16b9ec39ff46).  Closing report.

Rik <rik5>
Group administrator
Wed 26 Aug 2015 04:57:49 PM UTC, comment #13: 

It produces an error:


>>linspace (1, [], 3)
Error using .*
Matrix dimensions must agree.

Error in linspace (line 30)


Guillaume <gyom>
Wed 26 Aug 2015 04:32:59 PM UTC, comment #12: 

Thanks Guillaume for testing with an ancient version of Matlab.  I think it is safe to bring Octave up to date and announce the change in the NEWS file.

One last test


linspace (1, [], 3)


Does this silently return an empty matrix or does it produce an error because the second input is invalid?

Rik <rik5>
Group administrator
Wed 26 Aug 2015 10:52:27 AM UTC, comment #11: 

There has indeed been a change in MATLAB's behavior.

R2010b returns:


>> linspace(1,10,0)

ans =

    10


while R2011a returns:


>> linspace(1,10,0)

ans =

   Empty matrix: 1-by-0


The help text is the same in both releases:


For N < 2, LINSPACE returns X2.


but with recent versions of MATLAB, it now states:


For N = 1, LINSPACE returns X2.


Guillaume <gyom>
Wed 26 Aug 2015 08:16:18 AM UTC, comment #10: 

Oopsie that was the wrong window... (Octave)

Here's what ML r2014a really says:

>> x = [1:5]';
linspace (x, 10, 0)
ans =
Error using  .*
Matrix dimensions must agree.

Error in linspace ...   <ML code snipped>




Philip Nienhuis <philipnienhuis>
Group Member
Wed 26 Aug 2015 08:09:42 AM UTC, comment #9: 

@Rik, comment #7:
ML r2014a does this:

>> x = [1:5]';
>> linspace (x, 10, 0)
ans =

   10
   10
   10
   10
   10

>>


so it isn't quite empty as you expected.....

Philip Nienhuis <philipnienhuis>
Group Member
Wed 26 Aug 2015 12:25:27 AM UTC, comment #8: 

Attached is an attempt at a cset for this.  It appears to work. 

One of the things I get from reading the Matlab documentation is that matrices and vectors are not accepted inputs.  I kind of agree that matrices don't make much sense.  Maybe we should delete the implementation for that?  On the other hand, I do kind of like the ability to use vectors.  But if the input is a row vector, shouldn't the result be expanded down rather than across?  It appears that the input is always made a column vector before expanding.


linspace ([1 2 3], 4, 3)
ans =

   1.0000   2.5000   4.0000
   2.0000   3.0000   4.0000
   3.0000   3.5000   4.0000




(file #34730)

Rik <rik5>
Group administrator
Tue 25 Aug 2015 11:34:19 PM UTC, comment #7: 

Octave gets the single/double distinction correct.  Here is a part of the code in data.cc for Flinspace


octave_value arg_1 = args(0);
octave_value arg_2 = args(1);

if (arg_1.is_single_type () || arg_2.is_single_type ())
  {
    if (arg_1.is_complex_type () || arg_2.is_complex_type ())
      retval = do_linspace<FloatComplexMatrix> (arg_1, arg_2, npoints);
    else
      retval = do_linspace<FloatMatrix> (arg_1, arg_2, npoints);

  }
else
  {
    if (arg_1.is_complex_type () || arg_2.is_complex_type ())
      retval = do_linspace<ComplexMatrix> (arg_1, arg_2, npoints);
    else
      retval = do_linspace<Matrix> (arg_1, arg_2, npoints);
  }


What does Matlab return for this?


x = [1:5]';
linspace (x, 10, 0)


It should be empty, but I wonder if the dimensions are 5x0?  Although, I'm not certain that we need to copy them exactly in that regard.


Rik <rik5>
Group administrator
Tue 25 Aug 2015 10:21:39 PM UTC, comment #6: 

Forgot to tack these on the end...



>> class(linspace(1, single(10), 10))
ans =
single
>> class(linspace(1, 10, single(10)))
ans =
double


So only floating point arrays are valid, double or single, and the types of the first two arguments work according to the normal casting rules, type of 3rd argument doesn't matter.

Mike Miller <mtmiller>
Group Member
Tue 25 Aug 2015 10:19:19 PM UTC, comment #5: 


>> linspace(10, 20, 2)
ans =
    10    20
>> linspace(10, 20, 1)
ans =
    20
>> linspace(10, 20, 0)
ans =
   Empty matrix: 1-by-0
>> linspace(10, 20, -1)
ans =
   Empty matrix: 1-by-0

>> linspace(uint8(1), uint8(10), uint8(10))
Error using .*
Integers can only be combined with integers of the same class, or scalar doubles.

>> class(linspace(1, 10, 10))
ans =
double
>> class(linspace(1, 10, uint8(10))
ans =
double
>> class(linspace(single(1), 10, 10))
ans =
single


Mike Miller <mtmiller>
Group Member
Tue 25 Aug 2015 07:01:35 PM UTC, comment #4: 

I don't know what behavior to match, but the code in Octave is quite clear.  As an example, liboctave/array/dMatrix.cc begins with


Matrix linspace (const ColumnVector& x1,
                 const ColumnVector& x2,
                 octave_idx_type n)

{
  if (n < 1) n = 1;


which guarantees that at least one element is returned regardless of the value of N.

Rik <rik5>
Group administrator
Tue 25 Aug 2015 05:00:26 PM UTC, comment #3: 

I can't be sure or check now, but I'll bet that the strange behavior matches some old version of Matlab, even for negative values of N.

For sure, the fact that Matlab returns a 1x0 sized array is a new thing because old versions of Matlab did not have empty arrays with some non-zero dimensions.

Also, how does Matlab's linspace work for non-double arguments?  It looks like Octave doesn't preserve the type of the argument, so a double array is produced for something like


linspace (uint8(1), uint8(10), uint8(10))


Also, what does Matlab do if the types of the arguments don't match?

I'm guessing that's another compatibility problem that could be a separate bug report.

John W. Eaton <jwe>
Group administrator
Tue 25 Aug 2015 04:47:16 PM UTC, comment #2: 

I think there was already some special handling for linspace when N is small.  According to the documentation,


For compatibility with MATLAB, return the second argument (LIMIT)
if fewer than two values are requested.


What I think is supposed to happen is that for linspace (BASE, LIMIT, 1) it should return LIMIT rather than BASE.  But for linspace (BASE, LIMIT, 0) it does seem that an empty matrix rather than LIMIT should be returned.

Just to check, can the original reporter run the following examples in Matlab and report the results?


linspace (10, 20, 2)
linspace (10, 20, 1)
linspace (10, 20, 0)
linspace (10, 20, -1)


Octave is a little strange because even for -1 it returns 20 which is most likely to be incorrect.

Rik <rik5>
Group administrator
Tue 25 Aug 2015 01:24:45 PM UTC, comment #1: 

Confirmed w. Octave-4.1.0+ (tip from Aug. 25, 2015) and verified in ML r2014a.

Release -> dev  +  typo in title fixed.

Philip Nienhuis <philipnienhuis>
Group Member
Tue 25 Aug 2015 11:42:01 AM UTC, original submission:  

Hello:

In Octave, linspace(10,20,0) returns

ans = 20

while in Matlab, the same order returns

ans = Empty matrix: 1-by-0


I think this is an incompatibility issue. I'm using Octave 4.0, so I don't know if the behavior is also present in old releases

José Luis García Pallero <jgpallero>

 

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

Attach Files:
   
   
Comment:
   

Attached Files
file #34730:  linspace.cset added by rik5 (6KiB - application/octet-stream)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by gyom (Posted a comment)
  • -email is unavailable- added by mtmiller (Posted a comment)
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by jgpallero (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 7 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2015-08-29 rik5 StatusConfirmed Fixed
        Open/ClosedOpen Closed
    2015-08-26 rik5 Attached File- Added linspace.cset, #34730
    2015-08-25 rik5 Summarylinspace() incompatibility with Matlab, argumento \'N\' linspace() incompatibility with Matlab when N < 2
    2015-08-25 philipnienhuis Release4.0.0 dev
        Summarylinspace() incampatibility with Matlab, argumento \'N\' linspace() incompatibility with Matlab, argumento 'N'
        StatusNone Confirmed

    Back to the top

    Powered by Savane 3.13-5884.
    Corresponding source code