Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utilizing Hypothesis in the ntheory/polys directory #25428

Merged
merged 11 commits into from Aug 2, 2023
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -87,3 +87,6 @@ sample.tex

# Files for pyodide test
pyodide

# Ignore hypothesis files
.hypothesis
18 changes: 18 additions & 0 deletions sympy/ntheory/tests/test_tau_hypothesis.py
dianetc marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,18 @@
from hypothesis import given
from hypothesis import strategies as st
from sympy import divisors
from sympy.ntheory.primetest import is_square
from sympy.ntheory import totient
from sympy.ntheory import divisor_sigma


@given(n=st.integers(1, 10**10))
def test_tau_hypothesis(n):
div = divisors(n)
tau_n = len(div)
if tau_n % 2 == 1: # tau(n) odd iff n is perfect square
dianetc marked this conversation as resolved.
Show resolved Hide resolved
assert is_square(n)
sigmas = [divisor_sigma(i) for i in div]
totients = [totient(n // i) for i in div]
mul = [a * b for a, b in zip(sigmas, totients)]
assert n * tau_n == sum(mul)
15 changes: 15 additions & 0 deletions sympy/ntheory/tests/test_totient_hypothesis.py
@@ -0,0 +1,15 @@
from hypothesis import given
from hypothesis import strategies as st
from sympy.ntheory import totient
from sympy import divisors
import random


@given(n=st.integers(1,10**10))
def test_totient_hypothesis(n):
assert totient(n) <= n
div = divisors(n)
totients = [totient(i) for i in div]
assert n == sum(totients)
d = random.choice(div)
dianetc marked this conversation as resolved.
Show resolved Hide resolved
assert totient(n // (n // d)) == totient(d)
dianetc marked this conversation as resolved.
Show resolved Hide resolved
27 changes: 27 additions & 0 deletions sympy/polys/tests/test_gcd_hypothesis.py
@@ -0,0 +1,27 @@
from hypothesis import given
from hypothesis import strategies as st
from sympy.abc import x
from sympy.polys.polytools import Poly


@given(
coefficients1=st.lists(st.integers()),
coefficients2=st.lists(st.integers()),
coefficients3=st.lists(st.integers()),
)
def test_gcd_hypothesis(coefficients1, coefficients2, coefficients3):
# instantiate polynomials
f = Poly(coefficients1, x, domain="ZZ")
g = Poly(coefficients2, x, domain="ZZ")
r = Poly(coefficients3, x, domain="ZZ")

# get gdc
dianetc marked this conversation as resolved.
Show resolved Hide resolved
gcd_1 = f.gcd(g)
gcd_2 = g.gcd(f)

assert gcd_1 == gcd_2

# multiply by r
gcd_3 = g.gcd(f + r * g)

assert gcd_1 == gcd_3
23 changes: 23 additions & 0 deletions sympy/polys/tests/test_poly_hypothesis.py
@@ -0,0 +1,23 @@
from hypothesis import given
from hypothesis import assume
from hypothesis import strategies as st
from sympy.abc import x
from sympy.polys.polytools import Poly


@given(coefficients1=st.lists(st.integers()), coefficients2=st.lists(st.integers()))
def test_poly_hypothesis(coefficients1, coefficients2):
# non-zero division check.
assume(len(coefficients2) * [0] != coefficients2)
dianetc marked this conversation as resolved.
Show resolved Hide resolved

# Integer case
f_z = Poly(coefficients1, x, domain="ZZ")
g_z = Poly(coefficients2, x, domain="ZZ")
remainder_z = f_z.rem(g_z)
assert g_z.degree() > remainder_z.degree() or remainder_z.degree() == 0

# Rational case
f_q = Poly(coefficients1, x, domain="QQ")
g_q = Poly(coefficients2, x, domain="QQ")
dianetc marked this conversation as resolved.
Show resolved Hide resolved
remainder_q = f_q.rem(g_q)
assert g_q.degree() > remainder_q.degree() or remainder_q.degree() == 0