home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS - Coast to Coast
/
simteldosarchivecoasttocoast2.iso
/
calculat
/
sm30a.zip
/
SYMBMATH.H35
< prev
next >
Wrap
Text File
|
1993-11-07
|
6KB
|
178 lines
4.3 Defining Your Own Functions, Procedures and Rules
4.3.1 Defining Your Own Functions
Anytime when you find yourself using the same expression over
and over, you should turn it into a function.
You can define your own functions by
f(x_) := x^2
Here are some sample function definitions:
f(x_) := cos(x + pi/3)
g(x_, y_) := x^2 - y^2
The arguement in the definition should be the pattern x_.
Otherwise f() works only for a specific symbolic value, e.g. x when
defining f(x):=x^2. The pattern x_ should be only on the left side of
the assignment.
Once defined, functions can be used in expressions or in other
function definitions:
y := f(3.2)
z := g(4.1, -5.3)
Example 4.3.1.
Define a new function for x^2, then evaluate it.
IN: g(x) := x^2
IN: g(2), g(a), g(x)
OUT: g(2), g(a), x^2 # work only for a symbolic value x
IN: f(x_) := x^2
IN: f(2), f(a)
OUT: 4, a^2 # work for any value
4.3.1.1 Defining Conditional Functions
You can define a conditional function by the if() function:
f(x_) := if(x>0 then 1)
f(x_) := if(x>0 then x^2 else x)
or by inequalities:
f(x_) := x>0
f(x_) := (x>0) * x^2 + (x<=0) * x
On the first definition by if(), when f() is called it gives 1 if x>0,
or left unevaluated otherwise. On the second definition by the if(), when
f() is called it gives x^2 if x>0, x if x<=0, or left unevaluated
otherwise. On the third definision by the inequality, when f() is called,
it gives 1 for x>0, 0 for x<=0, or x>0 for symbolic value of x.
On the last definition, when f() is called, it is evaluated for any
numeric or symbolic value of x.
Remember that the words "then" and "else" can be replaced by
comma ,.
You cannot differentiate nor integrate the conditional function
defined by if(), but you can do the conditional functions defined by
inequalities.
You can define a function evaluated only for numbers by
f(x_) := if(isnumber(x) then x^2)
This definition is different from the definition by f(x_) := x^2. On the
latter, when f() is called, it gives x^2, regardless whatever x is. On the
former, when f() is called, it gives x^2 if x is a number, or left
unevaluated otherwise.
Example 4.3.2.
evaluate to x^2 only if x is number, by defining a conditional function.
IN: f(x_) := if(isnumber(x) then x^2)
IN: f(2), f(a)
OUT: 4, f(a)
IN: f(x_) := if(x>0 then x^2)
IN: f(2), f(-2), f(a)
OUT: 4, f(-2), f(a)
IN: f(x_) := if(x>0 then x^2 else x)
IN: f(2), f(-2), f(a)
OUT: 4, 2, f(a)
4.3.1.2 Defining Case Functions
You can define the case function by different pattern name. The
case function is similar to the case statement in BASIC language.
Example:
IN: f(x_) := if( x > 0 and x < 1 then 1)
IN: f(u_) := if( u > 1 and u < 2 then 2)
IN: f(v_) := if( v > 2 and v < 3 then 3)
IN: f(0.2), f(1.2), f(2.2)
OUT: 1, 2, 3
4.3.1.3 Defining Piece-wise Functions
You can define a piece-wise function.
Example 4.3.3.
define
/ x if x < 0
f(x) = 0 if x = 0
\ x^2 if x > 0
then evaluate f(-2), f(0), f(3), f(a), f'(x), diff(f(x), x=3).
IN: f(x_) := x*(x<0)+x^2*(x>0)
IN: f(-2), f(0), f(3), f(a)
OUT: -2, 0, 9, (a < 0) a + (a > 0) a^2
IN: f'(x)
OUT: (x < 0) + 2 x (x > 0)
IN: diff(f(x), x=3)
OUT: 6
4.3.1.4 Defining Recursion Functions
You can define a recursion function.
Example.
IN: factoria(n_) := if(n > 1, (n-1)*factoria(n-1))
IN: factoria(1) := 1
4.3.1.5 Defining Multi-Value Functions
You can define a function with the multi function values.
Example.
IN: squreroot(x_) := [sqrt(x), -sqrt(x)]
IN: squreroot(4)
OUT: [2, -2]
Anytime when you find yourself using the same definition over
and over, you should turn it into a library.
You can make your defined function as if the built-in function,
by saving your definition into disk file as a library with the function
name plus extension .(x) as the filename. e.g. saving above factoria
function as the factoria.(x) file (see Section 3.6 Libraies and Packages).
4.5.2 Defining Your Own Procedures
You can define a function as a procedure by
f(x_) := block(command1, command2, ..., commadN)
f(x_) := block(command1, command2, ..., commandN, local(a))
By default, all variables within procedure are global, except for
variables declared by local(). The mult-statement should be grouped by
block(). The block() only outputs the result of the last statement or the
second last one as its value. The mult-line can be teminated by a comma,
(not by a comma and a blank space). Local() must be the last one in
block().
Example 4.5.2.
define a numeric integration procedure ninte() and calculate integral
of x^2 from x=1 to x=2 by call ninte().
IN: ninte(y_,x_,a_,b_) := block( numeric:=on,
dd:=(b-a)/50,
aa:=a+dd,
bb:=b-dd,
y0:=subs(y, x = a),
yn:=subs(y, x = b),
(sum(y,x,aa,bb,dd)+(y0+yn)/2)*dd,
local(dd,aa,bb,y0,yn) )
IN: ninte(x^2, x from 1 to 2)
4.5.3 Defining Your Own Rules
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.
Example 4.5.3.1
define log rules.
IN: log(x_ * y_) := log(x) + log(y)
IN: log(x_ ^ n_) := n*log(x)
IN: log(a*b)
OUT: log(a) + log(b)
Example 4.5.3.2
IN: sin(-x_) := -sin(x)
IN: sin(-a)
OUT: -sin(a)
Example 4.5.3.3
define the trig simplification rules.
IN: simplify(sin(x_)^2, x_) := 1/2*(1-cos(x))
IN: simplify(sin(x)^2,x)
OUT: 1/2 (1 - cos(x))
Example 4.5.3.4
define Laplace transform rules.
IN: laplace(sin(t_), t_) := 1/(t^2+1)
IN: laplace(sin(s), s)
OUT: 1/(s^2 + 1)