@@ -402,7 +402,7 @@ def test_fmpz_poly():
402402 assert raises (lambda : [] // Z ([1 ,2 ]), TypeError )
403403 assert raises (lambda : [] % Z ([1 ,2 ]), TypeError )
404404 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 )
406406 assert raises (lambda : Z ([1 ,2 ,3 ]) ** Z ([1 ,2 ]), TypeError )
407407 assert raises (lambda : Z ([1 ,2 ]) // Z ([]), ZeroDivisionError )
408408 assert raises (lambda : Z ([]) // Z ([]), ZeroDivisionError )
@@ -2109,7 +2109,7 @@ def test_fmpz_mod_poly():
21092109 assert (f + 1 ) // f == 1
21102110
21112111 # pow
2112- assert raises (lambda : f ** (- 2 ), ValueError )
2112+ assert raises (lambda : f ** (- 2 ), DomainError )
21132113 assert f * f == f ** 2
21142114 assert f * f == f ** fmpz (2 )
21152115
@@ -2768,7 +2768,7 @@ def setbad(obj, i, val):
27682768 assert P ([1 , 1 ]) ** 0 == P ([1 ])
27692769 assert P ([1 , 1 ]) ** 1 == P ([1 , 1 ])
27702770 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 )
27722772 assert raises (lambda : P ([1 , 1 ]) ** None , TypeError )
27732773
27742774 # XXX: Not sure what this should do in general:
@@ -3254,7 +3254,7 @@ def quick_poly():
32543254 (0 , 1 ): 4 ,
32553255 (0 , 0 ): 1 ,
32563256 })
3257- assert raises (lambda : P (ctx = ctx ) ** - 1 , ValueError )
3257+ assert raises (lambda : P (ctx = ctx ) ** - 1 , ZeroDivisionError )
32583258 assert raises (lambda : P (ctx = ctx ) ** None , TypeError )
32593259
32603260 # # XXX: Not sure what this should do in general:
@@ -3464,6 +3464,32 @@ def _all_polys_mpolys():
34643464 yield P , S , [x , y ], is_field , characteristic
34653465
34663466
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+
34673493def test_factor_poly_mpoly ():
34683494 """Test that factor() is consistent across different poly/mpoly types."""
34693495
@@ -3671,6 +3697,52 @@ def factor_sqf(p):
36713697 assert (2 * (x + y )).gcd (4 * (x + y )** 2 ) == x + y
36723698
36733699
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+
36743746def _all_matrices ():
36753747 """Return a list of matrix types and scalar types."""
36763748 R163 = flint .fmpz_mod_ctx (163 )
@@ -4569,7 +4641,7 @@ def test_fq_default_poly():
45694641 # pow
45704642 # assert ui and fmpz exp agree for polynomials and generators
45714643 R_gen = R_test .gen ()
4572- assert raises (lambda : f ** (- 2 ), ValueError )
4644+ assert raises (lambda : f ** (- 2 ), DomainError )
45734645 assert pow (f , 2 ** 60 , g ) == pow (pow (f , 2 ** 30 , g ), 2 ** 30 , g )
45744646 assert pow (R_gen , 2 ** 60 , g ) == pow (pow (R_gen , 2 ** 30 , g ), 2 ** 30 , g )
45754647 assert raises (lambda : pow (f , - 2 , g ), ValueError )
@@ -4698,7 +4770,9 @@ def test_all_tests():
46984770 test_division_poly ,
46994771 test_division_matrix ,
47004772
4773+ test_properties_poly_mpoly ,
47014774 test_factor_poly_mpoly ,
4775+ test_division_poly_mpoly ,
47024776
47034777 test_polys ,
47044778 test_mpolys ,
0 commit comments