home *** CD-ROM | disk | FTP | other *** search
- 7.3 Defining Your Own Functions, Procedures and Rules
-
- Anytime you find yourself using the same expression over and
- over, you should turn it into a function.
- You can define your own functions for evaluation by
-
- f(x_) := x^2
- f(x_) := if(isnumber(x), x^2)
-
- On the first definition, when f() is called, it gives x^2, regardless
- what x is. On the second definition, when f() is called it gives x^2
- if x is a number, or left unevaluated otherwise.
- You can define the function by the immediate assignment =
- or the delayed assignment :=, but you cannot define a conditional
- function by the immediate assiment =. It is recommanded to define
- the function by the delayed assigment :=.
- The pattern x_ should be only on the left side of the
- assignment.
- Here are some sample function definitions:
- f(x_) := cos(x + pi/3)
- g(x_, y_) := x^2 - y^2
- Once defined, functions can be used in expressions or in other
- function definitions:
- y = f(3.2)
- z = g(4.1, -5.3)
-
- Example 7.3.1.
- Define a new function f(x)=x^2, then evaluate it.
- Input:
- f(x_) := x^2
- f(-2)
- f(3)
- f(a)
- end
- Output:
- f(x_) := x^2
- 4
- 9
- a^2
-
- Input:
- f(x_) := if(isnumber(x), x^2)
- f(-2)
- f(3)
- f(a)
- end
- Output:
- f(x_) := if(isnumber(x), x^2)
- 4
- 9
- f(a)
-
- To define a conditional function by
-
- f(x_) := if(x>0, x^2)
- f(x_) := if(x>0, x^2, x)
- f(x_) := x*(x<0) + x^2*(x>0)
-
- On the first definition, when f() is called it gives x^2 if x>0, or
- left unevaluated otherwise. On the second definition, when f() is
- called it gives x^2 if x>0, x if x<=0, or left unevaluated otherwise.
- On the last definition, when f() is called, it is evaluated regardless
- what x is.
- You cannot differentiate nor integrate the conditional function
- if you define it by if(). But you can do so if you define it by relative
- operators (e.g. the last definition).
-
- Input:
- f(x_) := if(x>0, x^2)
- f(2)
- f(a)
- end
- Output:
- f(x_) := if(x > 0, x^2)
- 4
- f(-2)
- f(a)
-
- Input:
- f(x_) := if(x>0, x^2, x)
- f(2)
- f(-2)
- f(a)
- end
- Output:
- f(x_) := if(x > 0, x^2, x)
- 4
- 2
- f(a)
-
-
- Example 7.3.2. Define a conditional function
-
- / x if x < 0
- f(x) = 0 if x = 0
- \ x^2 if x > 0
-
- then evaluate f(-2), f(0), f(3).
- Input:
- f(x_) := x*(x<0)+x^2*(x>0)
- f(-2)
- f(0)
- f(3)
- f(a)
- d(f(t), t=3)
- end
- Output:
- f(x_) := x*(x < 0) + x^2*(x > 0)
- -2
- 0
- 9
- a*(a < 0) + a^2*(a > 0)
- 6
-
- To define a recursion function.
- Input:
- factorial(n_) := if(n > 1, (n-1)*factorial(n-1))
- factorial(1) := 1
- end
-
- To define a function as a procedure.
- e.g. define a numerical integration procedure ninte() and
- calculate integral of x^2 from x=1 to x=2 by call ninte().
- Input:
- ninte(y_,x_,a_,b_) := block( num( dd=(b-a)/50,
- aa=a+dd,
- bb=b-dd,
- y0=subs(y, x=a),
- yn=subs(y, x=b),
- ff=(sum(y,x,aa,bb,dd)+(y0+yn)/2)*dd),
- ff )
- ninte(x^2,x,1,2)
- end
-
- Note that all variable within procedure are global. The mult-
- statement should be grouped by block(). The block() output only result
- of the last statement. The mult-line can be teminated by a comma (,).
-
- You can define transform rules. Defining rules is similar to
- defining functions. In defining functions, all arguments must be simple
- variables, but in defining rules, the first argument can be a
- complicated expression. In this version of SymbMath the rules only have
- two arguments and one pattern.
- e.g. define Laplace transform rules.
- Input:
- laplace(sqrt(t_), t_) := sqrt(pi)/2/t^(3/2)
- laplace(1/sqrt(t_), t_) := sqrt(pi/t)
- laplace(sin(t_), t_) := 1/(t^2+1)
- laplace(sin(s), s)
- end
- Output:
- laplace(sqrt(t_), t_) := sqrt(pi)/2/t^(3/2)
- laplace(1/sqrt(t_), t_) := sqrt(pi/t)
- laplace(sin(t_), t_) := 1/(t^2+1)
- 1/(s^2+1)
- end
-