I try create a function with a loop for inside. The script work without function but declare the python function don't work. (The original script is is more longer but with this part i think the is enough)
import numpy as np
import math as mt
from sympy import*
import fractions
init_printing(use_latex='mathjax')
PHnumbers=4
PHnumbers2=2
statetype= 1
for d in range(1,9):
modes=d+1
if statetype==1:
comb=modes
elif statetype==2:
comb=int((modes*(modes+1))/2)
exec(f'Phases{d}=[]'), exec(f'Phasesv{d}=[]')
for i in range(modes):
exec(f'theta_{i+1}= symbols(\'theta_{i+1}\', real=True)')
exec(f'Phases{d}.append(globals()[\'\'.join([\'theta_\',str(i+1)])])')
exec(f'Phasesv{d}.append(globals()[\'\'.join([\'theta_\',str(i+1)])])')
exec(f'v{i+1}=[[0]]*modes')
exec(f'Phasesv{d}[0]*=0')
for i in range(modes):
exec(f'globals()[\'\'.join([\'v\',str(i+1)])][i]=[PHnumbers*diff(Phasesv{d}[i],Phases{d}[i])]')
conteo = d
for j in range(modes):
for i in range(modes):
if j<i:
conteo = conteo + 1
exec(f'v{conteo+1}=[[0]]*modes')
exec(f'globals()[\'\'.join([\'v\',str(conteo+1)])][i]=[PHnumbers2*diff(Phasesv{d}[i],Phases{d}[i])]')
exec(f'globals()[\'\'.join([\'v\',str(conteo+1)])][j]=[PHnumbers2*diff(Phasesv{d}[j],Phases{d}[j])]')
exec(f'Vec{d}=[]'),exec(f'Coeff{d}=[]'), exec(f'Nii{d}=[]'), exec(f'Nij{d}=[]')
for i in range(comb):
exec(f'Vec{d}.append(globals()[\'\'.join([\'v\',str(i+1)])])')
for i in range(len(Vec4)): print(Vec4[i]) The previous script work, no problems up to here. Now I declare python function:
def metro(PHnumbers):
statetype=1
PHnumbers2=2
for d in range(1,9):
modes=d+1
if statetype==1:
comb=modes
elif statetype==2:
comb=int((modes*(modes+1))/2)
exec(f'Phases{d}=[]'), exec(f'Phasesv{d}=[]')
for i in range(modes):
exec(f'theta_{i+1}= symbols(\'theta_{i+1}\', real=True)')
exec(f'Phases{d}.append(globals()[\'\'.join([\'theta_\',str(i+1)])])')
exec(f'Phasesv{d}.append(globals()[\'\'.join([\'theta_\',str(i+1)])])')
exec(f'v{i+1}=[[0]]*modes')
exec(f'Phasesv{d}[0]*=0')
for i in range(modes):
exec(f'globals()[\'\'.join([\'v\',str(i+1)])][i]=[PHnumbers*diff(Phasesv{d}[i],Phases{d}[i])]')
conteo = d
for j in range(modes):
for i in range(modes):
if j<i:
conteo = conteo + 1
exec(f'v{conteo+1}=[[0]]*modes')
exec(f'globals()[\'\'.join([\'v\',str(conteo+1)])][i]=[PHnumbers2*diff(Phasesv{d}[i],Phases{d}[i])]')
exec(f'globals()[\'\'.join([\'v\',str(conteo+1)])][j]=[PHnumbers2*diff(Phasesv{d}[j],Phases{d}[j])]')
exec(f'Vec{d}=[]'),exec(f'Coeff{d}=[]'), exec(f'Nii{d}=[]'), exec(f'Nij{d}=[]')
for i in range(comb):
exec(f'Vec{d}.append(globals()[\'\'.join([\'v\',str(i+1)])])')
for i in range(len(Vec4)):
print(Vec4[i])
The second code show problem: 'theta_1 is not defined'