CoCalc Shared Fileswww / cgi-bin / mfd / pari.pyOpen in CoCalc with one click!
Author: William A. Stein
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
14
import os,string # operating system calls
15
import misc, constants
16
17
PARI='%s/gopari'%constants.bin
18
19
def 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')
26
ans = ansfile.read()
27
ansfile.close()
28
os.remove(file.name)
29
os.remove(ansfile.name)
30
return string.replace(ans,'\012','')
31
32
def 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
40
def 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
52
def issquarefree(N):
53
return pari("issquarefree(%s)"%N)=='1'
54
55
def isprime(N):
56
return pari("isprime(%s)"%N)=='1'
57
58
def arecoprime(x,y):
59
return pari("gcd(%s,%s)"%(x,y))=='1'
60
61
def 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
83
def 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
92
def eulerphi(n):
93
cmd = "eulerphi(%s)"%n
94
return pari(cmd)
95