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

Implement Field._gcd_univariate_polynomial() #18461

Closed
pjbruin opened this issue May 20, 2015 · 8 comments
Closed

Implement Field._gcd_univariate_polynomial() #18461

pjbruin opened this issue May 20, 2015 · 8 comments

Comments

@pjbruin
Copy link
Contributor

pjbruin commented May 20, 2015

The goal of this ticket is to implement a Cython method Field._gcd_univariate_polynomial() using the standard Euclidean algorithm. This is much faster than Fields.ParentMethods._gcd_univariate_polynomial(), which calls EuclideanDomains.ElementMethods.gcd(), since both are Python methods. (The _gcd_univariate_polynomial mechanism was introduced in #13442.)

The following bug can then be fixed by just removing PolynomialRealDense.gcd(), which does not take into account the case where one of the arguments of gcd is zero:

sage: R.<x> = RR[]
sage: x.gcd(R.zero())
Traceback (most recent call last):
...
TypeError: 'MinusInfinity' object cannot be interpreted as an index

Removing this method without implementing Field._gcd_univariate_polynomial() (falling back on Python code) is about twice as slow; with this ticket there is no slowdown.

Similarly, to make CC and QQbar use the new method instead of Python code, we also remove the now obsolete Polynomial_generic_field.gcd().

CC: @saraedum

Component: basic arithmetic

Keywords: polynomial gcd

Author: Peter Bruin

Branch/Commit: 908ace7

Reviewer: Bruno Grenet

Issue created by migration from https://trac.sagemath.org/ticket/18461

@pjbruin pjbruin added this to the sage-6.8 milestone May 20, 2015
@pjbruin

This comment has been minimized.

@pjbruin
Copy link
Contributor Author

pjbruin commented May 20, 2015

@pjbruin
Copy link
Contributor Author

pjbruin commented May 20, 2015

Commit: 908ace7

@bgrenet
Copy link

bgrenet commented May 21, 2015

comment:3

This looks good to me. Yet, I have not been able to reproduce the slowdown you mention without the new method: May you give some details?

@pjbruin
Copy link
Contributor Author

pjbruin commented May 21, 2015

comment:4

I just tested again with the following setup:

R.<x> = RR[]
z = R.zero()
f = x^2 + 3
g = x^3 + 5

With this branch (i.e. with Field._gcd_univariate_polynomial):

sage: %timeit z.gcd(z)
The slowest run took 37.08 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 1.77 µs per loop
sage: %timeit x.gcd(z)
The slowest run took 22.57 times longer than the fastest. This could mean that an intermediate result is being cached 
100000 loops, best of 3: 4.34 µs per loop
sage: %timeit z.gcd(x)
The slowest run took 66.36 times longer than the fastest. This could mean that an intermediate result is being cached 
100000 loops, best of 3: 10.5 µs per loop
sage: %timeit x.gcd(x)
The slowest run took 13.64 times longer than the fastest. This could mean that an intermediate result is being cached 
100000 loops, best of 3: 11.1 µs per loop
sage: %timeit f.gcd(g)
The slowest run took 24.83 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 33.6 µs per loop

With this branch after removing Field._gcd_univariate_polynomial:

sage: %timeit z.gcd(z)
The slowest run took 16.42 times longer than the fastest. This could mean that an intermediate result is being cached 
100000 loops, best of 3: 6.81 µs per loop
sage: %timeit x.gcd(z)
The slowest run took 92.70 times longer than the fastest. This could mean that an intermediate result is being cached 
100000 loops, best of 3: 11.4 µs per loop
sage: %timeit z.gcd(x)
The slowest run took 15.38 times longer than the fastest. This could mean that an intermediate result is being cached 
100000 loops, best of 3: 19.2 µs per loop
sage: %timeit x.gcd(x)
The slowest run took 4.43 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 19.9 µs per loop
sage: %timeit f.gcd(g)
The slowest run took 4.04 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 42.7 µs per loop

I don't have an explanation for the discrepancy between the slowest and fastest runs; as far as I know there is no caching.

For complicated polynomials, the difference is undoubtedly smaller because most time is spent in quo_rem, which is not touched by this branch.

@bgrenet
Copy link

bgrenet commented May 21, 2015

Reviewer: Bruno Grenet

@bgrenet
Copy link

bgrenet commented May 21, 2015

comment:5

Ok, I reproduced the same slowdown if Field._gcd_univariate_polynomial is removed. Passes all tests, fine for me!

@vbraun
Copy link
Member

vbraun commented May 21, 2015

Changed branch from u/pbruin/18461-Field_gcd_univariate_polynomial to 908ace7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants