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

Add benchmarks for GCD when input is Gaussian Integer #97

Merged
merged 7 commits into from
Sep 14, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 78 additions & 6 deletions benchmarks/polys.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sympy import symbols, prod, prem, rem, degree, LC, subresultants, gcd
from sympy import symbols, prod, prem, rem, degree, LC, subresultants, gcd, I, ZZ_I
from sympy.polys import QQ, Poly


Expand Down Expand Up @@ -30,7 +30,7 @@ def __init__(self, n):
self.x = syms[0]
self.y = syms[1:]
self.syms = syms
self.ring = QQ[syms]
self.ring = Poly(f).domain[syms]

def to_expr(self, expr):
return expr
Expand Down Expand Up @@ -175,6 +175,37 @@ def make_poly(self, n):
return f, g, d, syms


class _GaussianInteger(_GCDExample):
"""An example of Polynomial using Gaussian Integer"""

def make_poly(self, n):
x, y1, y2, y3, y4, y5, y6 = syms = symbols("x y1 y2 y3 y4 y5 y6")

d = -x**n

f = ((-I*(x)**4 - (x)**3 + I*(x)**2 + (x) + -I*(y2 + 1) - y1*(y3 + 1) -
2*y1*y2 - 5*y1*(y2 + 1) - y1*(y3 + 1) -
I*y4 + I*y6 + -y1*(y2 + 1) - 3*y1*y3 - y1*(y3 + 1) - I*y5 -
2*y1*y2 - 7*y1*(y2 + 1) - 3*y1*y3 - 5*y1*(y3 + 1) - I*(y4 + y5) -
y1*y2 - 2*y1*(y2 + 1) - y1*y3 - 3*y1*(y3 + 1) - I*(-y4 + y5) +
y1*(y2 + 1) + -y1*y2 - 5*y1*y3 - y1*(y3 + 1) - I*y5 + -I*y6 -
3*y1*(y2 + 1) - y1*y3 - 7*y1*(y3 + 1) + I*y5 + -y1*y2 + y1*(y3 + 1)
+ 5*y1*(y3 + 1) + -y1*y2 - y1*(y2 + 1) - y1*y3 + y1*y2 + y1*y3 +
7*y1*(y3 + 1) + y1*(y2 + 1) + y1*y3)*d)

g = ((-I*(x)**4 - (x)**3 + I*(x)**2 + (x) + -I*(y2 + 1) - y1*(y3 + 1) -
7*y1*y2 - y1*(y2 + 1) - y1*(y3 + 1) -
I*y4 + I*y6 - y1*(y2 + 1) - 3*y1*y3 - y1*(y3 + 1) - I*y5 -
y1*y2 - 7*y1*(y2 + 1) - 5*y1*y3 - 2*y1*(y3 + 1) - I*(y4 + y5) -
3*y1*y2 - 5*y1*(y2 + 1) - 9*y1*y3 - 7*y1*(y3 + 1) -
I*(-y4 + y5) + y1*(y2 + 1) + -y1*y2 - y1*y3 - y1*(y3 + 1) -
I*y5 + -I*y6 + -y1*(y2 + 1) - y1*y3 - y1*(y3 + 1) + I*y5 - 3*y1*y2
+ y1*(y3 + 1) + y1*(y3 + 1) + -y1*y2 - y1*(y2 + 1) - y1*y3 +
y1*y2 + y1*y3 + y1*(y3 + 1) + y1*(y2 + 1) + y1*y3)*d)

return f, g, d, syms


class _TimeOP:
"""
Benchmarks comparing Poly implementations of a given operation.
Expand All @@ -195,7 +226,10 @@ def setup(self, n, impl):
expected = examples.to_expr(expected)

elif impl == 'dense':
func = self.get_func_poly(*examples.as_poly())
if examples.ring.domain == ZZ_I:
func = self.get_func_poly(*examples.as_expr())
else:
func = self.get_func_poly(*examples.as_poly())
1e9abhi1e10 marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(expected, list):
# some methods output a list of polynomials
expected = [examples.to_poly(p) for p in expected]
Expand All @@ -204,7 +238,11 @@ def setup(self, n, impl):
expected = examples.to_poly(expected)

elif impl == 'sparse':
func = self.get_func_sparse(*examples.as_ring())
if examples.ring.domain == ZZ_I:
func = self.get_func_sparse(*examples.as_expr())
else:
func = self.get_func_sparse(*examples.as_ring())

if isinstance(expected, list):
expected = [examples.to_ring(p) for p in expected]
else:
Expand All @@ -217,7 +255,15 @@ def time_op(self, n, impl):
self.returned_result = self.func()

def teardown(self, n, impl):
assert self.expected_result == self.returned_result
examples = self.GCDExampleCLS(n)
domain = examples.ring.domain
if domain != ZZ_I:
assert self.expected_result == self.returned_result
else:
if impl == 'expr' or impl == 'sparse':
assert self.expected_result == self.returned_result
else:
assert self.expected_result.as_expr() == self.returned_result.as_expr()


class _TimePREM(_TimeOP):
Expand Down Expand Up @@ -341,4 +387,30 @@ class TimeGCD_QuadraticNonMonicGCD(_TimeGCD):

class TimeGCD_SparseNonMonicQuadratic(_TimeGCD):
GCDExampleCLS = _SparseNonMonicQuadratic
params = [(1, 3, 5), ('expr', 'dense', 'sparse')]
params = [(1, 3, 5), ('expr', 'dense', 'sparse')]


class _TimeGaussianInt(_TimeOP):
"""Benchmarks for GCDs method when input is Gaussian Integer"""
1e9abhi1e10 marked this conversation as resolved.
Show resolved Hide resolved

def expected(self, f, g, d, syms):
expected_gcd = gcd(f, g)

return expected_gcd

def get_func_expr(self, f, g, d, syms):
return lambda: gcd(f, g)

def get_func_poly(self, f, g, d, syms):
fp = Poly(f)
gp = Poly(g)
return lambda: fp.gcd(gp)

def get_func_sparse(self, f, g, d, ring):
fpe = ZZ_I[ring](f)
gpe = ZZ_I[ring](g)
return lambda: fpe.gcd(gpe)
1e9abhi1e10 marked this conversation as resolved.
Show resolved Hide resolved

class TimeGCD_GaussInt(_TimeGaussianInt):
GCDExampleCLS = _GaussianInteger
params = [(1, 2, 3), ('expr', 'dense', 'sparse')]