Path: blob/master/sage/schemes/elliptic_curves/gp_simon.py
4108 views
"""1Denis Simon's PARI scripts2"""34#*****************************************************************************5# Copyright (C) 2005 William Stein <[email protected]>6#7# Distributed under the terms of the GNU General Public License (GPL)8#9# This code is distributed in the hope that it will be useful,10# but WITHOUT ANY WARRANTY; without even the implied warranty of11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU12# General Public License for more details.13#14# The full text of the GPL is available at:15#16# http://www.gnu.org/licenses/17#*****************************************************************************1819from __future__ import with_statement202122from sage.structure.parent_gens import localvars2324from sage.interfaces.gp import Gp25from sage.misc.sage_eval import sage_eval26from sage.misc.randstate import current_randstate27from sage.rings.all import QQ28from constructor import EllipticCurve2930gp = None31def init():32"""33Function to initialize the gp process34"""35global gp36if gp is None:37gp = Gp(script_subdirectory='simon')38gp.read("ell.gp")39gp.read("ellQ.gp")40gp.read("qfsolve.gp")41gp.read("resultant3.gp")424344def simon_two_descent(E, verbose=0, lim1=5, lim3=50, limtriv=10, maxprob=20, limbigprime=30):45"""46Interface to Simon's gp script for two-descent.4748.. NOTE::4950Users should instead run E.simon_two_descent()5152EXAMPLES::5354sage: import sage.schemes.elliptic_curves.gp_simon55sage: E=EllipticCurve('389a1')56sage: sage.schemes.elliptic_curves.gp_simon.simon_two_descent(E)57[2, 2, [(1 : 0 : 1), (-11/9 : -55/27 : 1)]]58sage: E.simon_two_descent()59(2, 2, [(1 : 0 : 1), (-11/9 : -55/27 : 1)])6061TESTS::6263sage: E = EllipticCurve('37a1').change_ring(QuadraticField(-11,'x'))64sage: E.simon_two_descent()65(1, 1, [(-1 : 0 : 1)])6667"""68init()6970current_randstate().set_seed_gp(gp)7172K = E.base_ring()73K_orig = K74# The following is to correct the bug at \#5204: the gp script75# fails when K is a number field whose generator is called 'x'.76if not K is QQ:77K = K.change_names('a')78E_orig = E79E = EllipticCurve(K,[K(list(a)) for a in E.ainvs()])80F = E.integral_model()8182if K != QQ:83# Simon's program requires that this name be y.84with localvars(K.polynomial().parent(), 'y'):85gp.eval("K = bnfinit(%s);" % K.polynomial())86if verbose >= 2:87print "K = bnfinit(%s);" % K.polynomial()88gp.eval("%s = Mod(y,K.pol);" % K.gen())89if verbose >= 2:90print "%s = Mod(y,K.pol);" % K.gen()9192if K == QQ:93cmd = 'ellrank([%s,%s,%s,%s,%s]);' % F.ainvs()94else:95cmd = 'bnfellrank(K, [%s,%s,%s,%s,%s]);' % F.ainvs()9697gp('DEBUGLEVEL_ell=%s; LIM1=%s; LIM3=%s; LIMTRIV=%s; MAXPROB=%s; LIMBIGPRIME=%s;'%(98verbose, lim1, lim3, limtriv, maxprob, limbigprime))99100if verbose >= 2:101print cmd102s = gp.eval('ans=%s;'%cmd)103if s.find("###") != -1:104raise RuntimeError, "%s\nAn error occurred while running Simon's 2-descent program"%s105if verbose > 0:106print s107v = gp.eval('ans')108if v=='ans': # then the call to ellrank() or bnfellrank() failed109return 'fail'110if verbose >= 2:111print "v = ", v112# pari represents field elements as Mod(poly, defining-poly)113# so this function will return the respective elements of K114def _gp_mod(*args):115return args[0]116ans = sage_eval(v, {'Mod': _gp_mod, 'y': K.gen(0)})117inv_transform = F.isomorphism_to(E)118ans[2] = [inv_transform(F(P)) for P in ans[2]]119ans[2] = [E_orig([K_orig(list(c)) for c in list(P)]) for P in ans[2]]120return ans121122123124