home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-04-23 | 41.3 KB | 1,584 lines |
- SymbMath 2.2: A Symbolic Calculator with Learning
- (Version 2.2)
-
- by Dr. Weiguang HUANG
- Dept. Analytical Chemsitry, University of New South Wales,
- Kensington, Sydney, NSW 2033, Australia
- Phone: 61-2-697-4643
- Fax: 61-2-662-2835
- E-mail: w.huang@unsw.edu.au
-
- Copyright (C) 1990-1993
- April 15, 1993
-
- This is Part Two of SymbMath manual. The SymbMath manual has
- three parts in the files SymbMath.DOC, SymbMath.DO2 and SymbMath.DO3.
-
-
- 7. Examples
-
- In the following examples, a line of "Input:" means that
- typing the program in the Editor, then leaving the Editor by <Esc>,
- finally running the program by the command "Run", while a line of
- "Output:" means outputs in the "Output" window. # is a comment
- statement. All examples should be terminated by the end statement,
- but the end statement may omit in the output of the following examples.
-
-
- 7.1 Simplification and Assumption
-
- SymbMath automatically simplifies the output expression.
- Users can further simplify it by using the built-in variable "last"
- in a singe line again and again until they are happy.
-
- Example 7.1.1. Reduce sqrt(x^2).
- Input:
- sqrt(x^2)
- end
- Output:
- x*sgn(x)
- end
-
- A first way to reduce this output expression is to substitute
- sgn(x) by 1 if x is positive.
- Input:
- sqrt(x^2)
- subs(last, sgn(x)=1)
- end
- Output:
- x*sgn(x)
- x
- end
- A second way to reduce the output expression is to assume
- x>0 before evaluation. On this way, all x is affected. The first
- method is local simplification, but the second method is global
- simplification. If users declare the variable x is positive or
- negative by
-
- assume(x > 0)
- assume(x < 0)
-
- the output expression is simpler than that if users do not declare it.
-
- Example 7.1.2. Assume x>0 before reduce sqrt(x^2).
- Input:
- assume(x >0)
- sqrt(x^2)
- end
- Output:
- assumed
- x
- end
-
- Users can assume the variable b is even, odd, integer, real
- number, positive or negative by
-
- iseven(b) := 1 # assume b is even
- isodd(b) := 1 # assume b is odd
- isinteger(b) := 1 # assume b is integer
- isreal(b) := 1 # assume b is real
- isnumber(b) := 1 # assume b is number
- islist(b) := 1 # assume b is a list
- isfree(y,x) := 1 # assume y is free of x
- sgn(b) := 1 # assume b is positive
- sgn(b) := -1 # assume b is negative
-
- All variables are complex by default.
-
- Example 7.1.3.
- Input:
- isreal(b) := 1
- sqrt(b^2)
- end
- Output:
- isreal(b) := 1
- abs(b)
- end
-
- Example 7.1.4. Set f1=x^2+y^3, then put f1 into sin(f1)+
- cos(2*f1) and assign the last output to f2.
- Input:
- f1=x^2+y^3
- sin(f1)+cos(2*f1)
- f2=last
- end
- Output:
- f1 = x^2 + y^3
- sin(x^2 + y^3) + cos(2 (x^2 + y^3))
- f2 = sin(x^2 + y^3) + cos(2*(x^2 + y^3))
- end
-
- where a special word "last" stands for the last output, e.g. here
- "last" is sin(x^2+y^3)+cos(2*(x^2+y^3)).
-
-
- 7.2 Calculation
-
- SymbMath gives the exact value of calculation when the switch
- numerical=off (default), or the approximate value of calculation when
- the switch numerical=on or by the function num().
- SymbMath can manipulate units as well as numbers, be used as
- a symbolic calculator, and do exact computation. The range of real
- numbers is from -infinity to infinity, e.g. ln(-inf), exp(inf+pi*i),
- etc. SymbMath contains many built-in algorithms for performing
- numerical calculations when the switch numerical=on, e.g. ln(-9), i^i,
- (-2.3)^(-3.2), 2^3^4^5^6^7^8^9, etc.
- Warming that SymbMath only gives a principle value if there
- are multi-values, except for the function solve().
-
- Example 7.2.1. Find the values of sin(x) when x=pi/2
- and x=90 degree.
- Input:
- num(sin(pi/2))
- num(sin(90*degree))
- end
- Output:
- 1
- 1
- end
-
- Example 7.2.2. Set the units converter from the minute to
- the second, then calculate numbers with different units.
- Input:
- minute=60*second
- v=2*meter/second
- t=2*minute
- d0=10*meter
- v*t+d0
- end
- Output:
- 250 meter
-
- Example 7.2.3
- Input:
- 1/2+1/3
- end
- Output:
- 5/6
-
- Example 7.2.4. Assign sqrt(x) to z, then evaluate z
- when x=3 and y=4.
- Input:
- z=sqrt(x)
- subs(z, x=3) # evaluate z by substituting x=3
- x=4 # assign 4 to x
- z # evaluate z
- end
- Output:
- z = sqrt(x)
- sqrt(3)
- x=4
- 2
-
- Note that after assignment by x=4, x should be cleared from
- memory by clear(x) before differentiation of the new function.
- Otherwise the values of x still is 4 until new values assigned. If
- evaluating z by the function subs(z, x=3), the variable x is
- automatically cleared after evaluation, i.e. the variable x in subs()
- is local variable. This rule applies to other functions.
-
-
- 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
-
- 7.4 Limits
-
- SymbMath finds real or complex limits, and discontinuity
- when x approaches to x=x0 by functions
-
- subs(f, x=x0)
- lim(f, x=x0)
-
- First use subs() to find limits, if the result is undefined
- (indeterminate forms, e.g. 0/0, inf/inf, 0*inf, and 0^0), then use
- lim() to try again; if the result is discont, then use the one-side
- limit by c+zero or c-zero.
-
- Example 7.4.1. Find limits of types 0/0 and inf/inf.
- Input:
- p=(x^2-4)/(2*x-4)
- subs(p, x=2)
- lim(p, x=2)
- subs(p, x=inf)
- lim(p, x=inf)
- end
- Output:
- p = (x^2 - 4)/(2 x - 4)
- undefined
- 2
- undefined
- inf
-
- The "discont" (discontinuity) means that the expression has
- a discontinuity and only has the one-sided limit value at x=x0. Users
- should use x0+zero or x0-zero to find the one-sided limit. The
- f(x0+zero) or f(x0-zero) is the right-sided limit or left-sided limit
- as approaching x0 from positive (+inf) or negative (-inf) direction,
- respectively, i.e. limit as zero -> 0.
- SymbMath find a left-sided or right-sided limit when x
- approaches to x0 from positive (+inf) or negative (-inf) direction at
- discontinuity by functions
-
- subs(f, x=x0+zero)
- subs(f, x=x0-zero)
- lim(f, x=x0+zero)
- lim(f, x=x0-zero)
-
- Example 7.4.2. Find the left-sided and right-sided limits of
- y=exp(1/x), (i.e. when x approaches 0 from positive and negative
- directions).
- Input:
- y=exp(1/x)
- subs(y, x=0)
- subs(y, x=0+zero)
- subs(y, x=0-zero)
- end
- Output:
- y = exp(1/x)
- discont
- inf
- 0
-
- The built-in constants of inf or -inf, zero or -zero, and
- discont or undefined can be used as numbers in calculation of
- expressions or functions.
-
- Example 7.4.3.
- Input:
- 1/sgn(0)
- 1/sgn(zero)
- end
- Output:
- discont
- 1
-
-
- 7.5 Differentiation
-
- SymbMath differentiates an expression expr by functions
-
- d(expr, x)
- d(expr, x, order)
- d(expr, x=x0)
- d(expr, x=x0, order)
-
- Example 7.5.1. Differentiate x^(x^x).
- Input:
- d(x^(x^x), x)
- end
- Output:
- x^(x^x)*(x^(-1 + x) + x^x*ln(x)*(1 + ln(x)))
-
- Example 7.5.2. Differentiate the expression f=sin(x^2+y^3)+
- cos(2*(x^2+y^3)) with respect to x, and with respect to both x and y.
- Input:
- f=sin(x^2+y^3)+cos(2*(x^2+y^3))
- d(f, x)
- d(d(f, x), y)
- end
- Output:
- f = sin(x^2 + y^3) + cos(2*(x^2 + y^3))
- 2*x*cos(x^2 + y^3) - 4*x*sin(2*(x^2 + y^3))
- -6*x*y^2*sin(x^2 + y^3) - 12*x*y^2*cos(2*(x^2 + y^3))
-
- To define a derivative.
- Input:
- d(si(x_),x_) := sin(x)/x
- d(si(t),t)
- end
- Output:
- d(si(x_),x_) := sin(x)/x
- sin(t)/t
-
- Or the package d.sm is included before finding the derviatives.
- Example:
- Input:
- include 'd.sm'
- d(si(t),t)
- end
- Output:
- done
- sin(t)/t
-
- If SymbMath cannot find some derivatives, you should include
- the package 'd.sm' in your program or in the initial package 'init.sm'.
-
-
- 7.6 Integration
-
- SymbMath system itself can find integrals of x^m*e^(x^n),
- x^m*e^(-x^n), e^((a*x+b)^n), e^(-(a*x+b)^n), x^m*ln(x)^n, ln(a*x+b)^n,
- etc., where m and n are any real number.
- The package 'inte.sm' and/or 'd.sm' should be included before
- symbolic integration so that it become more powerful on integration. It
- is recommended that to expand the integrand by the function expand()
- and/or by setting the switch expand=on before symbolic integration.
- If symbolic integration fails, the user can define the simple
- integral or derivative, then evaluates the integration again (see
- 7.13 Learning from User).
- If the user wants numerical integration by ninte(), the
- package 'NInte.sm' should be included before doing numerical
- integration (see 8.5 Numeric Integration Package).
-
-
- 7.6.1 Indefinite Integration
-
- SymbMath finds indefinite integrals by functions
-
- inte(expr, x)
- Note that the arbitrary constant is not represented.
-
- Example 7.6.1. Find indefinite integrals.
- Input:
- inte(sinh(x)*e^sinh(x)*cosh(x), x)
- inte(sinh(x)^2*cosh(x), x)
- inte(x^1.5*exp(x), x)
- end
- Output:
- -e^sinh(x) + sinh(x)*e^sinh(x)
- (1/3)*sinh(x)^3
- ei(1.5, x)
-
- Example 7.6.2. Find indefinite double integrals.
- Input:
- inte(inte(x*y, x), y)
- end
- Output:
- (1/4)*x^2*y^2
-
- Example 7.6.3. Find the line integral.
- Input:
- x=2*t
- y=3*t
- z=5*t
- u=x+y
- v=x-y
- w=x+y+z
- inte((u*d(u,t)+v*d(v,t)+w*d(w,t), t)
- end
- Output:
- 63*t^2
-
- Example 7.6.4. Find the integral of sin(x)/x by the mean of
- the 'inte.sm' package.
- Input:
- include('inte.sm')
- inte(sin(x)/x, x)
- end
- Output:
- done
- si(x)
-
- Defining an integral is similar to defining a rule.
- Example 7.6.5
- Input:
- inte(sin(x_)/x_, x_) := si(x)
- inte(sin(t)/t, t)
- end
- Output:
- inte(sin(x_)/x_, x_) := si(x)
- si(t)
-
- 7.6.2 Definite Integration
-
- SymbMath finds definite integrals by functions
- inte(expr, x, xmin, xmax)
- Example 7.6.6. Find the definite integral of y=exp(1-x) with
- respect to x taken from x=0 to x=infinity.
- Input:
- inte(exp(1-x), x from 0 to inf)
- end
- Output:
- e
-
- Example 7.6.7. Discontinuous integration of 1/x^2 and 1/x^3
- with discontinuity at x=0.
- Input:
- inte(1/x^2, x from -1 to 2)
- inte(1/x^3, x from -1 to 1)
- end
- Output:
- inf
- 0
-
-
- 7.7 Equations
- 7.7.1 Algebraic Equations
-
- The equations can be operated (e.g. +, -, *, /, ^, expand(),
- diff(), inte()). The operation is done on both sides of the equation,
- as by hand. SymbMath is able to find roots of a polynomial, algebraic
- equations, systems of equations, differential and integral equations.
-
- Example 7.7.1. Solve an equation
- sqrt(x+2*k) - sqrt(x-k) === sqrt(k),
- then check the solution by substituting the root into the equation.
- Input:
- eq1=(sqrt(x + 2*k) - sqrt(x - k) === sqrt(k))
- eq1^2
- expand(last)
- last-k-2*x
- last/(-2)
- last^2
- expand(last)
- last-x^2+2*k^2
- last/k
- subs(eq1, x=right(last))
- end
- Output:
- eq1=(sqrt(x + 2*k) - sqrt(x - k) === sqrt(k))
- ((2*k + x)^0.5 - ((-k) + x)^0.5)^2 === k
- 2*x + k + (-2)*(2*k + x)^0.5*((-k) + x)^0.5 === k
- (-2)*(2*k + x)^0.5*((-k) + x)^0.5 === (-2)*x
- (2*k + x)^0.5*((-k) + x)^0.5 === x
- (2*k + x)*((-k) + x) === x^2
- (-2)*k^2 + k*x + x^2 === x^2
- k*x === 2*k^2
- x === 2*k
- k^0.5 === k^0.5
-
- SymbMath can solve many algebraic equations step by step, as
- by hand. This method is useful on teaching, e.g. to show students how
- to solve equations.
- SymbMath has the built-in function
-
- solve(expr1 === expr2, x)
- solve([expr1 === expr2, expr3 === expr4], [x, y])
-
- to solve a polynomial and systems of linear equations on one step.
- It is recommended to set the switch expand=on when solve the
- complicated equations. All of the real and complex roots of the
- equation will be found by solve(). The function solve() outputs a
- list of roots when there are multi-roots. Users can get one of roots
- from the list, (see 7.9 List, Array, Vector and Matrices).
- Users can get the left side of the equation by
-
- left(left_side === right_side)
-
- or the right side by
-
- right(left_side === right_side)
-
- Example 7.7.2. Solve a+b*x+x^2 === 0, save the root to x.
- Input:
- eq1 = (a+b*x+x^2===0)
- solve(eq1, x)
- x=right(last)
- x[1]
- x[2]
- end
- Output:
- eq1 = (a+b*x+x^2 === 0)
- x === [-b/2 + sqrt((b/2)^2 - a), -b/2 - sqrt((b/2)^2 - a)]
- x = [-b/2 + sqrt((b/2)^2 - a), -b/2 - sqrt((b/2)^2 - a)]
- -b/2 + sqrt((b/2)^2 - a)
- -b/2 - sqrt((b/2)^2 - a)
-
-
- Example 7.7.3. Solve x^3+x^2+x+5 === 2*x+6.
- Input:
- num(solve(x^3+x^2+x+5 === 2*x+6, x))
- end
- Output:
- x === [1, -1, -1]
-
- Function solve() not only solve for a simple variable x
- but also for an unknown function, e.g. ln(x).
-
- Example 7.7.4. Solve the equation for ln(x).
- Input:
- solve(ln(x)^2+5*ln(x) === -6, ln(x))
- exp(last)
- end
- Output:
- ln(x) === [-2, -3]
- x === [exp(-2), exp(-3)]
-
- Example 7.7.5. Rearrange the equations.
- Input:
- f = [x+y === 3+a+b, x-y === 1+a-b]
- solve(f, [x,y])
- solve(f, [a,b])
- solve(f, [a,y])
- solve(f, [x,b])
- end
- Output:
- f = [x + y === 3 + a + b, x - y === 1 + a - b]
- [x === -1/2*(-4 - 2 a), y === -1/2*(-2 - 2 b)]
- [a === -1/2*(4 - 2 x), b === -1/2*(2 - 2 y)]
- [b === -1/2*(2 - 2 y), x === -1/2*(-4 - 2 a)]
- [a === 1/2*(-4 + 2 x), y === 1/2*(2 + 2 b)]
-
-
-
- 7.7.2 Differential Equations
-
- SymbMath can solve the variables separable differential
- equations:
- y(x)*d(y(x), x) === f(x)
- by integration inte().
- Example: 7.7.2.1
- Input:
- y(x)*d(y(x), x) === sin(x)
- inte(last, x)
- end
- Output:
- y(x)*d(y(x), x) === sin(x)
- 1/2*y(x)^2 === constant - cos(x)
-
- The function
-
- dsolve(d(y)/d(x) === f(x,y), y)
-
- can solve the first order variables separable and linear differential
- equations
-
- d(y)/d(x) === h(x)
- d(y)/d(x) === g(y)
- d(y)/d(x) === g(y)*x
- d(y)/d(x) === g(x)*y
- d(y)/d(x) === g(x)*y+h(x)
-
- on one step. Notice that d(y)/d(x) must be alone on the left hand
- side of the equation. It is recommended to set the switch
- expand=on and/or to include 'inte.sm' when solving the complicated
- differential equations.
-
- Example 7.7.2.2 Solve d(y)/d(x)=== -sinh(x)*y+sinh(x)*cosh(x).
- Input:
- dsolve(d(y)/d(x) === -sinh(x)*y+sinh(x)*cosh(x), y)
- end
- Output:
- y === (constant + exp(cosh(x)) - cosh(x)*exp(cosh(x)))*exp(-cosh(x))
-
- Example 7.7.2.3 Solve differential equations. If the result is
- a polynomial, then rearrange the equation by solve().
- Input:
- dsolve(d(y)/d(x) === x/(2+y), y)
- solve(last, y)
- end
- Output:
- 2*y + 1/2*y^2 === constant + x^2
- y === [-2 + sqrt(4 - 2*(-constant - x^2)), -2 - sqrt(4 - 2*(-constant - x^2))]
-
- Example 7.7.2.4. Solve differential equations.
- Input:
- dsolve(d(y)/d(x) === x*exp(y), y)
- dsolve(d(y)/d(x) === y^2+5*y+6, y)
- dsolve(d(y)/d(x) === y/x, y)
- dsolve(d(y)/d(x) === y*x+1, y)
- end
- Output:
- -exp(-y) === constant + x^2
- ln((4 + 2*y)/(6 + 2*y)) === constant + x
- y === constant*x*sgn(x)
- y === exp(1/2*x^2)*(constant + sqrt(1/2)*sqrt(pi)*erf(sqrt(1/2)*x))
-
-
- 7.8 Sums and Products
-
- Users can compute partial, finite or infinite sums and
- products. Sums and products can be differentiated and integrated. You
- construct functions like Taylor polynomials or finite Fourier series.
- The procedure is the same for sums as products so all examples will
- be restricted to sums. The general formats for these functions are:
-
- sum(expr, x from xmin to xmax step dx)
- prod(expr, x from xmin to xmax step dx)
-
- The expression expr is evaluated at xmin, xmin+dx, ... up to the last
- entry in the series not greater than xmax, and the resulting values
- are added or multiplied. The part "step dx" is optional and defaults
- to 1. The values of xmin, xmax and dx can be any real number.
-
- Here are some examples:
- sum(j, j from 1 to 10)
- for 1 + 2 + .. + 10.
- sum(3^j, j from 0 to 10 step 2)
- for 1 + 3^2 + ... + 3^10.
- Here are some sample Taylor polynomials:
- sum(x^j/j!, j from 0 to n)
- for exp(x).
- sum((-1)^j*x^(2*j+1)/(2*j+1)!, j from 0 to n)
- for sin(x) of degree 2*n+2.
-
- The package SUM.sm should be included before computing
- partial and infinite sums, and the package PRODUCT.sm should be
- included before computing partial and infinite products (see 8.8
- Infinite Sum Package).
- Remember, the keywords "from", "to" and "step" can be
- replaced by the comma (,).
-
-
- 7.9 Lists, Arrays, Vectors and Matrices
-
- SymbMath can construct lists of arbitrary length, and the
- entries in the lists can be of any type of value whatsoever:
- constants, expressions with undefined variables, or even other lists.
- Lists are another kind of value in SymbMath, and they can be assigned
- to variables just like simple values. (Since variables in SymbMath
- language are untyped, you can assign any value to any variable.).
-
-
- 7.9.1 Entering Lists
-
- To define a list, put its elements between square brackets:
-
- a = [1,2,3]
- b = [f(2), g(1), h(1)] # assumes f,g,h defined
- c = [[1,2],3,[4,5]]
-
- A function can have a list for its value:
- f(x) = [1,x,x^2]
- You can define lists another way, with the list command:
-
- list(f(x), x from xmin to xmax step dx)
-
- This is similar to the sum command, but the result is a list:
- [f(xmin), f(xmin+dx), ..., f(xmin+x*dx), ...]
- which continues until the last value of xmin + x*dx <= xmax.
- Try
-
- a = list(j^2, j from 0 to 10 step 1)
- f(x) = list(x^j, j from 0 to 6 step 1)
- b = f(-2)
-
- The third way to construct a list is to transform the sum to
- the list.
-
- Input:
- y1=[a,b,c]
- sum(y1)+d
- list(last)
- end
- Output:
- y1 = [a, b, c]
- a + b + c + d
- [a, b, c, d]
-
- This is how you extend an existing list to include a new
- element.
-
-
- 7.9.2 Accessing Lists
-
- To find the value of the j-th element in a list x, use the
- formula x[j]. The first element of x is always x[1]. If the x[j]
- is itself a list, then its k-th element is accessed by repeating the
- similar step. Try:
-
- Input:
- x = [[1,2],3,[4,5]]
- x[1]
- x[2]
- end
- Output:
- x = [[1, 2], 3, [4, 5]]
- [1, 2]
- 3
-
- An entire sub-list of a list x can be accessed with the
- command x[j], which is the list:
- [x[j], x[j+1], ... ]
-
-
- 7.9.3 Modifying Lists
-
- The function subs() substitutes the value of the element in
- the list, as in the variables. e.g.
-
- Input:
- l=[a,b,c]
- subs(l, a=a0)
- end
- Output:
- l = [a, b, c]
- [a0, b, c]
-
- But you can't use this form unless the element of the list is
- already defined.
-
-
- 7.9.4 List Operations
-
- Lists can be added, subtracted, multiplied, and divided by
- other lists or by constants. When two lists are combined, they are
- combined term-by-term, and the combination stops when the shortest
- list is exhausted. When a scalar is combined with a list, it is
- combined with each element of the list. Try:
-
- a = [1,2,3]
- b = [4,5,6]
- a + b
- a / b
- 3 * a
- b - 4
-
- Example 7.9.1. Two lists are added.
- Input:
- list1=[a1,a2,a3]
- list2=[b1,b2,b3]
- list1+list2
- last[1]
- end
- Output:
- list1 = [a1,a2,a3]
- list2 = [b1,b2,b3]
- [a1 + b1, a2 + b2, a3 + b3]
- a1 + b1
-
- If L is a list, then f(L) results in a list of the values,
- even though f() is the differentiation or integration function d() or
- inte(). Try list with:
- Input:
- sqrt([a, b, c])
- d([x, x^2, x^3], x)
- end
-
- You can find the number of elements in a list with:
- length(a)
- If you use a list as the value of a variable in a user-
- defined function, SymbMath will try to use the list in the
- calculation.
- You can sum all the elements in a list x with the user-
- defined function:
-
- listsum(x)
- in the statistics package 'stat.sm'.
- Example:
-
- x=[1,2,3]
- listsum(x^2)
-
- This functions takes the sum of the squares of the elements of
- the list x.
- See the statistics package 'stat.sm' for other statistics
- operations (e.g. average, max, min).
- See the list plot package 'listplot.sm' for plotting a list.
-
- 7.9.5 Vector Operations
-
- SymbMath uses lists to represent vectors, and lists of lists to
- represent matrices.
- Vectors and matrices can be operated by "+" and "-" with vectors
- and matrixes, by "*" and "/" with a scalar, and by subs(), diff() and
- inte(). These operations are on each element, as in lists and arrays.
- You can use lists as vectors, adding them and multiplying them
- by scalars. For example, the dot product of two vectors of a and b is:
-
- dot = listsum(a*b)
-
- You can even make this into a function:
-
- Dot(x_, y_) = listsum(x*y)
- a = [2,3,5]
- b = [4,3,2]
- Dot(a,b)
-
- How about the cross product:
-
- Cross(a,b) = [a[2]*b[3]-b[2]*a[3],a[3]*b[1]-b[3]*a[1],a[1]*b[2]-b[1]*a[2]]
-
-
- 7.10 Complex Analysis
-
- The complex numbers, complex infinity and some complex
- functions can be calculated, and the complex expressions can be
- differentiated and integrated.
- Sign of complex numbers is defined as
-
- / 1 if re(z)>0, or re(z)=0 and im(z)>0, (i.e. z>0),
- sgn(z) = 0 if z=0,
- \ -1 otherwise, (i.e. z<0).
-
- Example 7.10.1.
- Input:
- sgn(1+i)
- sgn(1-i)
- sgn(-1-i)
- end
- Output:
- 1
- 1
- -1
-
- Input:
- exp(inf+pi*i)
- ln(last)
- end
- Output:
- -inf
- inf + pi*i
-
- Input:
- inte(1/x, x from i to 2*i)
- end
- Output:
- ln(2)
-
- 10.11 Tables of Function Values
-
- If you want to look at a table of values for a formula, you
- can use the table command:
-
- table(f(x), x)
- table(f(x), x from xmin to xmax)
- table(f(x), x from xmin to xmax step dx)
-
- It causes a table of values for f(x) to be displayed with x=xmin,
- xmin+dx, ..., xmax. If xmin, xmax, and step omit, then xmin=-5, xmax=5,
- and dx=1 for default. You can specify a function to be in table(),
- Example. Make a table of x^2.
- Input:
- table(x^2, x)
- end
- Output:
- -5, 25
- -4, 16
- -3, 9
- -2, 4
- : :
- : :
-
- Its output can be written into a disk file for interfacing
- with other software (e.g. the numeric computation software).
-
-
- 7.12 Transformation and Piece of Expressions
-
- Different types of data may be transformed each other.
- The complex number z is transformed to the real number x by
-
- re(z), im(z), abs(z), sgn(z).
-
- The functions
-
- left(x===a) and right(x===a)
-
- pick up one side of the equation.
- A list can be transformed to a table with the table command if
- the elements of the list are the numbers. e.g.
- Input:
- x=[5,4,3,2,1]
- table(x[j], j from 1 to 4 step 1)
- end
- Output:
- 1, 5
- 2, 4
- 3, 3
- 4, 2
-
- A piece of an expression can be picked up. e.g.
- Input:
- y=a+b*x+c*x^2+d*x^3
- coef(y, x)
- coef(y, x^2)*x^2
- list(y)
- last[3]
- end
- Output:
- y = a + b*x + c*x^2 + d*x^3
- b
- c*x^2
- [a, b*x, c*x^2, d*x^3]
- c*x^2
-
-
- Table 7.2 Expand and Factor
- ---------------------------------------------------------------------
- Expand Factor
-
- (a+b)^2 a^2+2*a*b+b^2
- (a+b)^n a^n+ ...... +b^n n is positive integer
- a*(b+c) a*b + a*c
- ---------------------------------------------------------------------
- a+b can be many terms or a-b.
-
-
-
- 7.13 Learning from Users
-
- One of the most important feature of SymbMath is its ability
- to deduce and expand its knowledge. If the user provides it with the
- necessary facts, SymbMath can solve many problems that it was unable
- to solve before. The followings are several ways in which SymbMath is
- able to learn from the user.
-
-
- 7.13.1 Learning indefinite and definite integrals from a derivative
-
- Finding derivatives is much easier than finding integrals.
- Therefore, you can find the integrals of a function from the
- derivative of that function.
- If the user provides the derivative of a known or unknown
- function, SymbMath can deduce the indefinite and definite integrals of
- that function. If the function is not a simple function, the user only
- need to provide the derivative of its simple function. For example,
- the user wants to evaluate the integral of f(a*x+b), the user only need
- to provide d(f(x), x).
-
- Example 7.13.1.1 :
-
- If a user know a derivative of an function f(x) (f(x) is a
- known or unknown function), SymbMath can learn the integrals of that
- function from its derivative.
- First check SymbMath wether or not it had already known
- indefinite and definite integrals of an unknown function f(x).
-
- In the input window type :
-
- inte(f(x), x)
- inte(f(x), x, 1, 2)
- end
-
- In the output window you'll see :
-
- inte(f(x), x)
- inte(f(x), x, 1, 2)
- end
-
- As the output windows displayed only what was typed in the input
- windows without any computed results, imply that SymbMath has no
- knowlege of the indefinite and definite integrals of the functions
- in question. Now teach SymbMath the derivative of f(x) on the first
- line, and then run the program again.
-
- Input:
- d(f(x_), x_) = exp(x)/x
- inte(f(x), x)
- inte(f(x), x, 1, 2)
- end
- Output:
- d(f(x_), x_) = e^x/x
- x*f(x) - e^x
- e - f(1) + 2*f(2) - e^2
-
- As demonstrated, the user only supplied the derivative of the function
- and in exchange SymbMath logically deduced its integral.
- An other example is
- Input:
- d(f(x_) ,x_) := 1/sqrt(1-x^2)
- inte(f(x), x)
- inte(k*f(a*x+b), x)
- inte(x*f(a*x^2+b), x)
- end
- Output:
- d(f(x_), x_) := 1/sqrt(1 - x^2)
- sqrt(1 - x^2) + x*f(x)
- k*(sqrt(1 - (b + a*x)^2) + (b + a*x)*f(b + a*x))/a
- sqrt(1-(a*x^2 + b)^2) + (a*x^2 + b)*f(a*x^2 + b)
-
- The derivative of the function that user supplied can be another
- derivative or integral.
-
- Example 7.13.1.2 :
-
- Input window :
- d(f(x_), x_) = inte(sin(x), x)
- inte(f(x), x)
- end
- Output window :
- d(f(x_), x_) = - cos(x)
- cos(x)
-
-
- 7.13.2 Learning complicated indefinite integrals from a simple
- indefinite integral
-
- The user supplies a simple indefinite integral, and in return, SymbMath
- will perform the related complicated integrals.
-
- Example 7.13.2.1 :
-
- Check whether SymbMath already knowns the following integrals or not.
-
- Input window :
- inte(tan(x)^2, x)
- inte((2*tan(x)^2+x), x)
- inte(inte(tan(x)^2+y), x), y)
- end
- Output window :
- inte(tan(x)^2, x)
- inte((2*tan(x)^2+x), x)
- inte(inte(tan(x)^2+y), x), y)
-
- Supply like in the previous examples the following in information
- integral of tan (x) is tan (x) - x; then ask the indefinite integral
- of 2*tan(x)^2+x, and a double indefinite integral of 2*tan (x)^2 + x,
- and a double indefinite integral of respect to both x and y. Change
- the first line, and then the program again.
-
- Input window :
- inte(tan(x)^2, x) = tan(x) - x
- inte((2*tan(x)^2+x), x)
- inte(inte(tan(x)^2+y), x), y)
- end
- Output window :
- inte(tan(x)^2, x) = tan(x) - x
- 2*(tan(x) - x) + 1/2*x^2
- tan(x)*y - x*y + x*y^2
-
- The user can also ask SymbMath to perform the following
- integrals:
- inte(inte(tan(x)^2+y^2), x), y),
- inte(inte(tan(x)^2*y), x), y),
- inte(x*tan(x)^2, x),
- triple integral of tan(x)^2-y+z, or others.
-
-
- 7.13.3 Learning definite integral from indefinite integral
-
- The user continues to ask indefinite integral.
-
- Input window :
- inte(inte(tan(x)^2+y, x from 0 to 1), y from 0 to 2)
- end
- Output:
- 2 tan(1)
-
-
- 7.13.4 Learning complicated derivative from simple derivative
-
- SymbMath can learn complicated derivatives from a simple derivative
- even thought the function to be differentiated is any function, not
- standard function.
-
- Example 7.13.4.1 :
-
- Differentiate f(x^2)^6, where f(x) is an known function, instead of
- a standard function.
-
- Input:
- d(f(x^2)^6, x)
- end
- Output:
- 12*x*f(x^2)^5*d(f(x^2), x^2)
-
- Output is only the part derivative. d(f(x^2), x^2) in the output
- suggest that the user should teach SymbMath d(f(x_), x_). e.g. the
- derivative of f(x) is another unknown function df(x), i.e.
- d(f(x_), x_) = df(x), do so and run it again.
-
- Input:
- d(f(x_), x_) = df(x)
- d(f(x^2)^6, x)
- end
- Output:
- d(f(x_), x_) = df(x)
- 12*x*f(x^2)^5*df(x^2)
-
- This time we get complete derivative.
-
- 7.13.5 Learning integration from algebra
-
- If the user shows SymbMath algebra, SymbMath can learn integrals.
-
-
- Example 7.13.5.1 :
-
- Input sin(x)^2=1/2-1/2*cos(2*x), then ask for the integral of sin(x)^2.
-
- Input window :
- sin(x)^2=1/2-1/2*cos(2*x)
- inte(sin(x)^2, x)
- end
- Output window :
- sin(x)^2 = 1/2 - 1/2*cos(2*x)
- 1/2*x - 1/4*sin(2*x)
-
- SymbMath is very flexible, It learned to solve these problems, even though
- the types of problems are different, e.g. learning integrals from
- derivatives or algebra.
-
-
- 7.13.6 Learning complicated algebra from simple algebra
-
- SymbMath has the ability to learn complicated algebra from simple algebra.
-
- Example F1 :
-
- Transform sin(x)/cos(x) into tan(x) in an expression.
-
- input window:
-
- sin(x)/cos(x) = tan(x)
- x+sin(x)/cos(x)+a
-
- Output window :
- sin(x)/cos(x) = tan(x)
- a + x + tan(x)
-
-
- The difference between learning and programming is as follows :
- the learning process of SymbMath is very similar to the way human
- beings learn, and that is accomplished by knowing certain rule that
- can be applied to several problems. Programming is diffrent in the way
- that the programmer have to accomplish many tasks before he can begin
- to solve a problem. First, the programmer defines many subroutines for
- the individual integrands (e.g. tan(x)^2, tan(x)^2+y^2, 2*tan(x)^2+x,
- x*tan(x)^2, etc.), and for individual integrals (e.g. the indefinite
- integral, definite integral, the indefinite double integrals,
- indefinite triple integrals, definite double integrals, definite
- triple integrals, etc.), second, write many lines of program for the
- individual subroutines, (i.e. to tell the computer how to calculate
- these integrals), third, load these subroutines, finally, call these
- subroutines. That is precisely what SymbMath do not ask the user to do.
- In one word, programming means that programmers must
- provide step-by-step procedures telling the computer how to solve
- each problems. By contrast, learning means that users need only supply
- the necessary facts, SymbMath will determine how to go about
- solutions.
- If the learning is saved into the initial file init.sm, the
- learning will become the knowledge of the SymbMath system, users need
- not to teach SymbMath again when users run SymbMath next time.
-
-
- 8. Packages
- A package is a SymbMath program file for special use.
- In order to expand the special ability of SymbMath, some
- packages should be included in the user program by
- include('filename')
- The filename is any MS-DOS filename.
- It is recommended that the filename is the same as the function
- name in the program file, and the extension of the filename is .sm in
- order to remember easily the name of the package. e.g. inte.sm is the
- filename of the integral package as the name of integral function is
- inte().
- After including the package, users can call the functions in
- the package from their program.
- The include command must be in a single line anywhere.
- Many packages can be included at a time, however, all names of
- the variables are public and name conflicts must be avoided.
- Alternately, a part of the package file rather than the whole
- package file can be copied into the Edit window by pressing <F7>
- (external copy) in order to save memory space.
- There are many packages. The following are some of them.
-
- Table 8.1 Packages
- ------------------------------------------------------------------------
- File Name Package Function
-
- init.sm initial package when running the program in
- the Edit window. It contains switches on the
- default status.
- plot.sm plotting functions.
- listplot.sm plotting a list of data.
- d.sm derivatives.
- inte.sm integrals.
-
- --------------- shareware version only has above packages --------------
-
- units.sm an units conversion.
- trig.sm reduction of trig functions.
- hyperbol.sm reduction of hyerbolic functions.
- dt.sm total derivatives.
- NInte.sm numeric integration.
- NSolve.sm numeric solver of equation.
- ODE.sm ordinary differential equation.
- gamma.sm gamma function.
- ei.sm exponential integral function.
- series.sm Taylor series.
- sum.sm symbolic sum.
- InfSum.sm infinite sum.
- chemical.sm the atomic weight of chemical elements.
- inorgani.sm inorganic chemical reactions.
- -----------------------------------------------------------------------
-
-
- 8.1 Initial Package
- ---------------------------------------------------------------------
- # The initial package init.sm
- output=off
- numerical=off
- expand=off
- sum(y_,x_,a_,b_) := sum(y,x,a,b,1)
- prod(y_,x_,a_,b_) := prod(y,x,a,b,1)
- list(y_,x_,a_,b_) := list(y,x,a,b,1)
- table(y_,x_,a_,b_) := table(y,x,a,b,1)
- table(y_,x_) := table(y,x,-5,5,1)
- degree=pi/180
- 0!=1
- inf!=inf
- sgn(0)=0
- sgn(zero)=1
- sgn(e)=1
- sgn(pi)=1
- sgn(inf)=1
- abs(zero)=zero
- abs(e)=e
- abs(pi)=pi
- abs(inf)=inf
- ln(0)=-inf
- ln(inf)=inf
- include('plot.sm')
- # include('d.sm')
- block(output=basic,null)
- end
- ---------------------------------------------------------------------
-
- When running the program in the Edit window, SymbMath
- automatically first includes (or runs) the initial package 'init.sm'.
- The commands in the 'init.sm' package seems the SymbMath system
- commands. You can include other packages (e.g. d.sm) in the initial
- package 'init.sm' so the derivatives table in the package 'd.sm' seems
- to be in SymbMath system. Doing this is by changing the comment
- statement in the last third line
- # include('d.sm')
- to include statement
- include('d.sm')
-
-
- 8.2 Derivative Package
- The package 'd.sm' is extention of the d(f(x),x) function.
-
- 8.3 Total Derivative Package
- The package 'dt.sm' is for the total derivative function
- dt(f(x,y),x,y).
- Example:
- Input:
- include('dt.sm')
- dt(x*y, x, y)
- end
- Output:
- y*d(x) + x*d(y)
-
- 8.4 Integral Package
- The package 'inte.sm' is the extention of the function inte(f(x),x).
-
- 8.5 Numeric Integration Package
- The function
- ninte(y, x from x0 to x1)
- in the package 'NInte.sm' can do numerical integration.
- Example 8.5.1. Compare numerical and symbolic integrations
- for 4/(x^2+1) with respect to x taken from x=0 to x=1.
- Input:
- include('NInte.sm')
- ninte(4/(x^2+1), x from 0 to 1)
- num(inte(4/(x^2+1), x from 0 to 1))
- end
- Output:
- done
- 3.1415
- 3.1416
-
- 8.6 Numerical Solving
- The function
- nsolve( f(x) === x, x, x0)
- in the package 'NSolve.sm' can numerically solve an algebraic equation
- with an initial value x0.
- Example 8.6.1. solve the equation sin(x) === 1 with x0=1.
- Input:
- include('NSolve.sm')
- nsolve( sin(x) === 1, x, 1)
- end
- Output:
- done
- 1.5364150214
-
- 8.7 Series Package
- The series package 'Series.sm' has a function series() for the
- Taylor series at x=0:
- You can call the function
- series(f(x), x, order)
- series(f(x), x)
- to find the Taylor series. The arguement (order) is optional and
- defaults to 6.
- Example 8.7.1. Find the power series expansion for cos(x)
- about the point x=0 to order x^4.
- Input:
- include('series.sm')
- series(cos(x), x, 4)
- end
- Output:
- 1 - 1/2*x^2 + 1/24*x^4
-
- 8.8 Infinite Sum Package
- The infinite sum package 'InfSum.sm' has a function
- infsum(f(x),x)
- to find the infinite sum, sum(f(x), x from 0 to inf).
- Example:
- Input:
- include('infsum.sm')
- infsum(1/n!, n)
- end
- Output:
- e
-
- 8.9 Chemical Calculation Package
- SymbMath recognizes 100 symbols of chemical elements and
- converts them into their atomic weights after the chemical package of
- 'Chemical.sm' is included.
-
- Example 8.9.1. Calculate the weight percentage of the
- element C in the molecule CH4.
- Input:
- include('chemical.sm')
- numerical=on
- C/(C+H*4)*100*%
- end
- Output:
- done
- numerical = on
- 74.868 %
-
- Example 8.9.2. Calculate the molar concentration of CuO when
- 3 gram of CuO is in 0.5 litre of a solution.
- Input:
- include('chemical.sm')
- numerical=on
- g=1/(Cu+O)*mol
- 3*g/(0.5*l)
- end
- Output:
- 0.07543*mol/l
-
- 8.10 Chemical Reactions Package
- SymbMath can provide the answers for inorganic chemical
- reactions after the inorganic reaction package "Inorgani.sm" is included.
-
- Example 8.10.1. What are the products when the reactors are
- HCl + NaOH ?
- Input:
- include('inorgani.sm')
- HCl+NaOH
- end
- Output:
- H2O + NaCl
-
-
- 8.11 Plot Function Package
- ---------------------------------------------------------------------
- # The plot function package plot.sm
- # to plot a function.
- # plot(x^2, x) # plot the function of x^2
- # plot(x^2, x,-6,6) # plot the function of x^2 from x=-6 to x=6
- # end
-
- output=off
- plot(y_,x_,x1_,x2_,dx_) := block(openfile('symbmath.out'),
- table(y,x,x1,x2,dx),
- closefile('symbmath.out'),
- system(plotdata))
- plot(y_,x_,x1_,x2_) := plot(y,x,x1,x2,(x2-x1)*0.5)
- plot(y_,x_) := plot(y,x,-5,5,0.5)
- output=on
- end
- ----------------------------------------------------------------------
-
- If SymbMath is interfaced with the software PlotData, SymbMath
- produces the data table of functions, and PlotData plots from the
- table. So SymbMath seems to plot the function. This interface can be
- used to solve equations graphically.
- In the plot package 'plot.sm' the funcion
- plot(y, x, x1, x2, dx)
- first open a file 'SymbMath.Out' for writing, then write the data table
- of the function y into the file 'SymbMath.out', then close the file,
- and finally automatically call the software PlotData to plot. After it
- exits from PlotData, it automatically return to SymbMath.
- The functions
- plot(y, x)
- plot(y, x from xmin to xmax)
- call the function plot(y,x,x1,x2,dx)
- e.g. plot x^2.
- Input:
- plot(x^2, x)
- end
-
- in the software PlotData, just select the option to read the file
- 'SymbMath.out' and to plot. PlotData read the data in the SymbMath
- format without any modification (and in many data format).
- In PlotData,
- in the main menu:
- 1 <Enter>
- in the read menu:
- 2 <Enter>
- <Enter>
- in the main menu:
- 2 <Enter>
- in the graph menu:
- 1 <Enter>
-
- where <Enter> is the <Enter> key.
- Refer to PlotData for detail.
- Note that if your monitor is Hercules, you must load the
- MSHERC.COM program as a TRS program before you run PlotData.
- Otherwise you will get Error when you plot.
-
- 8.12 List Plot Package
- ----------------------------------------------------------------------
- # the list plot package 'listplot.sm'
- # to plot a list of data.
- # listplot([1,2,3,4,5,4,3,2,1])
- # end
-
- output=off
- listplot(y_) := if(islist(y), block(yy=y, length=length(yy),
- openfile('symbmath.out'),
- table(yy[xx],xx,1,length),
- closefile('symbmath.out'),
- system(plotdata) ))
- output=on
- end
- ---------------------------------------------------------------------
-
- 15. Interface with Other Software
- Interface with other software, (e.g. CurFit, Lotus 123) is
- similar to interface with the software PlotData in the plot package
- 'plot.sm'.
-
- (continued on the file SymbMath.DO3)
-