Function Repository Resource:

GeneralizedSmoothStep

Source Notebook

A generalized sigmoidal interpolating polynomial

Contributed by: Jan Mangaldan

ResourceFunction["GeneralizedSmoothStep"][n,x]

is the generalized smoothstep function of order n at position x.

Details and Options

The generalized smoothstep function is defined as .
For nonnegative integer n and 0x1, ResourceFunction["GeneralizedSmoothStep"][n,x] is a polynomial of degree 2n+1.
For positions less than 0 or greater than 1, the result clamps to 0 and 1, respectively.
ResourceFunction["GeneralizedSmoothStep"] can be evaluated to arbitrary numerical precision.
ResourceFunction["GeneralizedSmoothStep"] automatically threads over lists.
The ResourceFunction["GeneralizedSmoothStep"]operation is common in computer graphics.

Examples

Basic Examples (2) 

Interpolate at a position on a step:

In[1]:=
ResourceFunction["GeneralizedSmoothStep"][3, 5]
Out[1]=

Show the smooth steps for multiple orders:

In[2]:=
Plot[Evaluate[
  Table[ResourceFunction["GeneralizedSmoothStep"][n, x], {n, 0, 3}]], {x, -1/2, 3/2}, PlotLegends -> Range[0, 3]]
Out[2]=

Scope (4) 

Evaluate at an exact position:

In[3]:=
ResourceFunction["GeneralizedSmoothStep"][3, 1/3]
Out[3]=

At a numeric position:

In[4]:=
ResourceFunction["GeneralizedSmoothStep"][3, 0.33]
Out[4]=

GeneralizedSmoothStep threads over lists:

In[5]:=
ResourceFunction["GeneralizedSmoothStep"][Range[0, 3], 2/3]
Out[5]=
In[6]:=
ResourceFunction["GeneralizedSmoothStep"][2, Subdivide[6]]
Out[6]=

Successive derivatives of GeneralizedSmoothStep can be expressed in terms of Piecewise:

In[7]:=
Table[Derivative[0, k][
ResourceFunction["GeneralizedSmoothStep"]][3, x], {k, 1, 8}]
Out[7]=

Plot successive derivatives:

In[8]:=
Plot[Evaluate[Table[Derivative[0, k][
ResourceFunction["GeneralizedSmoothStep"]][2, x], {k, 0, 3}]], {x, -1/
   2, 3/2}]
Out[8]=

Applications (1) 

Use GeneralizedSmoothStep to implement a "smooth" version of Hue (reference):

In[9]:=
SmoothHue[n_Integer?NonNegative, h_?NumberQ, s_ : 1, b_ : 1] /; 0 <= s <= 1 && 0 <= b <= 1 := RGBColor[
  b (1 - s + s ResourceFunction["GeneralizedSmoothStep"][n, Clip[Abs[Mod[6 h + {0, 4, 2}, 6] - 3] - 1, {0, 1}]])]
