bugGNU Octave - Bugs: bug #61705, Wrong number of columns when...

 
 

bug #61705: Wrong number of columns when removing entry from empty vector

Submitter:  Vassilis Virvilis <vasvir>
Submitted:  Sun 19 Dec 2021 05:18:14 PM UTC
 
Category:  Interpreter Severity:  2 - Minor
Priority:  3 - Low Item Group:  Matlab Compatibility
Status:  Confirmed Assigned to:  None
Originator Name:  vasvir Open/Closed:  * Open
Release:  * dev Operating System:  * Any
* Mandatory Fields

Add a New Comment (Rich Markup)
   

Comment Type & Canned Response:
       

 

( Jump to the original submission )

Tue 10 May 2022 07:27:51 PM UTC, comment #9: 

just fyi the matlab behavior for comment #6:

>> x = []
x =
     []
>> x([])
ans =
     []
>> x(logical([]))
ans =
     []

[] always being 0x0

there seem to be a good number of matlab compatibilities creeping up over the past ~5 years or so that deal with empty matrix size, so it may very well be that matlab changed behavior (intentionally or not).  

Nicholas Jankowski <nrjank>
Project Member
Tue 10 May 2022 07:11:21 PM UTC, comment #8: 

I don't know whether that was always an error in our implementation or maybe an attempt to provide compatible behavior and then Matlab changed?

John W. Eaton <jwe>
Project Administrator
Tue 10 May 2022 06:05:13 PM UTC, comment #7: 

Following up on jwe's comment #6, this is only an issue with logical indexing.  I would note this comment in Array.cc at line 695

  // Numeric array or logical mask:
  //
  //   Note that logical mask indices are always transformed to vectors
  //   before we see them.
  //
  //   object   | index    | result orientation
  //   ---------+----------+-------------------
  //   vector   | vector   | indexed object
  //            | other    | same size as index
  //   ---------+----------+-------------------
  //   array    | anything | same size as index

It may be that the problem lies upstream of Array.cc when the logical masx is transformed to a vector.

Rik <rik5>
Project Administrator
Tue 10 May 2022 02:54:54 PM UTC, comment #6: 

I see

x = []
x([])  ==> 0x0
x(logical ([]))  ==> 0x1

so the behavior appears to be different (and incorrect) when performing logical mask indexing.

If possible, this problem should be fixed without adding new special cases.

John W. Eaton <jwe>
Project Administrator
Tue 10 May 2022 02:24:34 PM UTC, comment #5: 

comment #4:

> I would like to work on this, and believe the solution would be to use an if condition to treat indexing differently for empty matrices. Could someone point the code that would need to be changed to me?


A good place to start looking is probably here:
https://hg.savannah.gnu.org/hgweb/octave/file/default/liboctave/array/Array.cc#l695

Carlo de Falco <cdf>
Project Member
Sun 17 Apr 2022 10:48:48 PM UTC, comment #4: 

> I've lowered the severity since this is a minor issue that can be more appropriately handled by detecting empty matrices.  I've also changed the Item Group to Matlab Compatibility since it seems to generate a 0x0 matrix for the test case.


I would like to work on this, and believe the solution would be to use an if condition to treat indexing differently for empty matrices. Could someone point the code that would need to be changed to me?

Sarrah Bastawala <sarrah>
Thu 23 Dec 2021 12:19:56 AM UTC, comment #3: 

This is a minor nuisance, and Matlab isn't much better.  To quickly summarize, you cannot rely on columns() when a matrix is empty.  If your code will encounter empty matrices you may need to separate them and use different code for them in a special case.  You can separate them using isempty() or "if (numel (x) == 0)".

For a slightly longer explanation, both Matlab and Octave allow you to create arbitrarily-sized N-dimensional matrices.  And you can request the number of columns for these matrices regardless of whether they actually hold anything.  Here's some code that shows that:

