@@ -285,7 +285,7 @@ def test_fmpz_factor():
285285 (1296814508839693536173209832765271992846610925502473758289451540212712414540699659186801 , 1 )]
286286
287287def test_fmpz_functions ():
288- T , F , VE , OE = True , False , ValueError , OverflowError
288+ T , F , VE , OE , DE = True , False , ValueError , OverflowError , DomainError
289289 cases = [
290290 # (f, [f(-1), f(0), f(1), f(2), ... f(10)]),
291291 (lambda n : flint .fmpz (n ).is_prime (),
@@ -331,11 +331,11 @@ def test_fmpz_functions():
331331 (lambda n : flint .fmpz (n ).euler_phi (),
332332 [0 , 0 , 1 , 1 , 2 , 2 , 4 , 2 , 6 , 4 , 6 , 4 ]),
333333 (lambda n : flint .fmpz (n ).isqrt (),
334- [VE , 0 , 1 , 1 , 1 , 2 , 2 , 2 , 2 , 2 , 3 , 3 ]),
334+ [DE , 0 , 1 , 1 , 1 , 2 , 2 , 2 , 2 , 2 , 3 , 3 ]),
335335 (lambda n : flint .fmpz (n ).sqrtrem (),
336- [VE , (0 , 0 ), (1 , 0 ), (1 , 1 ), (1 , 2 ), (2 , 0 ), (2 , 1 ), (2 , 2 ), (2 , 3 ), (2 , 4 ), (3 , 0 ), (3 , 1 )]),
336+ [DE , (0 , 0 ), (1 , 0 ), (1 , 1 ), (1 , 2 ), (2 , 0 ), (2 , 1 ), (2 , 2 ), (2 , 3 ), (2 , 4 ), (3 , 0 ), (3 , 1 )]),
337337 (lambda n : flint .fmpz (n ).sqrtmod (11 ),
338- [VE , 0 , 1 , VE , 5 , 2 , 4 , VE , VE , VE , 3 , VE ]),
338+ [DE , 0 , 1 , DE , 5 , 2 , 4 , DE , DE , DE , 3 , DE ]),
339339 (lambda n : flint .fmpz (n ).root (3 ),
340340 [VE , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 2 , 2 , 2 ]),
341341 (lambda n : flint .fmpz (n ).jacobi (3 ),
@@ -2781,8 +2781,6 @@ def setbad(obj, i, val):
27812781 elif nmod_poly_will_crash :
27822782 pass
27832783 else :
2784- # fmpz_mod_poly.sqrt() also crashes here:
2785- # GR_MUST_SUCCEED failure: src/fmpz_mod_poly/sqrt_series.c
27862784 assert raises (lambda : P ([1 , 2 , 1 ]).sqrt (), DomainError )
27872785
27882786 if P == flint .fmpq_poly :
@@ -3352,21 +3350,6 @@ def factor_sqf(p):
33523350 check (p , coeff , factors )
33533351 return coeff , sort (factors )
33543352
3355- def sqrt (a ):
3356- if type (x ) is flint .fq_default_poly :
3357- try :
3358- return S (a ).sqrt ()
3359- except ValueError :
3360- return None
3361- elif characteristic != 0 :
3362- # XXX: fmpz(0).sqrtmod crashes
3363- try :
3364- return flint .fmpz (- 1 ).sqrtmod (characteristic )
3365- except ValueError :
3366- return None
3367- else :
3368- return None
3369-
33703353 for P , S , [x , y ], is_field , characteristic in _all_polys_mpolys ():
33713354
33723355 if characteristic != 0 and not characteristic .is_prime ():
@@ -3397,25 +3380,39 @@ def sqrt(a):
33973380 assert factor_sqf (2 * (x + 1 )) == (S (2 ), [(x + 1 , 1 )])
33983381
33993382 assert factor (x * (x + 1 )) == (S (1 ), [(x , 1 ), (x + 1 , 1 )])
3383+
3384+ # mpoly types have a slightly different squarefree factorisation
3385+ # because they handle trivial factors differently. It looks like a
3386+ # monomial gcd is extracted but not recombined so the square-free
3387+ # factors might not have unique multiplicities.
3388+ #
3389+ # Maybe it is worth making them consistent by absorbing the power
3390+ # of x into a factor of equal multiplicity.
34003391 if y is None :
3392+ # *_poly types
34013393 assert factor_sqf (x * (x + 1 )) == (S (1 ), [(x ** 2 + x , 1 )])
34023394 else :
3403- # mpoly types have a different squarefree factorisation because
3404- # they handle trivial factors differently...
3405- #
3406- # Maybe it is worth making them consistent by absorbing the power
3407- # of x into a factor of equal multiplicity.
3395+ # *_mpoly types
34083396 assert factor_sqf (x * (x + 1 )) == (S (1 ), [(x , 1 ), (x + 1 , 1 )])
34093397
3398+ # This is the same for all types because the extracted monomial has
3399+ # a unique multiplicity.
34103400 assert factor_sqf (x ** 2 * (x + 1 )) == (S (1 ), [(x + 1 , 1 ), (x , 2 )])
34113401
3402+ # This is the same for all types because there is no tivial monomial
3403+ # factor to extract.
34123404 assert factor ((x - 1 )* (x + 1 )) == (S (1 ), sort ([(x - 1 , 1 ), (x + 1 , 1 )]))
34133405 assert factor_sqf ((x - 1 )* (x + 1 )) == (S (1 ), [(x ** 2 - 1 , 1 )])
34143406
3407+ # Some finite fields have sqrt(-1) so we can factor x**2 + 1
3408+ try :
3409+ i = S (- 1 ).sqrt ()
3410+ except DomainError :
3411+ i = None
3412+
34153413 p = 3 * (x - 1 )** 2 * (x + 1 )** 2 * (x ** 2 + 1 )** 3
34163414 assert factor_sqf (p ) == (S (3 ), [(x ** 2 - 1 , 2 ), (x ** 2 + 1 , 3 )])
34173415
3418- i = sqrt (- 1 )
34193416 if i is not None :
34203417 assert factor (p ) == (S (3 ), sort ([(x + 1 , 2 ), (x - 1 , 2 ), (x + i , 3 ), (x - i , 3 )]))
34213418 else :
@@ -4163,7 +4160,7 @@ def test_fq_default():
41634160 nqr = gf .random_element ()
41644161 if not nqr .is_square ():
41654162 break
4166- assert raises (lambda : nqr .sqrt (), ValueError )
4163+ assert raises (lambda : nqr .sqrt (), DomainError )
41674164
41684165
41694166def test_fq_default_poly ():
0 commit comments