Function Repository Resource:

SampleUnivariateFunctions

Source Notebook

Get one or more functions of a single complex variable

Contributed by: Paco Jain

ResourceFunction["SampleUnivariateFunctions"][]

returns a random univariate function in terms of the complex variable z, and possibly one or more parameters.

ResourceFunction["SampleUnivariateFunctions"][n]

returns a sample of n a random functions, for integer n.

ResourceFunction["SampleUnivariateFunctions"][All]

returns all available functions.

Details and Options

By default, ResourceFunction["SampleUnivariateFunctions"] returns expressions written in terms of the independent variable z (FormalZ) and zero or more other formal symbols representing unspecified parameters. Domains in z are presumed to be the entire complex plane, but extra parameters may have additional restrictions. It is up to the user to pick suitable values for these extra parameters.
ResourceFunction["SampleUnivariateFunctions"] takes the following options:
"IncludedSubexpressions"{}all of the supplied forms must appear as subexpressions of returned functions
"ExcludedSubexpressions"{}none of the supplied forms may appear as subexpressions of returned functions
"OutputForm"Automaticchoose the syntactic form in which to return the functions
"TrueUnivariate"Automaticwhether to return only functions free of extra symbolic parameters
Possible values for "OutputForm" include Automatic, "Held", "PureFunction" and "NestedFunction".
Possible values for "TrueUnivariate" include True, False and Automatic.

Examples

Basic Examples (2) 

Get some arbitrary sample functions:

In[1]:=
ResourceFunction["SampleUnivariateFunctions"][]
Out[1]=
In[2]:=
ResourceFunction["SampleUnivariateFunctions"][]
Out[2]=
In[3]:=
ResourceFunction["SampleUnivariateFunctions"][]
Out[3]=
In[4]:=
ResourceFunction["SampleUnivariateFunctions"][]
Out[4]=

Get a random sample of five univariate functions:

In[5]:=
ResourceFunction["SampleUnivariateFunctions"][5]
Out[5]=

Scope (2) 

To get all available functions, use the argument All:

In[6]:=
funcs = ResourceFunction["SampleUnivariateFunctions"][All];
Length[funcs]
Out[7]=

Besides the independent variable z, functions may contain unspecified parameters represented by formal symbols: (here, a, b, c, d and e):

In[8]:=
int = ResourceFunction["SampleUnivariateFunctions"][]
Out[8]=

Formal symbols reside in the "System`" context:

In[9]:=
Context[\[FormalA]]
Out[9]=

They are protected and cannot be assigned values:

In[10]:=
\[FormalA] = 3
Out[10]=

Instead, use pattern replacement to give values to such symbols:

In[11]:=
int /. {\[FormalA] -> 1, \[FormalB] -> 2, \[FormalC] -> 3, \[FormalD] -> 5, \[FormalE] -> 7}
Out[11]=

Options (12) 

IncludedSubexpressions (3) 

Get a single function containing BesselJ as a subexpression:

In[12]:=
ResourceFunction["SampleUnivariateFunctions"][
 "IncludedSubexpressions" -> {BesselJ}]
Out[12]=

Get all sample functions containing BesselJ as a subexpression and count the number available:

In[13]:=
besselJFuncs = ResourceFunction["SampleUnivariateFunctions"][All, "IncludedSubexpressions" -> {BesselJ}];
Length[besselJFuncs]
Out[14]=

View a subset of five of these functions:

In[15]:=
ResourceFunction["SampleUnivariateFunctions"][5, "IncludedSubexpressions" -> {BesselJ}]
Out[15]=

Every pattern listed in the value for "IncludedSubexpressions" must be separately included in the result. As such, additional forms in the list value of "IncludedSubexpressions" always reduce (or leave unchanged) the numbers of results:

In[16]:=
samp1 = ResourceFunction["SampleUnivariateFunctions"][All, "IncludedSubexpressions" -> {BesselJ}];
Length[samp1]
Out[17]=
In[18]:=
samp2 = ResourceFunction["SampleUnivariateFunctions"][All, "IncludedSubexpressions" -> {BesselJ, BesselK}];
Length[samp2]
Out[19]=

The second sample is a subset of the first:

In[20]:=
ContainsAll[samp1, samp2]
Out[20]=

To form a more inclusive pattern, allowing either BesselJ or BesselK, instead use Alternatives:

In[21]:=
samp = ResourceFunction["SampleUnivariateFunctions"][All, "IncludedSubexpressions" -> {BesselJ | BesselK}];
Length[samp]
Out[22]=

ExcludedSubexpressions (2) 

Get all sample functions containing BesselJ, but excluding Hypergeometric0F1Regularized and Sum:

In[23]:=
mySample = ResourceFunction["SampleUnivariateFunctions"][All, "IncludedSubexpressions" -> {BesselJ}, "ExcludedSubexpressions" -> {Hypergeometric0F1Regularized, Sum}];

Get a count of the number of such functions:

In[24]:=
Length[mySample]
Out[24]=

OutputForm (4) 

Results can be returned in one of several forms, as determined by the "OutputForm" option. The setting "OutputForm"Automatic (the default) returns bare expressions, written in terms of formal symbols:

