Function Repository Resource:

DefineFunctionByFormula

Source Notebook

Define a function based on its action on given mathematical expressions

Contributed by: Daniel McDonald

ResourceFunction["DefineFunctionByFormula"][{f[form1,vars1]expr1,f[form2,vars2]expr2,}]

defines a function f so that f[form,vars] returns expr where the same set of substitutions transforms formi,varsi, and expri to form,vars, and expr, respectively.

Details and Options

ResourceFunction["DefineFunctionByFormula"] creates downvalues to define a given function based on semantic matching of given rules, accounting for parameters that may disappear when certain non-variable parameters are set equal to zero or one.
The option ''Parameters'' can be used to specify which symbols not listed as variables are to be considered non-variable parameters, as opposed to fixed values.
ResourceFunction["DefineFunctionByFormula"] has the HoldAll attribute.

Examples

Basic Examples (2) 

Define downvalues for a function f:

In[1]:=
ResourceFunction["DefineFunctionByFormula"][
 f[a*x + b, x] :> g[a, b, x]]

Evaluate f at an expression that fits the formula:

In[2]:=
f[3 z, z]
Out[2]=

Scope (2) 

Define downvalues for a function f using multiple rules:

In[3]:=
ResourceFunction[
 "DefineFunctionByFormula"][{f[(b*x)^c Sin[y] + d, {x, y}] :> g[b, c, d, x, y], f[(b*x)^c Cos[y] + d, {x, y}] :> h[b, c, d, x, y]}]
TableForm@DownValues[f]
Out[4]=

Evaluate f at an expression that fits the formula:

In[5]:=
f[z*Sin[w], {z, w}]
Out[5]=

Options (3) 

Specify which symbols are to be considered non-variable parameters, as opposed to fixed values:

In[6]:=
ResourceFunction["DefineFunctionByFormula"][
 f[(b*x)^c Sin[y], {x, y}] :> g[b, c, x, y], "Parameters" -> {b}]
TableForm@DownValues[f]
Out[7]=

Evaluate the function at an expression that fits the formula:

In[8]:=
f[u^c*Sin[v], {u, v}]
Out[8]=

The function does not evaluate for the following input because c is a fixed value as opposed to a non-variable parameter, so it cannot be set equal to 2:

In[9]:=
f[u^2*Sin[v], {u, v}]
Out[9]=

Applications (2) 

Define a function based on an indefinite integration formula (ignoring the constant of integration):

In[10]:=
ResourceFunction["DefineFunctionByFormula"][
  int[Sin[x]^\[Alpha] Cos[
      x]^\[Beta] (a Cos[x]^2 + b Sin[x]^2)^\[Gamma], x] :> 1/(1 + \[Alpha]) Cos[x]^(-1 + \[Beta]) (Cos[x]^2)^(1/2 - \[Beta]/2)
     Sin[x]^(
    1 + \[Alpha]) (a + (-a + b) Sin[
        x]^2)^\[Gamma] (1 + (-1 + b/a) Sin[x]^2)^-\[Gamma] AppellF1[(
     1 + \[Alpha])/2, (1 - \[Beta])/2, -\[Gamma], (3 + \[Alpha])/2, Sin[x]^2, ((a - b) Sin[x]^2)/a]
  ];

Evaluate the function at an expression that fits the formula:

In[11]:=
int[Cos[x] (Cos[x]^2 - 3 Sin[x]^2)^(3/2), x]
Out[11]=

Publisher

Daniel McDonald

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 20 August 2025

Related Resources

License Information