bugGNU Octave - Bugs: bug #48867, delete elements from array with...

 
 

bug #48867: delete elements from array with indirect empty matrix

Submitter:  Amro <amro_octave>
Submitted:  Tue 23 Aug 2016 10:57:05 AM UTC
   
 
Category:  Interpreter Severity:  1 - Wish
Priority:  3 - Low Item Group:  WTF, Matlab?!?
Status:  Fixed Assigned to:  mtmiller
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

Thu 25 Aug 2016 11:41:30 PM UTC, comment #7: 

Mike, I pushed a changeset here:

http://hg.savannah.gnu.org/hgweb/octave/rev/6cbbd550aa91

Sorry, I should have posted here that I was going to do it.

John W. Eaton <jwe>
Group administrator
Thu 25 Aug 2016 11:38:17 PM UTC, comment #6: 

Ok, sounds good. I'll rework my change to use is_zero_by_zero and push that shortly.

Mike Miller <mtmiller>
Group Member
Thu 25 Aug 2016 11:35:00 PM UTC, comment #5: 

yes only a 2d 0x0 matrix works in subsasgn, anything else errors including zeros(0,1) or zeros(0,0,0) or zeros(0,0,1,0).

You're right it makes sense to have a consistent way to delete from arrays using the literal [], for both builtin numeric/struct/cell types and array of class objects.

Amro <amro_octave>
Thu 25 Aug 2016 12:30:51 AM UTC, comment #4: 

I'm pretty sure this is a bit of a kluge to allow "x(idx) = []" to work for classes the same way as for ordinary built-in objects.  For objects, the syntax is converted to a call to the subsasgn method for the class (if it is defined).  Because [] is an empty double array, it makes sense to limit it to this type, and I assume, also a 0x0 array, not 0x1, or any other shape of empty array.  But because the literal [] empty array loses its special status when it is assigned to a variable or passed to a function, subsasgn must accept "zeros(0,0)" as well.  And, because subsasgn can work for built-in types, it must also work this way for them.

Amro, can you verify that zeros(0,0) works but zeros(0,0,1,0) does not?

Mike, your patch should be fine, but if I'm right about the 0x0 thing, then it should probably use is_zero_by_zero instead of is_empty.  I just fixed is_zero_by_zero so that it requires exactly two dimensions and that both are 0.

John W. Eaton <jwe>
Group administrator
Wed 24 Aug 2016 09:05:34 PM UTC, comment #3: 

Funny enough, only double-type empty-matrix is accepted by subsasgn, even when the array is not double itself!


>> x = 1:10;
>> x = subsasgn(x, substruct('()',{1}), single([]))
In an assignment  A(:) = B, the number of elements in A and B must be the same.

>> x = single(1:10);
>> x = subsasgn(x, substruct('()',{1}), single([]))
In an assignment  A(:) = B, the number of elements in A and B must be the same.

>> x = subsasgn(x, substruct('()',{1}), double([]))
x =

     2     3     4     5     6     7     8     9    10


same thing with zeros(0,0,'single') as opposed to zeros(0,0).

Amro <amro_octave>
Tue 23 Aug 2016 10:34:05 PM UTC, comment #2: 

The following change makes the samples you've shown here compatible with Matlab:


diff --git a/libinterp/octave-value/ov.cc b/libinterp/octave-value/ov.cc
--- a/libinterp/octave-value/ov.cc
+++ b/libinterp/octave-value/ov.cc
@@ -3165,7 +3165,10 @@ If @var{idx} is an empty structure array

       arg0.make_unique ();

-      return ovl (arg0.subsasgn (type, idx, args(2)));
+      const octave_value& arg2 = args(2);
+      bool arg2_empty = args(2).is_empty () && args(2).is_double_type ();
+
+      return ovl (arg0.subsasgn (type, idx, arg2_empty ? octave_null_matrix::instance : args(2)));
     }
 }


Does subsasgn also work when the argument is single([]) or does it have to be double([])?

Mike Miller <mtmiller>
Group Member
Tue 23 Aug 2016 02:52:43 PM UTC, comment #1: 

The token "[]" is handled as the special type "null_matrix" in Octave, which looks like it is only handled specially for assignment statements in the language. Once it is assigned to something else or passed into a function like double(), it is no longer a null_matrix.

Maybe in the function subsasgn, any empty matrix argument could be recognized and replaced with a null_matrix when the underlying subsasgn object method is called.

Mike Miller <mtmiller>
Group Member
Tue 23 Aug 2016 10:57:05 AM UTC, original submission:  

In another thread, @jwe mentioned in a comment how "[]" is treated differently when deleting elements from an array.

I did a quick test and I found another minor case where behavior is different between MATLAB and Octave. I didn't wanna clutter the original thread with unrelated discussion, so I'm reporting it here as a new bug (feel free to mark it as low priority).

Here is an example (tested in Octave 4.0 and MATLAB R2016a, both on Windows):


x = 1:10;

% works in both MATLAB and Octave
x(1) = [];

% errors in both MATLAB and Octave
nil = []; x(1) = nil;
x(1) = double([]);
x(1) = zeros(0,0);

% works in both MATLAB and Octave
x = subsasgn(x, substruct('()',{1}), []);

% --> works in MATLAB, but errors in Octave! <--
x = subsasgn(x, substruct('()',{1}), nil);
x = subsasgn(x, substruct('()',{1}), double([]));
x = subsasgn(x, substruct('()',{1}), zeros(0,0));


So calling "subsasgn" with "nil" seems to work in MATLAB, even though the equivalent syntax with direct assignment doesn't... Go figure :)

Amro <amro_octave>

 

(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 jwe (Posted a comment)
  • -email is unavailable- added by mtmiller (Posted a comment)
  • -email is unavailable- added by amro_octave (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 8 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2016-08-25 jwe StatusReady For Test Fixed
    2016-08-25 jwe StatusPatch Submitted Ready For Test
        Open/ClosedOpen Closed
    2016-08-25 mtmiller Assigned toNone mtmiller
    2016-08-23 mtmiller StatusNone Patch Submitted
    2016-08-23 mtmiller Severity3 - Normal 1 - Wish
    2016-08-23 mtmiller Priority5 - Normal 3 - Low
        Release4.0.3 dev

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code