octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Making bsxfun automatic


From: Jordi Gutiérrez Hermoso
Subject: Re: Making bsxfun automatic
Date: Tue, 16 Aug 2011 04:05:36 -0500

On 5 August 2011 13:11, fork <address@hidden> wrote:
> John W. Eaton <jwe <at> octave.org> writes:
>
>> Currently, if you screw up the dimensions with .*, you get an error.
>> With your proposed change, you could end up with incorrect results and
>> no warning about it at all.
>
> The operator could throw an error if the dimensions don't match in a 
> reasonable
> way (I think this what numpy does, but I think SPLUS/R recycles and forces a
> match, dropping leftovers -- not good)
>
> For example:
>
> A = [1 2 3
>     4 5 6]
> B = [10 20 30]
>
> So A .* B would yield [10 20 90; 40 100 180] (multiplying "down", over the
> singleton dimension), but A .* B' would throw an error.

Here is the document that explains in glorious detail numpy's
behaviour. They call it "broadcasting", and it looks like automatic
bsxfun to me:

     http://www.scipy.org/EricsBroadcastingDoc

I've started looking into this. Seems easier than I thought it would
be. Looks like I can catch all common operators in
liboctave/mx-inlines.cc:346 (at cset 12970:7b4ec6f841a2)
do_mm_binary_op(...) and redirect them to do_bsx_fun(..) in
liboctave/bsxfun-defs.cc:38, after checking the dimensions are
bsxfun-compatible. That catches many common operators except power
operators.

I wonder if there is Matlab code out there that relies on mismatched
singleton dimensions  throwing an error...

Btw, I noticed the docstring for bsxfun never expands (haha) the
cryptic and horrible name: "binary singleton expansion function". I've
pushed a changeset in the meantime that does this:

     http://hg.savannah.gnu.org/hgweb/octave/rev/e4f82a337d66

The Octave manual overall is very sparse on details over bsxfun and
there isn't even a single example how to use it, whereas it should be
a basic vectorisation technique. I frequently see people trying to
write loopless code which could be done very easily with bsxfun, if it
were not so obscure. I think making bsxfun more accessible by making
it automatic for common operators could be a definite usability
improvement.

- Jordi G. H.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]