14
14
15
15
"""
16
16
17
- from hypothesis import given , assume
18
- from hypothesis .strategies import composite , just
19
-
20
17
import math
21
18
22
- from .hypothesis_helpers import (integer_dtype_objects ,
23
- floating_dtype_objects ,
24
- numeric_dtype_objects ,
19
+ from hypothesis import assume , given
20
+ from hypothesis import strategies as st
21
+
22
+ from . import _array_module as xp
23
+ from .array_helpers import (assert_exactly_equal , assert_integral ,
24
+ assert_same_sign , dtype_ranges , false , infinity ,
25
+ inrange , int_to_dtype , is_float_dtype ,
26
+ is_integer_dtype , isintegral , isnegative , ndindex ,
27
+ negative_mathematical_sign , one ,
28
+ positive_mathematical_sign , promote_dtypes , true ,
29
+ zero , π )
30
+ from .hypothesis_helpers import (array_scalars , boolean_dtype_objects ,
31
+ boolean_dtypes , floating_dtype_objects ,
32
+ floating_dtypes , integer_dtype_objects ,
25
33
integer_or_boolean_dtype_objects ,
26
- boolean_dtype_objects , floating_dtypes ,
27
- numeric_dtypes , integer_or_boolean_dtypes ,
28
- boolean_dtypes , mutually_promotable_dtypes ,
29
- array_scalars , two_mutual_arrays , xps , shapes )
30
- from .array_helpers import (assert_exactly_equal , negative ,
31
- positive_mathematical_sign ,
32
- negative_mathematical_sign , logical_not ,
33
- logical_or , logical_and , inrange , π , one , zero ,
34
- infinity , isnegative , all as array_all , any as
35
- array_any , int_to_dtype ,
36
- assert_integral , less_equal , isintegral , isfinite ,
37
- ndindex , promote_dtypes , is_integer_dtype ,
38
- is_float_dtype , not_equal , asarray ,
39
- dtype_ranges , full , true , false , assert_same_sign ,
40
- isnan , equal , less )
34
+ integer_or_boolean_dtypes ,
35
+ mutually_promotable_dtypes ,
36
+ numeric_dtype_objects , numeric_dtypes , shapes ,
37
+ two_mutual_arrays , xps )
41
38
# We might as well use this implementation rather than requiring
42
39
# mod.broadcast_shapes(). See test_equal() and others.
43
40
from .test_broadcasting import broadcast_shapes
44
41
45
- from . import _array_module as xp
46
-
47
42
# integer_scalars = array_scalars(integer_dtypes)
48
43
floating_scalars = array_scalars (floating_dtypes )
49
44
numeric_scalars = array_scalars (numeric_dtypes )
57
52
two_boolean_dtypes = mutually_promotable_dtypes (boolean_dtype_objects )
58
53
two_any_dtypes = mutually_promotable_dtypes ()
59
54
60
- @composite
55
+ @st . composite
61
56
def two_array_scalars (draw , dtype1 , dtype2 ):
62
57
# two_dtypes should be a strategy that returns two dtypes (like
63
58
# mutually_promotable_dtypes())
64
- return draw (array_scalars (just (dtype1 ))), draw (array_scalars (just (dtype2 )))
59
+ return draw (array_scalars (st . just (dtype1 ))), draw (array_scalars (st . just (dtype2 )))
65
60
66
61
def sanity_check (x1 , x2 ):
67
62
try :
@@ -74,17 +69,17 @@ def test_abs(x):
74
69
if is_integer_dtype (x .dtype ):
75
70
minval = dtype_ranges [x .dtype ][0 ]
76
71
if minval < 0 :
77
- # abs of the smallest representable negative integer is not defined
78
- mask = not_equal (x , full (x .shape , minval , dtype = x .dtype ))
72
+ # abs of the smallest representable xp. negative integer is not defined
73
+ mask = xp . not_equal (x , xp . full (x .shape , minval , dtype = x .dtype ))
79
74
x = x [mask ]
80
75
a = xp .abs (x )
81
- assert array_all ( logical_not (negative_mathematical_sign (a ))), "abs(x) did not have positive sign"
76
+ assert xp . all ( xp . logical_not (negative_mathematical_sign (a ))), "abs(x) did not have positive sign"
82
77
less_zero = negative_mathematical_sign (x )
83
- negx = negative (x )
78
+ negx = xp . negative (x )
84
79
# abs(x) = -x for x < 0
85
80
assert_exactly_equal (a [less_zero ], negx [less_zero ])
86
81
# abs(x) = x for x >= 0
87
- assert_exactly_equal (a [logical_not (less_zero )], x [logical_not (less_zero )])
82
+ assert_exactly_equal (a [xp . logical_not (less_zero )], x [xp . logical_not (less_zero )])
88
83
89
84
@given (xps .arrays (dtype = xps .floating_dtypes (), shape = shapes ))
90
85
def test_acos (x ):
@@ -170,7 +165,7 @@ def test_atan2(x1_and_x2):
170
165
# atan2 maps [-inf, inf] x [-inf, inf] to [-pi, pi]. Values outside
171
166
# this domain are mapped to nan, which is already tested in the special
172
167
# cases.
173
- assert_exactly_equal (logical_and (domainx1 , domainx2 ), codomain )
168
+ assert_exactly_equal (xp . logical_and (domainx1 , domainx2 ), codomain )
174
169
# From the spec:
175
170
#
176
171
# The mathematical signs of `x1_i` and `x2_i` determine the quadrant of
@@ -187,10 +182,10 @@ def test_atan2(x1_and_x2):
187
182
negx2 = negative_mathematical_sign (x2 )
188
183
posa = positive_mathematical_sign (a )
189
184
nega = negative_mathematical_sign (a )
190
- assert_exactly_equal (logical_or (logical_and (posx1 , posx2 ),
191
- logical_and (posx1 , negx2 )), posa )
192
- assert_exactly_equal (logical_or (logical_and (negx1 , posx2 ),
193
- logical_and (negx1 , negx2 )), nega )
185
+ assert_exactly_equal (xp . logical_or (xp . logical_and (posx1 , posx2 ),
186
+ xp . logical_and (posx1 , negx2 )), posa )
187
+ assert_exactly_equal (xp . logical_or (xp . logical_and (negx1 , posx2 ),
188
+ xp . logical_and (negx1 , negx2 )), nega )
194
189
195
190
@given (xps .arrays (dtype = xps .floating_dtypes (), shape = shapes ))
196
191
def test_atanh (x ):
@@ -232,8 +227,7 @@ def test_bitwise_left_shift(args):
232
227
x1 , x2 = args
233
228
sanity_check (x1 , x2 )
234
229
negative_x2 = isnegative (x2 )
235
- if array_any (negative_x2 ):
236
- assume (False )
230
+ assume (not xp .any (negative_x2 ))
237
231
a = xp .bitwise_left_shift (x1 , x2 )
238
232
# Compare against the Python << operator.
239
233
# TODO: Generalize this properly for inputs that are arrays.
@@ -296,8 +290,7 @@ def test_bitwise_right_shift(args):
296
290
x1 , x2 = args
297
291
sanity_check (x1 , x2 )
298
292
negative_x2 = isnegative (x2 )
299
- if array_any (negative_x2 ):
300
- assume (False )
293
+ assume (not xp .any (negative_x2 ))
301
294
a = xp .bitwise_right_shift (x1 , x2 )
302
295
# Compare against the Python >> operator.
303
296
# TODO: Generalize this properly for inputs that are arrays.
@@ -335,10 +328,10 @@ def test_bitwise_xor(args):
335
328
def test_ceil (x ):
336
329
# This test is almost identical to test_floor()
337
330
a = xp .ceil (x )
338
- finite = isfinite (x )
331
+ finite = xp . isfinite (x )
339
332
assert_integral (a [finite ])
340
- assert array_all ( less_equal (x [finite ], a [finite ]))
341
- assert array_all ( less_equal (a [finite ] - x [finite ], one (x [finite ].shape , x .dtype )))
333
+ assert xp . all ( xp . less_equal (x [finite ], a [finite ]))
334
+ assert xp . all ( xp . less_equal (a [finite ] - x [finite ], one (x [finite ].shape , x .dtype )))
342
335
integers = isintegral (x )
343
336
assert_exactly_equal (a [integers ], x [integers ])
344
337
@@ -380,7 +373,7 @@ def test_equal(x1_and_x2):
380
373
x1 , x2 = x1_and_x2
381
374
sanity_check (x1 , x2 )
382
375
a = xp .equal (x1 , x2 )
383
- # NOTE: assert_exactly_equal() itself uses equal(), so we must be careful
376
+ # NOTE: assert_exactly_equal() itself uses xp. equal(), so we must be careful
384
377
# not to use it here. Otherwise, the test would be circular and
385
378
# meaningless. Instead, we implement this by iterating every element of
386
379
# the arrays and comparing them. The logic here is also used for the tests
@@ -397,15 +390,15 @@ def test_equal(x1_and_x2):
397
390
_x2 = xp .broadcast_to (x2 , shape )
398
391
399
392
# Second, manually promote the dtypes. This is important. If the internal
400
- # type promotion in equal() is wrong, it will not be directly visible in
393
+ # type promotion in xp. equal() is wrong, it will not be directly visible in
401
394
# the output type, but it can lead to wrong answers. For example,
402
- # equal(array(1.0, dtype=float32), array(1.00000001, dtype=xp.float64)) will
403
- # be wrong if the xp. float64 is downcast to float32. # be wrong if the
395
+ # xp. equal(array(1.0, dtype=xp. float32), array(1.00000001, dtype=xp.float64)) will
396
+ # be wrong if the float64 is downcast to float32. # be wrong if the
404
397
# xp.float64 is downcast to float32. See the comment on
405
398
# test_elementwise_function_two_arg_bool_type_promotion() in
406
- # test_type_promotion.py. The type promotion for equal() is not *really*
399
+ # test_type_promotion.py. The type promotion for xp. equal() is not *really*
407
400
# tested in that file, because doing so requires doing the consistency
408
- # check we do here rather than just checking the result dtype.
401
+ # check we do here rather than st. just checking the result dtype.
409
402
promoted_dtype = promote_dtypes (x1 .dtype , x2 .dtype )
410
403
_x1 = xp .asarray (_x1 , dtype = promoted_dtype )
411
404
_x2 = xp .asarray (_x2 , dtype = promoted_dtype )
@@ -450,10 +443,10 @@ def test_expm1(x):
450
443
def test_floor (x ):
451
444
# This test is almost identical to test_ceil
452
445
a = xp .floor (x )
453
- finite = isfinite (x )
446
+ finite = xp . isfinite (x )
454
447
assert_integral (a [finite ])
455
- assert array_all ( less_equal (a [finite ], x [finite ]))
456
- assert array_all ( less_equal (x [finite ] - a [finite ], one (x [finite ].shape , x .dtype )))
448
+ assert xp . all ( xp . less_equal (a [finite ], x [finite ]))
449
+ assert xp . all ( xp . less_equal (x [finite ] - a [finite ], one (x [finite ].shape , x .dtype )))
457
450
integers = isintegral (x )
458
451
assert_exactly_equal (a [integers ], x [integers ])
459
452
@@ -467,8 +460,8 @@ def test_floor_divide(x1_and_x2):
467
460
# we avoid passing it in entirely.
468
461
assume (not xp .any (x1 == 0 ) and not xp .any (x2 == 0 ))
469
462
div = xp .divide (
470
- asarray (x1 , dtype = xp .float64 ),
471
- asarray (x2 , dtype = xp .float64 ),
463
+ xp . asarray (x1 , dtype = xp .float64 ),
464
+ xp . asarray (x2 , dtype = xp .float64 ),
472
465
)
473
466
else :
474
467
div = xp .divide (x1 , x2 )
@@ -477,7 +470,7 @@ def test_floor_divide(x1_and_x2):
477
470
478
471
# TODO: The spec doesn't clearly specify the behavior of floor_divide on
479
472
# infinities. See https://github.com/data-apis/array-api/issues/199.
480
- finite = isfinite (div )
473
+ finite = xp . isfinite (div )
481
474
assert_integral (out [finite ])
482
475
483
476
# TODO: Test the exact output for floor_divide.
@@ -548,9 +541,9 @@ def test_isfinite(x):
548
541
TRUE = true (x .shape )
549
542
if is_integer_dtype (x .dtype ):
550
543
assert_exactly_equal (a , TRUE )
551
- # Test that isfinite, isinf, and isnan are self-consistent.
552
- inf = logical_or (xp .isinf (x ), xp .isnan (x ))
553
- assert_exactly_equal (a , logical_not (inf ))
544
+ # Test that xp. isfinite, isinf, and xp. isnan are self-consistent.
545
+ inf = xp . logical_or (xp .isinf (x ), xp .isnan (x ))
546
+ assert_exactly_equal (a , xp . logical_not (inf ))
554
547
555
548
# Test the exact value by comparing to the math version
556
549
if is_float_dtype (x .dtype ):
@@ -564,8 +557,8 @@ def test_isinf(x):
564
557
FALSE = false (x .shape )
565
558
if is_integer_dtype (x .dtype ):
566
559
assert_exactly_equal (a , FALSE )
567
- finite_or_nan = logical_or (xp .isfinite (x ), xp .isnan (x ))
568
- assert_exactly_equal (a , logical_not (finite_or_nan ))
560
+ finite_or_nan = xp . logical_or (xp .isfinite (x ), xp .isnan (x ))
561
+ assert_exactly_equal (a , xp . logical_not (finite_or_nan ))
569
562
570
563
# Test the exact value by comparing to the math version
571
564
if is_float_dtype (x .dtype ):
@@ -579,8 +572,8 @@ def test_isnan(x):
579
572
FALSE = false (x .shape )
580
573
if is_integer_dtype (x .dtype ):
581
574
assert_exactly_equal (a , FALSE )
582
- finite_or_inf = logical_or (xp .isfinite (x ), xp .isinf (x ))
583
- assert_exactly_equal (a , logical_not (finite_or_inf ))
575
+ finite_or_inf = xp . logical_or (xp .isfinite (x ), xp .isinf (x ))
576
+ assert_exactly_equal (a , xp . logical_not (finite_or_inf ))
584
577
585
578
# Test the exact value by comparing to the math version
586
579
if is_float_dtype (x .dtype ):
@@ -767,12 +760,12 @@ def test_negative(x):
767
760
# Negation is an involution
768
761
assert_exactly_equal (x , xp .negative (out ))
769
762
770
- mask = isfinite (x )
763
+ mask = xp . isfinite (x )
771
764
if is_integer_dtype (x .dtype ):
772
765
minval = dtype_ranges [x .dtype ][0 ]
773
766
if minval < 0 :
774
767
# negative of the smallest representable negative integer is not defined
775
- mask = not_equal (x , full (x .shape , minval , dtype = x .dtype ))
768
+ mask = xp . not_equal (x , xp . full (x .shape , minval , dtype = x .dtype ))
776
769
777
770
# Additive inverse
778
771
y = xp .add (x [mask ], out [mask ])
@@ -837,15 +830,15 @@ def test_remainder(x1_and_x2):
837
830
838
831
# out and x2 should have the same sign.
839
832
# assert_same_sign returns False for nans
840
- not_nan = logical_not (logical_or (isnan (out ), isnan (x2 )))
833
+ not_nan = xp . logical_not (xp . logical_or (xp . isnan (out ), xp . isnan (x2 )))
841
834
assert_same_sign (out [not_nan ], x2 [not_nan ])
842
835
843
836
@given (xps .arrays (dtype = xps .numeric_dtypes (), shape = shapes ))
844
837
def test_round (x ):
845
838
a = xp .round (x )
846
839
847
840
# Test that the result is integral
848
- finite = isfinite (x )
841
+ finite = xp . isfinite (x )
849
842
assert_integral (a [finite ])
850
843
851
844
# round(x) should be the nearest integer to x. The case where there is a
@@ -858,8 +851,8 @@ def test_round(x):
858
851
ceil = xp .ceil (x )
859
852
over = xp .subtract (x , floor )
860
853
under = xp .subtract (ceil , x )
861
- round_down = less (over , under )
862
- round_up = less (under , over )
854
+ round_down = xp . less (over , under )
855
+ round_up = xp . less (under , over )
863
856
assert_exactly_equal (a [round_down ], floor [round_down ])
864
857
assert_exactly_equal (a [round_up ], ceil [round_up ])
865
858
@@ -910,7 +903,7 @@ def test_trunc(x):
910
903
assert out .dtype == x .dtype , f"{ x .dtype = !s} but { out .dtype = !s} "
911
904
assert out .shape == x .shape , f"{ x .shape } but { out .shape } "
912
905
if x .dtype in integer_dtype_objects :
913
- assert array_all ( equal (x , out )), f"{ x = !s} but { out = !s} "
906
+ assert xp . all ( xp . equal (x , out )), f"{ x = !s} but { out = !s} "
914
907
else :
915
908
finite_mask = xp .isfinite (out )
916
909
for idx in ndindex (out .shape ):
0 commit comments