x = zeros (0, 101, 5)
x = [](0x101x5)
columns (x)
ans = 101

Now if you do indexing the result is the size of the index.  This applies even to empty indexing.

x = 1:10;
idx = zeros (0,3)
idx = [](0x3)
y = x(idx)
y = [](0x3)

Of course, this is where it gets silly/stupid.  The expression "x != 2" should be of size 0x0.  It is when it is executed directly, but not when it is executed as part of an indexing operation.

x = []
x = [](0x0)
x ~= 2
ans = [](0x0)
x(x ~= 2)
ans = [](0x1)

I've lowered the severity since this is a minor issue that can be more appropriately handled by detecting empty matrices.  I've also changed the Item Group to Matlab Compatibility since it seems to generate a 0x0 matrix for the test case.

Rik <rik5>
Project Administrator
Sun 19 Dec 2021 08:40:18 PM UTC, comment #2: 

Hi I don't get "invalid operator" for != in 6.2 linux

Looks like ~= and != are the same.

It is even documented at https://octave.org/doc/v6.4.0/Comparison-Ops.html#Comparison-Ops

I didn't see you replicating the problem in the console output you posted. Maybe I missed something...

Here is the minimal way to replicate the problem.

octave:6> vector = []
vector = [](0x0)
octave:7> columns(vector)
ans = 0                <---------------- OK

# Now let's remove a non existent entry from a zero size vector

octave:8> vector = vector(vector != 2)
vector = [](0x1)
octave:9> columns(vector)
ans = 1                <---------------- ????

Vassilis Virvilis <vasvir>
Sun 19 Dec 2021 05:57:34 PM UTC, comment #1: 

This is still the same with a build from the current default branch (hg id 6ffa6dbbf42a), and also occurs with 6.4.0 on Windows 10.

With Matlab R2021a:

>> vector = []

vector =

     []

>> size(vector)

ans =

     0     0

>> vector = vector(vector != 2)
 vector = vector(vector != 2)
                        ↑
Invalid use of operator.

Did you mean:
>> vector = vector(vector ~= 2)

vector =

     []

>> size(vector)

ans =

     0     0

>> vector = [2]

vector =

     2

>> vector = vector(vector ~= 2)

vector =

     []

>> size(vector)

ans =

     0     0

Markus Mützel <mmuetzel>
Project Member
Sun 19 Dec 2021 05:18:14 PM UTC, original submission:  

Hi,

Debian unstable here with octave 6.2.0

Is the following behavior correct? I would expect columns to be zero.

octave:6> vector = []
vector = [](0x0)
octave:7> columns(vector)
ans = 0                <---------------- OK
octave:8> vector = vector(vector != 2)
vector = [](0x1)
octave:9> columns(vector)
ans = 1                <---------------- ????

If there is something to remove everything is ok

octave:10> vector = [2]
vector = 2
octave:11> columns(vector)
ans = 2
octave:12> vector = vector(vector != 2)
vector = [](0x0)
octave:7> columns(vector)
ans = 0                <---------------- OK

This seems inconsistent and I thought it would be best to report it. I tried to search if a bug like this has been reported but I didn't find anything.

Hope that helps.

   Vassilis

Vassilis Virvilis <vasvir>

 

(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 nrjank (Posted a comment)
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by cdf (Posted a comment)
  • -email is unavailable- added by sarrah (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by mmuetzel (Posted a comment)
  • -email is unavailable- added by vasvir (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 project members can vote.

     

     

     

     

    Follow 7 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2021-12-23 rik5 Severity3 - Normal 2 - Minor
        Priority5 - Normal 3 - Low
        Item GroupIncorrect Result Matlab Compatibility
    2021-12-19 mmuetzel StatusNone Confirmed
        Release6.2.0 dev
        Operating SystemGNU/Linux Any
    2021-12-19 mmuetzel Item GroupNone Incorrect Result

    Back to the top


    Powered by Savane 3.9