Index

Module Index

Search Page

npplus.basic module

Enhancements for basic numpy functionality.

Enhancements fall into several categories:

  1. Array building. Provides a_(a1, a2, ...) and cat_(a1, a2, ...), like array([a1, a2, ...]) and concatenate((a1, a2, ...)) except that the arguments are broadcst to the required shapes if needed. Provides span(a,b,n) and spanl(a,b,n) like linspace(a,b,n) and logspace(log10(a),log10(b),n), except that a and b may be points in multidimensional space. All npplus array building functions accept the axis= keyword.

  2. Finite difference axis methods zcen, cum, and pcen to to supplement the cumsum, cumprod, and diff functions in numpy. For example, cum(zcen(y)*diff(x)) is the finite difference analog of the indefinite integral of y dx.

  3. Redefine the range function in python 2 to be xrange so that it works the same as in python 3. The python 3 way is a better choice, especially since numpy provides the arange function.

  4. Provide multiple argument elementwise min_ and max_ functions. Also provide a multiple argument abs_ function that gives Euclidean distance in multdimensional space.

  5. Combine the one and two argument arctan in a single atan function. In two argument mode, provides for branch cut at any angle. Note that abs_(y,x) and atan(y,x) work well together.


a_(*args, **kwargs)[source]

Stack arrays on one axis.

This is like np.stack, except that the input arrays are broadcast to a common shape before stacking, so that they need only be conformable rather than exactly the same shape:

