Function Repository Resource:

ScalarPureFunction

Source Notebook

Get a pure function whose argument is a vector for a given multivariable scalar function

Contributed by: E. Chan-López, Jorge Luis Ramos Castellano, Bhaskar Shukla & Alex Trounev

ResourceFunction["ScalarPureFunction"][s,vars]

gives the corresponding pure function representation of a multivariable scalar function s for a vector of variables vars.

ResourceFunction["ScalarPureFunction"][s]

represents an operator form that can be applied to arguments.

Details and Options

The s must be a multivariable scalar function.
The argument vars must be a vector containing the variables of interest.
ResourceFunction["ScalarPureFunction"][s,vars ] can be used to obtain different functions with vector arguments in the basic multivariable calculus, for example, to study chaotic Hamiltonian systems.
ResourceFunction["ScalarPureFunction"][s,vars ] has an option to specify the method used for indexing the elements of vars.

Examples

Basic Examples (2) 

Define a function of two variables taking as input a vector of variables:

In[1]:=
func = ResourceFunction["ScalarPureFunction"][x^2 + y, {x, y}]
Out[1]=

Apply the function:

In[2]:=
func[{a, b}]
Out[2]=

Scope (2) 

Use ScalarPureFunction to obtain a linear pure function of three variables and evaluate it at (1,-2,1):

In[3]:=
ResourceFunction["ScalarPureFunction"][3 x - 2 y + z, {x, y, z}]
Out[3]=
In[4]:=
%[{1, -2, 1}]
Out[4]=

Use ScalarPureFunction to obtain a transcendental pure function of three variables and evaluate it at (1,1,1):

In[5]:=
ResourceFunction["ScalarPureFunction"][
 Exp[-1 - x^2 - y^2 - z^2], {x, y, z}]
Out[5]=
In[6]:=
%[{1, 1, 1}]
Out[6]=

Options (1) 

IndexingMethod (1) 

The IndexingMethod option allows specifying the method used to indexing elements of argvars in the ScalarPureFunction. By default, the method is set to "UseIndexed", which uses the Indexed function. Alternatively, can be set the option to "UsePart" to use the Part function. This option provides flexibility in choosing the preferred method of indexing elements of argvars within the ScalarPureFunction:

In[7]:=
With[{f = x^2 + y^3, vars = {x, y}}, ResourceFunction["ScalarPureFunction"][f, vars, "IndexingMethod" -> "UsePart"]]
Out[7]=

Applications (3) 

Generate a Height Map (1) 

To generate a height map for an imaginary mountain in the shape of a two-dimensional Gaussian function, use ScalarPureFunction to represent the height at each point of the mountain as follows:

In[8]:=
h = ResourceFunction["ScalarPureFunction"][
  300 Exp[-10 ((x - 1/2)^2 + (y - 1/2)^2)], {x, y}]
Out[8]=
In[9]:=
{Splice@#, h@#} & /@ Tuples@Range[{0, 0}, 1, 0.01] // Short
Out[9]=
In[10]:=
ListPlot3D[%, PlotRange -> All, Mesh -> None, ColorFunction -> "ArmyColors"]
Out[10]=

Poincaré Sections of Hamiltonian Systems with Two Degrees of Freedom (2) 

Use ScalarPureFunction in combination with ClickPoincarePlot2D to plot Poincaré sections of particle motion near a black hole horizon:

In[11]:=
H1 = ResourceFunction["ScalarPureFunction"][
   1/2 (100 - 200 x + 2 Sqrt[x] Sqrt[1 + py^2 + px^2 x] + 100 (x^2 + y^2)), {x, px, y, py}];
hamEqns1 = {x'[t] == px[t]* x[t] Sqrt[x[t]/(1 + py[t]^2 + px[t]^2 x[t])],
   		px'[t] == 100 - 100 x[t] - Sqrt[x[t]/(
      1 + py[t]^2 + px[t]^2 x[t])] (px[t]^2 + (1 + py[t]^2)/(2 x[t])),
   		y'[t] == py[t] Sqrt[x[t]/(1 + py[t]^2 + px[t]^2 x[t])],
   		py'[t] == -100 y[t]
   		};
name = "Black Hole Motion";
In[12]:=
ResourceFunction["ClickPoincarePlot2D"][hamEqns1, H1, 40, t, 4000, y[t], {x[t], px[t]}, name, {PlotStyle -> {{
AbsolutePointSize[1], 
GrayLevel[0], 
Opacity[0.4]}}, AspectRatio -> 1, PlotHighlighting -> None}]
Out[12]=

