INTRODUCTION:
Numerical v.s. Symbolic
Computing

Monday, 1 August 2005

A. Numerical Computation

With some exceptions, most programming languages are designed for numerical computing, e.g., Fortran, C, and Matlab packages. The most obvious limitations are (1) limited range of integers, (2) finite accuracy of floating point numbers.

B. Symbolic Computing

Symbolic computing environment overcomes the two limitations (arbitrary sizes for integers, and a user-specified accuracy in floating point calculation).   Most importantly, symbolic packages provided ability of manipulating formulas, e.g., differentiation or integration of a function.  The disadvantage of symbolic systems is that it is typically slower.

Symbolic Packages (Languages)

(1)  LISP:  low level but powerful programming language.
(2)  Maple, Mathematica:  two popular symbolic computing packages.
(3)  and many other special packages: reduce, axiom, macsyma, etc.

How to run Mathematica

A. Text-Based Interfaces

Log into your UNIX system, type

math

You are engaged in a dialog with the system, one line at time, RETURN to enter an input.

How to run Mathematica
B. Notebook Interfaces (GUI)

Log into your Window system (Start button, All Programs, Mathematica),  click

mathematica

You can access the help as well as the whole book "Mathematica: a system for doing Mathematics by Computer" by S. Wolfram.

IMPORTANT :  You execute an Mathematica Expression with 
		SHIFT - ENTER

Arbitrary Large Integers

Simple integer calculations

2 + 2

2^90

+, -, *, /, ^ 
for add,subtract,multiply, divide,and power,as usual.

Integer functions (GCD[a,b], greatest common divisor, FactorInteger[a], etc):

GCD[10, 4]

FactorInteger[40]

This means 40 = (2^3) 5.

Function arguments are enclosed in square brakets [  ...  ].

Floating Point with Arbitrary Precision

The function N[...] converts an exact number into floating point. (N[ ] does not increase the precision of a number).

N[2^90]

N[Pi, 25]

1/3  +  2/7

N[1/3  +  2/7]

Mathematica's rules 

for numerical calculations:
    	(1): If input is exact, the computation will be performed exactly.
    	(2): If input is approximate, the result is approximate, with accuracy not great than the input.

452/62

452.3/62

Why is there a difference in the two results?

Precision[1.34]

$MachinePrecision

Precision[123.44444444444444444]

Two kinds of approximate real nos:
(1): Arbitrary precision numbers contains any number of digits.
(2): Machine precision numbers contain fixed number of digits.
If less than $MachinePrecision digits, number treated as (2) else as (1)

Accuracy[134.01]

Precision[x] : total no. of significant digits in x
Accuracy[x] : no. of significant digits to right of decimal point in x

x  =  SetPrecision[1.3,  28]

Precision[x]

y  =  x * 1.0;  Precision[y]

Why x has 28 digits of precision but y is 16?

Symbol Data Type

Like any programming language, Mathematica is provided with the usual data type of integer, floating point numbers and advanced structures. In addition, Mathematica provides a type called "Symbol" for symbolic computation.  For example:

x  +  3  x

The function Head[...] tells you what data type a particular expression is.

Head[x]

Head[3]

Head[3  +  I ]

Head[2.0]

Some Examples of Symbolic Computation

Expand[ (1 + x)^2]

Factor[ 1  +  4 x  +  3  x^2]

Factor[x^99 + y^99]

Solve[x^2 + 2*x + 1  ==  0]

Solve[ {y^2  +  x  - y  == 0,  
        y  -  x^2   + 2 ==  0},  {x, y}]

Solve[a*x^2 + b*x + c  ==  0,  x]

D[Sin[x], x]

D[x^n,  {x,  2}]

Integrate [ e^{x^2},  x]

Integrate[ (1 + x)^3/(1 - x^2)^2,  x]

Simplify[Sin[x]^2  +  Cos[x]^2]

Graphical Computations

Mathematica supports graphics in a convenient way, as well as more advanced programming environment.  A simple plot :

Plot[ Sin[x],  {x,  -Pi,  Pi}]

