Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Convert a Wolfram Language function to a Python function
ResourceFunction["ToPythonFunction"][func] gives a string of Python code corresponding to the Wolfram Language function func. | |
ResourceFunction["ToPythonFunction"][session,func,…] deploys the function in the running Python session and returns an ExternalFunction object. |
body& | pure functions in formal parameters #,#2,… |
Function[{x,y,…},body,…] | pure functions in named arguments |
f | function f defined in the Wolfram Language or C code |
"code" | Python code string |
Method | Automatic | method to use |
Prolog | {} | Python commands executed before deploying the function |
Epilog | {} | Python functions applied to the result |
"BuiltIns" | attempt to use Python built-in functions and the standard library |
"Callback" | execute a callback to the Wolfram Language session |
"numpy" or "np" | use the numpy package, if possible; otherwise "BuiltIns" |
Automatic | "BuiltIns", if possible; otherwise "Callback" |
Convert a pure function to a Python lambda function:
In[1]:= | ![]() |
Out[1]= | ![]() |
Deploy a Wolfram Language function in a Python session:
In[2]:= | ![]() |
Out[2]= | ![]() |
In[3]:= | ![]() |
Out[3]= | ![]() |
In[4]:= | ![]() |
Out[4]= | ![]() |
Evaluate the function value in Python:
In[5]:= | ![]() |
Out[5]= | ![]() |
Compare with the result in the Wolfram Language:
In[6]:= | ![]() |
Out[6]= | ![]() |
Clean up by closing the Python session:
In[7]:= | ![]() |
Elementary functions are convertible to Python using the Python standard library:
In[8]:= | ![]() |
Out[8]= | ![]() |
Special functions are computed by a Wolfram Language callback:
In[9]:= | ![]() |
Out[9]= | ![]() |
Pure functions in formal parameters # (#1), #2,…:
In[10]:= | ![]() |
Out[10]= | ![]() |
In[11]:= | ![]() |
Out[11]= | ![]() |
Pure functions in named arguments:
In[12]:= | ![]() |
Out[12]= | ![]() |
In[13]:= | ![]() |
Out[13]= | ![]() |
Vector arguments:
In[14]:= | ![]() |
Out[14]= | ![]() |
Built-in Wolfram Language functions:
In[15]:= | ![]() |
Out[15]= | ![]() |
User-defined functions:
In[16]:= | ![]() |
In[17]:= | ![]() |
Out[17]= | ![]() |
Python code strings:
In[18]:= | ![]() |
Out[18]= | ![]() |
In[19]:= | ![]() |
Out[19]= | ![]() |
In[20]:= | ![]() |
Out[20]= | ![]() |
In[21]:= | ![]() |
Specify whether to use "math" or "cmath" module on the Python side by giving the optional argument domain:
In[22]:= | ![]() |
Out[22]= | ![]() |
In[23]:= | ![]() |
Out[23]= | ![]() |
Decide heuristically, depending whether the function body contains explicit complex values:
In[24]:= | ![]() |
Out[24]= | ![]() |
In[25]:= | ![]() |
Out[25]= | ![]() |
Use a Python-side function from the standard library:
In[26]:= | ![]() |
Out[26]= | ![]() |
Implement a callback into a WolframLangauge session to compute the function:
In[27]:= | ![]() |
Out[27]= | ![]() |
Use the Python package numpy:
In[28]:= | ![]() |
Out[28]= | ![]() |
Use the default heuristic to decide between the methods:
In[29]:= | ![]() |
Out[29]= | ![]() |
In[30]:= | ![]() |
Out[30]= | ![]() |
Use the Prolog option to ensure that the Python interpreter meets the requirements before deploying the function:
In[31]:= | ![]() |
Out[31]= | ![]() |
In[32]:= | ![]() |
Out[32]= | ![]() |
In[33]:= | ![]() |
Create a function which returns a Python array:
In[34]:= | ![]() |
Out[34]= | ![]() |
In[35]:= | ![]() |
Out[35]= | ![]() |
In[36]:= | ![]() |
Out[36]= | ![]() |
Use the Epilog option to convert the result to numpy.array:
In[37]:= | ![]() |
Out[37]= | ![]() |
In[38]:= | ![]() |
Out[38]= | ![]() |
In[39]:= | ![]() |
Out[39]= | ![]() |
In[40]:= | ![]() |
Start a Python session and create a PythonObject for the Probabilistic Numerics Python package ProbNum:
In[41]:= | ![]() |
Out[41]= | ![]() |
In[42]:= | ![]() |
Out[42]= | ![]() |
Deploy a Python function describing an ODE vector field:
In[43]:= | ![]() |
Out[43]= | ![]() |
Pass the deployed Wolfram Language function f back to Python as a field to solve the logistic ODE:
In[44]:= | ![]() |
In[45]:= | ![]() |
Out[45]= | ![]() |
Plot the solution:
In[46]:= | ![]() |
Out[46]= | ![]() |
In[47]:= | ![]() |
Callback functions contain a template slot for the Wolfram Language session defined on the Python side:
In[48]:= | ![]() |
Out[48]= | ![]() |
Given such a string as an argument, ToPythonFunction automatically fills the slot with the variable wl_session defined in the Python session:
In[49]:= | ![]() |
Out[49]= | ![]() |
In[50]:= | ![]() |
Out[50]= | ![]() |
Alternatively, you can fill the slot with the name of another variable of your choice:
In[51]:= | ![]() |
Out[51]= | ![]() |
Call the function:
In[52]:= | ![]() |
Out[52]= | ![]() |
Clean up:
In[53]:= | ![]() |
For Method→"BuiltIns" only functions with a single downvalue are currently supported:
In[54]:= | ![]() |
Out[54]= | ![]() |
In[55]:= | ![]() |
In[56]:= | ![]() |
Out[56]= | ![]() |
Use callback into the Wolfram Language to define the function successfully:
In[57]:= | ![]() |
Out[57]= | ![]() |
In[58]:= | ![]() |
Out[58]= | ![]() |
In[59]:= | ![]() |
Method→"BuiltIns" and Method→"numpy" automatically handle only a subset of functions convertible to Python code:
In[60]:= | ![]() |
Out[60]= | ![]() |
In[61]:= | ![]() |
In[62]:= | ![]() |
Out[62]= | ![]() |
Deploy the function manually:
In[63]:= | ![]() |
Out[63]= | ![]() |
Verify against the direct computation in the Wolfram Language:
In[64]:= | ![]() |
Out[64]= | ![]() |
In[65]:= | ![]() |
Out[65]= | ![]() |
Clean up:
In[66]:= | ![]() |
Functions with lists on the left-hand side in downvalues are currently processed with a callback:
In[67]:= | ![]() |
In[68]:= | ![]() |
Out[68]= | ![]() |
Extract values on the right-hand side of the function to compute the function in Python without callback:
In[69]:= | ![]() |
In[70]:= | ![]() |
Out[70]= | ![]() |
For Method→"Callback", functions in the contexts other than "Global`" or "System`" are not currently supported:
In[71]:= | ![]() |
Out[71]= | ![]() |
In[72]:= | ![]() |
Out[72]= | ![]() |
Export the function by redefining it in the "Global`" context:
In[73]:= | ![]() |
In[74]:= | ![]() |
Out[74]= | ![]() |
Use the Python function:
In[75]:= | ![]() |
Out[75]= | ![]() |
In[76]:= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License