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

Small optimizations to arithmetic in number fields of degree > 2 #28297

Closed
mezzarobba opened this issue Jul 31, 2019 · 19 comments
Closed

Small optimizations to arithmetic in number fields of degree > 2 #28297

mezzarobba opened this issue Jul 31, 2019 · 19 comments

Comments

@mezzarobba
Copy link
Member

Micro-benchmark (maybe of limited relevance):

sage: NF.<a> = NumberField(x^7 + 2, embedding=QQbar(-2)^(1/7))
sage: b = ((-7*a^6 + 3*a^4 - 5*a^3 + 2*a^2 - 4)/42)^10
sage: c = ((-7*a^6 + 7*a^4 - 5*a^3 + 2*a^2 - 4)/56)^10

Before:

sage: %timeit b+c
The slowest run took 5.91 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 4.76 µs per loop

After:

sage: %timeit b+c
The slowest run took 8.70 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 3.34 µs per loop

There seem to be a lot more opportunities to further improve basic arithmetic operations by removing gcds at the right moment. Please feel free to hijack the ticket if you find the time and energy to have a look!

Component: performance

Author: Marc Mezzarobba, Michael Orlitzky

Branch/Commit: c71c660

Reviewer: Michael Orlitzky, Marc Mezzarobba

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

@mezzarobba mezzarobba added this to the sage-8.9 milestone Jul 31, 2019
@mezzarobba
Copy link
Member Author

Commit: 2c361d6

@mezzarobba
Copy link
Member Author

New commits:

2c361d6trivial optimization in addition of num field elts

@mezzarobba

This comment has been minimized.

@mezzarobba
Copy link
Member Author

Branch: public/ticket/28297-nfe

@mezzarobba mezzarobba changed the title A small optimization to arithmetic in number fields A small optimization to arithmetic in number fields of degree > 2 Jul 31, 2019
@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 31, 2019

Changed commit from 2c361d6 to c14daea

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 31, 2019

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

c14daeatrivial opts in basic arithmetic with number field elements

@mezzarobba

This comment has been minimized.

@mezzarobba mezzarobba changed the title A small optimization to arithmetic in number fields of degree > 2 Small optimizations to arithmetic in number fields of degree > 2 Jul 31, 2019
@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 18, 2019

Changed commit from c14daea to 8f93f0a

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 18, 2019

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

8f93f0atrivial opts in basic arithmetic with number field elements

@mezzarobba
Copy link
Member Author

comment:5

Rebased.

@orlitzky
Copy link
Contributor

Reviewer: Michael Orlitzky

@orlitzky
Copy link
Contributor

comment:6

Seems straightforward:

The addition to _reduce_c_() bails out early if the denominator is one because in that case there's nothing to do. The check is cheap and avoids several other function calls that do nothing when the denominator is one, so I believe it's an improvement on average.

The changes to _add_() and _sub_() are identical. We now pull out the GCD of the two denominators (which are smallish numbers) early, and it eventually cancels out. This would happen anyway when we call _reduce_c_() before returning, but at that point we would be cancelling the same GCD out of the relatively-largish product of the denominators. I have faith that doing it early at least makes things no worse, and your numbers show an improvement, so it's fine by me. (This also makes it more likely that we hit the new "fast return" in _reduce_c_().)

Could you please describe the changes in a bit more detail in the commit message? That's my only nitpick.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 21, 2019

Branch pushed to git repo; I updated commit sha1 and set ticket back to needs_review. This was a forced push. New commits:

c71c660trivial opts in basic arithmetic with number field elements

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 21, 2019

Changed commit from 8f93f0a to c71c660

@mezzarobba
Copy link
Member Author

Changed author from Marc Mezzarobba to Marc Mezzarobba, Michael Orlitzky

@mezzarobba
Copy link
Member Author

comment:8

Replying to @orlitzky:

Could you please describe the changes in a bit more detail in the commit message? That's my only nitpick.

I've copied your detailed description (minus a few sentences) there. I hope that's okay.

Thank you for the review!

@mezzarobba
Copy link
Member Author

Changed reviewer from Michael Orlitzky to Michael Orlitzky, Marc Mezzarobba

@orlitzky
Copy link
Contributor

comment:9

Yes that's fine with me. You don't have to give me author credit for just describing what you did, but you're the boss =)

@vbraun
Copy link
Member

vbraun commented Aug 29, 2019

Changed branch from public/ticket/28297-nfe to c71c660

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