bugGNU Octave - Bugs: bug #66963, blkdiag() with no inputs throws...

 
 

bug #66963: blkdiag() with no inputs throws error (unlike Matlab)

Submitter:  Ray Zimmerman <rdzman>
Submitted:  Sat 29 Mar 2025 08:59:06 PM UTC
   
 
Category:  Octave Function Severity:  2 - Minor
Priority:  5 - Normal Item Group:  Unexpected Error or Warning
Status:  Confirmed Assigned to:  None
Originator Name:  Open/Closed:  * Open
Release:  * 9.4.0 Release: 
Operating System:  * Any Fixed Release:  None
Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Mon 31 Mar 2025 04:35:54 PM UTC, comment #6: 

If you are going to make this change it would be worth syncing the rest of the cat family of functions with Matlab.  Maybe they already return [] for no input?


cat
horzcat
vertcat
strcat
cstrcat
strvcat


I found another oddity.  The function char() for converting an input to a character array returns '' for no input, but double() which converts an input to double precision numeric array does not return [] for an empty input. 

Rik <rik5>
Group administrator
Sun 30 Mar 2025 06:38:24 PM UTC, comment #5: 

As you like.

Rik <rik5>
Group administrator
Sun 30 Mar 2025 05:55:59 PM UTC, comment #4: 

I also don't see an explicit statement in the Matlab docs for vertcat and horzcat that say that they should return an empty matrix when there are no arguments.  It is somewhat implied tht they would do that based on the statement that they are equivalent to [a;b] or [a,b], and if there are elements those expressions reduce to [].  Octave allows calling both of those functions without arguments.  I assume (but have not checked) that Matlab does the same.  Since it is simple to provide compatibility for blkdiag here, the same as for vertcat and horzcat, I don't see why we shouldn't do it.  I'd be more reluctant if the fix was complicated but here it seems to me to be reasonable behavior and easy to do.

John W. Eaton <jwe>
Group administrator
Sun 30 Mar 2025 02:12:51 PM UTC, comment #3: 

Just because it is easy, doesn't mean it is correct.  What should the output of this command be?


printf ()


By your logic, since there was no input the output should be an empty value of the appropriate class, in this case the null string "".

But if someone has invoked printf() in that manner then they haven't understood how the function works and it is better to emit an error so that the code can be corrected.  Octave, philosophically, tries to prevent Garbage-In, Garbage-Out calculations.

As I said, the Octave project also does not chase bug-for-bug compatibility with Matlab.  If they have a documented behavior then we try to match that, but many times (I've been volunteering for ~20 years) we've seen the Matlab internals change and some artifact of the implementation will disappear.

Octave is open source, and blkdiag is an m-file, so you are free to modify your copy of blkdiag.m.  Alternatively, if you modify your code to run on Octave it would still run perfectly well on Matlab.  I imagine something as simple as


if (~ isempty (A))
  B = blkdiag (A{:});
else
  B = [];
end


which is platform independent.

Rik <rik5>
Group administrator
Sun 30 Mar 2025 03:23:20 AM UTC, comment #2: 

Well, especially given how I encountered this issue, I would still argue that returning an empty matrix given no inputs (as Matlab does) is the correct answer, not just an arbitrary unspecified behavior. I.e. a block diagonal matrix with no block diagonal elements is an empty matrix.

Someone contributed some Matlab code for my project that includes forming a block diagonal matrix from a cell array of matrices that can be empty in some circumstances. In Matlab, no special handling is needed for the case when the cell array is empty. However, the code throws a fatal error in Octave, requiring me to add a conditional to check the inputs before calling blkdiag().

It's absolutely trivial to fix this, just have it return [] instead of throw the error if nargin < 1.

Ray Zimmerman <rdzman>
Sun 30 Mar 2025 12:56:12 AM UTC, comment #1: 

The example can be simplified to just


blkdiag ()


What is the proper way to handle no input?  This is a different situation than empty input, such as '[]', for which Octave returns '[]'.

If you look at the function signature in Octave or Matlab it is


M = blkdiag (A, B, C, ...)


There is no documented acceptable way to call


M = blkdiag ()


It appears to be an accident of implementation that Matlab returns something here rather than emitting an error that the function was called incorrectly.

We don't bother to chase bug-for-bug compatability with undocumented Matlab features because they can change at any time.

    Matlab returns an empty matrix, but that

Rik <rik5>
Group administrator
Sat 29 Mar 2025 08:59:06 PM UTC, original submission:  

Matlab:

>> A = {};
>> B = blkdiag(A{:})

B =

     []


Octave:

>> A = {};
>> B = blkdiag(A{:})
error: Invalid call to blkdiag.  Correct usage is:

 -- M = blkdiag (A, B, C, ...)

Additional help for built-in functions and operators is
available in the online version of the manual.  Use the command
'doc <topic>' to search the manual index.

Help and information about Octave is also available on the WWW
at https://www.octave.org and via the help@octave.org


Ray Zimmerman <rdzman>

 

(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 (Updated the item)
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by rdzman (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
    2025-03-31 mmuetzel Item GroupMatlab Compatibility Unexpected Error or Warning
        StatusWont Fix Confirmed
        Open/ClosedClosed Open
    2025-03-30 rik5 Open/ClosedOpen Closed
        Summaryblkdiag() with no inputs incorrectly throws error blkdiag() with no inputs throws error (unlike Matlab)
    2025-03-30 rik5 Severity3 - Normal 2 - Minor
        StatusNone Wont Fix

    Back to the top

    Powered by Savane 3.14-962f.
    Corresponding source code