Plot3D[ 
      Exp[-(x^2 + y^2)],  {x,  -2, 2},  {y,  -2,  
        2}]

DEFINING FUNCTIONS

Simple One-Line Function Definition

f[x_]  :=  x^2

This defines a function that takes the square of its argument.   x_ (the symbol called x with an underscore) is called a pattern (which "matches" anything), and := is delayed assignment, different from assignment =.  We use the function like this:

f[2]

f[a]

f[1 + x]

The function takes any argument, including strings:

f["SMA"]

Here f[x_] := x^2 specifies value to set of array elements with arbitrary indices while f[x] := x^2 specifies value to an element of array.

g[x]  :=  x^2

g[x]  +  g[y]

Assignment = and Delayed Assignment :=

= (or Set[ ])  The right-hand side expression is evaluated, and result sets to the variable (expression) on the left-hand side.
:= (or SetDelayed[ ]) The right-hand side expression without evaluation is set to the variable (expression) on the left-hand side.

a  =  3;  x  =  a + 1

?x

With "=", we associate number 4 with variable x, since a + 1 has been evaluated to 4 before setting the value for x.

a  =  3;  x  :=  a  +  1

?x

With ":=", we associate a + 1 with x, the expression a+1 is delayed for evaluation.

More on Function Definition

If we want a function to take only integer arguments, we can say:

g[z_Integer]  :=  z^2

{g[3],  g[x],  g[3.5],  g[p]}

We can define a function with any number of arguments:

h[a_,  b_,  c_]  :=  (a + b + c)

h[1, 2, q]

You can define a function with variable number of arguments:

v[a_]  :=  Plus[a]

{v[1],  v[1, 3],  v[1, 2, 3, 4]}

v[a__]  :=  Plus[a]

{v[1],  v[1, 3],  v[1, 2, 3, 4]}

Function Definition with More than One-Line of Code: Programs

If we define a function with a code of more than one line, we can used the Module[ ] to group the code as a single unit.

\(\(\(prog[
        x_]\)\(\ \)\(:=\)\(\ \)\(Module[\ {a, b, c}, \ 
			a\  = \ 
          1; 
			b\  = \ 2; 
			c\  = \ a\  + \ b; 
			x\  + \ 
          c]\)\(	\)\)\)

prog[z]

The variables in the first list {a,b,c} specifies LOCAL variables to this function. Statements are separated by semicolon ";".   The return value of the function is last expression, x + c, without an ending semicolon. The argument x must be treated as a constant, rather than a variable (i.e., you cannot do x = a + b).

Patterns

Pattern is an important concept in Mathematica's syntax:  f[x_] is a pattern.  Patterns are used for matching an expression.  So f[x_] matched any expression with an explicit f, followed by square brackets with x_ being replaced by any expression.  So f[x_] matches f[1], f[x], or f["this"], but not g[2].   If pattern matches, the definition associated with that pattern is executed.   In the above example, f[something] is replaced by something^2.  

g[x_Integer]  matches a function g with integer argument, so, it matches g[2], but not g[a] (assuming we did not say a = 3, so a is not an integer).  Unmatched patterns are not executed and returned the same as input.   h[a_, b_, c_] matches function h with three arbitrary arguments.

v[a__] (two underscores) matches function v with 1 or more arguments.  So it matches v[1], v[1,2],  or v[a,b,c].
You can test if pattern ABC matches XYZ by the command:

MatchQ[ABC,  XYZ]

For example,

MatchQ[f[y],  f[x_]]

Substitution Operations

A convenient way for evaluation of an expression is by variable substitution.

(1 + x)^5    /.  x ->  3

While x = 3 globally sets x to 3, x -> 3 replaces x by 3 only locally at the current expression.

{Sin[x], Cos[x], Exp[x]} /. x -> 0

Expression of the form x -> 0 is called a rule,.

(x + y) (x - y)^2 /. {x ->  3,  
        y ->  1 - a}

Prefix, postfix, and infix notation 
for function application.

We normally say Sin[x], but we could say:

Sin @ Pi

Pi  //  Sin

Join[{a, b, c}, {d, e}]

{a, b, c}~ Join ~{d, e}