sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.45k stars 482 forks source link

add a fast gcd algorithm for univariate polynomials over absolute number fields #8558

Open lftabera opened 14 years ago

lftabera commented 14 years ago

Question arised here,

http://groups.google.com/group/sage-devel/browse_thread/thread/0f5b029970e1a4e2/fcec7d0e35474fbd#fcec7d0e35474fbd

univariate gcd is performed using euclidean algorithm, which causes explosion of coefficients and is slow but for trivial examples.

For relative number fields, pass to an absolute representation. This may be slow. But for the cases where this is slow the current implementation may be unfeasible.

Depends on #14186 Depends on #15803 Depends on #15804

CC: @slel

Component: algebra

Keywords: gcd, pari, ntl, days94

Author: Luis Felipe Tabera Alonso

Branch/Commit: u/lftabera/ticket/8558 @ f16a49b

Reviewer: Jeroen Demeyer

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

pjbruin commented 10 years ago
comment:42

(I'm not sure if the merge conflict was caused by this ticket or one of its dependencies; I just reported what the patchbot told me.)

I don't really have time to review any of this at the moment, but I agree with earlier reviewers that this looks like a very nice addition. I was just wondering whether it would make sense to do this via a NumberField._gcd_univariate_polynomial() method as introduced by #13442. On the one hand, this would mean you didn't have to introduce new classes (potentially many of them: we have absolute and relative number fields, quadratic fields, cyclotomic fields, sparse and dense polynomials, who knows what other distinctions we'll have in the future). On the other hand, the code of this ticket has already been written and #13442 is still at needs_work, so it probably isn't worth the effort for now.

pjbruin commented 10 years ago

Changed work issues from does not merge with 6.2 to none

lftabera commented 10 years ago
comment:43

Thanks anyway, this made me update my local branches :)

The code is ok, but the branch of patches is a little mess due to my poor "git-fu". As of now I think that it is more important to get the dependencies merged, they are much simpler and would allow to prepare cleaner patches for this ticket.

Concerning #13442, I had in mind an implementation of these rings as in ticket #10591, so it would be better to have different classes for polynomials over number fields. The long term goal would be to have nice multiple extensions without the need of computing a primitive element for basic arithmetic, IMO it would be faster than using singular (cf. #9541).

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

Changed commit from f57b651 to add0e63

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

775f2c7Merge tag '6.3' into u/lftabera/ticket/8558
a2622a9Merge tag '6.4' into u/lftabera/ticket/8558
9da4eabMerge branch 'master' into u/lftabera/lift_centered
406dca9Fix import error
e81e7a0Merge branch 'master' into u/lftabera/lift_centered
07f725eMerge branch 'u/lftabera/lift_centered' into u/lftabera/ticket/8558
add0e63Fix an exception that has changed.
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Changed commit from add0e63 to c89c734

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

2f30718Merge tag '6.5' into u/lftabera/lift_centered
fff5565Merge tag '6.6' into u/lftabera/lift_centered
7a5e891Merge tag '6.7' into u/lftabera/lift_centered
3dd232bTicket 15804
c89c734Merge branch 'u/lftabera/lift_centered' into u/lftabera/ticket/8558
lftabera commented 8 years ago
comment:48

After so much time, I want to check if the code needs refactoring.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 8 years ago

Changed commit from c89c734 to 43d1c12

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 8 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

f6a168eMerge branch 'develop' into u/lftabera/ticket/8558
6c2dca3Merge branch 'develop' into u/lftabera/ticket/8558
43d1c12- Make the code work with 7.1.beta4
lftabera commented 8 years ago
comment:50

It works again with latest beta

videlec commented 8 years ago
comment:51
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 8 years ago

Changed commit from 43d1c12 to eef9fe1

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 8 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

a897e89Merge tag '7.3.beta7' into u/lftabera/ticket/8558
8eaf6b8Add more cdef variables
eef9fe1Fixed examples docstring.
lftabera commented 8 years ago
comment:53

Pari seems to use either Euclid or some subresultant variation. I would expect pari to perform better when the gcd is big with respect to the degree of the inputs. Or, when the input has small degree.

The polynomials are generated with K.random_element() so small coefficients. This benefits pari.

Small extension, not impressing except for huge polynomials

sage: K=QQ[I]['x']
deg f, deg g, deg gcd, timeit pari, timeit modular
2, 2, 0, 111 µs, 176 µs
2, 2, 1, 173 µs, 360 µs
2, 2, 2, 185 µs, 466 µs

100, 100, 0, 30.6 ms, 7.06 ms
100, 100, 50, 22.5 ms, 34.7 ms
100, 100, 100, 4.82 ms, 6.58 ms

300, 300, 0, 1.8 s, 45.1 ms
300, 300, 150, 534 ms, 201 ms
300, 300, 300, 13.3 ms, 11.3 ms

1000, 1000, 100, 2min 52s, 2.07 s

A degree 3, easy extension

sage: R=NumberField(x^3-2,'a')['x']
deg f, deg g, deg gcd, timeit pari, timeit modular
2, 2, 0, 163 µs, 243 µs
2, 2, 1, 239 µs, 597 µs
2, 2, 2, 257 µs, 587 µs

100, 100, 0, 19 s, 11.2 ms (1000x faster)
100, 100, 50, 8.6 s, 47.3 ms (100x faster)
100, 100, 100, 6.57 ms, 11.9 ms (2x slower)

300, 300, 150, > 600 s, 403 ms

Tu support my claim that big coefficients benefits modular

sage: K=QQ[I]['x']
sage: f=K.random_element(2,10**10)
100, 100, 0, 282 ms, 6.92 ms
100, 100, 50, 117 ms, 15.4 ms
100, 100, 100, 4.69 ms, 5.01 ms
lftabera commented 8 years ago
comment:54

lift methods still need better documentation according to #18

JohnCremona commented 8 years ago
comment:55

If I were a pari developer reasing this, I would ask: why not implement the algorithm in pari itself, rather than in Sage?

lftabera commented 8 years ago
comment:56

I am not a pari developer, it would certainly benefit more people than an implementation in Sage. I thought about an implementation in ntl, but I do not touch C++ since 15 years ago. Flint would also be a good place to put a similar code.

According to the documentation:

gcd uses:

So, it may be the case that we are not translating polynomials to the correct pari setting and so I get bad timings? It may be the case, in order not to compute the discriminant of the number field we are dealing with...

JohnCremona commented 8 years ago
comment:57

One thing to watch when comparing timings with pari is that when Sage calls pari to compute class number s and units in a number field, unless Proof=False it assumes no hypothesis (like GRH) so is often a lot slower than the same thing done in pari, where the default is to assume everything (and you can ask for certification later). This should be as issue if you only do basic number field arithmetic, but if you do anything which requires knowing the units or class group it can be quite significant. You might want to try setting Proof=False in Sage and seeing if your timings change.

lftabera commented 8 years ago
comment:58

It it is not that, I only need basic arithmetic, at most, pari could be interested in the discriminant, but I am working in QQ[I] in my examples. Which should be easy.

I have tried to write polynomials in pure gp as

f= Mod(3*y,y<sup>2-1)*x</sup>2+Mod(1,y<sup>2-1)*x+Mod(y,y</sup>2-1)

or as

w=quadgen(-4)
f= 3*w*x^2+1*x+w

in both cases I get bad timing. Reading the documentation now to see how to deal with polynomials with number field coefficients in pari...

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 8 years ago

Changed commit from eef9fe1 to 5cfbce8

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 8 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

b89dac9Merge tag '7.3.beta8' into u/lftabera/ticket/8558
9685a00Minor documentation fix
5cfbce8Be more conservative with the primes.
lftabera commented 8 years ago
comment:60

It looks that Pari is not really using a modular algorithm...

In any case, I have fixed the documentation, I have also fixed some heuristic assumptions about the primes. Do not use proof=False and be more conservative about the size of the primes. In principle, with proof=False we could try a composite number such that the gcd in the residue ring success but some prime factors are good and some are bad. Not sure if this is possible. Also, chinese remainder could potentially fail, in this case I think that can only happens if the gcd is already unfeasible by any method. But let's be conservative and use only failproof methods.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 6 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

41d24b4Merge tag '8.1.beta5' into u/lftabera/ticket/8558
41f33f5Merge tag '8.2.beta6' into u/lftabera/ticket/8558
02639b7Merge tag '8.2.beta7' into u/lftabera/ticket/8558
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 6 years ago

Changed commit from 5cfbce8 to 02639b7

lftabera commented 6 years ago

Changed keywords from gcd, pari, ntl to gcd, pari, ntl, days94

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 6 years ago

Changed commit from 02639b7 to b894a30

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 6 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

e08165bMerge branch 'develop' into u/lftabera/ticket/8558
9c94c68One should not use a method only inteded for doctest
5584a90Moved the bibliography to the main reference file.
b894a30Fix doctest error in rational_reconstruction
lftabera commented 6 years ago
comment:64

This code uses ntl ZZ_pEX while it should use the faster lzz_pEX for word-sized primes.

embray commented 5 years ago
comment:66

Ticket retargeted after milestone closed (if you don't believe this ticket is appropriate for the Sage 8.8 release please retarget manually)

embray commented 5 years ago
comment:67

Tickets still needing working or clarification should be moved to the next release milestone at the soonest (please feel free to revert if you think the ticket is close to being resolved).

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 5 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

3c40475Merge tag '8.6' into u/lftabera/ticket/8558
8c678f6Merge branch 'develop' into u/lftabera/ticket/8558
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 5 years ago

Changed commit from b894a30 to 8c678f6

lftabera commented 5 years ago
comment:69

lzz_pEX is not interfaced in Sage, so that should be a different ticket.

Still, with latest ntl library the code should be even much faster since the modular gcds are subquadratic for these types.

develop merged.

slel commented 5 years ago
comment:70

Replying to @lftabera:

lzz_pEX is not interfaced in Sage, so that should be a different ticket.

There is #8109 for that.

embray commented 4 years ago
comment:71

Ticket retargeted after milestone closed

mkoeppe commented 4 years ago
comment:72

Batch modifying tickets that will likely not be ready for 9.1, based on a review of the ticket title, branch/review status, and last modification date.

seblabbe commented 4 years ago
comment:73

The branch has conflict with the sage develop branch since at least 9.1.beta7 according to the patchbot reports.

mkoeppe commented 3 years ago
comment:75

Setting new milestone based on a cursory review of ticket status, priority, and last modification date.

mkoeppe commented 3 years ago
comment:76

Setting a new milestone for this ticket based on a cursory review.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

cac17c2Merge tag '8.9' into u/lftabera/ticket/8558
83d1864Merge tag '9.0' into u/lftabera/ticket/8558
b27ae79Merge tag '9.1' into u/lftabera/ticket/8558
58fc36cMerge tag '9.2' into u/lftabera/ticket/8558
f16a49bMerge branch 'develop' into u/lftabera/ticket/8558
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 8c678f6 to f16a49b