@@ -402,7 +402,7 @@ def test_fmpz_poly():
402
402
assert raises (lambda : [] // Z ([1 ,2 ]), TypeError )
403
403
assert raises (lambda : [] % Z ([1 ,2 ]), TypeError )
404
404
assert raises (lambda : divmod ([], Z ([1 ,2 ])), TypeError )
405
- assert raises (lambda : Z ([1 ,2 ,3 ]) ** - 1 , ( OverflowError , ValueError ) )
405
+ assert raises (lambda : Z ([1 ,2 ,3 ]) ** - 1 , DomainError )
406
406
assert raises (lambda : Z ([1 ,2 ,3 ]) ** Z ([1 ,2 ]), TypeError )
407
407
assert raises (lambda : Z ([1 ,2 ]) // Z ([]), ZeroDivisionError )
408
408
assert raises (lambda : Z ([]) // Z ([]), ZeroDivisionError )
@@ -2109,7 +2109,7 @@ def test_fmpz_mod_poly():
2109
2109
assert (f + 1 ) // f == 1
2110
2110
2111
2111
# pow
2112
- assert raises (lambda : f ** (- 2 ), ValueError )
2112
+ assert raises (lambda : f ** (- 2 ), DomainError )
2113
2113
assert f * f == f ** 2
2114
2114
assert f * f == f ** fmpz (2 )
2115
2115
@@ -2768,7 +2768,7 @@ def setbad(obj, i, val):
2768
2768
assert P ([1 , 1 ]) ** 0 == P ([1 ])
2769
2769
assert P ([1 , 1 ]) ** 1 == P ([1 , 1 ])
2770
2770
assert P ([1 , 1 ]) ** 2 == P ([1 , 2 , 1 ])
2771
- assert raises (lambda : P ([1 , 1 ]) ** - 1 , ValueError )
2771
+ assert raises (lambda : P ([1 , 1 ]) ** - 1 , DomainError )
2772
2772
assert raises (lambda : P ([1 , 1 ]) ** None , TypeError )
2773
2773
2774
2774
# XXX: Not sure what this should do in general:
@@ -3254,7 +3254,7 @@ def quick_poly():
3254
3254
(0 , 1 ): 4 ,
3255
3255
(0 , 0 ): 1 ,
3256
3256
})
3257
- assert raises (lambda : P (ctx = ctx ) ** - 1 , ValueError )
3257
+ assert raises (lambda : P (ctx = ctx ) ** - 1 , ZeroDivisionError )
3258
3258
assert raises (lambda : P (ctx = ctx ) ** None , TypeError )
3259
3259
3260
3260
# # XXX: Not sure what this should do in general:
@@ -3464,6 +3464,32 @@ def _all_polys_mpolys():
3464
3464
yield P , S , [x , y ], is_field , characteristic
3465
3465
3466
3466
3467
+ def test_properties_poly_mpoly ():
3468
+ """Test is_zero, is_one etc for all polynomials."""
3469
+ for P , S , [x , y ], is_field , characteristic in _all_polys_mpolys ():
3470
+
3471
+ zero = 0 * x
3472
+ one = zero + 1
3473
+ two = one + 1
3474
+
3475
+ assert zero .is_zero () is True
3476
+ assert one .is_zero () is False
3477
+ assert two .is_zero () is False
3478
+ assert x .is_zero () is False
3479
+
3480
+ assert zero .is_one () is False
3481
+ assert one .is_one () is True
3482
+ assert two .is_one () is False
3483
+ assert x .is_one () is False
3484
+
3485
+ assert zero .is_constant () is True
3486
+ assert one .is_constant () is True
3487
+ assert two .is_constant () is True
3488
+ assert x .is_constant () is False
3489
+
3490
+ # is_gen?
3491
+
3492
+
3467
3493
def test_factor_poly_mpoly ():
3468
3494
"""Test that factor() is consistent across different poly/mpoly types."""
3469
3495
@@ -3671,6 +3697,52 @@ def factor_sqf(p):
3671
3697
assert (2 * (x + y )).gcd (4 * (x + y )** 2 ) == x + y
3672
3698
3673
3699
3700
+ def test_division_poly_mpoly ():
3701
+ """Test that division is consistent across different poly/mpoly types."""
3702
+
3703
+ Z = flint .fmpz
3704
+
3705
+ for P , S , [x , y ], is_field , characteristic in _all_polys_mpolys ():
3706
+
3707
+ if characteristic != 0 and not characteristic .is_prime ():
3708
+ # nmod_poly crashes for many operations with non-prime modulus
3709
+ # https://github.com/flintlib/python-flint/issues/124
3710
+ # so we can't even test it...
3711
+ nmod_poly_will_crash = type (x ) is flint .nmod_poly
3712
+ if nmod_poly_will_crash :
3713
+ continue
3714
+
3715
+ one = x ** 0 # 1 as a polynomial
3716
+ two = one + one
3717
+
3718
+ if is_field or characteristic == 0 :
3719
+ assert x / x == x ** 0 == 1 == one
3720
+ assert x / 1 == x / S (1 ) == x / one == x ** 1 == x
3721
+ assert 1 / one == one ** - 1 == one ** Z (- 1 ) == 1 , type (one )
3722
+ assert - 1 / one == 1 / - one == (- one )** - 1 == (- one )** Z (- 1 ) == - one == - 1
3723
+ assert (- one ) ** - 2 == (- one )** Z (- 2 ) == one
3724
+ assert raises (lambda : 1 / x , DomainError )
3725
+ assert raises (lambda : x ** - 1 , DomainError )
3726
+
3727
+ if is_field :
3728
+ half = S (1 )/ 2 * one # 1/2 as a polynomial
3729
+ assert half == S (1 )/ 2
3730
+ assert x / half == 2 * x
3731
+ assert 1 / half == S (1 ) / half == one / half == one / (S (1 )/ 2 ) == 2
3732
+ assert half ** - 1 == half ** Z (- 1 ) == 2
3733
+ assert two ** - 1 == two ** Z (- 1 ) == half
3734
+ elif characteristic == 0 :
3735
+ assert raises (lambda : x / 2 , DomainError )
3736
+ assert raises (lambda : x / two , DomainError ), characteristic
3737
+ assert raises (lambda : two ** - 1 , DomainError )
3738
+ assert raises (lambda : two ** Z (- 1 ), DomainError )
3739
+ else :
3740
+ # Non-prime modulus...
3741
+ # nmod can crash and fmpz_mod_poly won't crash but has awkward
3742
+ # behaviour under division.
3743
+ pass
3744
+
3745
+
3674
3746
def _all_matrices ():
3675
3747
"""Return a list of matrix types and scalar types."""
3676
3748
R163 = flint .fmpz_mod_ctx (163 )
@@ -4569,7 +4641,7 @@ def test_fq_default_poly():
4569
4641
# pow
4570
4642
# assert ui and fmpz exp agree for polynomials and generators
4571
4643
R_gen = R_test .gen ()
4572
- assert raises (lambda : f ** (- 2 ), ValueError )
4644
+ assert raises (lambda : f ** (- 2 ), DomainError )
4573
4645
assert pow (f , 2 ** 60 , g ) == pow (pow (f , 2 ** 30 , g ), 2 ** 30 , g )
4574
4646
assert pow (R_gen , 2 ** 60 , g ) == pow (pow (R_gen , 2 ** 30 , g ), 2 ** 30 , g )
4575
4647
assert raises (lambda : pow (f , - 2 , g ), ValueError )
@@ -4698,7 +4770,9 @@ def test_all_tests():
4698
4770
test_division_poly ,
4699
4771
test_division_matrix ,
4700
4772
4773
+ test_properties_poly_mpoly ,
4701
4774
test_factor_poly_mpoly ,
4775
+ test_division_poly_mpoly ,
4702
4776
4703
4777
test_polys ,
4704
4778
test_mpolys ,
0 commit comments