Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
_ufunclike_impl.py200 linesDownload Raw Back to lib
1"""
2Module of functions that are like ufuncs in acting on arrays and optionally
3storing results in an output array.
4
5"""
6__all__ = ['fix', 'isneginf', 'isposinf']
7
8import numpy._core.numeric as nx
9from numpy._core.overrides import array_function_dispatch
10
11
12def _dispatcher(x, out=None):
13    return (x, out)
14
15
16@array_function_dispatch(_dispatcher, verify=False, module='numpy')
17def fix(x, out=None):
18    """
19    Round to nearest integer towards zero.
20
21    Round an array of floats element-wise to nearest integer towards zero.
22    The rounded values have the same data-type as the input.
23
24    Parameters
25    ----------
26    x : array_like
27        An array to be rounded
28    out : ndarray, optional
29        A location into which the result is stored. If provided, it must have
30        a shape that the input broadcasts to. If not provided or None, a
31        freshly-allocated array is returned.
32
33    Returns
34    -------
35    out : ndarray of floats
36        An array with the same dimensions and data-type as the input.
37        If second argument is not supplied then a new array is returned
38        with the rounded values.
39
40        If a second argument is supplied the result is stored there.
41        The return value ``out`` is then a reference to that array.
42
43    See Also
44    --------
45    rint, trunc, floor, ceil
46    around : Round to given number of decimals
47
48    Examples
49    --------
50    >>> import numpy as np
51    >>> np.fix(3.14)
52    3.0
53    >>> np.fix(3)
54    3
55    >>> np.fix([2.1, 2.9, -2.1, -2.9])
56    array([ 2.,  2., -2., -2.])
57
58    """
59    return nx.trunc(x, out=out)
60
61
62@array_function_dispatch(_dispatcher, verify=False, module='numpy')
63def isposinf(x, out=None):
64    """
65    Test element-wise for positive infinity, return result as bool array.
66
67    Parameters
68    ----------
69    x : array_like
70        The input array.
71    out : array_like, optional
72        A location into which the result is stored. If provided, it must have a
73        shape that the input broadcasts to. If not provided or None, a
74        freshly-allocated boolean array is returned.
75
76    Returns
77    -------
78    out : ndarray
79        A boolean array with the same dimensions as the input.
80        If second argument is not supplied then a boolean array is returned
81        with values True where the corresponding element of the input is
82        positive infinity and values False where the element of the input is
83        not positive infinity.
84
85        If a second argument is supplied the result is stored there. If the
86        type of that array is a numeric type the result is represented as zeros
87        and ones, if the type is boolean then as False and True.
88        The return value `out` is then a reference to that array.
89
90    See Also
91    --------
92    isinf, isneginf, isfinite, isnan
93
94    Notes
95    -----
96    NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
97    (IEEE 754).
98
99    Errors result if the second argument is also supplied when x is a scalar
100    input, if first and second arguments have different shapes, or if the
101    first argument has complex values
102
103    Examples
104    --------
105    >>> import numpy as np
106    >>> np.isposinf(np.inf)
107    True
108    >>> np.isposinf(-np.inf)
109    False
110    >>> np.isposinf([-np.inf, 0., np.inf])
111    array([False, False,  True])
112
113    >>> x = np.array([-np.inf, 0., np.inf])
114    >>> y = np.array([2, 2, 2])
115    >>> np.isposinf(x, y)
116    array([0, 0, 1])
117    >>> y
118    array([0, 0, 1])
119
120    """
121    is_inf = nx.isinf(x)
122    try:
123        signbit = ~nx.signbit(x)
124    except TypeError as e:
125        dtype = nx.asanyarray(x).dtype
126        raise TypeError(f'This operation is not supported for {dtype} values '
127                        'because it would be ambiguous.') from e
128    else:
129        return nx.logical_and(is_inf, signbit, out)
130
131
132@array_function_dispatch(_dispatcher, verify=False, module='numpy')
133def isneginf(x, out=None):
134    """
135    Test element-wise for negative infinity, return result as bool array.
136
137    Parameters
138    ----------
139    x : array_like
140        The input array.
141    out : array_like, optional
142        A location into which the result is stored. If provided, it must have a
143        shape that the input broadcasts to. If not provided or None, a
144        freshly-allocated boolean array is returned.
145
146    Returns
147    -------
148    out : ndarray
149        A boolean array with the same dimensions as the input.
150        If second argument is not supplied then a numpy boolean array is
151        returned with values True where the corresponding element of the
152        input is negative infinity and values False where the element of
153        the input is not negative infinity.
154
155        If a second argument is supplied the result is stored there. If the
156        type of that array is a numeric type the result is represented as
157        zeros and ones, if the type is boolean then as False and True. The
158        return value `out` is then a reference to that array.
159
160    See Also
161    --------
162    isinf, isposinf, isnan, isfinite
163
164    Notes
165    -----
166    NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
167    (IEEE 754).
168
169    Errors result if the second argument is also supplied when x is a scalar
170    input, if first and second arguments have different shapes, or if the
171    first argument has complex values.
172
173    Examples
174    --------
175    >>> import numpy as np
176    >>> np.isneginf(-np.inf)
177    True
178    >>> np.isneginf(np.inf)
179    False
180    >>> np.isneginf([-np.inf, 0., np.inf])
181    array([ True, False, False])
182
183    >>> x = np.array([-np.inf, 0., np.inf])
184    >>> y = np.array([2, 2, 2])
185    >>> np.isneginf(x, y)
186    array([1, 0, 0])
187    >>> y
188    array([1, 0, 0])
189
190    """
191    is_inf = nx.isinf(x)
192    try:
193        signbit = nx.signbit(x)
194    except TypeError as e:
195        dtype = nx.asanyarray(x).dtype
196        raise TypeError(f'This operation is not supported for {dtype} values '
197                        'because it would be ambiguous.') from e
198    else:
199        return nx.logical_and(is_inf, signbit, out)
200 
codekingpro/portable-devtools · Team Ai