In[25]:=
SeedRandom[2];
samp = ResourceFunction["SampleUnivariateFunctions"][4]
Out[26]=

For ease in substituting user-supplied parameter/variable values, the setting "OutputType""PureFunction" can be useful (note that the independent variable, z, always appears as the first argument of the returned Function expressions):

In[27]:=
SeedRandom[2];
samp = ResourceFunction["SampleUnivariateFunctions"][4, "OutputForm" -> "PureFunction"]
Out[28]=

On supplying values, Function ignores extra parameters, using only the number needed to fill its slots:

In[29]:=
Through[samp[t, x, y, z, w]]
Out[29]=

The setting "OutputType""NestedFunction" separates the independent variable (z) from other parameters using separate Function wrappers:

In[30]:=
SeedRandom[2];
samp = ResourceFunction["SampleUnivariateFunctions"][4, "OutputForm" -> "NestedFunction"]
Out[31]=

The setting "OutputForm" "Held" causes results to be returned wrapped in Hold:

In[32]:=
SeedRandom[2];
ResourceFunction["SampleUnivariateFunctions"][4, "OutputForm" -> "Held"]
Out[33]=

The setting "OutputForm""PureFunction" can be preferable for certain calculations (e.g., using Derivative, Plot, …):

In[34]:=
f = ResourceFunction["SampleUnivariateFunctions"][
  "OutputForm" -> "PureFunction", "TrueUnivariate" -> True]
Out[34]=
In[35]:=
Derivative[1][f]
Out[35]=
In[36]:=
Plot[f[x], {x, 1, 2}]
Out[36]=

Using "OutputForm" "Held" can result in quicker returns for large batches of functions (e.g., All), as each expression is shielded from kernel evaluation:

In[37]:=
all = ResourceFunction["SampleUnivariateFunctions"][All, "OutputForm" -> "Held"]; // EchoTiming
0.6864938`
In[38]:=
all = ResourceFunction["SampleUnivariateFunctions"][
    All]; // EchoTiming
107.6802886`

Auto-evaluation can cause some functions to change form, hiding the inclusion of certain subexpressions:

In[39]:=
SeedRandom[2];
myFunc = ResourceFunction["SampleUnivariateFunctions"][
  "IncludedSubexpressions" -> {Sqrt}]
Out[40]=

Though we requested a function containing Sqrt as a subexpression, it is absent in the result:

In[41]:=
FreeQ[myFunc, Sqrt]
Out[41]=

By viewing the FullForm, we see that what the front end renders as is actually Power[#,Rational[1,2]& at the expression level:

In[42]:=
FullForm[myFunc]
Out[42]=

Using "OutputForm" "Held" preserves the "original" form of expressions, prior to kernel evaluation:

In[43]:=
SeedRandom[2];
myHeldFunc = ResourceFunction["SampleUnivariateFunctions"][
  "IncludedSubexpressions" -> {Sqrt}, "OutputForm" -> "Held"]
Out[44]=

We now see the Sqrt we expect in the expression's FullForm:

In[45]:=
FullForm[myHeldFunc]
Out[45]=

TrueUnivariate (3) 

To get functions free of any symbolic parameters besides the independent variable z, use the option setting "TrueUnivariate"True:

In[46]:=
ResourceFunction["SampleUnivariateFunctions"][
 "TrueUnivariate" -> True]
Out[46]=

The setting "TrueUnivariate" False returns only functions that do contain extra parameters:

In[47]:=
ResourceFunction["SampleUnivariateFunctions"][
 "TrueUnivariate" -> False, "OutputForm" -> "PureFunction"]
Out[47]=

The default setting "TrueUnivariate" Automatic returns a mix of functions that do and do not contain extra parameters:

In[48]:=
ResourceFunction["SampleUnivariateFunctions"][5, "TrueUnivariate" -> Automatic]
Out[48]=

Properties and Relations (1) 

Use SeedRandom to get a repeatable sequence of functions:

In[49]:=
SeedRandom[2345];
ResourceFunction["SampleUnivariateFunctions"][]
Out[15]=
In[50]:=
ResourceFunction["SampleUnivariateFunctions"][]
Out[50]=
In[51]:=
ResourceFunction["SampleUnivariateFunctions"][]
Out[51]=

Neat Examples (2) 

Create a gallery of plots of random functions and their first derivatives. First get six random functions:

In[52]:=
funcList = ResourceFunction["SampleUnivariateFunctions"][6, "OutputForm" -> "PureFunction", "TrueUnivariate" -> True]
Out[52]=

Partition the function list and create a grid of plots:

In[53]:=
funcArray = Partition[funcList, 3];
Grid@MapAt[
  Plot[{#[x], Derivative[1][#][x]}, {x, 1, 2}, PlotLegends -> Placed[{"f", "f'"}, Below]] &, funcArray, {All, All}]
Out[2]=

Publisher

Wolfram|Alpha Math Team

Version History

  • 1.2.1 – 17 January 2023
  • 1.2.0 – 10 January 2023
  • 1.1.0 – 13 December 2022
  • 1.0.0 – 29 November 2022

Related Resources

License Information