In[10]:=
SmoothHue[1, 1/2, 2/3, 3/4]
Out[10]=
In[11]:=
Manipulate[
 LinearGradientImage[SmoothHue[n, #] &, {300, 30}], {{n, 0}, 0, 5, 1},
  SaveDefinitions -> True]
Out[11]=

Properties and Relations (7) 

GeneralizedSmoothStep is continuous from the left at x=0:

In[12]:=
Limit[Derivative[0, 5][
ResourceFunction["GeneralizedSmoothStep"]][4, x], x -> 0, Direction -> "FromAbove"]
Out[12]=
In[13]:=
Limit[Derivative[0, 5][
ResourceFunction["GeneralizedSmoothStep"]][4, x], x -> 0, Direction -> "FromBelow"]
Out[13]=
In[14]:=
Derivative[0, 5][
ResourceFunction["GeneralizedSmoothStep"]][4, 0]
Out[14]=

GeneralizedSmoothStep is continuous from the right at x=1:

In[15]:=
Limit[Derivative[0, 5][
ResourceFunction["GeneralizedSmoothStep"]][4, x], x -> 1, Direction -> "FromAbove"]
Out[15]=
In[16]:=
Limit[Derivative[0, 5][
ResourceFunction["GeneralizedSmoothStep"]][4, x], x -> 1, Direction -> "FromBelow"]
Out[16]=
In[17]:=
Derivative[0, 5][
ResourceFunction["GeneralizedSmoothStep"]][4, 1]
Out[17]=

GeneralizedSmoothStep[0,x] is equivalent to Clip:

In[18]:=
Plot[{ResourceFunction["GeneralizedSmoothStep"][0, x], Clip[x, {0, 1}]}, {x, -1/2, 3/2}, PlotStyle -> {AbsoluteThickness[4], AbsoluteThickness[1]}]
Out[18]=

GeneralizedSmoothStep[1,x] is equivalent to the resource function SmoothStep:

In[19]:=
Plot[{ResourceFunction["GeneralizedSmoothStep"][1, x], ResourceFunction["SmoothStep"][x]}, {x, -1/2, 3/2}, PlotStyle -> {AbsoluteThickness[4], AbsoluteThickness[1]}]
Out[19]=

GeneralizedSmoothStep[2,x] is equivalent to the resource function SmootherStep:

In[20]:=
Plot[{ResourceFunction["GeneralizedSmoothStep"][2, x], ResourceFunction["SmootherStep"][x]}, {x, -1/2, 3/2}, PlotStyle -> {AbsoluteThickness[4], AbsoluteThickness[1]}]
Out[20]=

GeneralizedSmoothStep can be expressed in terms of InterpolatingPolynomial and Clip:

In[21]:=
gss1[n_, x_] := InterpolatingPolynomial[{PadRight[{{0}, 0}, n + 2], PadRight[{{1}, 1}, n + 2]}, Clip[x, {0, 1}]]
In[22]:=
With[{n = 4}, Plot[Evaluate[{ResourceFunction["GeneralizedSmoothStep"][n, x], gss1[n, x]}], {x, -1/2, 3/2}, PlotStyle -> {AbsoluteThickness[4], AbsoluteThickness[1]}]]
Out[22]=

GeneralizedSmoothStep can be expressed in terms of BetaRegularized and Clip:

In[23]:=
gss2[n_, x_] := BetaRegularized[Clip[x, {0, 1}], n + 1, n + 1]
In[24]:=
With[{n = 4}, Plot[Evaluate[{ResourceFunction["GeneralizedSmoothStep"][n, x], gss2[n, x]}], {x, -1/2, 3/2}, PlotStyle -> {AbsoluteThickness[4], AbsoluteThickness[1]}]]
Out[24]=

Possible Issues (3) 

GeneralizedSmoothStep is undefined for nonpositive integer orders:

In[25]:=
ResourceFunction["GeneralizedSmoothStep"][1/2, 1]
Out[25]=

GeneralizedSmoothStep is undefined for complex arguments:

In[26]:=
ResourceFunction["GeneralizedSmoothStep"][2, I]
Out[26]=

GeneralizedSmoothStep[n,x] is (n+1)th-order discontinuous at x=0 and x=1:

In[27]:=
With[{n = 4}, Plot[Derivative[0, n + 1][
ResourceFunction["GeneralizedSmoothStep"]][n, x], {x, -1/2, 3/2}]]
Out[27]=

Neat Examples (1) 

Use GeneralizedSmoothStep to demonstrate "ease-in / ease-out":

In[28]:=
Manipulate[
 Graphics[
  Text["Zoom!", {ResourceFunction["GeneralizedSmoothStep"][n, x], 0}], PlotRange -> {{-.1, 1.1}, {-.1, .1}}], {{n, 1}, 0, 5, 1}, {x, -1, 2, ControlType -> Animator, AnimationRunning -> False}]
Out[28]=

Version History

  • 2.0.0 – 20 March 2020
  • 1.0.0 – 21 November 2019

Source Metadata

Related Resources

License Information