# numutils¶

## Module: numutils¶

A set of convenient utilities for numerical work.

Most of this module requires Numerical Python or is meant to be used with it. See http://www.pfdubois.com/numpy for details.

## Functions¶

IPython.numutils.amap(fn, *args)

amap(function, sequence[, sequence, ...]) -> array.

Works like map(), but it returns an array. This is just a convenient shorthand for Numeric.array(map(...))

IPython.numutils.amax(m, axis=0)
amax(m,axis=0) returns the maximum of m along dimension axis.
IPython.numutils.amin(m, axis=0)
amin(m,axis=0) returns the minimum of m along dimension axis.
Return the representation of a number in any given base.
IPython.numutils.binary_repr(number, max_length=1025)

Return the binary representation of the input number as a string.

This is more efficient than using base_repr with base 2.

Increase the value of max_length for very large numbers. Note that on 32-bit machines, 2**1023 is the largest integer power of 2 which can be converted to a Python float.

IPython.numutils.diagonal_matrix(diag)
Return square diagonal matrix whose non-zero elements are given by the input array.
IPython.numutils.empty_like(a)

Return an empty (uninitialized) array of the shape and typecode of a.

Note that this does NOT initialize the returned array. If you require your array to be initialized, you should use zeros_like().

This requires Numeric.empty(), which appeared in Numeric 23.7.

IPython.numutils.exp_safe(x)

Compute exponentials which safely underflow to zero.

Slow but convenient to use. Note that NumArray will introduce proper floating point exception handling with access to the underlying hardware.

IPython.numutils.frange(xini, xfin=None, delta=None, **kw)

frange([start,] stop[, step, keywords]) -> array of floats

Return a Numeric array() containing a progression of floats. Similar to arange(), but defaults to a closed interval.

frange(x0, x1) returns [x0, x0+1, x0+2, ..., x1]; start defaults to 0, and the endpoint is included. This behavior is different from that of range() and arange(). This is deliberate, since frange will probably be more useful for generating lists of points for function evaluation, and endpoints are often desired in this use. The usual behavior of range() can be obtained by setting the keyword ‘closed=0’, in this case frange() basically becomes arange().

When step is given, it specifies the increment (or decrement). All arguments can be floating point numbers.

frange(x0,x1,d) returns [x0,x0+d,x0+2d,...,xfin] where xfin<=x1.

frange can also be called with the keyword ‘npts’. This sets the number of points the list should contain (and overrides the value ‘step’ might have been given). arange() doesn’t offer this option.

Examples: >>> frange(3) array([ 0., 1., 2., 3.]) >>> frange(3,closed=0) array([ 0., 1., 2.]) >>> frange(1,6,2) array([1, 3, 5]) >>> frange(1,6.5,npts=5) array([ 1. , 2.375, 3.75 , 5.125, 6.5 ])

IPython.numutils.fromfunction_kw(function, dimensions, **kwargs)

Drop-in replacement for fromfunction() from Numerical Python.

Allows passing keyword arguments to the desired function.

Call it as (keywords are optional): fromfunction_kw(MyFunction, dimensions, keywords)

The function MyFunction() is responsible for handling the dictionary of keywords it will recieve.

IPython.numutils.identity(n, rank=2, typecode='l')

identity(n,r) returns the identity matrix of shape (n,n,...,n) (rank r).

For ranks higher than 2, this object is simply a multi-index Kronecker delta:

/ 1 if i0=i1=...=iR,
id[i0,i1,...,iR] = -|
0 otherwise.

Optionally a typecode may be given (it defaults to ‘l’).

Since rank defaults to 2, this function behaves in the default case (when only n is given) like the Numeric identity function.

IPython.numutils.ispower2(n)

Returns the log base 2 of n if n is a power of 2, zero otherwise.

Note the potential ambiguity if n==1: 2**0==1, interpret accordingly.

IPython.numutils.l1norm(a)

Return the l1 norm of a, flattened out.

Implemented as a separate function (not a call to norm() for speed).

IPython.numutils.l2norm(a)

Return the l2 norm of a, flattened out.

Implemented as a separate function (not a call to norm() for speed).

IPython.numutils.log2(x, ln2=0.69314718055994529)

Return the log(x) in base 2.

This is a _slow_ function but which is guaranteed to return the correct integer value if the input is an ineger exact power of 2.

IPython.numutils.mean_flat(a)
Return the mean of all the elements of a, flattened out.
IPython.numutils.norm(a, p=2)

norm(a,p=2) -> l-p norm of a.flat

Return the l-p norm of a, considered as a flat array. This is NOT a true matrix norm, since arrays of arbitrary rank are always flattened.

p can be a number or one of the strings (‘inf’,’Infinity’) to get the L-infinity norm.

Ref: http://mathworld.wolfram.com/VectorNorm.html
http://mathworld.wolfram.com/L-Infinity-Norm.html
IPython.numutils.rms_flat(a)
Return the root mean square of all the elements of a, flattened out.
IPython.numutils.sum_flat(a)

Return the sum of all the elements of a, flattened out.

It uses a.flat, and if a is not contiguous, a call to ravel(a) is made.

IPython.numutils.zeros_like(a)

Return an array of zeros of the shape and typecode of a.

If you don’t explicitly need the array to be zeroed, you should instead use empty_like(), which is faster as it only allocates memory.