CoCalc Public Fileswww / cgi-bin / mfd / pari.py
Author: William A. Stein
Compute Environment: Ubuntu 18.04 (Deprecated)
1###########################################################
2#                                                         #
3# pari.py -- compute things using system calls to PARI    #
4#                                                         #
5# William Stein, 11/26/2000                               #
6#                                                         #
7# pari(cmd) is the result of executing the command "cmd"  #
8# in PARI.  There is no need to have an "ans" variable    #
9# like with MAGMA, because PARI treats the last argument  #
10# correctly.                                              #
11#                                                         #
12###########################################################
13
14import os,string   # operating system calls
15import misc, constants
16
17PARI='%s/gopari'%constants.bin
18
19def pari(cmd):
20    file = misc.newtempfile()
21    outname = misc.newtempfilename()
22    file.write(cmd + '\n write(\"' + outname + '\",%);')
23    file.close()
24    os.system(PARI + ' < ' + file.name + ' > /dev/null' )
25    ansfile = open(outname,'r')
27    ansfile.close()
28    os.remove(file.name)
29    os.remove(ansfile.name)
30    return string.replace(ans,'\012','')
31
32def divisors(N):
33    s=pari("divisors(%s)"%N)
34    s=s.replace("[","['")
35    s=s.replace("]","']")
36    s=s.replace(", ","', '")
37    return eval(s)
38
39
40def factor(N):
41    s = pari("factor(%s)"%N)
42    if s[:1] == 'm':
43        return [[]]
44    if s[:1] == 'M':
45        s = string.replace(s,'Mat(','[')
46        s = string.replace(s,')',']')
47        return eval(s)
48    s = string.replace(s,";","],[")
49    s = "["+s+"]"
50    return eval(s)
51
52def issquarefree(N):
53    return pari("issquarefree(%s)"%N)=='1'
54
55def isprime(N):
56    return pari("isprime(%s)"%N)=='1'
57
58def arecoprime(x,y):
59    return pari("gcd(%s,%s)"%(x,y))=='1'
60
61def torsion_bound(N,prec,charpolys):
62    code = "{torbound(N,prec,cps)= \n"\
63           "ans=0;\n"\
64           "forprime(p=3,prec,\n"\
65           "   if(gcd(N,p)==1,\n"\
66           "      at_p=0;\n"\
67           "      for(i=1,length(cps),\n"\
68           "         if(cps[i][1]==p,\n"\
69           "            at_p=subst(cps[i][2],x,p+1);\n"\
70           "            break\n"\
71           "         )\n"\
72           "      );\n"\
73           "      if(at_p==0,return(0));\n"\
74           "      ans=gcd(ans,at_p)\n"\
75           "   )\n"\
76           ");\n"\
77           "return(ans);}\n"
78    cmd = code + "\ntorbound(%s,%s,%s)"%(N,prec,charpolys)
79    cmd = string.replace(cmd,"'","")
80    return pari(cmd)
81
82
83def primes(first,last):  # sequence of primes between first and last
84    cmd = "v=[]; forprime(p=%s,%s,v=concat(v,[p])); v"%(first,last);
85    ans = pari(cmd)
86    ans = string.replace(ans," ","")
87    ans = string.replace(ans,",","','")
88    ans = string.replace(ans,"[","['")
89    ans = string.replace(ans,"]","']")
90    return eval(ans)
91
92def eulerphi(n):
93    cmd = "eulerphi(%s)"%n
94    return pari(cmd)
95