bugGNU Octave - Bugs: bug #63950, trapz inconsistent results and...

 
 

bug #63950: trapz inconsistent results and error messages with empty inputs

Submitter:  Chad Oldfield <chadoldfield>
Submitted:  Tue 21 Mar 2023 03:18:58 PM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Unexpected Error or Warning
Status:  None Assigned to:  None
Originator Name:  Chad Oldfield Open/Closed:  * Open
Release:  * 7.1.0 Operating System:  * Microsoft Windows
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Wed 22 Mar 2023 04:57:25 PM UTC, comment #8: 

As I think more on this, it seems that the context matters.

In one example,

x = a:dx:(a + b);
y = trapz(x, f(x))

When repeated with b approaching zero, the integration range decreases, as does the number of elements in x.  Then y would approach 0, unless there's an asymptote in f at a.  This might explain the choice by Mathworks to return 0 in some cases.

In another example,

x = linspace(a, a + b, n);
y = trapz(x, f(x))

When repeated with n approaching 0, the numerical integration becomes progressively more erroneous until it becomes 0 when n = 1, and quite undefined when n = 0.  This is even though in theory one might instead choose to reduce the trapezoidal technique to just b * f(x+b/2) when n = 1 (but Octave cannot because trapz no longer has the integration bounds with n = 1).

Chad Oldfield <chadoldfield>
Wed 22 Mar 2023 12:17:58 AM UTC, comment #7: 

I really strive to avoid Garbage In / Garbage Out situations in Octave.  If there are improvements to the input validation for trapz() I would be in favor of making those.  For example, I dislike this result which conjures a valid return value (0) from non-existent input data


a = [];
trapz (a,a)
ans = 0


Rik <rik5>
Group administrator
Tue 21 Mar 2023 05:18:07 PM UTC, comment #6: 

also worth noting that octave currently allows a superset of matlab allowed inputs to trapz.  e.g. it allows different domains for X:

Octave:

>> x = reshape(0:15,4,4)
x =

    0    4    8   12
    1    5    9   13
    2    6   10   14
    3    7   11   15

>> y = magic(4)
y =

   16    2    3   13
    5   11   10    8
    9    7    6   12
    4   14   15    1

>> trapz(x(:),y(:))
ans = 127.50

>> trapz(x(:,1),y)
ans =

   24   26   25   27

>> trapz(x,y)
ans =

   24   26   25   27


whereas matlab:

>> trapz(x(:),y(:))
ans =
  127.5000
>> trapz(x(:,1),y)
ans =
    24    26    25    27
>> trapz(x,y)
Error using trapz
X must be a vector.


so, some of that will play into octave having a different error set and possibly handling empties for input 1 differently.

I guess this report really boils down to whether Octave's current empty input handling is:

 1. compatible for valid matlab inputs.  so far seems like it is.

 2. consistent/useful for any other non-error producing input empty input.  not sure about this.

 3. do any error messages make sense. not sure here either. (e.g., 'x and y must be have same shape' when trapz(x(:,1),y) is perfectly valid.)

Nicholas Jankowski <nrjank>
Group Member
Tue 21 Mar 2023 05:02:33 PM UTC, comment #5: 

comment #3:

> also, note that integrating a zero-valued function should give you 0.  integrating an empty input is not the same thing, and shouldn't necessarily give zero. I would more expect an undefined, NaN, or empty answer.


This is a good point, and thanks also for the earlier MATLAB comparison.  Together, it leaves one wondering why Mathworks chose 0-valued returns for some of the cases.

Overall, the user code calling trapz could certainly do its own handling of empty inputs to avoid this altogether (mine previously relied on the zero values returned by Octave version 3.6.4).

Chad Oldfield <chadoldfield>
Tue 21 Mar 2023 04:21:08 PM UTC, comment #4: 

