8000 Merge pull request #24950 from mtsokol/test-public-functions-one-loca… · lesteve/numpy@a547936 · GitHub
[go: up one dir, main page]

Skip to content

Commit a547936

Browse files
authored
Merge pull request numpy#24950 from mtsokol/test-public-functions-one-location
TEST: Add test for checking functions' one location rule
2 parents a082ed8 + 06e9e12 commit a547936

File tree

10 files changed

+1116
-1008
lines changed

10 files changed

+1116
-1008
lines changed

numpy/lib/_scimath_impl.py

Lines changed: 625 additions & 0 deletions
Large diffs are not rendered by default.

numpy/lib/_scimath_impl.pyi

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
from typing import overload, Any
2+
3+
from numpy import complexfloating
4+
5+
from numpy._typing import (
6+
NDArray,
7+
_ArrayLikeFloat_co,
8+
_ArrayLikeComplex_co,
9+
_ComplexLike_co,
10+
_FloatLike_co,
11+
)
12+
13+
__all__: list[str]
14+
15+
@overload
16+
def sqrt(x: _FloatLike_co) -> Any: ...
17+
@overload
18+
def sqrt(x: _ComplexLike_co) -> complexfloating[Any, Any]: ...
19+
@overload
20+
def sqrt(x: _ArrayLikeFloat_co) -> NDArray[Any]: ...
21+
@overload
22+
def sqrt(x: _ArrayLikeComplex_co) -> NDArray[complexfloating[Any, Any]]: ...
23+
24+
@overload
25+
def log(x: _FloatLike_co) -> Any: ...
26+
@overload
27+
def log(x: _ComplexLike_co) -> complexfloating[Any, Any]: ...
28+
@overload
29+
def log(x: _ArrayLikeFloat_co) -> NDArray[Any]: ...
30+
@overload
31+
def log(x: _ArrayLikeComplex_co) -> NDArray[complexfloating[Any, Any]]: ...
32+
33+
@overload
34+
def log10(x: _FloatLike_co) -> Any: ...
35+
@overload
36+
def log10(x: _ComplexLike_co) -> complexfloating[Any, Any]: ...
37+
@overload
38+
def log10(x: _ArrayLikeFloat_co) -> NDArray[Any]: ...
39+
@overload
40< A3E2 code class="diff-text syntax-highlighted-line addition">+
def log10(x: _ArrayLikeComplex_co) -> NDArray[complexfloating[Any, Any]]: ...
41+
42+
@overload
43+
def log2(x: _FloatLike_co) -> Any: ...
44+
@overload
45+
def log2(x: _ComplexLike_co) -> complexfloating[Any, Any]: ...
46+
@overload
47+
def log2(x: _ArrayLikeFloat_co) -> NDArray[Any]: ...
48+
@overload
49+
def log2(x: _ArrayLikeComplex_co) -> NDArray[complexfloating[Any, Any]]: ...
50+
51+
@overload
52+
def logn(n: _FloatLike_co, x: _FloatLike_co) -> Any: ...
53+
@overload
54+
def logn(n: _ComplexLike_co, x: _ComplexLike_co) -> complexfloating[Any, Any]: ...
55+
@overload
56+
def logn(n: _ArrayLikeFloat_co, x: _ArrayLikeFloat_co) -> NDArray[Any]: ...
57+
@overload
58+
def logn(n: _ArrayLikeComplex_co, x: _ArrayLikeComplex_co) -> NDArray[complexfloating[Any, Any]]: ...
59+
60+
@overload
61+
def power(x: _FloatLike_co, p: _FloatLike_co) -> Any: ...
62+
@overload
63+
def power(x: _ComplexLike_co, p: _ComplexLike_co) -> complexfloating[Any, Any]: ...
64+
@overload
65+
def power(x: _ArrayLikeFloat_co, p: _ArrayLikeFloat_co) -> NDArray[Any]: ...
66+
@overload
67+
def power(x: _ArrayLikeC F438 omplex_co, p: _ArrayLikeComplex_co) -> NDArray[complexfloating[Any, Any]]: ...
68+
69+
@overload
70+
def arccos(x: _FloatLike_co) -> Any: ...
71+
@overload
72+
def arccos(x: _ComplexLike_co) -> complexfloating[Any, Any]: ...
73+
@overload
74+
def arccos(x: _ArrayLikeFloat_co) -> NDArray[Any]: ...
75+
@overload
76+
def arccos(x: _ArrayLikeComplex_co) -> NDArray[complexfloating[Any, Any]]: ...
77+
78+
@overload
79+
def arcsin(x: _FloatLike_co) -> Any: ...
80+
@overload
81+
def arcsin(x: _ComplexLike_co) -> complexfloating[Any, Any]: ...
82+
@overload
83+
def arcsin(x: _ArrayLikeFloat_co) -> NDArray[Any]: ...
84+
@overload
85+
def arcsin(x: _ArrayLikeComplex_co) -> NDArray[complexfloating[Any, Any]]: ...
86+
87+
@overload
88+
def arctanh(x: _FloatLike_co) -> Any: ...
89+
@overload
90+
def arctanh(x: _ComplexLike_co) -> complexfloating[Any, Any]: ...
91+
@overload
92+
def arctanh(x: _ArrayLikeFloat_co) -> NDArray[Any]: ...
93+
@overload
94+
def arctanh(x: _ArrayLikeComplex_co) -> NDArray[complexfloating[Any, Any]]: ...

