I want to use MATLAB feval with python

python function definition

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

I want to use MATLAB feval with python
I want to use Temporary Directory with Python2
I want to debug with Python
I want to use jar from python
I want to analyze logs with Python
I want to play with aws with python
[Python] I want to use the -h option with argparse
I want to use a wildcard that I want to shell with Python remove
I want to make a game with Python
I want to use ceres solver from python
I don't want to use -inf with np.log
#Unresolved I want to compile gobject-introspection with Python3
I want to use ip vrf with SONiC
I want to solve APG4b with Python (Chapter 2)
I want to write to a file with Python
I want to handle optimization with python and cplex
I wanted to use the Python library from MATLAB
I want to inherit to the back with python dataclass
I want to work with a robot in python.
I want to AWS Lambda with Python on Mac!
[ML Ops] I want to do multi-project with Python
I want to use the R dataset in python
I want to run a quantum computer with Python
Python: How to use async with
How to use FTP with Python
I want to do ○○ with Pandas
Solution when you want to use cv_bridge with python3 (virtualenv)
I want to be able to analyze data with Python (Part 3)
I want to specify another version of Python with pyvenv
I want to be able to analyze data with Python (Part 1)
I want to be able to analyze data with Python (Part 4)
I want to be able to analyze data with Python (Part 2)
I want to automatically attend online classes with Python + Selenium!
I want to use a virtual environment with jupyter notebook!
[Python] I want to use only index when looping a list with a for statement
I want to detect objects with OpenCV
I want to blog with Jupyter Notebook
I wanted to solve ABC160 with Python
I want to build a Python environment
I want to use Linux on mac
I want to pip install with PythonAnywhere
[Introduction to Python] Let's use foreach with Python
I want to use IPython Qt Console
I wanted to solve ABC172 with Python
I want to know the weather with LINE bot feat.Heroku + Python
I want to monitor UNIQLO + J page updates [Scraping with python]
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
I want to output the beginning of the next month with Python
I want to do a full text search with elasticsearch + python
I want to use an external library with IBM Cloud Functions
I want to use both key and value of Python iterator
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I really want to use GitHub Flavored Markdown (GFM) with Pelican!
I know? Data analysis using Python or things you want to use when you want with numpy
I want to do Dunnett's test in Python
I tried to use lightGBM, xgboost with Boruta
I want to do it with Python lambda Django, but I will stop
I want to analyze songs with Spotify API 2
I want to tweet on Twitter with Python, but I'm addicted to it
I wanted to solve NOMURA Contest 2020 with Python
I want to memoize including Python keyword arguments