Chris@87: """ Chris@87: Module of functions that are like ufuncs in acting on arrays and optionally Chris@87: storing results in an output array. Chris@87: Chris@87: """ Chris@87: from __future__ import division, absolute_import, print_function Chris@87: Chris@87: __all__ = ['fix', 'isneginf', 'isposinf'] Chris@87: Chris@87: import numpy.core.numeric as nx Chris@87: Chris@87: def fix(x, y=None): Chris@87: """ Chris@87: Round to nearest integer towards zero. Chris@87: Chris@87: Round an array of floats element-wise to nearest integer towards zero. Chris@87: The rounded values are returned as floats. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: x : array_like Chris@87: An array of floats to be rounded Chris@87: y : ndarray, optional Chris@87: Output array Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: out : ndarray of floats Chris@87: The array of rounded numbers Chris@87: Chris@87: See Also Chris@87: -------- Chris@87: trunc, floor, ceil Chris@87: around : Round to given number of decimals Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> np.fix(3.14) Chris@87: 3.0 Chris@87: >>> np.fix(3) Chris@87: 3.0 Chris@87: >>> np.fix([2.1, 2.9, -2.1, -2.9]) Chris@87: array([ 2., 2., -2., -2.]) Chris@87: Chris@87: """ Chris@87: x = nx.asanyarray(x) Chris@87: y1 = nx.floor(x) Chris@87: y2 = nx.ceil(x) Chris@87: if y is None: Chris@87: y = nx.asanyarray(y1) Chris@87: y[...] = nx.where(x >= 0, y1, y2) Chris@87: return y Chris@87: Chris@87: def isposinf(x, y=None): Chris@87: """ Chris@87: Test element-wise for positive infinity, return result as bool array. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: x : array_like Chris@87: The input array. Chris@87: y : array_like, optional Chris@87: A boolean array with the same shape as `x` to store the result. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: y : ndarray Chris@87: A boolean array with the same dimensions as the input. Chris@87: If second argument is not supplied then a boolean array is returned Chris@87: with values True where the corresponding element of the input is Chris@87: positive infinity and values False where the element of the input is Chris@87: not positive infinity. Chris@87: Chris@87: If a second argument is supplied the result is stored there. If the Chris@87: type of that array is a numeric type the result is represented as zeros Chris@87: and ones, if the type is boolean then as False and True. Chris@87: The return value `y` is then a reference to that array. Chris@87: Chris@87: See Also Chris@87: -------- Chris@87: isinf, isneginf, isfinite, isnan Chris@87: Chris@87: Notes Chris@87: ----- Chris@87: Numpy uses the IEEE Standard for Binary Floating-Point for Arithmetic Chris@87: (IEEE 754). Chris@87: Chris@87: Errors result if the second argument is also supplied when `x` is a Chris@87: scalar input, or if first and second arguments have different shapes. Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> np.isposinf(np.PINF) Chris@87: array(True, dtype=bool) Chris@87: >>> np.isposinf(np.inf) Chris@87: array(True, dtype=bool) Chris@87: >>> np.isposinf(np.NINF) Chris@87: array(False, dtype=bool) Chris@87: >>> np.isposinf([-np.inf, 0., np.inf]) Chris@87: array([False, False, True], dtype=bool) Chris@87: Chris@87: >>> x = np.array([-np.inf, 0., np.inf]) Chris@87: >>> y = np.array([2, 2, 2]) Chris@87: >>> np.isposinf(x, y) Chris@87: array([0, 0, 1]) Chris@87: >>> y Chris@87: array([0, 0, 1]) Chris@87: Chris@87: """ Chris@87: if y is None: Chris@87: x = nx.asarray(x) Chris@87: y = nx.empty(x.shape, dtype=nx.bool_) Chris@87: nx.logical_and(nx.isinf(x), ~nx.signbit(x), y) Chris@87: return y Chris@87: Chris@87: def isneginf(x, y=None): Chris@87: """ Chris@87: Test element-wise for negative infinity, return result as bool array. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: x : array_like Chris@87: The input array. Chris@87: y : array_like, optional Chris@87: A boolean array with the same shape and type as `x` to store the Chris@87: result. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: y : ndarray Chris@87: A boolean array with the same dimensions as the input. Chris@87: If second argument is not supplied then a numpy boolean array is Chris@87: returned with values True where the corresponding element of the Chris@87: input is negative infinity and values False where the element of Chris@87: the input is not negative infinity. Chris@87: Chris@87: If a second argument is supplied the result is stored there. If the Chris@87: type of that array is a numeric type the result is represented as Chris@87: zeros and ones, if the type is boolean then as False and True. The Chris@87: return value `y` is then a reference to that array. Chris@87: Chris@87: See Also Chris@87: -------- Chris@87: isinf, isposinf, isnan, isfinite Chris@87: Chris@87: Notes Chris@87: ----- Chris@87: Numpy uses the IEEE Standard for Binary Floating-Point for Arithmetic Chris@87: (IEEE 754). Chris@87: Chris@87: Errors result if the second argument is also supplied when x is a scalar Chris@87: input, or if first and second arguments have different shapes. Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> np.isneginf(np.NINF) Chris@87: array(True, dtype=bool) Chris@87: >>> np.isneginf(np.inf) Chris@87: array(False, dtype=bool) Chris@87: >>> np.isneginf(np.PINF) Chris@87: array(False, dtype=bool) Chris@87: >>> np.isneginf([-np.inf, 0., np.inf]) Chris@87: array([ True, False, False], dtype=bool) Chris@87: Chris@87: >>> x = np.array([-np.inf, 0., np.inf]) Chris@87: >>> y = np.array([2, 2, 2]) Chris@87: >>> np.isneginf(x, y) Chris@87: array([1, 0, 0]) Chris@87: >>> y Chris@87: array([1, 0, 0]) Chris@87: Chris@87: """ Chris@87: if y is None: Chris@87: x = nx.asarray(x) Chris@87: y = nx.empty(x.shape, dtype=nx.bool_) Chris@87: nx.logical_and(nx.isinf(x), nx.signbit(x), y) Chris@87: return y