bugGNU Octave - Bugs: bug #52009, allow creation of 1-by-1 array...

 
 

bug #52009: allow creation of 1-by-1 array with a single function handle

Submitter:  Ernst Reissner <ernstreissner>
Submitted:  Wed 13 Sep 2017 10:32:59 AM UTC
   
 
Category:  Interpreter Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Confirmed Assigned to:  None
Originator Name:  ernstreissner Open/Closed:  * Open
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Mon 18 Sep 2017 07:39:29 AM UTC, comment #8: 

My initial idea of getting this done quickly, does not pay off, feeling similar to Rik in comment #7.

My suggestion of comment #1, just implementing `resize` for `octave_fcn_handle`, was not the ultimate solution.  It requires to implement it for `octave_user_function` and some others too, as resize is called recursively in a way I do not understand.  I am afraid to introduce more bad and unintended corner cases than just "fixing" that one.

Mike's idea of comment #5 appeals a lot to me.  Avoid the resizing just where it is called without need, but this goes beyond my knowledge of that code.

So for now I am giving up on this item.

Kai Torben Ohlhus <siko1056>
Group Member
Fri 15 Sep 2017 07:09:15 PM UTC, comment #7: 

This is probably beyond me.  I took a look and I couldn't follow all the twists and turns between different templates for do_single_type_concat and single_type_concat in pt-tm-const.[cc|h].  It seems like eventually there is just an octave_value_extract<T> (...), but not clear to me how to promote some of that code up in to pt-eval.cc.  Alternatively, it looks like there are occasional optimizations in the concat routines.  Maybe they should be checking for the 1x1 case and optimizing there.

Rik <rik5>
Group administrator
Fri 15 Sep 2017 04:38:30 PM UTC, comment #6: 

I like the suggestion.  It is a general phenomenon that


[ x ] == x


so for efficiency it would be nice if every data type avoided the unnecessary call to resize.  But, yes we would have to have special decode logic for ranges since '[ ]' is the equivalent of 'full (x)'.

Rik <rik5>
Group administrator
Fri 15 Sep 2017 04:22:05 PM UTC, comment #5: 

I wonder if this could be handled (and would make sense to handle) in octave::tree_evaluator::visit_matrix with a special case. If the dimensions of the matrix expression are 1×1, there is no need to do any concatenation or resizing of the arguments. The result would just be the single object in the matrix expression as-is.

If this were only done for the 'else' case (non-numeric types) this should be safe. If it were done for all types it would break the case where '[1:10]' is turned into a normal Matrix, I don't think we want that.

Mike Miller <mtmiller>
Group Member
Thu 14 Sep 2017 09:08:22 AM UTC, comment #4: 

@Rik: this should be a special case:

Again Matlab 2017a:


mysin = [@sin]
mysin =

  function_handle with value:

    @sin


and



I try to give the overload approach a try.
x = @sin;
reshape (x, [1, 1])
Undefined function 'reshape' for input arguments of type 'function_handle'.

Error in bla (line 4)
reshape (x, [1, 1])


Kai Torben Ohlhus <siko1056>
Group Member
Wed 13 Sep 2017 10:51:36 PM UTC, comment #3: 

Is this just a special case?  What happens in Matlab if you try to reshape a function handle?  Sample code:


x = @sin;
reshape (x, [1, 1])
reshape (x, [2, 1])
reshape (x, [0, 1])


As Kai pointed out, it might be very simple to overload resize() for function handles to allow 1x1.

Rik <rik5>
Group administrator
Wed 13 Sep 2017 06:29:09 PM UTC, comment #2: 

Same bug applies to function handle to a real function, not just anonymous functions


>> [@sin]
error: octave_base_value::resize (): wrong type argument 'function handle'
>> [@()true]
error: octave_base_value::resize (): wrong type argument 'function handle'


Mike Miller <mtmiller>
Group Member
Wed 13 Sep 2017 12:12:16 PM UTC, comment #1: 

Confirmed, Matlab 2017a treats this case like there were no brackets:


>> a = [@() true]

a =

  function_handle with value:

    @()true

>> a = @() true

a =

  function_handle with value:

    @()true

>> a = {@() true}

a =

  cell

    @()true

>> b = [@() true; @() false]
Error using vertcat
Nonscalar arrays of function handles are not allowed; use cell arrays instead.

>> b = {@() true; @() false}

b =

  2×1 cell array

    @()true
    @()false


vs. Octave (hg id


>> a = [@() true]
error: octave_base_value::resize (): wrong type argument 'function handle'
>> a = @() true
a =

@() true

>> a = {@() true}
a =
{
  [1,1] =

@() true

}

>> b = [@() true; @() false]
error: octave_base_value::resize (): wrong type argument 'function handle'
>> b = {@() true; @() false}
b =
{
  [1,1] =

@() true

  [2,1] =

@() false

}


This is because Octave's `octave_fcn_handle`

https://hg.savannah.gnu.org/hgweb/octave/file/ec3d37eeafa2/libinterp/octave-value/ov-fcn-handle.h#l45

does not overwrite `octave_base_value::resize()`

https://hg.savannah.gnu.org/hgweb/octave/file/ec3d37eeafa2/libinterp/octave-value/ov-base.cc#l364

Maybe you can implement this method to just return itself, if the dimension vector is (1,1).

Anyways, I was curious, where did you find/use this border case?

Kai Torben Ohlhus <siko1056>
Group Member
Wed 13 Sep 2017 10:32:59 AM UTC, original submission:  

The expression [@() true]
yields
error: octave_base_value::resize (): wrong type argument 'function handle'

Ernst Reissner <ernstreissner>

 

(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 rik5 (Posted a comment)
  • -email is unavailable- added by siko1056 (Posted a comment)
  • -email is unavailable- added by ernstreissner (Submitted the item)
  • -email is unavailable- added by ernstreissner
  •  

    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
    2019-02-22 mtmiller Carbon-CopyRemoved 80942 -
    2017-09-18 siko1056 Assigned tosiko1056 None
    2017-09-14 siko1056 Assigned toNone siko1056
    2017-09-13 mtmiller Item GroupUnexpected Error or Warning Matlab Compatibility
        SummaryOctave does not ignore brackets around anonymous function [@() true] allow creation of 1-by-1 array with a single function handle
    2017-09-13 siko1056 StatusNone Confirmed
        Summaryhandle in array--&gt;error Octave does not ignore brackets around anonymous function [@() true]
    2017-09-13 ernstreissner Carbon-Copy- Added -email is unavailable-

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code