Xah Talk Show 2025-05-06 Ep653 Wolfram Language, Define a Function, via Function (lambda) and pattern matching

xah talk show 2025-05-06 1e04f
xah talk show 2025-05-06 1e04f
(* most simple way to define a function  *)
Function[x, x+1][3]
(* 4 *)

(* HHHH------------------------------ *)

Clear[myfun]
myfun = Function[x, x+1];
myfun[3]
(* 4 *)

(* HHHH------------------------------ *)

Clear[myfun]
myfun = Function[{x,y}, x+y];
myfun[3,4]
(* 7 *)

(* HHHH------------------------------ *)

(* using Function to define a function has a limitation,
on the function parameters.
namely, you can only have ordered param, rest param.
but you cannot have default values, optional param, or named param.
 *)

(* HHHH------------------------------ *)

(* in order to have default values, optional param, or named param.
you need to define function by using pattern matching. *)

Clear[myfun];
myfun[x_] := x+1
(* SetDelayed[myfun[Pattern[x, Blank[]]], Plus[x, 1]] *)
myfun[3]
(* 4 *)

(* SetDelayed sets a pattern, whenever left-hand-side match, replace it by right-hand-side  *)

(* the full form is this *)

(* HHHH------------------------------ *)

(* this is how to show the full form syntax
of some expression *)
FullForm[Hold[myfun[x_] := x+1]]

(* HHHH------------------------------ *)

(* practical examples of definite a function via pattern matching *)

Clear[myfun];
myfun[x_,y_] := x+y
myfun[3,4]
(* 7 *)

(* HHHH------------------------------ *)

(*  example of default vaule *)
Clear[myfun];
myfun[x_, y_:4] := x+y
myfun[3]
(* 7 *)

(* HHHH------------------------------ *)

(*  example of 2 default vaules, and
how, you cannot have the first param as optional.
this showcase how
param with default values is not completely equivalent to optional param.
 *)
Clear[myfun];
myfun[x_:3, y_:4] := x+y
myfun[]
(* 7 *)

(* if we want y to be 5, and use default value for x,
this wont work
 *)
myfun[,5]

(* HHHH------------------------------ *)

(*  example of optional named params. *)

Clear[ myfun ];
Options[ myfun ] = { x -> 3, y -> 4 };
myfun[OptionsPattern[]] := OptionValue[x] + OptionValue[y]
myfun[]
(* 7 *)

Clear[ myfun ];
Options[ myfun ] = { NumberOfPlotPoints -> 30, Color -> Red,
launchMissle -> False
 };
myfun[OptionsPattern[]] := OptionValue[myOpt1] + OptionValue[myOpt2]
myfun[]
(* 7 *)
xah talk show 2025-05-06 31351
xah talk show 2025-05-06 31351
Plot3D[Sin[x y], {x, 0, 2 Pi}, {y, -2 Pi, 2 Pi},
PlotRange -> {{0, 2 Pi}, {0, 2 Pi}, {-1.1, 1.1}}, ViewPoint -> {2, -2, 1.},
PlotPoints -> 50
]
mygra = Table[
   Plot3D[Sin[x  y + t], {x, -2  Pi, 2  Pi}, {y, -2  Pi, 2  Pi},
    PlotRange -> {{0, 2  Pi}, {0, 2  Pi}, {-1.1, 1.1}},
    ViewPoint -> {4, -2, 2.}, PlotPoints -> 50], {t, 0, 2  Pi,
    2  Pi/30}];
ListAnimate[mygra]
Clear[NegativePedalPlot]

NegativePedalPlot::usage =
"NegativePedalPlot[{xf,yf}, {min, max, step}, {x0, y0}]
Draws the negative pedal lines of the parametric curve {xf,yf} at points from min to max with step
xf and yf must be pure functions with head Function.
Example:
NegativePedalPlot[ {Function[x, Cos[x]], Function[x, Sin[x]]}, {0, 2.*Pi, (2.*Pi)/40.}, {0.8, 0.}, PlotRange -> {{-1, 1}*3, {-1, 1}*3}]";

NegativePedalPlot[{xf_Function, yf_Function}, {tmin_, tmax_, dt_}, {a_, b_}, opts___Rule] :=
 Module[{curvePoints, curvePointsGP, pedalPointGP, linesToCurveGP,
   negativePedalLinesGP},
  curvePoints =
   Map[Function[x, {xf[x], yf[x]}], Range[tmin, tmax, dt]];
  curvePointsGP =
   Map[Function[{Red, PointSize[.02], Point[#]}], curvePoints];
  pedalPointGP = {Blue, PointSize[.02], Point[{a, b}]};
  linesToCurveGP = Map[Function[Line[{{a, b}, #}]], curvePoints];
  negativePedalLinesGP =
   Map[Function[
     Line[{100  Reverse[
          Normalize[# - {a, b}] {1, -1}] + #, -100  Reverse[
          Normalize[# - {a, b}] {1, -1}] + #}]], curvePoints];
  Graphics[{negativePedalLinesGP, curvePointsGP, pedalPointGP}, opts]]

(* HHHH------------------------------ *)

NegativePedalPlot[
{ Function[{x}, x], Function[{x}, x^2/4 ] },
{-7, 7, .25},
{0, 1},
Axes -> True, AspectRatio -> Automatic,
PlotRange -> {{-7, 7}, {-1, 18}}]