In MATLAB, you should put all the functions you want to use in the function at the end, but in python, you need to define them before using the function in the function.
I rewrote it for python because I wanted to use something like MATLAB's feval (handle function?). Somehow it looks ugly, but I think it's useful when you want to put the same variable in different functions repeatedly.
test_function.py
fx=0
fp=10000
fL=10
fW=60
fq=20
fdip=5
fnu=0.25
def chinnery(fu,x,p,L,W,q,dip,nu):
def feval(funcName, *args):
return eval(funcName)(*args)
u=feval(fu, fx, fp, fq, fdip, fnu)\
- feval(fu, fx, fp-fL, fq, fdip, fnu)\
- feval(fu, fx, fp, fq-fW, fdip, fnu)\
+ feval(fu, fx, fp-fL, fq-fW, fdip, fnu)
return u
def myfunc(a,b,c,d,e):
dp=a+b*c+d-e
return dp
def myfunc2(a,b,c,d,e):
dp3=a*b-c*d-e
return dp3
up=chinnery('myfunc', fx, fp, fL, fW, fq, fdip, fnu)
print(up)
#output: 600.0
By simply changing the first variable (name of the in-function function?) Of the function chinary, only the function can be changed while keeping the combination of several variables. Currently, only myfunc and myfunc2 are available, but you can increase as many as you like.
def feval(funcName, *args):
return eval(funcName)(*args)
In feval, the function name is taken as the first variable, and the number of variables after the second variable is variable.
Maybe there is a more convenient way.
Recommended Posts