Skip to content

Commit 3bd4dc6

Browse files
committed
Use asarray() from array_helpers.py
1 parent e49331e commit 3bd4dc6

File tree

5 files changed

+76
-80
lines changed

5 files changed

+76
-80
lines changed

array_api_tests/special_cases/test_dunder_iadd.py

+17-18
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99

1010
from operator import iadd
1111

12-
from ..array_helpers import (NaN, assert_exactly_equal, exactly_equal, infinity, isfinite,
12+
from ..array_helpers import (NaN, asarray, assert_exactly_equal, exactly_equal, infinity, isfinite,
1313
logical_and, logical_or, non_zero, zero)
1414
from ..hypothesis_helpers import numeric_arrays
15-
from .. import _array_module as xp
1615

1716
from hypothesis import given
1817

@@ -25,7 +24,7 @@ def test_iadd_special_cases_two_args_either(arg1, arg2):
2524
- If either `x1_i` or `x2_i` is `NaN`, the result is `NaN`.
2625
2726
"""
28-
res = xp.asarray(arg1, copy=True)
27+
res = asarray(arg1, copy=True)
2928
iadd(res, arg2)
3029
mask = logical_or(exactly_equal(arg1, NaN(arg1.shape, arg1.dtype)), exactly_equal(arg2, NaN(arg1.shape, arg1.dtype)))
3130
assert_exactly_equal(res[mask], (NaN(arg1.shape, arg1.dtype))[mask])
@@ -39,7 +38,7 @@ def test_iadd_special_cases_two_args_equal__equal_1(arg1, arg2):
3938
- If `x1_i` is `+infinity` and `x2_i` is `-infinity`, the result is `NaN`.
4039
4140
"""
42-
res = xp.asarray(arg1, copy=True)
41+
res = asarray(arg1, copy=True)
4342
iadd(res, arg2)
4443
mask = logical_and(exactly_equal(arg1, infinity(arg1.shape, arg1.dtype)), exactly_equal(arg2, -infinity(arg2.shape, arg2.dtype)))
4544
assert_exactly_equal(res[mask], (NaN(arg1.shape, arg1.dtype))[mask])
@@ -53,7 +52,7 @@ def test_iadd_special_cases_two_args_equal__equal_2(arg1, arg2):
5352
- If `x1_i` is `-infinity` and `x2_i` is `+infinity`, the result is `NaN`.
5453
5554
"""
56-
res = xp.asarray(arg1, copy=True)
55+
res = asarray(arg1, copy=True)
5756
iadd(res, arg2)
5857
mask = logical_and(exactly_equal(arg1, -infinity(arg1.shape, arg1.dtype)), exactly_equal(arg2, infinity(arg2.shape, arg2.dtype)))
5958
assert_exactly_equal(res[mask], (NaN(arg1.shape, arg1.dtype))[mask])
@@ -67,7 +66,7 @@ def test_iadd_special_cases_two_args_equal__equal_3(arg1, arg2):
6766
- If `x1_i` is `+infinity` and `x2_i` is `+infinity`, the result is `+infinity`.
6867
6968
"""
70-
res = xp.asarray(arg1, copy=True)
69+
res = asarray(arg1, copy=True)
7170
iadd(res, arg2)
7271
mask = logical_and(exactly_equal(arg1, infinity(arg1.shape, arg1.dtype)), exactly_equal(arg2, infinity(arg2.shape, arg2.dtype)))
7372
assert_exactly_equal(res[mask], (infinity(arg1.shape, arg1.dtype))[mask])
@@ -81,7 +80,7 @@ def test_iadd_special_cases_two_args_equal__equal_4(arg1, arg2):
8180
- If `x1_i` is `-infinity` and `x2_i` is `-infinity`, the result is `-infinity`.
8281
8382
"""
84-
res = xp.asarray(arg1, copy=True)
83+
res = asarray(arg1, copy=True)
8584
iadd(res, arg2)
8685
mask = logical_and(exactly_equal(arg1, -infinity(arg1.shape, arg1.dtype)), exactly_equal(arg2, -infinity(arg2.shape, arg2.dtype)))
8786
assert_exactly_equal(res[mask], (-infinity(arg1.shape, arg1.dtype))[mask])
@@ -95,7 +94,7 @@ def test_iadd_special_cases_two_args_equal__equal_5(arg1, arg2):
9594
- If `x1_i` is `+infinity` and `x2_i` is a finite number, the result is `+infinity`.
9695
9796
"""
98-
res = xp.asarray(arg1, copy=True)
97+
res = asarray(arg1, copy=True)
9998
iadd(res, arg2)
10099
mask = logical_and(exactly_equal(arg1, infinity(arg1.shape, arg1.dtype)), isfinite(arg2))
101100
assert_exactly_equal(res[mask], (infinity(arg1.shape, arg1.dtype))[mask])
@@ -109,7 +108,7 @@ def test_iadd_special_cases_two_args_equal__equal_6(arg1, arg2):
109108
- If `x1_i` is `-infinity` and `x2_i` is a finite number, the result is `-infinity`.
110109
111110
"""
112-
res = xp.asarray(arg1, copy=True)
111+
res = asarray(arg1, copy=True)
113112
iadd(res, arg2)
114113
mask = logical_and(exactly_equal(arg1, -infinity(arg1.shape, arg1.dtype)), isfinite(arg2))
115114
assert_exactly_equal(res[mask], (-infinity(arg1.shape, arg1.dtype))[mask])
@@ -123,7 +122,7 @@ def test_iadd_special_cases_two_args_equal__equal_7(arg1, arg2):
123122
- If `x1_i` is a finite number and `x2_i` is `+infinity`, the result is `+infinity`.
124123
125124
"""
126-
res = xp.asarray(arg1, copy=True)
125+
res = asarray(arg1, copy=True)
127126
iadd(res, arg2)
128127
mask = logical_and(isfinite(arg1), exactly_equal(arg2, infinity(arg2.shape, arg2.dtype)))
129128
assert_exactly_equal(res[mask], (infinity(arg1.shape, arg1.dtype))[mask])
@@ -137,7 +136,7 @@ def test_iadd_special_cases_two_args_equal__equal_8(arg1, arg2):
137136
- If `x1_i` is a finite number and `x2_i` is `-infinity`, the result is `-infinity`.
138137
139138
"""
140-
res = xp.asarray(arg1, copy=True)
139+
res = asarray(arg1, copy=True)
141140
iadd(res, arg2)
142141
mask = logical_and(isfinite(arg1), exactly_equal(arg2, -infinity(arg2.shape, arg2.dtype)))
143142
assert_exactly_equal(res[mask], (-infinity(arg1.shape, arg1.dtype))[mask])
@@ -151,7 +150,7 @@ def test_iadd_special_cases_two_args_equal__equal_9(arg1, arg2):
151150
- If `x1_i` is `-0` and `x2_i` is `-0`, the result is `-0`.
152151
153152
"""
154-
res = xp.asarray(arg1, copy=True)
153+
res = asarray(arg1, copy=True)
155154
iadd(res, arg2)
156155
mask = logical_and(exactly_equal(arg1, -zero(arg1.shape, arg1.dtype)), exactly_equal(arg2, -zero(arg2.shape, arg2.dtype)))
157156
assert_exactly_equal(res[mask], (-zero(arg1.shape, arg1.dtype))[mask])
@@ -165,7 +164,7 @@ def test_iadd_special_cases_two_args_equal__equal_10(arg1, arg2):
165164
- If `x1_i` is `-0` and `x2_i` is `+0`, the result is `+0`.
166165
167166
"""
168-
res = xp.asarray(arg1, copy=True)
167+
res = asarray(arg1, copy=True)
169168
iadd(res, arg2)
170169
mask = logical_and(exactly_equal(arg1, -zero(arg1.shape, arg1.dtype)), exactly_equal(arg2, zero(arg2.shape, arg2.dtype)))
171170
assert_exactly_equal(res[mask], (zero(arg1.shape, arg1.dtype))[mask])
@@ -179,7 +178,7 @@ def test_iadd_special_cases_two_args_equal__equal_11(arg1, arg2):
179178
- If `x1_i` is `+0` and `x2_i` is `-0`, the result is `+0`.
180179
181180
"""
182-
res = xp.asarray(arg1, copy=True)
181+
res = asarray(arg1, copy=True)
183182
iadd(res, arg2)
184183
mask = logical_and(exactly_equal(arg1, zero(arg1.shape, arg1.dtype)), exactly_equal(arg2, -zero(arg2.shape, arg2.dtype)))
185184
assert_exactly_equal(res[mask], (zero(arg1.shape, arg1.dtype))[mask])
@@ -193,7 +192,7 @@ def test_iadd_special_cases_two_args_equal__equal_12(arg1, arg2):
193192
- If `x1_i` is `+0` and `x2_i` is `+0`, the result is `+0`.
194193
195194
"""
196-
res = xp.asarray(arg1, copy=True)
195+
res = asarray(arg1, copy=True)
197196
iadd(res, arg2)
198197
mask = logical_and(exactly_equal(arg1, zero(arg1.shape, arg1.dtype)), exactly_equal(arg2, zero(arg2.shape, arg2.dtype)))
199198
assert_exactly_equal(res[mask], (zero(arg1.shape, arg1.dtype))[mask])
@@ -207,7 +206,7 @@ def test_iadd_special_cases_two_args_equal__equal_13(arg1, arg2):
207206
- If `x1_i` is a nonzero finite number and `x2_i` is `-x1_i`, the result is `+0`.
208207
209208
"""
210-
res = xp.asarray(arg1, copy=True)
209+
res = asarray(arg1, copy=True)
211210
iadd(res, arg2)
212211
mask = logical_and(logical_and(isfinite(arg1), non_zero(arg1)), exactly_equal(arg2, -arg1))
213212
assert_exactly_equal(res[mask], (zero(arg1.shape, arg1.dtype))[mask])
@@ -221,7 +220,7 @@ def test_iadd_special_cases_two_args_either__equal(arg1, arg2):
221220
- If `x1_i` is either `+0` or `-0` and `x2_i` is a nonzero finite number, the result is `x2_i`.
222221
223222
"""
224-
res = xp.asarray(arg1, copy=True)
223+
res = asarray(arg1, copy=True)
225224
iadd(res, arg2)
226225
mask = logical_and(logical_or(exactly_equal(arg1, zero(arg1.shape, arg1.dtype)), exactly_equal(arg1, -zero(arg1.shape, arg1.dtype))), logical_and(isfinite(arg2), non_zero(arg2)))
227226
assert_exactly_equal(res[mask], (arg2)[mask])
@@ -235,7 +234,7 @@ def test_iadd_special_cases_two_args_equal__either(arg1, arg2):
235234
- If `x1_i` is a nonzero finite number and `x2_i` is either `+0` or `-0`, the result is `x1_i`.
236235
237236
"""
238-
res = xp.asarray(arg1, copy=True)
237+
res = asarray(arg1, copy=True)
239238
iadd(res, arg2)
240239
mask = logical_and(logical_and(isfinite(arg1), non_zero(arg1)), logical_or(exactly_equal(arg2, zero(arg2.shape, arg2.dtype)), exactly_equal(arg2, -zero(arg2.shape, arg2.dtype))))
241240
assert_exactly_equal(res[mask], (arg1)[mask])

array_api_tests/special_cases/test_dunder_imul.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99

1010
from operator import imul
1111

12-
from ..array_helpers import (NaN, assert_exactly_equal, assert_isinf,
12+
from ..array_helpers import (NaN, asarray, assert_exactly_equal, assert_isinf,
1313
assert_negative_mathematical_sign, assert_positive_mathematical_sign,
1414
exactly_equal, infinity, isfinite, logical_and, logical_not,
1515
logical_or, non_zero, same_sign, zero)
1616
from ..hypothesis_helpers import numeric_arrays
17-
from .. import _array_module as xp
1817

1918
from hypothesis import given
2019

@@ -27,7 +26,7 @@ def test_imul_special_cases_two_args_either(arg1, arg2):
2726
- If either `x1_i` or `x2_i` is `NaN`, the result is `NaN`.
2827
2928
"""
30-
res = xp.asarray(arg1, copy=True)
29+
res = asarray(arg1, copy=True)
3130
imul(res, arg2)
3231
mask = logical_or(exactly_equal(arg1, NaN(arg1.shape, arg1.dtype)), exactly_equal(arg2, NaN(arg1.shape, arg1.dtype)))
3332
assert_exactly_equal(res[mask], (NaN(arg1.shape, arg1.dtype))[mask])
@@ -41,7 +40,7 @@ def test_imul_special_cases_two_args_either__either_1(arg1, arg2):
4140
- If `x1_i` is either `+infinity` or `-infinity` and `x2_i` is either `+0` or `-0`, the result is `NaN`.
4241
4342
"""
44-
res = xp.asarray(arg1, copy=True)
43+
res = asarray(arg1, copy=True)
4544
imul(res, arg2)
4645
mask = logical_and(logical_or(exactly_equal(arg1, infinity(arg1.shape, arg1.dtype)), exactly_equal(arg1, -infinity(arg1.shape, arg1.dtype))), logical_or(exactly_equal(arg2, zero(arg2.shape, arg2.dtype)), exactly_equal(arg2, -zero(arg2.shape, arg2.dtype))))
4746
assert_exactly_equal(res[mask], (NaN(arg1.shape, arg1.dtype))[mask])
@@ -55,7 +54,7 @@ def test_imul_special_cases_two_args_either__either_2(arg1, arg2):
5554
- If `x1_i` is either `+0` or `-0` and `x2_i` is either `+infinity` or `-infinity`, the result is `NaN`.
5655
5756
"""
58-
res = xp.asarray(arg1, copy=True)
57+
res = asarray(arg1, copy=True)
5958
imul(res, arg2)
6059
mask = logical_and(logical_or(exactly_equal(arg1, zero(arg1.shape, arg1.dtype)), exactly_equal(arg1, -zero(arg1.shape, arg1.dtype))), logical_or(exactly_equal(arg2, infinity(arg2.shape, arg2.dtype)), exactly_equal(arg2, -infinity(arg2.shape, arg2.dtype))))
6160
assert_exactly_equal(res[mask], (NaN(arg1.shape, arg1.dtype))[mask])
@@ -69,7 +68,7 @@ def test_imul_special_cases_two_args_either__either_3(arg1, arg2):
6968
- If `x1_i` is either `+infinity` or `-infinity` and `x2_i` is either `+infinity` or `-infinity`, the result is a signed infinity with the mathematical sign determined by the rule already stated above.
7069
7170
"""
72-
res = xp.asarray(arg1, copy=True)
71+
res = asarray(arg1, copy=True)
7372
imul(res, arg2)
7473
mask = logical_and(logical_or(exactly_equal(arg1, infinity(arg1.shape, arg1.dtype)), exactly_equal(arg1, -infinity(arg1.shape, arg1.dtype))), logical_or(exactly_equal(arg2, infinity(arg2.shape, arg2.dtype)), exactly_equal(arg2, -infinity(arg2.shape, arg2.dtype))))
7574
assert_isinf(res[mask])
@@ -83,7 +82,7 @@ def test_imul_special_cases_two_args_same_sign_except(arg1, arg2):
8382
- If `x1_i` and `x2_i` have the same mathematical sign, the result has a positive mathematical sign, unless the result is `NaN`. If the result is `NaN`, the "sign" of `NaN` is implementation-defined.
8483
8584
"""
86-
res = xp.asarray(arg1, copy=True)
85+
res = asarray(arg1, copy=True)
8786
imul(res, arg2)
8887
mask = logical_and(same_sign(arg1, arg2), logical_not(exactly_equal(res, NaN(res.shape, res.dtype))))
8988
assert_positive_mathematical_sign(res[mask])
@@ -97,7 +96,7 @@ def test_imul_special_cases_two_args_different_signs_except(arg1, arg2):
9796
- If `x1_i` and `x2_i` have different mathematical signs, the result has a negative mathematical sign, unless the result is `NaN`. If the result is `NaN`, the "sign" of `NaN` is implementation-defined.
9897
9998
"""
100-
res = xp.asarray(arg1, copy=True)
99+
res = asarray(arg1, copy=True)
101100
imul(res, arg2)
102101
mask = logical_and(logical_not(same_sign(arg1, arg2)), logical_not(exactly_equal(res, NaN(res.shape, res.dtype))))
103102
assert_negative_mathematical_sign(res[mask])
@@ -111,7 +110,7 @@ def test_imul_special_cases_two_args_either__equal(arg1, arg2):
111110
- If `x1_i` is either `+infinity` or `-infinity` and `x2_i` is a nonzero finite number, the result is a signed infinity with the mathematical sign determined by the rule already stated above.
112111
113112
"""
114-
res = xp.asarray(arg1, copy=True)
113+
res = asarray(arg1, copy=True)
115114
imul(res, arg2)
116115
mask = logical_and(logical_or(exactly_equal(arg1, infinity(arg1.shape, arg1.dtype)), exactly_equal(arg1, -infinity(arg1.shape, arg1.dtype))), logical_and(isfinite(arg2), non_zero(arg2)))
117116
assert_isinf(res[mask])
@@ -125,7 +124,7 @@ def test_imul_special_cases_two_args_equal__either(arg1, arg2):
125124
- If `x1_i` is a nonzero finite number and `x2_i` is either `+infinity` or `-infinity`, the result is a signed infinity with the mathematical sign determined by the rule already stated above.
126125
127126
"""
128-
res = xp.asarray(arg1, copy=True)
127+
res = asarray(arg1, copy=True)
129128
imul(res, arg2)
130129
mask = logical_and(logical_and(isfinite(arg1), non_zero(arg1)), logical_or(exactly_equal(arg2, infinity(arg2.shape, arg2.dtype)), exactly_equal(arg2, -infinity(arg2.shape, arg2.dtype))))
131130
assert_isinf(res[mask])

0 commit comments

Comments
 (0)