Skip to content

Commit be4e863

Browse files
committed
test: add polys tests for div/sqrt
1 parent 040761c commit be4e863

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

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)
@@ -3415,6 +3418,18 @@ def factor_sqf(p):
34153418
assert S(1).sqrt() == S(1)
34163419
assert S(4).sqrt()**2 == S(4)
34173420

3421+
if is_field:
3422+
for n in range(1, 10):
3423+
try:
3424+
sqrtn = S(n).sqrt()
3425+
except DomainError:
3426+
sqrtn = None
3427+
if sqrtn is None:
3428+
assert raises(lambda: ((x + 1)**2/n).sqrt(), DomainError)
3429+
else:
3430+
assert ((x + 1)**2/n).sqrt() ** 2 == (x + 1)**2/n
3431+
assert raises(lambda: ((x**2 + 1)/n).sqrt(), DomainError)
3432+
34183433
for i in range(-100, 100):
34193434
try:
34203435
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
@@ -480,15 +477,8 @@ cdef class fmpq_poly(flint_poly):
480477
1/2*x + 1/2
481478
482479
"""
483-
d = self.denom()
484-
n = self.numer()
485-
d, r = d.sqrtrem()
486-
if r != 0:
487-
raise ValueError(f"Cannot compute square root of {self}")
488-
n = n.sqrt()
489-
if n is None:
490-
raise ValueError(f"Cannot compute square root of {self}")
491-
return fmpq_poly(n, d)
480+
d = self.denom().sqrt()
481+
return fmpq_poly(self.numer().sqrt(), d)
492482

493483
def deflation(self):
494484
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(f"Cannot divide by zero")
442438
elif not other.is_unit():

0 commit comments

Comments
 (0)