a_(2, 3, 5, ...)  # instead of
array([2, 3, 5, ...])
a_(0, [2, 3, 5])  # instead of
array([zeros(3,dtype=int), [2, 3, 5])
Parameters
  • a1 (array_like) – The arrays to be joined. The arrays will be broadcast to a common shape before being joined.

  • a2 (array_like) – The arrays to be joined. The arrays will be broadcast to a common shape before being joined.

  • ... (array_like) – The arrays to be joined. The arrays will be broadcast to a common shape before being joined.

  • axis (int, optional keyword) – The axis for the new dimension in the result, by default axis=0, meaning the first axis of the result.

Returns

joined – The stacked array.

Return type

ndarray

abs_(a, *args)[source]

Return elementwise 2-norm of any number of array-like arguments.

See also

numpy.linalg.norm

norm along one axis of a single array

atan(a, b=None, out=None, branch=None)[source]

Return arctan with one argument, arctan2 with two arguments.

Parameters
  • a (array_like) –

  • b (array_like, optional) –

  • out (ndarray of proper shape to hold result, optional) –

  • branch (array_like, optional) – Branch cut angle, the minimum value that can be returned. Ignored unless b is given.

Returns

The angle in radians whose tangent is a if b not given, or the angle from the ray (1,0) to the point (b,a) if b is given.

Return type

ndarray

Notes

In two argument mode, branch <= angle < branch+2*pi. Default is essentially -pi, except that atan(0,-1) returns pi, but atan(0,-1,branch=-pi) returns -pi. The most import case is arguably branch=0, which returns 0<=angle<2*pi as expected.

cat_(*args, **kwargs)[source]

Concatenate arrays on one axis.

This is like np.concatenate, except that the input arrays are passed as multiple arguments rather than as a sequence in one argument, and, more importantly, they are broadcast to a common shape on all axes except the one being joined.

Parameters
  • a1 (array_like) – The arrays to be joined. The arrays will be broadcast to a common shape over all axes except the one being joined.

  • a2 (array_like) – The arrays to be joined. The arrays will be broadcast to a common shape over all axes except the one being joined.

  • ... (array_like) – The arrays to be joined. The arrays will be broadcast to a common shape over all axes except the one being joined.

  • axis (int, optional keyword) – The axis along which to join the arrays, by default axis=0, meaning the first axis of the input with the maximum number of dimensions.

Returns

joined – The concatenated array.

Return type

ndarray

cum(a, axis=- 1)[source]

Calculate cumulative sums (cumsum) with prepended 0.

cum is an inverse of diff, the finite difference analog of integration if diff is the analog of differentiation.

Parameters
  • a (array_like) –

  • axis (int, optional) – The axis along which to accumulate. Default -1 means apply to the final axis, like diff.

Returns

The cumulative sums, starting with 0. The shape of the output is the same as a, except along axis, which is larger by 1.

Return type

ndarray

See also

numpy.cumsum

same except missing leading 0

numpy.diff

pairwise differences

zcen

pairwise means

Examples

>>> x = array([[1,1,1,1], [2,2,2,2]])
>>> axismeth.cum(x,axis=None)
array([ 0,  1,  2,  3,  4,  6,  8, 10, 12])
>>> axismeth.cum(x,axis=0)
array([[0, 0, 0, 0],
       [1, 1, 1, 1],
       [3, 3, 3, 3]])
>>> axismeth.cum(x)
array([[0, 1, 2, 3, 4],
       [0, 2, 4, 6, 8]])
max_(a, *args)[source]

Return elementwise maximum of any number of array-like arguments.

min_(a, *args)[source]

Return elementwise minimum of any number of array-like arguments.

pcen(a, axis=- 1)[source]

Point center by computing adjacent means and leaving endpoints same.

Parameters
  • a (array_like) –

  • axis (int, optional) – The axis along which to operate. Default -1 means apply to the final axis, like zcen.

Returns

The zone centered values. The shape of the output is the same as a, except along axis, which is larger by 1.

Return type

ndarray

See also

numpy.diff

pairwise differences

zcen

zone center

Examples

>>> x = array([[1, 2, 3, 4], [5, 6, 7, 8]])
>>> axismeth.pcen(x)
array([[ 1. ,  1.5,  2.5,  3.5,  4. ],
       [ 5. ,  5.5,  6.5,  7.5,  8. ]])
>>> axismeth.pcen(x,axis=0)
array([[ 1.,  2.,  3.,  4.],
       [ 3.,  4.,  5.,  6.],
       [ 5.,  6.,  7.,  8.]])
span(start, stop, num=100, axis=0, dtype=None)[source]

Return numbers with equal spacing between start and stop.

Parameters
  • start (array_like) – Shapes must be conformable but need not match exactly.

  • stop (array_like) – Shapes must be conformable but need not match exactly.

  • num (int, optional) – Number of points in result.

  • axis (int, optional) – If start and stop are not scalars, the position of the new axis in the result (default 0).

  • dtype (dtype, optional) – Type of output array, by default infer from start and stop.

Returns

samplessamples[0] == start, samples[num-1] == stop, with equal differences between successive intervening values

Return type

ndarray

See also

spanl

equal ratio (log) spacing

numpy.linspace

standard numpy function

numpy.arange

standard numpy function

spanl(start, stop, num=100, axis=0, dtype=None)[source]

Return numbers with equal ratios (log spaced) between start and stop.

Both start and stop may be negative, but they may not have opposite sign, nor may either be zero.

Parameters
  • start (array_like) – Shapes must be conformable but need not match exactly.

  • stop (array_like) – Shapes must be conformable but need not match exactly.

  • num (int, optional) – Number of points in result.

  • axis (int, optional) – If start and stop are not scalars, the position of the new axis in the result (default 0).

  • dtype (dtype, optional) – Type of output array, by default infer from start and stop.

Returns

samplessamples[0] == start, samples[num-1] == stop, with equal ratios between successive intervening values

Return type

ndarray

See also

span

equal difference (linear) spacing

numpy.logspace

standard numpy function

zcen(a, axis=- 1)[source]

Zone center by computing means of adjacent elements along an axis.

This is a companion to diff. For example, given values f at sorted points x, you can compute the definite and indefinite trapezoid rule integrals with:

sum(zcen(f) * diff(x))  # definite integral
cum(zcen(f) * diff(x))  # indefinite integral
Parameters
  • a (array_like) –

  • axis (int, optional) – The axis along which to operate. Default -1 means apply to the final axis, like diff.

Returns

The zone centered values. The shape of the output is the same as a, except along axis, which is smaller by 1.

Return type

ndarray

See also

numpy.diff

pairwise differences

cum

cumulative sums starting from 0

pcen

point center

Examples

>>> x = array([[1, 2, 3, 4], [5, 6, 7, 8]])
>>> axismeth.zcen(x)
array([[ 1.5,  2.5,  3.5],
       [ 5.5,  6.5,  7.5]])
>>> axismeth.zcen(x,axis=0)
array([[ 3.,  4.,  5.,  6.]])