home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / utility / sm22a.zip / SYMBMATH.H05 < prev    next >
Text File  |  1993-04-23  |  4KB  |  157 lines

  1.     7.3  Defining Your Own Functions, Procedures and Rules
  2.  
  3.     Anytime you find yourself using the same expression over and 
  4. over, you should turn it into a function.  
  5.     You can define your own functions for evaluation by
  6.  
  7.         f(x_) := x^2
  8.         f(x_) := if(isnumber(x), x^2)
  9.  
  10. On the first definition, when f() is called, it gives x^2, regardless
  11. what x is. On the second definition, when f() is called it gives x^2 
  12. if x is a number, or left unevaluated otherwise.
  13.     You can define the function by the immediate assignment =
  14. or the delayed assignment :=, but you cannot define a conditional
  15. function by the immediate assiment =. It is recommanded to define
  16. the function by the delayed assigment :=.
  17.     The pattern x_ should be only on the left side of the 
  18. assignment.
  19.     Here are some sample function definitions:
  20.            f(x_) := cos(x + pi/3)
  21.            g(x_, y_) := x^2 - y^2
  22. Once defined, functions can be used in expressions or in other 
  23. function definitions:
  24.            y = f(3.2)
  25.            z = g(4.1, -5.3)
  26.  
  27.     Example 7.3.1.
  28. Define a new function f(x)=x^2, then evaluate it.
  29.     Input:
  30. f(x_) := x^2
  31. f(-2)
  32. f(3)
  33. f(a)
  34. end
  35.     Output:
  36. f(x_) := x^2 
  37. 4
  38. 9
  39. a^2
  40.  
  41.     Input:
  42. f(x_) := if(isnumber(x), x^2)
  43. f(-2)
  44. f(3)
  45. f(a)
  46. end
  47.     Output:
  48. f(x_) := if(isnumber(x), x^2)
  49. 4
  50. 9
  51. f(a)
  52.  
  53.     To define a conditional function by
  54.  
  55.         f(x_) := if(x>0, x^2)
  56.         f(x_) := if(x>0, x^2, x)
  57.         f(x_) := x*(x<0) + x^2*(x>0)
  58.  
  59. On the first definition, when f() is called it gives x^2 if x>0, or 
  60. left unevaluated otherwise. On the second definition, when f() is 
  61. called it gives x^2 if x>0, x if x<=0, or left unevaluated otherwise. 
  62. On the last definition, when f() is called, it is evaluated regardless 
  63. what x is.
  64.     You cannot differentiate nor integrate the conditional function
  65. if you define it by if(). But you can do so if you define it by relative
  66. operators (e.g. the last definition).
  67.  
  68.     Input:
  69. f(x_) := if(x>0, x^2)
  70. f(2)
  71. f(a)
  72. end
  73.     Output:
  74. f(x_) := if(x > 0, x^2)
  75. 4
  76. f(-2)
  77. f(a)
  78.  
  79.     Input:
  80. f(x_) := if(x>0, x^2, x)
  81. f(2)
  82. f(-2)
  83. f(a)
  84. end
  85.     Output:
  86. f(x_) := if(x > 0, x^2, x)
  87. 4
  88. 2
  89. f(a)
  90.  
  91.  
  92.     Example 7.3.2. Define a conditional function
  93.  
  94.       / x       if x < 0
  95. f(x) =  0       if x = 0
  96.       \ x^2     if x > 0
  97.  
  98. then evaluate f(-2), f(0), f(3).
  99.     Input:
  100. f(x_) := x*(x<0)+x^2*(x>0)
  101. f(-2)
  102. f(0)
  103. f(3)
  104. f(a)
  105. d(f(t), t=3)
  106. end
  107.     Output:
  108. f(x_) := x*(x < 0) + x^2*(x > 0)
  109. -2
  110. 0
  111. 9
  112. a*(a < 0) + a^2*(a > 0)
  113. 6
  114.  
  115.     To define a recursion function.
  116.     Input:
  117. factorial(n_) := if(n > 1, (n-1)*factorial(n-1))
  118. factorial(1) := 1
  119. end
  120.  
  121.     To define a function as a procedure.
  122.     e.g. define a numerical integration procedure ninte() and 
  123. calculate integral of x^2 from x=1 to x=2 by call ninte().
  124.     Input:
  125. ninte(y_,x_,a_,b_) := block( num( dd=(b-a)/50,
  126.     aa=a+dd,
  127.     bb=b-dd,
  128.     y0=subs(y, x=a),
  129.     yn=subs(y, x=b),
  130.     ff=(sum(y,x,aa,bb,dd)+(y0+yn)/2)*dd),
  131.     ff )
  132. ninte(x^2,x,1,2)
  133. end
  134.  
  135.     Note that all variable within procedure are global. The mult-
  136. statement should be grouped by block(). The block() output only result
  137. of the last statement. The mult-line can be teminated by a comma (,).
  138.  
  139.     You can define transform rules. Defining rules is similar to 
  140. defining functions. In defining functions, all arguments must be simple
  141. variables, but in defining rules, the first argument can be a
  142. complicated expression. In this version of SymbMath the rules only have
  143. two arguments and one pattern.
  144.     e.g. define Laplace transform rules.
  145.     Input:
  146. laplace(sqrt(t_), t_) := sqrt(pi)/2/t^(3/2)
  147. laplace(1/sqrt(t_), t_) := sqrt(pi/t)
  148. laplace(sin(t_), t_) := 1/(t^2+1)
  149. laplace(sin(s), s)
  150. end
  151.     Output:
  152. laplace(sqrt(t_), t_) := sqrt(pi)/2/t^(3/2)
  153. laplace(1/sqrt(t_), t_) := sqrt(pi/t)
  154. laplace(sin(t_), t_) := 1/(t^2+1)
  155. 1/(s^2+1)
  156. end
  157.