numpy/lib/_user_array_impl.py

Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
"""
2+
Container class for backward compatibility with NumArray.
3+
4+
The user_array.container class exists for backward compatibility with NumArray
5+
and is not meant to be used in new code. If you need to create an array
6+
container class, we recommend either creating a class that wraps an ndarray
7+
or subclasses ndarray.
8+
9+
"""
10+
from numpy._core import (
11+
array, asarray, absolute, add, subtract, multiply, divide,
12+
remainder, power, left_shift, right_shift, bitwise_and, bitwise_or,
13+
bitwise_xor, invert, less, less_equal, not_equal, equal, greater,
14+
greater_equal, shape, reshape, arange, sin, sqrt, transpose
15+
)
16+
17+
18+
class container:
19+
"""
20+
container(data, dtype=None, copy=True)
21+
22+
Standard container-class for easy multiple-inheritance.
23+
24+
Methods
25+
-------
26+
copy
27+
tostring
28+
byteswap
29+
astype
30+
31+
"""
32+
def __init__(self, data, dtype=None, copy=True):
33+
self.array = array(data, dtype, copy=copy)
34+
35+
def __repr__(self):
36+
if self.ndim > 0:
37+
return self.__class__.__name__ + repr(self.array)[len("array"):]
38+
else:
39+
return self.__class__.__name__ + "(" + repr(self.array) + ")"
40+
41+
def __array__(self, t=None):
42+
if t:
43+
return self.array.astype(t)
44+
return self.array
45+
46+
# Array as sequence
47+
def __len__(self):
48+
return len(self.array)
49+
50+
def __getitem__(self, index):
51+
return self._rc(self.array[index])
52+
53+
def __setitem__(self, index, value):
54+
self.array[index] = asarray(value, self.dtype)
55+
56+
def __abs__(self):
57+
return self._rc(absolute(self.array))
58+
59+
def __neg__(self):
60+
return self._rc(-self.array)
61+
62+
def __add__(self, other):
63+
return self._rc(self.array + asarray(other))
64+
65+
__radd__ = __add__
66+
67+
def __iadd__(self, other):
68+
add(self.array, other, self.array)
69+
return self
70+
71+
def __sub__(self, other):
72+
return self._rc(self.array - asarray(other))
73+
74+
def __rsub__(self, other):
75+
return self._rc(asarray(other) - self.array)
76+
77+
def __isub__(self, other):
78+
subtract(self.array, other, self.array)
79+
return self
80+
81+
def __mul__(self, other):
82+
return self._rc(multiply(self.array, asarray(other)))
83+
84+
__rmul__ = __mul__
85+
86+
def __imul__(self, other):
87+
multiply(self.array, other, self.array)
88+
return self
89+
90+
def __div__(self, other):
91+
return self._rc(divide(self.array, asarray(other)))
92+
93+
def __rdiv__(self, other):
94+
return self._rc(divide(asarray(other), self.array))
95+
96+
def __idiv__(self, other):
97+
divide(self.array, other, self.array)
98+
return self
99+
100+
def __mod__(self, other):
101+
return self._rc(remainder(self.array, other))
102+
103+
def __rmod__(self, other):
104+
return self._rc(remainder(other, self.array))
105+
106+
def __imod__(self, other):
107+
remainder(self.array, other, self.array)
108+
return self
109+
110+
def __divmod__(self, other):
111+
return (self._rc(divide(self.array, other)),
112+
self._rc(remainder(self.array, other)))
113+
114+
def __rdivmod__(self, other):
115+
return (self._rc(divide(other, self.array)),
116+
self._rc(remainder(other, self.array)))
117+
118+
def __pow__(self, other):
119+
return self._rc(power(self.array, asarray(other)))
120+
121+
def __rpow__(self, other):
122+
return self._rc(power(asarray(other), self.array))
123+
124+
def __ipow__(self, other):
125+
power(self.array, other, self.array)
126+
return self
127+
128+
def __lshift__(self, other):
129+
return self._rc(left_shift(self.array, other))
130+
131+
def __rshift__(self, other):
132+
return self._rc(right_shift(self.array, other))
133+
134+
def __rlshift__(self, other):
135+
return self._rc(left_shift(other, self.array))
136+
137+
def __rrshift__(self, other):
138+
return self._rc(right_shift(other, self.array))
139+
140+
def __ilshift__(self, other):
141+
left_shift(self.array, other, self.array)
142+
return self
143+
144+
def __irshift__(self, other):
145+
right_shift(self.array, other, self.array)
146+
return self
147+
148+
def __and__(self, other):
149+
return self._rc(bitwise_and(self.array, other))
150+
151+
def __rand__(self, other):
152+
return self._rc(bitwise_and(other, self.array))
153+
154+
def __iand__(self, other):
155+
bitwise_and(self.array, other, self.array)
156+
return self
157+
158+
def __xor__(self, other):
159+
return self._rc(bitwise_xor(self.array, other))
160+
161+
def __rxor__(self, other):
162+
return self._rc(bitwise_xor(other, self.array))
163+
164+
def __ixor__(self, other):
165+
bitwise_xor(self.array, other, self.array)
166+
return self
167+
168+
def __or__(self, other):
169+
return self._rc(bitwise_or(self.array, other))
170+
171+
def __ror__(self, other):
172+
return self._rc(bitwise_or(other, self.array))
173+
174+
def __ior__(self, other):
175+
bitwise_or(self.array, other, self.array)
176+
return self
177+
178+
def __pos__(self):
179+
return self._rc(self.array)
180+
181+
def __invert__(self):
182+
return self._rc(invert(self.array))
183+
184+
def _scalarfunc(self, func):
185+
if self.ndim == 0:
186+
return func(self[0])
187+
else:
188+
raise TypeError(
189+
"only rank-0 arrays can be converted to Python scalars.")
190+
191+
def __complex__(self):
192+
return self._scalarfunc(complex)
193+
194+
def __float__(self):
195+
return self._scalarfunc(float)
196+
197+
def __int__(self):
198+
return self._scalarfunc(int)
199+
200+
def __hex__(self):
201+
return self._scalarfunc(hex)
202+
203+
def __oct__(self):
204+
return self._scalarfunc(oct)
205+
206+
def __lt__(self, other):
207+
return self._rc(less(self.array, other))
208+
209+
def __le__(self, other):
210+
return self._rc(less_equal(self.array, other))
211+
212+
def __eq__(self, other):
213+
return self._rc(equal(self.array, other))
214+
215+
def __ne__(self, other):
216+
return self._rc(not_equal(self.array, other))
217+
218+
def __gt__(self, other):
219+
return self._rc(greater(self.array, other))
220+
221+
def __ge__(self, other):
222+
return self._rc(greater_equal(self.array, other))
223+
224+
def copy(self):
225+
""
226+
return self._rc(self.array.copy())
227+
228+
def tostring(self):
229+
""
230+
return self.array.tostring()
231+
232+
def tobytes(self):
233+
""
234+
return self.array.tobytes()
235+
236+
def byteswap(self):
237+
""
238+
return self._rc(self.array.byteswap())
239+
240+
def astype(self, typecode):
241+
""
242+
return self._rc(self.array.astype(typecode))
243+
244+
def _rc(self, a):
245+
if len(shape(a)) == 0:
246+
return a
247+
else:
248+
return self.__class__(a)
249+
250+
def __array_wrap__(self, *args):
251+
return self.__class__(args[0])
252+
253+
def __setattr__(self, attr, value):
254+
if attr == 'array':
255+
object.__setattr__(self, attr, value)
256+
return
257+
try:
258+
self.array.__setattr__(attr, value)
259+
except AttributeError:
260+
object.__setattr__(self, attr, value)
261+
262+
# Only called after other approaches fail.
263+
def __getattr__(self, attr):
264+
if (attr == 'array'):
265+
return object.__getattribute__(self, attr)
266+
return self.array.__getattribute__(attr)
267+
268+
269+
#############################################################
270+
# Test of class container
271+
#############################################################
272+
if __name__ == '__main__':
273+
temp = reshape(arange(10000), (100, 100))
274+
275+
ua = container(temp)
276+
# new object created begin test
277+
print(dir(ua))
278+
print(shape(ua), ua.shape) # I have changed Numeric.py
279+
280+
ua_small = ua[:3, :5]
281+
print(ua_small)
282+
# this did not change ua[0,0], which is not normal behavior
283+
ua_small[0, 0] = 10
284+
print(ua_small[0, 0], ua[0, 0])
285+
print(sin(ua_small) / 3. * 6. + sqrt(ua_small ** 2))
286+
print(less(ua_small, 103), type(less(ua_small, 103)))
287+
print(type(ua_small * reshape(arange(15), shape(ua_small))))
288+
print(reshape(ua_small, (5, 3)))
289+
print(transpose(ua_small))

0 commit comments

Comments
 (0)
0