f2m.sh (2946B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | #!/bin/sh str=$(cat $1 | perl -pe 's|\[G\^(.*?)_(.*?)\]|G\2|' |\ perl -pe 's|\[alpha_s/pi\]|1/pi|' |\ perl -pe 's|NF|N_c|g' |\ perl -pe 's|\[ln\((.*?)\)\]|Log[\1]|g' |\ perl -pe 's|Log\[\(v(\d+).n\)\^2\/n\^2\]|log(nu\1)|g' |\ perl -pe 's|v(\d+).v(\d+)|v\1\2|g' |\ perl -pe 's|\[L_beta_(.*?)\]|L_beta_\1|g' |\ perl -pe 's|Log\[(.*?)\]|log(\1)|g' |\ perl -pe 's|;|"""|'| perl -pe 's| ||' |\ perl -pe 's|=\n?|="""|' \ ) vars=$(echo "$str" | grep -o ".*=") add_vars=$(echo "$vars" | perl -pe 's|(.*?)=|var.append((\1,"\1"))|') echo "import sympy as sy" > simp.py echo "import re" >> simp.py echo "import subprocess" >> simp.py cat <<EOF >>simp.py def runFORM(expr): expr = str(expr) expr = re.sub('L_beta_',r"Lbeta",expr) expr = re.sub('N_c',r"Nc",expr) expr = re.sub('I',r"i_",expr) code = '''\ #- off stats; S Nc, pi, s, MT; Auto S Lbeta; Auto S nu, v; CF log; L expr = {0}; .sort AB Nc; Print; .end '''.format(expr) form_file_name = '/tmp/FORM_Code.frm' with open(form_file_name,'w') as f: f.write(code) form_res = subprocess.run(["form","-f",form_file_name],capture_output=True,text=True).stdout form_res=form_res.replace('\n','') form_res=form_res.replace(' ','') form_res=form_res.replace(';','') form_res=form_res.replace('expr=','') form_res=form_res.replace('Nc','N_c') form_res=form_res.replace('Lbeta','L_beta') form_res=form_res.replace('i_','I') form_res=form_res.replace('MT',r"m_t") return form_res EOF echo "$str" >> simp.py cat <<EOF >> simp.py var = list() pi = sy.pi a = sy.Symbol('alpha_s') Nc = sy.Symbol('N_c') EOF echo "$add_vars" >> simp.py cat <<EOF >> simp.py res = list() for v in var: #expr = sy.fraction(sy.sympify(v[0]).simplify()) expr = (sy.sympify(v[0]).expand()*2*pi).simplify() #expr = sy.fraction(2*sy.sympify(v[0]).collect(pi)) expr = sy.sympify(runFORM(expr)).simplify() expr = sy.fraction(expr) num = expr[0] for i in range(10): num=sy.collect(num,Nc**2-i) num=sy.collect(num,Nc-i) num=sy.collect(num,Nc+i) num=sy.collect(num,-Nc*(Nc-1)+2) num=sy.collect(num,Nc*(Nc+1)-2) if(len(num.args)==2): pre = sy.latex(num.args[0]/expr[1]) num = num.args[1] elif(expr[1] != 1): pre = sy.latex(1/expr[1]) else: pre = '' g=re.sub('G(\d+)x(\d+)',r"\\\\Gamma^{(1)}_{\1,\2}",v[1]) prefact = sy.latex(a/(2*pi)) prefact = '' if(pre!=''): rhs = prefact+pre+'\\\\breaktowidth{0.9\\\\linewidth}{\\\\delimiterswithbreaks{[}{]}{' else: rhs = '\\\\breaktowidth{0.9\\\\linewidth}{' #rhs += re.sub(r"\\\\left\\((.*)\\\\right\\)",r"\\\\delimiterswithbreaks{(}{)}{\1}",sy.latex(num)) rhs += sy.latex(num) if(pre!=''): rhs += '}}' else: rhs += '}' if num == 0: rhs = 0 print("{0}=&{1}\\\\nonumber \\\\\\\\".format(g,rhs)) EOF ipython simp.py > result.txt |