Poincaré sections of the Yang-Mills-Higgs system:

In[13]:=
H2 = ResourceFunction["ScalarPureFunction"][
   1/2 (px^2 + py^2) + x^2 + y^2 + 1/2*x^2 y^2, {x, px, y, py}];
hamEqns2 = {x'[t] == px[t],
   		px'[t] == -(2 x[t] + x[t]*y[t]^2),
   		y'[t] == py[t],
   		py'[t] == -(2 y[t] + x[t]^2*y[t])
   		};
name = "Yang-Mills-Higgs System";
In[14]:=
ResourceFunction["ClickPoincarePlot2D"][hamEqns2, H2, 10, t, 4000, x[t], {y[t], py[t]}, name, {PlotStyle -> {{
AbsolutePointSize[1], 
GrayLevel[0], 
Opacity[0.4]}}, AspectRatio -> 1, PlotHighlighting -> None}]
Out[14]=

Properties and Relations (2) 

The function ScalarPureFunction shares some similarities with ResourceFunction["ExpressionToFunction"], which also converts expressions into pure functions. However, the two functions differ in their design and in how they handle the same use cases. Below is a comparison:

In[15]:=
ResourceFunction[
 "ScalarPureFunction"][(3 E^(9/16 (-x^2 + (2 x y)/3 - y^2)))/(4 Sqrt[
     2] \[Pi]), {x, y}]
Out[15]=
In[16]:=
ResourceFunction[
  "ExpressionToFunction"][(3 E^(9/
        16 (-x^2 + (2 x y)/3 - y^2)))/(4 Sqrt[2] \[Pi]), x, y]
Out[16]=

Use ScalarPureFunction in combination with ResourceFunction["TensorPureFunction"] to compute a Hessian matrix:

In[17]:=
ResourceFunction["ScalarPureFunction"][x^3 - 2 x y - y^6, {x, y}]
Out[17]=
In[18]:=
hessian = ResourceFunction["TensorPureFunction"][
  D[%[{x, y}], {{x, y}, 2}], {{x, y}}]
Out[18]=

Possible Issues (3) 

If any of the specified variables have existing definitions, ScalarPureFunction issues a warning and returns unevaluated. For example:

In[19]:=
x1 = 1;
x2 = 2;
ResourceFunction["ScalarPureFunction"][x1 x2 x3, {x1, x2, x3}]
Out[21]=

To avoid this issue, wrap the variables with Block to prevent conflicts with global definitions:

In[22]:=
Block[{x1, x2}, ResourceFunction["ScalarPureFunction"][x1 x2 x3, {x1, x2, x3}]]
Out[22]=

Since ScalarPureFunction explicitly checks for global definitions before proceeding, it remains unevaluated when global definitions of the arguments are found. For example:

In[23]:=
s = x - y^2/(x*z);
vars = {x, y, z};
In[24]:=
ResourceFunction["ScalarPureFunction"][s, vars]
Out[24]=

Evaluate overrides the hold attribute so that ScalarPureFunction behaves as expected with the previously defined inputs:

In[25]:=
ResourceFunction["ScalarPureFunction"][Evaluate@s, Evaluate@vars]
Out[25]=

On the other hand, when using Evaluate explicitly on the function or variable arguments before passing them to ScalarPureFunction, and at least one of the variables in the argument list has a global definition, the expected behavior of holding evaluation and issuing a warning will not occur. Instead, the function will be evaluated with the assigned values, potentially leading to incorrect results. For example:

In[26]:=
x = 1;
s = x - y^2/(x*z);
vars = {x, y, z};
In[27]:=
ResourceFunction["ScalarPureFunction"][Evaluate@s, Evaluate@vars]
Out[27]=

In this case, since Evaluate forces the immediate evaluation of the input, ScalarPureFunction does not detect existing global definitions and proceeds incorrectly. To avoid this issue, it is recommended to use Clear to remove any prior definitions before applying the function.

Publisher

Ramón Eduardo Chan López

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.1.0 – 26 March 2025
  • 1.0.0 – 20 March 2024

Source Metadata

Related Resources

Author Notes

The current implementation draws inspiration from the in-depth discussion and contributions of various users on the Mathematica Stack Exchange Community in the post titled Converting to a scalar pure function without getting the warning messages.

License Information