Disregard my comment (#1) -- I used b=[].

Dmitri.
--

Dmitri A. Sergatskov <dasergatskov>
Tue 21 Mar 2023 04:10:44 PM UTC, comment #3: 

also, note that integrating a zero-valued function should give you 0.  integrating an empty input is not the same thing, and shouldn't necessarily give zero. I would more expect an undefined, NaN, or empty answer.

Nicholas Jankowski <nrjank>
Group Member
Tue 21 Mar 2023 04:08:02 PM UTC, comment #2: 

checking matlab compatibility for different types of empty inputs:

matlab 2022b:

>> a = []
a =
     []
>> b = 1:0
b =
  1×0 empty double row vector
>> c = ones(0,1)
c =
  0×1 empty double column vector
>> d = ones(0,0,0)
d =
  0×0×0 empty double array
>> trapz(a,a)
Error using trapz
X must be a vector.
>> trapz(b,a)
ans =
     0
>> trapz(a,c)
Error using trapz
X must be a vector.
>> trapz(c,a)
ans =
     0
>> trapz(b,c)
ans =
     0
>> trapz(c,b)
ans =
     0
>> trapz(d,a)
Error using trapz
X must be a vector.
>> trapz(d,b)
Error using trapz
X must be a vector.
>> trapz(d,c)
Error using trapz
X must be a vector.
>> trapz(d,d)
Error using trapz
X must be a vector.
>> trapz(a,d)
Error using trapz
X must be a vector.
>> trapz(b,d)
ans =
  1×0×0 empty double array
>> trapz(c,d)
ans =
  1×0×0 empty double array


same commands in Octave 8.1.0:

>> a = []
a = [](0x0)
>> b = 1:0
b = [](1x0)
>> c = ones(0,1)
c = [](0x1)
>> d = ones(0,0,0)
d = [](0x0x0)
>> trapz(a,a)
ans = 0
>> trapz(a,b)
error: trapz: X and Y must have same shape
error: called from
    trapz at line 126 column 7
>> trapz(b,a)
ans = 0
>> trapz(a,c)
error: trapz: X and Y must have same shape
error: called from
    trapz at line 126 column 7
>> trapz(c,a)
ans = 0
>> trapz(b,c)
ans = 0
>> trapz(c,b)
error: trapz: length of X and length of Y along DIM must match
error: called from
    trapz at line 117 column 7
>> trapz(d,a)
error: trapz: X and Y must have same shape
error: called from
    trapz at line 126 column 7
>> trapz(d,b)
error: trapz: X and Y must have same shape
error: called from
    trapz at line 126 column 7
>> trapz(d,c)
error: trapz: X and Y must have same shape
error: called from
    trapz at line 126 column 7
>> trapz(d,d)
ans = [](1x0x0)
>> trapz(a,d)
error: trapz: X and Y must have same shape
error: called from
    trapz at line 126 column 7
>> trapz(b,d)
ans = [](1x0x0)
>> trapz(c,d)
ans = [](1x0x0)


Octave tries to produce compatible output for inputs that don't result in errors. Note that [], ones(0,1), and ones(1,0) are not necessarily equivalent, and they sometimes can and should produce different outputs for certain functions.  In this case, however, matlab produces errors for a number of input combinations. And judging by brevity in some of the messages (e.g., "Error using trapz" sans any detail), this one has probably been around for a long time without being revisited by mathworks like some of the other functions we've recently been trying to bring back into empty-input compatibility.

Anyway, for inputs where matlab isn't producing an error,  we appear to be producing compatible output.  Maybe we could be more consistent on some of the others either in error message or by producing an actual output, but I'm not seeing a specific problem here.


Nicholas Jankowski <nrjank>
Group Member
Tue 21 Mar 2023 03:25:44 PM UTC, comment #1: 

It gives me 0 in 8.1 (you should consider upgrading).

Dmitri.
--

Dmitri A. Sergatskov <dasergatskov>
Tue 21 Mar 2023 03:18:58 PM UTC, original submission:  

The trapz function sometimes gives errors when the input parameters are empty.  This depends on specifically how the parameters are defined.  The below shows this for inputs that are 0x0 and 1x0.  I would expect the trapz result to be 0 in all cases.


octave:1> version
ans = 7.1.0
octave:2> a = []
a = [](0x0)
octave:3> b = 1:0
b = [](1x0)
octave:4> trapz(a,a)
ans = 0
octave:5> trapz(a,b)
error: trapz: X and Y must have same shape
error: called from
    trapz at line 126 column 7
octave:6> trapz(b,a)
ans = 0
octave:7> trapz(b,b)
error: trapz: length of X and length of Y along DIM must match
error: called from
    trapz at line 117 column 7


Chad Oldfield <chadoldfield>

 

(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 nrjank (Posted a comment)
  • -email is unavailable- added by dasergatskov (Posted a comment)
  • -email is unavailable- added by chadoldfield (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.

     

    Follows 1 latest change.

    Date Changed by Updated Field Previous Value => Replaced by
    2023-03-21 nrjank Summarytrapz error with empty inputs trapz inconsistent results and error messages with empty inputs

    Back to the top

    Powered by Savane 3.13-89af.
    Corresponding source code