Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/numpy/lib/_ufunclike_impl.py: 38%
32 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-09 06:12 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-09 06:12 +0000
1"""
2Module of functions that are like ufuncs in acting on arrays and optionally
3storing results in an output array.
5"""
6__all__ = ['fix', 'isneginf', 'isposinf']
8import numpy._core.numeric as nx
9from numpy._core.overrides import array_function_dispatch
10import warnings
11import functools
14def _dispatcher(x, out=None):
15 return (x, out)
18@array_function_dispatch(_dispatcher, verify=False, module='numpy')
19def fix(x, out=None):
20 """
21 Round to nearest integer towards zero.
23 Round an array of floats element-wise to nearest integer towards zero.
24 The rounded values are returned as floats.
26 Parameters
27 ----------
28 x : array_like
29 An array of floats to be rounded
30 out : ndarray, optional
31 A location into which the result is stored. If provided, it must have
32 a shape that the input broadcasts to. If not provided or None, a
33 freshly-allocated array is returned.
35 Returns
36 -------
37 out : ndarray of floats
38 A float array with the same dimensions as the input.
39 If second argument is not supplied then a float array is returned
40 with the rounded values.
42 If a second argument is supplied the result is stored there.
43 The return value `out` is then a reference to that array.
45 See Also
46 --------
47 rint, trunc, floor, ceil
48 around : Round to given number of decimals
50 Examples
51 --------
52 >>> np.fix(3.14)
53 3.0
54 >>> np.fix(3)
55 3.0
56 >>> np.fix([2.1, 2.9, -2.1, -2.9])
57 array([ 2., 2., -2., -2.])
59 """
60 # promote back to an array if flattened
61 res = nx.asanyarray(nx.ceil(x, out=out))
62 res = nx.floor(x, out=res, where=nx.greater_equal(x, 0))
64 # when no out argument is passed and no subclasses are involved, flatten
65 # scalars
66 if out is None and type(res) is nx.ndarray:
67 res = res[()]
68 return res
71@array_function_dispatch(_dispatcher, verify=False, module='numpy')
72def isposinf(x, out=None):
73 """
74 Test element-wise for positive infinity, return result as bool array.
76 Parameters
77 ----------
78 x : array_like
79 The input array.
80 out : array_like, optional
81 A location into which the result is stored. If provided, it must have a
82 shape that the input broadcasts to. If not provided or None, a
83 freshly-allocated boolean array is returned.
85 Returns
86 -------
87 out : ndarray
88 A boolean array with the same dimensions as the input.
89 If second argument is not supplied then a boolean array is returned
90 with values True where the corresponding element of the input is
91 positive infinity and values False where the element of the input is
92 not positive infinity.
94 If a second argument is supplied the result is stored there. If the
95 type of that array is a numeric type the result is represented as zeros
96 and ones, if the type is boolean then as False and True.
97 The return value `out` is then a reference to that array.
99 See Also
100 --------
101 isinf, isneginf, isfinite, isnan
103 Notes
104 -----
105 NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
106 (IEEE 754).
108 Errors result if the second argument is also supplied when x is a scalar
109 input, if first and second arguments have different shapes, or if the
110 first argument has complex values
112 Examples
113 --------
114 >>> np.isposinf(np.inf)
115 True
116 >>> np.isposinf(-np.inf)
117 False
118 >>> np.isposinf([-np.inf, 0., np.inf])
119 array([False, False, True])
121 >>> x = np.array([-np.inf, 0., np.inf])
122 >>> y = np.array([2, 2, 2])
123 >>> np.isposinf(x, y)
124 array([0, 0, 1])
125 >>> y
126 array([0, 0, 1])
128 """
129 is_inf = nx.isinf(x)
130 try:
131 signbit = ~nx.signbit(x)
132 except TypeError as e:
133 dtype = nx.asanyarray(x).dtype
134 raise TypeError(f'This operation is not supported for {dtype} values '
135 'because it would be ambiguous.') from e
136 else:
137 return nx.logical_and(is_inf, signbit, out)
140@array_function_dispatch(_dispatcher, verify=False, module='numpy')
141def isneginf(x, out=None):
142 """
143 Test element-wise for negative infinity, return result as bool array.
145 Parameters
146 ----------
147 x : array_like
148 The input array.
149 out : array_like, optional
150 A location into which the result is stored. If provided, it must have a
151 shape that the input broadcasts to. If not provided or None, a
152 freshly-allocated boolean array is returned.
154 Returns
155 -------
156 out : ndarray
157 A boolean array with the same dimensions as the input.
158 If second argument is not supplied then a numpy boolean array is
159 returned with values True where the corresponding element of the
160 input is negative infinity and values False where the element of
161 the input is not negative infinity.
163 If a second argument is supplied the result is stored there. If the
164 type of that array is a numeric type the result is represented as
165 zeros and ones, if the type is boolean then as False and True. The
166 return value `out` is then a reference to that array.
168 See Also
169 --------
170 isinf, isposinf, isnan, isfinite
172 Notes
173 -----
174 NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
175 (IEEE 754).
177 Errors result if the second argument is also supplied when x is a scalar
178 input, if first and second arguments have different shapes, or if the
179 first argument has complex values.
181 Examples
182 --------
183 >>> np.isneginf(-np.inf)
184 True
185 >>> np.isneginf(np.inf)
186 False
187 >>> np.isneginf([-np.inf, 0., np.inf])
188 array([ True, False, False])
190 >>> x = np.array([-np.inf, 0., np.inf])
191 >>> y = np.array([2, 2, 2])
192 >>> np.isneginf(x, y)
193 array([1, 0, 0])
194 >>> y
195 array([1, 0, 0])
197 """
198 is_inf = nx.isinf(x)
199 try:
200 signbit = nx.signbit(x)
201 except TypeError as e:
202 dtype = nx.asanyarray(x).dtype
203 raise TypeError(f'This operation is not supported for {dtype} values '
204 'because it would be ambiguous.') from e
205 else:
206 return nx.logical_and(is_inf, signbit, out)