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 for details.



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


amax(m,axis=0) returns the maximum of m along dimension axis.


amin(m,axis=0) returns the minimum of m along dimension axis.


Return the representation of a number in any given base.


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.


Return square diagonal matrix whose non-zero elements are given by the input array.


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.


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.


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 ])


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.


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.


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.


Return the l1 norm of a, flattened out.

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



Return the l2 norm of a, flattened out.

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



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.


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


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.


Return the root mean square of all the elements of a, flattened out.


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.


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.

Table Of Contents

Previous topic


Next topic


This Page