bugGNU Octave - Bugs: bug #33292, trapz on matrices does not allow a...

 
 

bug #33292: trapz on matrices does not allow a vector as first argument

Submitter:  Helios De Rosario <heliosdr>
Submitted:  Wed 11 May 2011 05:02:02 PM UTC
   
 
Category:  None Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Incorrect Result
Status:  Fixed Assigned to:  jordigh
Originator Name:  Helios De Rosario Open/Closed:  * Closed
Release:  * dev Operating System:  * Microsoft Windows
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Thu 03 Aug 2017 12:46:17 PM UTC, comment #8: 

I pushed the following changeset:

http://hg.savannah.gnu.org/hgweb/octave/rev/3d67c7b99546

Is there a better way?

John W. Eaton <jwe>
Group administrator
Thu 03 Aug 2017 10:58:35 AM UTC, comment #7: 

This bug is still in 4.2.1


x     = -3:.1:3;
y     = -5:.1:5;
[X,Y] = meshgrid (x, y);
F     = X.^2 + Y.^2;
trapz (y, trapz (x, F, 2))


produces

error: trapz: X and Y must have same shape
error: called from
    trapz at line 120 column 9


Should be: Matlab incompatibility.

Can anyone check that the patch is still valid?

Juan Pablo Carbajal <juanpi>
Group Member
Sat 14 May 2011 05:34:01 PM UTC, comment #6: 

Thanks, I was still toiling with this today, but I see your approach is better. At any rate, I think I found a weird bug in bsxfun during my toiling, so it wasn't all in vain!

Jordi Gutiérrez Hermoso <jordigh>
Group Member
Sat 14 May 2011 05:22:21 PM UTC, comment #5: 

I checked in changes to the stable branch (http://hg.savannah.gnu.org/hgweb/octave/rev/d8aff843a9e9) which correct this bug.  To access the changes you will need to download from the Mercurial archives and build from source, or wait for the 3.4.1 release.  Instructions for downloading the source code are here (http://www.gnu.org/software/octave/download.html)

For Jordi, I took a slightly different approach from your patch.  In your patch the pseudocode was

unwind_protect block
{
  expand x to size of y with bsxfun
  recursively call trapz with new x, old y matrices
}

If y is a large ND-array, then reproducing x to be the same size as y can be wasteful of memory.  I also guessed that the overhead of an unwind_protect block and an extra function call might be heavy.  Instead I tried something like this

extra_input_validation (which obviates unwind_protect)
{
  reshape x vector to point along correct dimension
  use bsxfun to implement trapz computation between a vector and a matrix
}

Benchmarking showed the new approach to take 84% less time.  I didn't investigate further, so I still don't know which code construct is actually responsible for the slowdown.

Rik <rik5>
Group administrator
Thu 12 May 2011 08:04:31 PM UTC, comment #4: 

Yes, this works in Matlab. A friend just confirmed it. I'll modify my patch accordingly.

Jordi Gutiérrez Hermoso <jordigh>
Group Member
Thu 12 May 2011 07:19:36 PM UTC, comment #3: 

trapz is supposed to work for ND-arrays.  Could the original poster verify that Matlab accepts vectors for the first argument even when the array is multi-dimensional?  The proposed patch doesn't handle this case, but it may not be important if Matlab doesn't as well.  Sample test code:


y = reshape (1:24, [4 3 2]);
x = [1 2];
trapz (x, y, 3)


Rik <rik5>
Group administrator
Wed 11 May 2011 06:55:46 PM UTC, comment #2: 

Oops, left some stray debug code in trapz.patch, removed it in attached trapz-2.patch

(file #23390)

Jordi Gutiérrez Hermoso <jordigh>
Group Member
Wed 11 May 2011 06:52:18 PM UTC, comment #1: 

Confirmed in the dev sources. Attached is a proposed fix that uses bsxfun to expand the first argument to the size of the second. If there are no complaints within a few days, I'll push this fix to the stable branch.

(file #23389)

Jordi Gutiérrez Hermoso <jordigh>
Group Member
Wed 11 May 2011 05:02:02 PM UTC, original submission:  

According to documentation, the trapz function (and cumtrapz as well) assumes an equally spaced vector as first argument x, if it is ommited in the call. Thus, I expected the function to work when a vector is explicitly entered, even though the second argument is a matrix (whenever its first non-singleton dimension is equal to the lenght of x).

However, that does not work (unlike in MATLAB):


x = linspace(0,1,100)';
y = [x, 2*x, x.^2];
intg = cumtrapz(x,y);


That can be easily solved by automatically expanding x to match the dimensions of y.

Helios De Rosario <heliosdr>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #23390:  trapz-2.patch added by jordigh (4KiB - text/x-patch)
file #23389:  trapz.patch added by jordigh (4KiB - text/x-patch)

 

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 juanpi (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by jordigh (Updated the item)
  • -email is unavailable- added by heliosdr (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
    2011-05-14 rik5 StatusPatch Submitted Fixed
        Open/ClosedOpen Closed
    2011-05-11 jordigh Attached File- Added trapz-2.patch, #23390
    2011-05-11 jordigh Attached File- Added trapz.patch, #23389
        StatusNone Patch Submitted
        Assigned toNone jordigh
        Release3.2.4 dev

    Back to the top

    Powered by Savane 3.13-4448.
    Corresponding source code