-
Notifications
You must be signed in to change notification settings - Fork 160
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
More optimizations for integer functions #2061
Conversation
1bd66d6
to
b5dd9cb
Compare
This looks essentially good. But it breaks:
|
@frankluebeck could you please elaborate what is broken? I get the same output on this branch, master, and various older GAP versions, namely:
UPDATE: ah OK, if I load
With the old code, we get this weird result (for unsupported inputs, of course, so there is nothing wrong with it per se)
Ideally, In the meantime, I could add a workaround for backwards compatibility, I'll just have to figure out what it ought to do. (Finally, there is an undocumented operation |
This patch for
There is a reason I don't load |
I made a pull request for FactInt to correct the issue there, see gap-packages/FactInt#6 Moreover, I added a new commit to this PR which tweaks the primality prover, which (mostly) avoids the slowdown with FactInt loaded for me, and by coincidence (?) also the problem with RootInt. |
I silently assumed that RootInt does something sensible if the first argument is a rational. If RootInt is changed such that this is no longer the case, the argument needs to be converted to an integer first, like in your fix. Thanks for pointing this out. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we have a few tests for bigger ints, where we multiply some more things together?
@Stefan-Kohl @ChrisJefferson I am not sure I understand -- for what do you want additional tests? BTW, this PR also has a test failure, which I don't understand right now: It seems some |
elif n < 0 and k mod 2 = 0 then Error("<n> must be positive"); | ||
elif n < 0 and k mod 2 = 1 then return -RootInt( -n, k ); | ||
elif n = 0 then return 0; | ||
elif n <= k then return 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the problem with the old code which caused it to accept some non-integer inputs: suppose n <= k
holds (and none of the prior checks either handled the input, or indirectly caused an assertion, e.g. k mod 2
will error out if k
is a rational with even denominator). Then it returns 1. This is only sensible if also 1 <= n
holds, which is of course automatic if n
is an integer (due to the preceding checks), but fails for rationals (such as e.g. n=1/32
).
Since this "support" for rational inputs clearly was accidental (it's not documented; it's wrong for certain inputs; most rational inputs are not supported, e.g. RootInt(4+1/2, 2);
always gave an error), I am strongly tempted to proceed with this PR as-is (targeting GAP 4.10, not 4.9), at least once a new FactInt
version with the fix has been released.
In terms of FactInt, 1 was just as sensible as 0 -- and even 2 or 3 would hardly do more harm than causing a little slow-down on average ... . On the other hand, something like e.g. 100 would cause pretty poor performance in many cases. |
For tests, was just thinking if testing some more numbers which don't fit in a small integer, which have more factors, also big numbers with small mods, small numbers with big mods. Nothing too serious. |
0ef031a
to
1978125
Compare
@ChrisJefferson I have now extended the tests, which revealed a bug in the new It remains to be decided whether I should add a workaround to |
1978125
to
370a093
Compare
@fingolfin new FactInt release within a week, maybe - would that be OK then? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
Before: gap> SumX([0 .. 100000], [2,3,5,7,251,256], RootInt);; time; 784 gap> SumX([0 .. 100000], [2,3,5,7,251,256], {n,k}->RootInt(2^100+n,k));; time; 1890 After: gap> SumX([0 .. 100000], [2,3,5,7,251,256], RootInt);; time; 293 gap> SumX([0 .. 100000], [2,3,5,7,251,256], {n,k}->RootInt(2^100+n,k));; time; 775
Namely, by passing it the 'cheap' option (which implies the FactIntPartial option), as the surrounding code would suggest. This makes calling e.g. IsPrimeInt(100000000000000000000000000000000000000000000000151); with FactInt loaded almost as fast as without it loaded.
370a093
to
facf179
Compare
Rebased this, now that PR #2086 has been merged. Only three commits remain in here. Still waiting for new |
@fingolfin #2087 is so far only in my work branch. Shall I put it into master? |
Added to release notes for GAP 4.10 at https://github.com/gap-system/gap/wiki/GAP-4.10-release-notes (one common entry for this PR and #2086). |
This PR speeds up
GcdInt
,PValuation
andRootInt
; see the respective commit message for some timings.UPDATE: also improved
SmallestRootInt
andIsPrimePowerInt