flopscope.numpy.ptp
fnp.ptp(a, axis=None, out=None, keepdims=<no value>)[flopscope source][numpy source]
Range of values (maximum - minimum) along an axis.
Adapted from NumPy docs np.ptp
Peak-to-peak (max - min) range of array.
The name of the function comes from the acronym for 'peak to peak'.
ptp preserves the data type of the array. This means the
return value for an input of signed integers with n bits
(e.g. flops.int8, flops.int16, etc) is also a signed integer
with n bits. In that case, peak-to-peak values greater than
2**(n-1)-1 will be returned as negative values. An example
with a work-around is shown below.
Parameters
- a:array_like
Input values.
- axis:None or int or tuple of ints, optional
Axis along which to find the peaks. By default, flatten the array.
axismay be negative, in which case it counts from the last to the first axis. If this is a tuple of ints, a reduction is performed on multiple axes, instead of a single axis or all the axes as before.- out:array_like
Alternative output array in which to place the result. It must have the same shape and buffer length as the expected output, but the type of the output values will be cast if necessary.
- keepdims:bool, optional
If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
If the default value is passed, then
keepdimswill not be passed through to the ptp method of sub-classes ofndarray, however any non-default value will be. If the sub-class' method does not implementkeepdimsany exceptions will be raised.
Returns
- ptp:ndarray or scalar
The range of a given array -
scalarif array is one-dimensional or a new array holding the result along the given axis
Examples
>>> import flopscope.numpy as fnp
>>> x = flops.array([[4, 9, 2, 10],
... [6, 9, 7, 12]])>>> flops.ptp(x, axis=1)
array([8, 6])>>> flops.ptp(x, axis=0)
array([2, 0, 5, 2])>>> flops.ptp(x)
10This example shows that a negative value can be returned when the input is an array of signed integers.
>>> y = flops.array([[1, 127],
... [0, 127],
... [-1, 127],
... [-2, 127]], dtype=flops.int8)
>>> flops.ptp(y, axis=1)
array([ 126, 127, -128, -127], dtype=int8)A work-around is to use the view() method to view the result as
unsigned integers with the same bit width:
>>> flops.ptp(y, axis=1).view(flops.uint8)
array([126, 127, 128, 129], dtype=uint8)