Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/numpy/lib/ufunclike.py
7763 views
"""1Module of functions that are like ufuncs in acting on arrays and optionally2storing results in an output array.34"""5__all__ = ['fix', 'isneginf', 'isposinf']67import numpy.core.numeric as nx8from numpy.core.overrides import (9array_function_dispatch, ARRAY_FUNCTION_ENABLED,10)11import warnings12import functools131415def _deprecate_out_named_y(f):16"""17Allow the out argument to be passed as the name `y` (deprecated)1819In future, this decorator should be removed.20"""21@functools.wraps(f)22def func(x, out=None, **kwargs):23if 'y' in kwargs:24if 'out' in kwargs:25raise TypeError(26"{} got multiple values for argument 'out'/'y'"27.format(f.__name__)28)29out = kwargs.pop('y')30# NumPy 1.13.0, 2017-04-2631warnings.warn(32"The name of the out argument to {} has changed from `y` to "33"`out`, to match other ufuncs.".format(f.__name__),34DeprecationWarning, stacklevel=3)35return f(x, out=out, **kwargs)3637return func383940def _fix_out_named_y(f):41"""42Allow the out argument to be passed as the name `y` (deprecated)4344This decorator should only be used if _deprecate_out_named_y is used on45a corresponding dispatcher function.46"""47@functools.wraps(f)48def func(x, out=None, **kwargs):49if 'y' in kwargs:50# we already did error checking in _deprecate_out_named_y51out = kwargs.pop('y')52return f(x, out=out, **kwargs)5354return func555657def _fix_and_maybe_deprecate_out_named_y(f):58"""59Use the appropriate decorator, depending upon if dispatching is being used.60"""61if ARRAY_FUNCTION_ENABLED:62return _fix_out_named_y(f)63else:64return _deprecate_out_named_y(f)656667@_deprecate_out_named_y68def _dispatcher(x, out=None):69return (x, out)707172@array_function_dispatch(_dispatcher, verify=False, module='numpy')73@_fix_and_maybe_deprecate_out_named_y74def fix(x, out=None):75"""76Round to nearest integer towards zero.7778Round an array of floats element-wise to nearest integer towards zero.79The rounded values are returned as floats.8081Parameters82----------83x : array_like84An array of floats to be rounded85out : ndarray, optional86A location into which the result is stored. If provided, it must have87a shape that the input broadcasts to. If not provided or None, a88freshly-allocated array is returned.8990Returns91-------92out : ndarray of floats93A float array with the same dimensions as the input.94If second argument is not supplied then a float array is returned95with the rounded values.9697If a second argument is supplied the result is stored there.98The return value `out` is then a reference to that array.99100See Also101--------102rint, trunc, floor, ceil103around : Round to given number of decimals104105Examples106--------107>>> np.fix(3.14)1083.0109>>> np.fix(3)1103.0111>>> np.fix([2.1, 2.9, -2.1, -2.9])112array([ 2., 2., -2., -2.])113114"""115# promote back to an array if flattened116res = nx.asanyarray(nx.ceil(x, out=out))117res = nx.floor(x, out=res, where=nx.greater_equal(x, 0))118119# when no out argument is passed and no subclasses are involved, flatten120# scalars121if out is None and type(res) is nx.ndarray:122res = res[()]123return res124125126@array_function_dispatch(_dispatcher, verify=False, module='numpy')127@_fix_and_maybe_deprecate_out_named_y128def isposinf(x, out=None):129"""130Test element-wise for positive infinity, return result as bool array.131132Parameters133----------134x : array_like135The input array.136out : array_like, optional137A location into which the result is stored. If provided, it must have a138shape that the input broadcasts to. If not provided or None, a139freshly-allocated boolean array is returned.140141Returns142-------143out : ndarray144A boolean array with the same dimensions as the input.145If second argument is not supplied then a boolean array is returned146with values True where the corresponding element of the input is147positive infinity and values False where the element of the input is148not positive infinity.149150If a second argument is supplied the result is stored there. If the151type of that array is a numeric type the result is represented as zeros152and ones, if the type is boolean then as False and True.153The return value `out` is then a reference to that array.154155See Also156--------157isinf, isneginf, isfinite, isnan158159Notes160-----161NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic162(IEEE 754).163164Errors result if the second argument is also supplied when x is a scalar165input, if first and second arguments have different shapes, or if the166first argument has complex values167168Examples169--------170>>> np.isposinf(np.PINF)171True172>>> np.isposinf(np.inf)173True174>>> np.isposinf(np.NINF)175False176>>> np.isposinf([-np.inf, 0., np.inf])177array([False, False, True])178179>>> x = np.array([-np.inf, 0., np.inf])180>>> y = np.array([2, 2, 2])181>>> np.isposinf(x, y)182array([0, 0, 1])183>>> y184array([0, 0, 1])185186"""187is_inf = nx.isinf(x)188try:189signbit = ~nx.signbit(x)190except TypeError as e:191dtype = nx.asanyarray(x).dtype192raise TypeError(f'This operation is not supported for {dtype} values '193'because it would be ambiguous.') from e194else:195return nx.logical_and(is_inf, signbit, out)196197198@array_function_dispatch(_dispatcher, verify=False, module='numpy')199@_fix_and_maybe_deprecate_out_named_y200def isneginf(x, out=None):201"""202Test element-wise for negative infinity, return result as bool array.203204Parameters205----------206x : array_like207The input array.208out : array_like, optional209A location into which the result is stored. If provided, it must have a210shape that the input broadcasts to. If not provided or None, a211freshly-allocated boolean array is returned.212213Returns214-------215out : ndarray216A boolean array with the same dimensions as the input.217If second argument is not supplied then a numpy boolean array is218returned with values True where the corresponding element of the219input is negative infinity and values False where the element of220the input is not negative infinity.221222If a second argument is supplied the result is stored there. If the223type of that array is a numeric type the result is represented as224zeros and ones, if the type is boolean then as False and True. The225return value `out` is then a reference to that array.226227See Also228--------229isinf, isposinf, isnan, isfinite230231Notes232-----233NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic234(IEEE 754).235236Errors result if the second argument is also supplied when x is a scalar237input, if first and second arguments have different shapes, or if the238first argument has complex values.239240Examples241--------242>>> np.isneginf(np.NINF)243True244>>> np.isneginf(np.inf)245False246>>> np.isneginf(np.PINF)247False248>>> np.isneginf([-np.inf, 0., np.inf])249array([ True, False, False])250251>>> x = np.array([-np.inf, 0., np.inf])252>>> y = np.array([2, 2, 2])253>>> np.isneginf(x, y)254array([1, 0, 0])255>>> y256array([1, 0, 0])257258"""259is_inf = nx.isinf(x)260try:261signbit = nx.signbit(x)262except TypeError as e:263dtype = nx.asanyarray(x).dtype264raise TypeError(f'This operation is not supported for {dtype} values '265'because it would be ambiguous.') from e266else:267return nx.logical_and(is_inf, signbit, out)268269270