home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / e / e032 / 3.ddi / FILES / PROGRAMM.PAK / MAKEFUNC.M < prev    next >
Encoding:
Text File  |  1992-07-29  |  1.5 KB  |  56 lines

  1.  
  2. (*********************************************************************
  3.  
  4.         Adapted from
  5.         Roman E. Maeder: Programming in Mathematica,
  6.         Second Edition, Addison-Wesley, 1991.
  7.  
  8.  *********************************************************************)
  9.  
  10.  
  11. BeginPackage["Programming`MakeFunctions`"]
  12.  
  13. StepFunction::usage = "StepFunction[f, a, x0, b] defines rules for f
  14.     such that f[x] = a for x <= x0, f[x] = b for x > x0."
  15.  
  16. LinearFunction::usage = "LinearFunction[f, a, x0, x1, b] defines rules for f
  17.     such that f[x] = a for x <= x0, f[x] = b for x >= x1 and
  18.     f increases linearly from a to b between x0 and x1."
  19.  
  20. MakeRule::usage = "MakeRule[f, x, rhs] globally defines the rule f[x_] := rhs."
  21.  
  22. MakeRuleConditional::usage = "MakeRuleConditional[f, x, rhs, cond]
  23.     globally defines the rule f[x_] := rhs /; cond."
  24.  
  25.  
  26. Begin["`Private`"]
  27.  
  28. SetAttributes[MakeRule, HoldAll]
  29.  
  30. MakeRule[f_Symbol, var_Symbol, rhs_] :=
  31.     f[var_] := rhs
  32.  
  33. SetAttributes[MakeRuleConditional, HoldAll]
  34.  
  35. MakeRuleConditional[f_Symbol, var_Symbol, rhs_, condition_] :=
  36.     (f[var_] := rhs /; condition)
  37.  
  38.  
  39. StepFunction[f_Symbol, a_, x0_, b_] := (
  40.     MakeRuleConditional[f, x, a, x <= x0];
  41.     MakeRuleConditional[f, x, b, x > x0]
  42.     )
  43.  
  44. LinearFunction[f_Symbol, a_, x0_, x1_, b_] :=
  45.     With[{slope = (b-a)/(x1-x0)},
  46.         MakeRuleConditional[f, x, a, x <= x0];
  47.         MakeRuleConditional[f, x, a + (x-x0) slope, x0 < x < x1];
  48.         MakeRuleConditional[f, x, b, x >= x1]
  49.     ]
  50.  
  51. End[]
  52.  
  53. Protect[StepFunction, LinearFunction, MakeRule, MakeRuleConditional]
  54.  
  55. EndPackage[]
  56.