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 5 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
72 changes: 64 additions & 8 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 All @@ -23,14 +23,15 @@ class _GCDExample:
"""A benchmark example with two polynomials and their gcd."""

def __init__(self, n):
f, g, d, syms = self.make_poly(n)
f, g, d, syms, domain = self.make_poly(n)
self.f = f
self.g = g
self.d = d
self.x = syms[0]
self.y = syms[1:]
self.syms = syms
self.ring = QQ[syms]
self.domain = domain
self.ring = domain[syms]

def to_expr(self, expr):
return expr
Expand Down Expand Up @@ -79,7 +80,7 @@ def make_poly(self, n):
d = (1 + x + sum(y[:n])) ** 2
f = d * (-2 + x - sum(y[:n])) ** 2
g = d * (2 + x + sum(y[:n])) ** 2
return f, g, d, syms
return f, g, d, syms, QQ


class _SparseGCDHighDegree(_GCDExample):
Expand Down Expand Up @@ -110,7 +111,7 @@ def make_poly(self, n):
d = 1 + x ** (n + 1) + sum([y[i] ** (n + 1) for i in range(n)])
f = d * (-2 + x ** (n + 1) + sum([y[i] ** (n + 1) for i in range(n)]))
g = d * (2 + x ** (n + 1) + sum([y[i] ** (n + 1) for i in range(n)]))
return f, g, d, syms
return f, g, d, syms, QQ


class _QuadraticNonMonicGCD(_GCDExample):
Expand Down Expand Up @@ -141,7 +142,7 @@ def make_poly(self, n):
d = 1 + x ** 2 * y[0] ** 2 + sum([y[i] ** 2 for i in range(1, n)])
f = d * (-1 + x ** 2 - y[0] ** 2 + sum([y[i] ** 2 for i in range(1, n)]))
g = d * (2 + x * y[0] + sum(y[1:n])) ** 2
return f, g, d, syms
return f, g, d, syms, QQ


class _SparseNonMonicQuadratic(_GCDExample):
Expand Down Expand Up @@ -172,7 +173,38 @@ def make_poly(self, n):
d = -1 + x * prod(y[:n])
f = d * (3 + x * prod(y[:n]))
g = d * (-3 + x * prod(y[:n]))
return f, g, d, syms
return f, g, d, syms, QQ


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 + I*y1)*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, ZZ_I


class _TimeOP:
Expand Down Expand Up @@ -341,4 +373,28 @@ 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):
return lambda: f.gcd(g)

def get_func_sparse(self, f, g, d, ring):
return lambda: f.gcd(g)



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