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]:=
H = 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}];

eq1 = x'[t] == px[t]* x[t] Sqrt[x[t]/(1 + py[t]^2 + px[t]^2*x[t])];

eq2 = px'[t] == 100 - 100 x[t] - px[t]^2 Sqrt[
      x[t]/(1 + py[t]^2 + px[t]^2*x[t])] - ((1 + py[t]^2) Sqrt[
        x[t]/(1 + py[t]^2 + px[t]^2*x[t])])/(2 x[t]);

eq3 = y'[t] == py[t] Sqrt[x[t]/(1 + py[t]^2 + px[t]^2 x[t])];

eq4 = py'[t] == -100 y[t];
In[12]:=
ResourceFunction["ClickPoincarePlot2D"][{eq1, eq2, eq3, eq4}, H, 40, t, 4000, y[t], {x[t], px[t]}, {PlotStyle -> {{
AbsolutePointSize[1], 
GrayLevel[0], 
Opacity[0.4]}}, AspectRatio -> 1, PlotHighlighting -> None}]
Out[12]=

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

In[13]:=
H = ResourceFunction["ScalarPureFunction"][
   1/2 (px^2 + py^2) + x^2 + y^2 + 1/2*x^2 y^2, {x, px, y, py}];

eq1 = x'[t] == px[t];

eq2 = px'[t] == -(2 x[t] + x[t]*y[t]^2);

eq3 = y'[t] == py[t];

eq4 = py'[t] == -(2 y[t] + x[t]^2*y[t]);
In[14]:=
ResourceFunction["ClickPoincarePlot2D"][{eq1, eq2, eq3, eq4}, H, 10, t, 4000, x[t], {y[t], py[t]}, {PlotStyle -> {{
AbsolutePointSize[1], 
GrayLevel[0], 
Opacity[0.4]}}, AspectRatio -> 1, PlotHighlighting -> None}]
Out[14]=

Properties and Relations (1) 

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

In[15]:=
ResourceFunction["ScalarPureFunction"][x^3 - 2 x y - y^6, {x, y}, "IndexingMethod" -> "UsePart"]
Out[15]=
In[16]:=
hessian = ResourceFunction["TensorPureFunction"][
  D[%[{x, y}], {{x, y}, 2}], {{x, y}}]
Out[16]=

Publisher

Ramón Eduardo Chan López

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 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