bugGNU Octave - Bugs: bug #62405, Out of Bound(OOB) on subsindex()

 
 

bug #62405: Out of Bound(OOB) on subsindex()

Submitter:  Glite <linuxbckp>
Submitted:  Tue 03 May 2022 08:19:34 AM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Unexpected Error or Warning
Status:  Invalid / Not an Octave Bug Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * 7.1.0 Operating System:  * GNU/Linux
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Wed 04 May 2022 06:31:12 AM UTC, comment #8: 

I'm glad you found a solution that works for you.

Closing as invalid.

Markus Mützel <mmuetzel>
Group administrator
Tue 03 May 2022 02:21:00 PM UTC, comment #7: 

Now I finally realize that this is not a bug. Thanks for your reply.
Please close this bug.
I also looked for document of MATLAB. In MATLAB, if A is a mat and B is an object, it calls A(B) "use objects of the class as array indices". So the 'index' in 'subsindex' is not for real indexing for the object. That solved my problem too.

Glite <linuxbckp>
Tue 03 May 2022 12:12:21 PM UTC, comment #6: 

It's still not clear to me which output you expect.
I'm guessing, you'd like to see something like:

>> input_container = InputContainer([1 2 3 4]);
>> b = 1:2;
>> input_container(b)
  ans =
    1:2


That is an indexing operation into the object. You'd need to overload `subsref` (not `subsindex`) to handle that.

The overloaded function could probably look something like:

function val = subsref (this, idx)
  val = subsref (this.mat, idx);
endfunction


Is that what you are looking for?

Markus Mützel <mmuetzel>
Group administrator
Tue 03 May 2022 12:06:38 PM UTC, comment #5: 

If Octave bring input_container(b) or input_container.b back as method call(not only as field indexing), the paradox will be naturally solved.

Glite <linuxbckp>
Tue 03 May 2022 12:03:22 PM UTC, comment #4: 

I know your input_container(b) means method call. But Octave will interpret input_container(b) or input_container.b as a indexing to object.
To call a method in @+ClassName class, I have to write b(input_container).
So maybe this is the real bug?

comment #3:

> From the documentation of subsindex:
> https://octave.org/doc/v7.1.0/Defining-Indexing-And-Indexed-Assignment.html#index-subsindex
>
> > When obj is a class object defined with a class constructor, then subsindex is the overloading method that allows the conversion of this class object to a valid indexing vector.
>
> If I understand that correctly, `subsindex` converts the object to an indexing vector (to be able to index into something else). And that seems to be what is happening (and working ass expected) in your examples.
>
> From what you are describing, I guess that you would like to index into the object with an already existing indexing vector. Is that correct? In that case, you might want to overload `subsref`:
> https://octave.org/doc/v7.1.0/Defining-Indexing-And-Indexed-Assignment.html#index-subsref
>
> And index into your object like, e.g., `input_container(b)` (so, the other way round).

Glite <linuxbckp>
Tue 03 May 2022 11:48:51 AM UTC, comment #3: 

From the documentation of subsindex:
https://octave.org/doc/v7.1.0/Defining-Indexing-And-Indexed-Assignment.html#index-subsindex

> When obj is a class object defined with a class constructor, then subsindex is the overloading method that allows the conversion of this class object to a valid indexing vector.


If I understand that correctly, `subsindex` converts the object to an indexing vector (to be able to index into something else). And that seems to be what is happening (and working ass expected) in your examples.

From what you are describing, I guess that you would like to index into the object with an already existing indexing vector. Is that correct? In that case, you might want to overload `subsref`:
https://octave.org/doc/v7.1.0/Defining-Indexing-And-Indexed-Assignment.html#index-subsref

And index into your object like, e.g., `input_container(b)` (so, the other way round).

Markus Mützel <mmuetzel>
Group administrator
Tue 03 May 2022 10:45:56 AM UTC, comment #2: 

I give a comparison between mat calculation and the result I expect on subsindex():

  If i want to get the 1:2 index of [1 2 3 4], I can write this code:

>> a=[1 2 3 4]


a =

     1     2     3     4

>> a(1:2)


ans =

     1     2

On subsindex() I should get the same result by:

>> input_container = InputContainer([1 2 3 4]);

InputContainer class initialized.

>> b = 1:2;
>> b(input_container)


But subsindex() throws an error. That's the bug.


comment #1:

> Maybe, I'm missing the point. Could you please elaborate what you think is the bug?
>
> What does `subsindex(input_container)` return in your example. IIUC, it returns `[0 1 2 3]`. `b` in your last example has only two elements. So, indexing it with effectively `b([0 1 2 3]+1)` leads to the error you are seeing.
>

Glite <linuxbckp>
Tue 03 May 2022 09:11:55 AM UTC, comment #1: 

Maybe, I'm missing the point. Could you please elaborate what you think is the bug?

What does `subsindex(input_container)` return in your example. IIUC, it returns `[0 1 2 3]`. `b` in your last example has only two elements. So, indexing it with effectively `b([0 1 2 3]+1)` leads to the error you are seeing.

Markus Mützel <mmuetzel>
Group administrator
Tue 03 May 2022 08:19:34 AM UTC, original submission:  

An object with overloaded subsindex() should get part of its field. Now Octave can only get the whole field.
Here is my code:


Class InputContainer with overloaded subsindex():

#!/usr/bin/octave
# @InputContainer/InputContainer.m
function ret = InputContainer(mat)
    ret = class(struct("mat", mat), "InputContainer");
    fprintf('InputContainer class initialized.\n')
endfunction

#!/usr/bin/octave
# @InputContainer/subsindex.m
function ret = subsindex(this)
    ret = double(this.mat) - 1;
endfunction


Tnen I init InputContainer with [1 2 3 4].
The input range is [1 2 3 4]:

code line 1
>> input_container = InputContainer([1 2 3 4]);
InputContainer class initialized.


When I define a range that can contain input range, subindexing works fine:

>> b = 1:10;
>> b(input_container)
ans =

   1   2   3   4

>> b = 1:4;
>> b(input_container)
ans =

   1   2   3   4

But, when I define a range that can not contain input range, subindexing throws error:

>> b = 1:2;
>> b(input_container)
error: b(4): out of bound 2 (dimensions are 1x2)


Glite <linuxbckp>

 

(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 linuxbckp (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 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2022-05-04 mmuetzel StatusNeed Info Invalid / Not an Octave Bug
        Open/ClosedOpen Closed
    2022-05-03 mmuetzel StatusNone Need Info

    Back to the top

    Powered by Savane 3.13-02a9.
    Corresponding source code