Wolfram Research

Function Repository Resource:

ExpressionToFunction

Source Notebook

Convert an expression to a pure function by specifying which symbols should be used as input arguments

Contributed by: Sjoerd Smit

ResourceFunction["ExpressionToFunction"][expr,var1,var2,]

returns Function[{var1,var2,},expr].

ResourceFunction["ExpressionToFunction"][expr,,{vari,1,vari,2,},]

bundles {vari,1,vari,2,} together in one function slot as a vector argument.

ResourceFunction["ExpressionToFunction"][expr,varspec1index1,varspec2index2,]

binds variables specified by varspeci to Slot[indexi].

Details and Options

ResourceFunction["ExpressionToFunction"] can be used as an alternative to ReplaceAll (/.) or Block for evaluating symbolic formulas at given variable values.
ResourceFunction["ExpressionToFunction"] holds all of its arguments and should work even when the variables already have definitions.
You can use ResourceFunction["ExpressionToFunction"][Evaluate[expr],] if you want to pass expr in evaluated form.
Indexed is used to vectorize variables together.
The attributes of the returned function can be specified with the Attributes option.
In ExpressionToFunction[expr,varspec1 index1,varspec2 index2,] the indexi can be either integers or strings. String indices can be used together with associations. If strings are used as indices, it is up to the user to make sure the strings do not contain illegal characters (see Slot).
When using indexed slots, the returned function is of the form Function[Null,expr,{attribute1, }]. This form makes it possible to use Function attributes in a Function that uses Slot for slotting the arguments.

Examples

Basic Examples

Create a function from a simple polynomial:

In[1]:=
polyFun = ResourceFunction["ExpressionToFunction"][1 + 2 x + x^2, x]
Out[1]=

Evaluate the polynomial at a given value:

In[2]:=
polyFun[1]
Out[2]=

Convert a multivariate PDF to a function:

In[3]:=
pdf = PDF[BinormalDistribution[1/3], {x, y}]
Out[3]=
In[4]:=
pdfFun = ResourceFunction["ExpressionToFunction"][Evaluate[pdf], x, y]
Out[4]=
In[5]:=
N@pdfFun[0, 1]
Out[5]=

Bind arguments to keys in an Association:

In[6]:=
powerFun = ResourceFunction["ExpressionToFunction"][x^y, x -> "Base", y -> "Exponent"]
Out[6]=
In[7]:=
powerFun[<|"Base" -> 2, "Exponent" -> 3|>]
Out[7]=

Bind multiple symbols to a single slot:

In[8]:=
ResourceFunction["ExpressionToFunction"][x + y, {x, y}][{E, Pi}]
Out[8]=
In[9]:=
powerFun2 = ResourceFunction["ExpressionToFunction"][x^
  y, {x, y} -> "BaseExponentTuple"]
Out[9]=
In[10]:=
powerFun2[<|"BaseExponentTuple" -> {2, 3}|>]
Out[10]=

Options

Use the Attributes option to return a function that holds its arguments:

In[11]:=
addToSymbol = ResourceFunction["ExpressionToFunction"][var = var + val, var, val, Attributes -> HoldFirst]
Out[11]=
In[12]:=
counter = 1
Out[12]=
In[13]:=
addToSymbol[counter, 2]
Out[13]=
In[14]:=
counter
Out[14]=

Applications

Group x and y together as a vector argument and map over a list of points:

In[15]:=
pdf = PDF[BinormalDistribution[1/3], {x, y}]
Out[15]=
In[16]:=
pdfVectorFun = ResourceFunction["ExpressionToFunction"][Evaluate[pdf], {x, y}]
Out[16]=
In[17]:=
dataRange = {{-3, 3}, {-3, 3}};
In[18]:=
points = Map[pdfVectorFun, CoordinateBoundsArray[dataRange, 0.1], {2}];
In[19]:=
ListContourPlot[points, DataRange -> dataRange]
Out[19]=

Add a parameter of the PDF as an argument:

In[20]:=
parameterizedPDF = ResourceFunction["ExpressionToFunction"][
  Evaluate[
   PDF[BinormalDistribution[\[Rho]], {x, y}]
   ],
  {x, y},
  \[Rho]
  ]
Out[20]=
In[21]:=
parameterizedPDF[{2, 1}, 1/10]
Out[21]=

Convert the solution of a differential equation to a function:

In[22]:=
sol = DSolveValue[{y'[x] + y[x] == a Sin[x], y[0] == 0}, y[x], x]
Out[22]=
In[23]:=
dSolveFun = ResourceFunction["ExpressionToFunction"][Evaluate[sol], x, a]
Out[23]=
In[24]:=
dSolveFun[10, 1]
Out[24]=

Represent the function at parameter value a=10 with Curry, then map over a range of x values:

In[25]:=
AssociationMap[
  Curry[dSolveFun][10],
  Range[0, 10, 0.1]
  ] // ListPlot
Out[25]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

See Also

License Information