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
10import warnings
11import functools
12
13
14def _dispatcher(x, out=None):
15 return (x, out)
16
17
18@array_function_dispatch(_dispatcher, verify=False, module='numpy')
19def fix(x, out=None):
20 """
21 Round to nearest integer towards zero.
22
23 Round an array of floats element-wise to nearest integer towards zero.
24 The rounded values are returned as floats.
25
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.
34
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.
41
42 If a second argument is supplied the result is stored there.
43 The return value `out` is then a reference to that array.
44
45 See Also
46 --------
47 rint, trunc, floor, ceil
48 around : Round to given number of decimals
49
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.])
58
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))
63
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
69
70
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.
75
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.
84
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.
93
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.
98
99 See Also
100 --------
101 isinf, isneginf, isfinite, isnan
102
103 Notes
104 -----
105 NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
106 (IEEE 754).
107
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
111
112 Examples
113 --------
114 >>> np.isposinf(np.PINF)
115 True
116 >>> np.isposinf(np.inf)
117 True
118 >>> np.isposinf(np.NINF)
119 False
120 >>> np.isposinf([-np.inf, 0., np.inf])
121 array([False, False, True])
122
123 >>> x = np.array([-np.inf, 0., np.inf])
124 >>> y = np.array([2, 2, 2])
125 >>> np.isposinf(x, y)
126 array([0, 0, 1])
127 >>> y
128 array([0, 0, 1])
129
130 """
131 is_inf = nx.isinf(x)
132 try:
133 signbit = ~nx.signbit(x)
134 except TypeError as e:
135 dtype = nx.asanyarray(x).dtype
136 raise TypeError(f'This operation is not supported for {dtype} values '
137 'because it would be ambiguous.') from e
138 else:
139 return nx.logical_and(is_inf, signbit, out)
140
141
142@array_function_dispatch(_dispatcher, verify=False, module='numpy')
143def isneginf(x, out=None):
144 """
145 Test element-wise for negative infinity, return result as bool array.
146
147 Parameters
148 ----------
149 x : array_like
150 The input array.
151 out : array_like, optional
152 A location into which the result is stored. If provided, it must have a
153 shape that the input broadcasts to. If not provided or None, a
154 freshly-allocated boolean array is returned.
155
156 Returns
157 -------
158 out : ndarray
159 A boolean array with the same dimensions as the input.
160 If second argument is not supplied then a numpy boolean array is
161 returned with values True where the corresponding element of the
162 input is negative infinity and values False where the element of
163 the input is not negative infinity.
164
165 If a second argument is supplied the result is stored there. If the
166 type of that array is a numeric type the result is represented as
167 zeros and ones, if the type is boolean then as False and True. The
168 return value `out` is then a reference to that array.
169
170 See Also
171 --------
172 isinf, isposinf, isnan, isfinite
173
174 Notes
175 -----
176 NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
177 (IEEE 754).
178
179 Errors result if the second argument is also supplied when x is a scalar
180 input, if first and second arguments have different shapes, or if the
181 first argument has complex values.
182
183 Examples
184 --------
185 >>> np.isneginf(np.NINF)
186 True
187 >>> np.isneginf(np.inf)
188 False
189 >>> np.isneginf(np.PINF)
190 False
191 >>> np.isneginf([-np.inf, 0., np.inf])
192 array([ True, False, False])
193
194 >>> x = np.array([-np.inf, 0., np.inf])
195 >>> y = np.array([2, 2, 2])
196 >>> np.isneginf(x, y)
197 array([1, 0, 0])
198 >>> y
199 array([1, 0, 0])
200
201 """
202 is_inf = nx.isinf(x)
203 try:
204 signbit = nx.signbit(x)
205 except TypeError as e:
206 dtype = nx.asanyarray(x).dtype
207 raise TypeError(f'This operation is not supported for {dtype} values '
208 'because it would be ambiguous.') from e
209 else:
210 return nx.logical_and(is_inf, signbit, out)