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?
|
Thu 03 Aug 2017 10:58:35 AM UTC, comment #7:
This bug is still in 4.2.1
produces
Should be: Matlab incompatibility.
Can anyone check that the patch is still valid?
|
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!
|
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.
|
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.
|
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:
|
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)
|
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)
|
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):
That can be easily solved by automatically expanding x to match the dimensions of y.
|