# [SciPy-user] numpy's math library?

Arnd Baecker arnd.baecker at web.de
Thu Jan 12 01:17:57 CST 2006

```On Wed, 11 Jan 2006, David M. Cooke wrote:

> Robert Kern <robert.kern at gmail.com> writes:
>
> > David M. Cooke wrote:
> >
> >> I've also exposed log1p(x) = log(1+x) and expm1(x) = exp(x)-1 as
> >> ufuncs, since those are quite useful if you're worrying about
> >> cancellation errors.
> >
> > Of course, they're useful, but they're also in scipy.special. Let's try not to
> > migrate more things from scipy to numpy than we strictly have to. So, I'm -1 on
> > exposing log1p() and expm1() in numpy.
>
> They're also part of the C99 standard, so I'd say there is some
> argument for making them part of numpy: exposing functions already
> defined by the C library. Mind you, we're also missing exp10, pow10,
> exp2, log2, cbrt, erf, erfc, lgamma, tgamma, and a few others.

Thanks a lot for spending thought on this - it can be very important
in some cases!

BTW: This brings me to something which I wanted to post anyway:

In [1]: import numpy
In [2]: x=2**numpy.arange(5)
In [3]: y=0*x
In [4]: print numpy.log2(x)
[ 0.  1.  2.  3.  4.]                  # fine so far
In [5]: print numpy.log2(x,y)
[0 0 1 2 2]                            # looks surprsing ;-)
In [6]: print y
[0 0 1 2 2]

The reason is clear when looking at the code for log2:

def log2(x, y=None):
"""Returns the base 2 logarithm of x

If y is an array, the result replaces the contents of y.
"""
x = asarray(x)
if y is None:
y = umath.log(x)
else:
umath.log(x, y)
y /= _log2
return y

A natively implemented umath.log2 would cure the above
surprise, I'd guess?

Best,

Arnd

```