Chris@87
|
1 """
|
Chris@87
|
2 Module of functions that are like ufuncs in acting on arrays and optionally
|
Chris@87
|
3 storing results in an output array.
|
Chris@87
|
4
|
Chris@87
|
5 """
|
Chris@87
|
6 from __future__ import division, absolute_import, print_function
|
Chris@87
|
7
|
Chris@87
|
8 __all__ = ['fix', 'isneginf', 'isposinf']
|
Chris@87
|
9
|
Chris@87
|
10 import numpy.core.numeric as nx
|
Chris@87
|
11
|
Chris@87
|
12 def fix(x, y=None):
|
Chris@87
|
13 """
|
Chris@87
|
14 Round to nearest integer towards zero.
|
Chris@87
|
15
|
Chris@87
|
16 Round an array of floats element-wise to nearest integer towards zero.
|
Chris@87
|
17 The rounded values are returned as floats.
|
Chris@87
|
18
|
Chris@87
|
19 Parameters
|
Chris@87
|
20 ----------
|
Chris@87
|
21 x : array_like
|
Chris@87
|
22 An array of floats to be rounded
|
Chris@87
|
23 y : ndarray, optional
|
Chris@87
|
24 Output array
|
Chris@87
|
25
|
Chris@87
|
26 Returns
|
Chris@87
|
27 -------
|
Chris@87
|
28 out : ndarray of floats
|
Chris@87
|
29 The array of rounded numbers
|
Chris@87
|
30
|
Chris@87
|
31 See Also
|
Chris@87
|
32 --------
|
Chris@87
|
33 trunc, floor, ceil
|
Chris@87
|
34 around : Round to given number of decimals
|
Chris@87
|
35
|
Chris@87
|
36 Examples
|
Chris@87
|
37 --------
|
Chris@87
|
38 >>> np.fix(3.14)
|
Chris@87
|
39 3.0
|
Chris@87
|
40 >>> np.fix(3)
|
Chris@87
|
41 3.0
|
Chris@87
|
42 >>> np.fix([2.1, 2.9, -2.1, -2.9])
|
Chris@87
|
43 array([ 2., 2., -2., -2.])
|
Chris@87
|
44
|
Chris@87
|
45 """
|
Chris@87
|
46 x = nx.asanyarray(x)
|
Chris@87
|
47 y1 = nx.floor(x)
|
Chris@87
|
48 y2 = nx.ceil(x)
|
Chris@87
|
49 if y is None:
|
Chris@87
|
50 y = nx.asanyarray(y1)
|
Chris@87
|
51 y[...] = nx.where(x >= 0, y1, y2)
|
Chris@87
|
52 return y
|
Chris@87
|
53
|
Chris@87
|
54 def isposinf(x, y=None):
|
Chris@87
|
55 """
|
Chris@87
|
56 Test element-wise for positive infinity, return result as bool array.
|
Chris@87
|
57
|
Chris@87
|
58 Parameters
|
Chris@87
|
59 ----------
|
Chris@87
|
60 x : array_like
|
Chris@87
|
61 The input array.
|
Chris@87
|
62 y : array_like, optional
|
Chris@87
|
63 A boolean array with the same shape as `x` to store the result.
|
Chris@87
|
64
|
Chris@87
|
65 Returns
|
Chris@87
|
66 -------
|
Chris@87
|
67 y : ndarray
|
Chris@87
|
68 A boolean array with the same dimensions as the input.
|
Chris@87
|
69 If second argument is not supplied then a boolean array is returned
|
Chris@87
|
70 with values True where the corresponding element of the input is
|
Chris@87
|
71 positive infinity and values False where the element of the input is
|
Chris@87
|
72 not positive infinity.
|
Chris@87
|
73
|
Chris@87
|
74 If a second argument is supplied the result is stored there. If the
|
Chris@87
|
75 type of that array is a numeric type the result is represented as zeros
|
Chris@87
|
76 and ones, if the type is boolean then as False and True.
|
Chris@87
|
77 The return value `y` is then a reference to that array.
|
Chris@87
|
78
|
Chris@87
|
79 See Also
|
Chris@87
|
80 --------
|
Chris@87
|
81 isinf, isneginf, isfinite, isnan
|
Chris@87
|
82
|
Chris@87
|
83 Notes
|
Chris@87
|
84 -----
|
Chris@87
|
85 Numpy uses the IEEE Standard for Binary Floating-Point for Arithmetic
|
Chris@87
|
86 (IEEE 754).
|
Chris@87
|
87
|
Chris@87
|
88 Errors result if the second argument is also supplied when `x` is a
|
Chris@87
|
89 scalar input, or if first and second arguments have different shapes.
|
Chris@87
|
90
|
Chris@87
|
91 Examples
|
Chris@87
|
92 --------
|
Chris@87
|
93 >>> np.isposinf(np.PINF)
|
Chris@87
|
94 array(True, dtype=bool)
|
Chris@87
|
95 >>> np.isposinf(np.inf)
|
Chris@87
|
96 array(True, dtype=bool)
|
Chris@87
|
97 >>> np.isposinf(np.NINF)
|
Chris@87
|
98 array(False, dtype=bool)
|
Chris@87
|
99 >>> np.isposinf([-np.inf, 0., np.inf])
|
Chris@87
|
100 array([False, False, True], dtype=bool)
|
Chris@87
|
101
|
Chris@87
|
102 >>> x = np.array([-np.inf, 0., np.inf])
|
Chris@87
|
103 >>> y = np.array([2, 2, 2])
|
Chris@87
|
104 >>> np.isposinf(x, y)
|
Chris@87
|
105 array([0, 0, 1])
|
Chris@87
|
106 >>> y
|
Chris@87
|
107 array([0, 0, 1])
|
Chris@87
|
108
|
Chris@87
|
109 """
|
Chris@87
|
110 if y is None:
|
Chris@87
|
111 x = nx.asarray(x)
|
Chris@87
|
112 y = nx.empty(x.shape, dtype=nx.bool_)
|
Chris@87
|
113 nx.logical_and(nx.isinf(x), ~nx.signbit(x), y)
|
Chris@87
|
114 return y
|
Chris@87
|
115
|
Chris@87
|
116 def isneginf(x, y=None):
|
Chris@87
|
117 """
|
Chris@87
|
118 Test element-wise for negative infinity, return result as bool array.
|
Chris@87
|
119
|
Chris@87
|
120 Parameters
|
Chris@87
|
121 ----------
|
Chris@87
|
122 x : array_like
|
Chris@87
|
123 The input array.
|
Chris@87
|
124 y : array_like, optional
|
Chris@87
|
125 A boolean array with the same shape and type as `x` to store the
|
Chris@87
|
126 result.
|
Chris@87
|
127
|
Chris@87
|
128 Returns
|
Chris@87
|
129 -------
|
Chris@87
|
130 y : ndarray
|
Chris@87
|
131 A boolean array with the same dimensions as the input.
|
Chris@87
|
132 If second argument is not supplied then a numpy boolean array is
|
Chris@87
|
133 returned with values True where the corresponding element of the
|
Chris@87
|
134 input is negative infinity and values False where the element of
|
Chris@87
|
135 the input is not negative infinity.
|
Chris@87
|
136
|
Chris@87
|
137 If a second argument is supplied the result is stored there. If the
|
Chris@87
|
138 type of that array is a numeric type the result is represented as
|
Chris@87
|
139 zeros and ones, if the type is boolean then as False and True. The
|
Chris@87
|
140 return value `y` is then a reference to that array.
|
Chris@87
|
141
|
Chris@87
|
142 See Also
|
Chris@87
|
143 --------
|
Chris@87
|
144 isinf, isposinf, isnan, isfinite
|
Chris@87
|
145
|
Chris@87
|
146 Notes
|
Chris@87
|
147 -----
|
Chris@87
|
148 Numpy uses the IEEE Standard for Binary Floating-Point for Arithmetic
|
Chris@87
|
149 (IEEE 754).
|
Chris@87
|
150
|
Chris@87
|
151 Errors result if the second argument is also supplied when x is a scalar
|
Chris@87
|
152 input, or if first and second arguments have different shapes.
|
Chris@87
|
153
|
Chris@87
|
154 Examples
|
Chris@87
|
155 --------
|
Chris@87
|
156 >>> np.isneginf(np.NINF)
|
Chris@87
|
157 array(True, dtype=bool)
|
Chris@87
|
158 >>> np.isneginf(np.inf)
|
Chris@87
|
159 array(False, dtype=bool)
|
Chris@87
|
160 >>> np.isneginf(np.PINF)
|
Chris@87
|
161 array(False, dtype=bool)
|
Chris@87
|
162 >>> np.isneginf([-np.inf, 0., np.inf])
|
Chris@87
|
163 array([ True, False, False], dtype=bool)
|
Chris@87
|
164
|
Chris@87
|
165 >>> x = np.array([-np.inf, 0., np.inf])
|
Chris@87
|
166 >>> y = np.array([2, 2, 2])
|
Chris@87
|
167 >>> np.isneginf(x, y)
|
Chris@87
|
168 array([1, 0, 0])
|
Chris@87
|
169 >>> y
|
Chris@87
|
170 array([1, 0, 0])
|
Chris@87
|
171
|
Chris@87
|
172 """
|
Chris@87
|
173 if y is None:
|
Chris@87
|
174 x = nx.asarray(x)
|
Chris@87
|
175 y = nx.empty(x.shape, dtype=nx.bool_)
|
Chris@87
|
176 nx.logical_and(nx.isinf(x), nx.signbit(x), y)
|
Chris@87
|
177 return y
|