[Numpy-discussion] Log Arrays

Anne Archibald peridot.faceted@gmail....
Thu May 8 15:14:36 CDT 2008

2008/5/8 T J <tjhnson@gmail.com>:
> On 5/8/08, Anne Archibald <peridot.faceted@gmail.com> wrote:
>  > Is "logarray" really the way to handle it, though? it seems like you
>  > could probably get away with providing a logsum ufunc that did the
>  > right thing. I mean, what operations does one want to do on logarrays?
>  >
>  > add -> logsum
>  > subtract -> ?
>  > multiply -> add
>  > mean -> logsum/N
>  > median -> median
>  > exponentiate to recover normal-space values -> exp
>  > str -> ?
>  >
>  That's about it, as far as my usage goes.  Additionally, I would also
>  benefit from:
>    logdot
>    logouter
>  In addition to the elementwise operations, it would be nice to have
>    logsum along an axis
>    logprod along an axis
>    cumlogsum
>    cumlogprod
>  Whether these are through additional ufuncs or through a subclass is
>  not so much of an issue for me---either would be a huge improvement to
>  the current situation.  One benefit of a subclass, IMO, is that it
>  maintains the feel of non-log arrays.  That is, when I want to
>  multiply to logarrays, I just do x*y, rather than x+y....but I can
>  understand arguments that this might not be desirable.

Well, how about a two-step process? Let's come up with a nice
implementation of each of the above, then we can discuss whether a
subclass is warranted (and at that point each operation on the
subclass will be very simple to implement).

Most of them could be implemented almost for free by providing a ufunc
that did logsum(); then logsum along an axis becomes logsum.reduce(),
and cumlogsum becomes logsum.accumulate(). logprod is of course just
add. logouter is conveniently and efficiently written as
logprod.outer. logdot can be written in terms of logsum() and
logprod() at the cost of a sizable temporary, but should really have
its own implementation.

It might be possible to test this by using vectorize() on a
single-element version of logsum(). This would be slow but if
vectorize() makes a real ufunc object should provide all the usual
handy methods (reduce, accumulate, outer, etcetera). Alternatively,
since logsum can be written in terms of existing ufuncs, it should be
possible to implement a class providing all the ufunc methods by hand
without too too much pain.


More information about the Numpy-discussion mailing list