Skip to content

Commit 9b3d7c4

Browse files
committed
test: add polys tests for div/sqrt
1 parent 5b16c20 commit 9b3d7c4

3 files changed

Lines changed: 18 additions & 17 deletions

File tree

src/flint/test/test_all.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,7 @@ def test_fmpq_poly():
933933
assert raises(lambda: Q([1,[]]), TypeError)
934934
assert raises(lambda: Q({}), TypeError)
935935
assert raises(lambda: Q([1], []), TypeError)
936+
assert raises(lambda: Q(1, 1, 1), TypeError)
936937
assert raises(lambda: Q([1], 0), ZeroDivisionError)
937938
assert bool(Q()) == False
938939
assert bool(Q([1])) == True
@@ -2735,6 +2736,8 @@ def setbad(obj, i, val):
27352736

27362737
assert raises(lambda: 1 / P([1, 1]), DomainError)
27372738
assert raises(lambda: P([1, 2, 1]) / P([1, 2]), DomainError)
2739+
assert raises(lambda: [] / P([1, 1]), TypeError)
2740+
assert raises(lambda: P([1, 1]) / [], TypeError)
27382741

27392742
if is_field:
27402743
assert P([1, 1]) // 2 == P([S(1)/2, S(1)/2])
@@ -2827,7 +2830,7 @@ def setbad(obj, i, val):
28272830
assert raises(lambda: P([1, 2, 2]).sqrt(), DomainError)
28282831

28292832
if P == flint.fmpq_poly:
2830-
assert raises(lambda: P([1, 2, 1], 3).sqrt(), ValueError)
2833+
assert raises(lambda: P([1, 2, 1], 3).sqrt(), DomainError)
28312834
assert P([1, 2, 1], 4).sqrt() == P([1, 1], 2)
28322835

28332836
assert P([]).deflation() == (P([]), 1)
@@ -3471,6 +3474,18 @@ def factor_sqf(p):
34713474
assert S(1).sqrt() == S(1)
34723475
assert S(4).sqrt()**2 == S(4)
34733476

3477+
if is_field:
3478+
for n in range(1, 10):
3479+
try:
3480+
sqrtn = S(n).sqrt()
3481+
except DomainError:
3482+
sqrtn = None
3483+
if sqrtn is None:
3484+
assert raises(lambda: ((x + 1)**2/n).sqrt(), DomainError)
3485+
else:
3486+
assert ((x + 1)**2/n).sqrt() ** 2 == (x + 1)**2/n
3487+
assert raises(lambda: ((x**2 + 1)/n).sqrt(), DomainError)
3488+
34743489
for i in range(-100, 100):
34753490
try:
34763491
assert S(i).sqrt() ** 2 == S(i)

src/flint/types/fmpq_poly.pyx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,6 @@ cdef class fmpq_poly(flint_poly):
120120
cdef bint r
121121
if op != 2 and op != 3:
122122
raise TypeError("polynomials cannot be ordered")
123-
self = any_as_fmpq_poly(self)
124-
if self is NotImplemented:
125-
return self
126123
other = any_as_fmpq_poly(other)
127124
if other is NotImplemented:
128125
return other
@@ -476,15 +473,8 @@ cdef class fmpq_poly(flint_poly):
476473
1/2*x + 1/2
477474
478475
"""
479-
d = self.denom()
480-
n = self.numer()
481-
d, r = d.sqrtrem()
482-
if r != 0:
483-
raise ValueError(f"Cannot compute square root of {self}")
484-
n = n.sqrt()
485-
if n is None:
486-
raise ValueError(f"Cannot compute square root of {self}")
487-
return fmpq_poly(n, d)
476+
d = self.denom().sqrt()
477+
return fmpq_poly(self.numer().sqrt(), d)
488478

489479
def deflation(self):
490480
num, n = self.numer().deflation()

src/flint/types/fmpz_mod_poly.pyx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,6 @@ cdef class fmpz_mod_poly(flint_poly):
433433
def _div_(self, other):
434434
cdef fmpz_mod_poly res
435435

436-
other = self.ctx.mod.any_as_fmpz_mod(other)
437-
if other is NotImplemented:
438-
return NotImplemented
439-
440436
if other == 0:
441437
raise ZeroDivisionError("Cannot divide by zero")
442438
elif not other.is_unit():

0 commit comments

Comments
 (0)