Universal Functions (ufunc)
A universal function (or nlcpy.ufunc
for short) is a function that operates on ndarrays in an element-by-element fashion, supporting array broadcasting, type casting, and several other standard features. That is, a ufunc is a “vectorized” wrapper for a function that takes a fixed number of specific inputs and produces a fixed number of specific outputs.
As with NumPy, Broadcasting rules are applied to input arrays of the universal functions of NLCPy.
Broadcasting
Each universal function takes array inputs and produces array outputs by performing the core function element-wise on the inputs (where an element is generally a scalar, but can be a vector or higher-order sub-array for generalized ufuncs). Standard broadcasting rules are applied so that inputs not sharing exactly the same shapes can still be usefully operated on. Broadcasting can be understood by four rules:
All input arrays with ndim smaller than the input array of largest ndim, have 1’s prepended to their shapes.
The size in each dimension of the output shape is the maximum of all the input sizes in that dimension.
An input can be used in the calculation if its size in a particular dimension either matches the output size in that dimension, or has value exactly 1.
If an input has a dimension size of 1 in its shape, the first data entry in that dimension will be used for all calculations along that dimension. In other words, the stepping machinery of the ufunc will simply not step along that dimension (the stride will be 0 for that dimension).
Examples
If a.shape is (5,1), b.shape is (1,6), c.shape is (6,) and d.shape is () so that d is a scalar, then a, b, c, and d are all broadcastable to dimension (5,6); and
a acts like a (5,6) array where a[:,0] is broadcast to the other columns,
b acts like a (5,6) array where b[0,:] is broadcast to the other rows,
c acts like a (1,6) array and therefore like a (5,6) array where c[:] is broadcast to every row, and finally,
d acts like a (5,6) array where the single value is repeated.
Available Ufuncs
The following tables show Universal functions(ufuncs) provided by NLCPy.
Math Operations
Computes the element-wise addition of the inputs. |
|
Computes the element-wise subtraction of the inputs. |
|
Computes the element-wise multiplication of the inputs. |
|
Matrix product of two arrays. |
|
Computes the element-wise division of the inputs. |
|
Computes the element-wise natural logarithm of . |
|
Computes the element-wise base-2 logarithm of . |
|
Computes the element-wise division of the inputs. |
|
Computes the element-wise floor division of the inputs. |
|
Computes numerical negative, element-wise. |
|
Computes numerical positive, element-wise. |
|
Computes the element-wise exponentiation of the inputs. |
|
Computes the element-wise remainder of division. |
|
Computes the element-wise remainder of division. |
|
Computes the element-wise remainder of division. |
|
Computes the element-wise absolute value. |
|
Computes the element-wise absolute value. |
|
Computes the element-wise nearest integer. |
|
Computes Heaviside step function. |
|
Returns the element-wise complex conjugate. |
|
Returns the element-wise complex conjugate. |
|
Computes the exponential of all elements in the input array. |
|
Computes 2**x for all elements in the array. |
|
Computes the element-wise natural logarithm of x. |
|
Computes the element-wise base-2 logarithm of x. |
|
Computes the element-wise base-10 logarithm of x. |
|
Computes the element-wise natural logarithm of 1 + x. |
|
Computes exp(x) - 1 for all elements in the array. |
|
Computes the element-wise square-root of the input. |
|
Computes the element-wise square of the input. |
|
Computes the element-wise cubic-root of the input. |
|
Computes the element-wise reciprocal of the input. |
Trigonometric Functions
Computes the element-wise sine. |
|
Computes the element-wise cosine. |
|
Computes the element-wise tangent. |
|
Computes the element-wise inverse sine. |
|
Computes the element-wise inverse cosine. |
|
Computes the element-wise inverse tangent. |
|
Computes the element-wise inverse tangent of x1/x2 choosing the quadrant correctly. |
|
Computes the "legs" of a right triangle. |
|
Computes the element-wise hyperbolic sine. |
|
Computes the element-wise hyperbolic cosine. |
|
Computes the element-wise hyperbolic tangent. |
|
Computes the element-wise inverse hyperbolic sine. |
|
Computes the element-wise inverse hyperbolic cosine. |
|
Computes the element-wise inverse hyperbolic tangent. |
|
Converts angles from radians to degrees. |
|
Converts angles from degrees to radians. |
|
Converts angles from degrees to radians. |
|
Converts angles from radians to degrees. |
Bit-Twiddling Functions
Computes the bit-wise AND of two arrays element-wise. |
|
Computes the bit-wise OR of two arrays element-wise. |
|
Computes the bit-wise XOR of two arrays element-wise. |
|
Computes the bit-wise NOT element-wise. |
|
Shifts bits of an integer to the left, element-wise. |
|
Shifts bits of an integer to the right, element-wise. |
Comparison Functions
Returns (x1 > x2), element-wise. |
|
Returns (x1 >= x2), element-wise. |
|
Returns (x1 < x2), element-wise. |
|
Returns (x1 <= x2), element-wise. |
|
Returns (x1 != x2), element-wise. |
|
Returns (x1 == x2), element-wise. |
|
Computes the logical AND of two arrays element-wise. |
|
Computes the logical OR of two arrays element-wise. |
|
Computes the logical XOR of two arrays element-wise. |
|
Computes the logical NOT of the input array element-wise. |
|
Computes the element-wise maximum of the inputs. |
|
Computes the element-wise minimum of the inputs. |
|
Computes the element-wise maximum of the inputs. |
|
Computes the element-wise minimum of the inputs. |
Floating Point Functions
Tests whether input elements are neither inf nor nan, or not. |
|
Tests whether input elements are inf, or not. |
|
Tests whether input elements are nan, or not. |
|
Returns True where signbit is set (less than zero), element-wise. |
|
Changes the sign of x1 to that of x2, element-wise. |
|
Returns the element-wise indication of the sign of a number. |
|
Returns the next floating-point value after x1 towards x2, element-wise. |
|
Returns the distance between x and the nearest adjacent number, element-wise. |
|
Returns , element-wise. |
|
Returns the floor of the input, element-wise. |
|
Returns the ceiling of the input, element-wise. |
|
Returns the truncated value of the input, element-wise. |
Methods
In NLCPy, ufuncs are instances of the nlcpy.ufunc
class.
nlcpy.ufunc
have four methods. However, these methods only make
sense on scalar ufuncs that take two input arguments and return one output argument.
Attempting to call these methods on other ufuncs will cause a ValueError.
Reduces one of the dimension of the input array, by applying ufunc along one axis. |
|
Accumulates the result of applying the operator to all elements. |
|
Performs a (local) reduce with specified slices over a single axis. |
|
Applies the ufunc op to all pairs (a, b) with a in A and b in B. |
The current version of NLCPy does not provide nlcpy.ufunc.at()
, which is supported
by NumPy.
Optional Keyword Arguments
All ufuncs take optional keyword arguments. Most of these represent advanced usage and will not typically be used.
out
A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.
where
This condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default
out=None
, locations within it where the condition is False will remain uninitialized.
casting
Controls what kind of data casting may occur.
‘no’ means the data types should not be cast at all.
‘equiv’ means only byte-order changes are allowed.
‘safe’ means only casts which can preserve values are allowed.
‘same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed.
NLCPy does NOT support ‘unsafe’, which is supported in NumPy.
order
Specifies the calculation iteration order/memory layout of the output array. Defaults to ‘K’. ‘C’ means the output should be C-contiguous, ‘F’ means F-contiguous, ‘A’ means F-contiguous if the inputs are F-contiguous and not also not C-contiguous, C-contiguous otherwise, and ‘K’ means to match the element ordering of the inputs as closely as possible.
dtype
Overrides the dtype of the calculation and output arrays.
subok
Not implemented in NLCPy.