0% found this document useful (0 votes)
224 views44 pages

Eigenmath Manual: George Weigt June 1, 2018

The document is an Eigenmath manual written by George Weigt on June 1, 2018. It provides an introduction and overview of the key capabilities of the Eigenmath software, including arithmetic, exponents, symbols, user-defined functions, scripts, drawing, complex numbers, linear algebra, calculus, examples, built-in functions and syntax. The manual serves to familiarize users with the basic usage and features of the Eigenmath computational software.

Uploaded by

Sasa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
224 views44 pages

Eigenmath Manual: George Weigt June 1, 2018

The document is an Eigenmath manual written by George Weigt on June 1, 2018. It provides an introduction and overview of the key capabilities of the Eigenmath software, including arithmetic, exponents, symbols, user-defined functions, scripts, drawing, complex numbers, linear algebra, calculus, examples, built-in functions and syntax. The manual serves to familiarize users with the basic usage and features of the Eigenmath computational software.

Uploaded by

Sasa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Eigenmath Manual

George Weigt

June 1, 2018

1
Contents
1 Introduction 3
1.1 Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Exponents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 User-defined functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5 Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.6 Draw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.7 Complex numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.8 Linear algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

2 Calculus 13
2.1 Derivative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2 Gradient . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.3 Template functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.4 Integral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.5 Arc length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.6 Line integrals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.7 Surface area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.8 Surface integrals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.9 Green’s theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.10 Stokes’ theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

3 Examples 23
3.1 François Viète . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.2 Curl in tensor form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.3 Quantum harmonic oscillator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.4 Hydrogen wavefunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.5 Space shuttle and Corvette . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.6 Avogadro’s constant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.7 Zero to the zero power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.8 Euler’s identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

4 Built-in functions 30

5 Syntax 43

6 Tricks 44

2
1 Introduction

The field at the bottom of the Eigenmath window is for entering calculations that get evaluated right
away.

For example, let us check the following arithmetic from Vladimir Nabokov’s autobiography Speak,
Memory.
A foolish tutor had explained logarithms to me much too early, and I had read (in a British
publication, the Boy’s Own Paper, I believe) about a certain Hindu calculator who in
exactly two seconds could find the seventeenth root of, say, 3529471145760275132301897342
055866171392 (I am not sure I have got this right; anyway the root was 212).
We can check Nabokov’s arithmetic by entering the following calculation.
212^17
After pressing the return key, Eigenmath displays the following result.
3529471145760275132301897342055866171392
So Nabokov did get it right after all. Now let us see if Eigenmath can find the seventeenth root of this
number, like the Hindu calculator could.
N = 212^17
N^(1/17)
Eigenmath displays the following result.
212
When a symbol is assigned a value, such as N above, no result is printed. To see the value of a symbol,
just evaluate it.
N
N = 3529471145760275132301897342055866171392
The previous example shows a convention that will be used throughout this manual. That is, the color
blue indicates something that the user should type. The computer response is shown in black.

1.1 Arithmetic

Normally Eigenmath uses integer and rational number arithmetic.

3
1/2+1/3
5
6
A floating point value causes Eigenmath to switch to floating point arithmetic.
1/2+1/3.0
0.833333
An integer or rational number result can be converted to a floating point value by entering float.
212^17
3529471145760275132301897342055866171392
float
3.52947 × 1039
The following example shows how to enter a floating point value using scientific notation.
epsilon = 1.0*10^(-6)
epsilon
ε = 1.0 × 10−6

1.2 Exponents

Eigenmath requires parentheses around negative exponents. For example,


10^(-3)
instead of
10^-3
The reason for this is that the binding of the negative sign is not always obvious. For example, consider
x^-1/2
It is not clear whether the exponent should be −1 or −1/2. So Eigenmath requires
x^(-1/2)
which is unambiguous. In general, parentheses are always required when the exponent is an expresssion.
For example, x^1/2 is evaluated as (x1 )/2 which is probably not the desired result.
x^1/2
1
x
2
Using x^(1/2) yields the desired result.
x^(1/2)
x1/2

4
1.3 Symbols

As we saw earlier, symbols are defined using an equals sign.


N = 212^17
No result is printed when a symbol is defined. To see the value of a symbol, just evaluate it.
N
N = 3529471145760275132301897342055866171392
Symbols can have more that one letter. Everything after the first letter is displayed as a subscript.
NA = 6.02214*10^23
NA
NA = 6.02214 × 1023
A symbol can be the name of a Greek letter.
xi = 1/2
xi
1
ξ=
2
Greek letters can appear in subscripts.
Amu = 2.0
Amu
Aµ = 2.0
The following example shows how Eigenmath scans the entire symbol to find Greek letters.
alphamunu = 1
alphamunu
αµν = 1
When a symbolic chain is defined, Eigenmath follows the chain as far as possible. The following
example sets A = B followed by B = C. Then when A is evaluated, the result is C.
A = B
B = C
A
A=C
Although A = C is printed, inside the program the binding of A is still B, as can be seen with the
binding function.
binding(A)
B
The quote function returns its argument unevaluated and can be used to clear a symbol. The following
example clears A so that its evaluation goes back to being A instead of C.

5
A = quote(A)
A
A

1.4 User-defined functions

The following example shows a user-defined function with a single argument.


f(x) = sin(x)/x
f(pi/2)
2
π
The following example defines a function with two arguments.
g(x,y) = abs(x) + abs(y)
g(1,-2)
3
User-defined functions can be evaluated without an argument list. The binding of the function name
is returned when there is no argument list.
f(x) = sin(x)/x
f
sin(x)
f=
x
Normally a function body is not evaluated when a function is defined. However, in some cases it is
required that the function body be the result of something. The eval function is used to accomplish
this. For example, the following code causes the function body to be a sixth order Taylor series
expansion of cos x.
f(x) = eval(taylor(cos(x),x,6))
f
1 6 1 1
f =− x + x4 − x2 + 1
720 24 2

1.5 Scripts

Scripting is a way of automatically running a sequence of calculations. A script is entered in the


left-hand field of the Eigenmath window.

6
To create a script, enter one calculation per line in the script field. Nothing happens until the Run
button is clicked. When the Run button is clicked, Eigenmath evaluates the script line by line. After
a script runs, all of its symbols are available for immediate mode calculation. Scripts can be saved and
loaded using the File menu.
Here is an example script that can be pasted into the script field and then run by clicking the Run
button.
"Solve for vector X in AX = B"
A = ((1,2),(3,4))
B = (5,6)
X = dot(inv(A),B)
X
After clicking the Run button, the following result is displayed.
Solve for vector X in AX = B
 
−4
X= 9
2
A handy debugging aid is to include the line trace = 1 in the script. When trace = 1 each line of
the script is displayed as it is evaluated. For example, here is the previous script with the addition of
trace = 1.
"Solve for vector X in AX = B"
trace = 1
A = ((1,2),(3,4))
B = (5,6)
X = dot(inv(A),B)
X
The result is
Solve for vector X in AX = B
A = ((1,2),(3,4))
B = (5,6)
X = dot(inv(A),B)
X
 
−4
X= 9
2

7
1.6 Draw

draw(f, x) draws a graph of the function f of x. The second argument can be omitted when the
dependent variable is literally x or t. The vectors xrange and yrange control the scale of the graph.
draw(x^2)

xrange = (-1,1)
yrange = (0,2)
draw(x^2)

Parametric drawing occurs when a function returns a vector. The vector trange controls the parametric
range. The default is trange = (−π, π). In the following example, draw varies theta over the default
range −π to +π.
xrange = (-10,10)
yrange = (-10,10)
f = (cos(theta),sin(theta))
draw(5*f,theta)

8
In the following example, trange is reduced to draw a quarter circle instead of a full circle.
trange = (0,pi/2)
draw(5*f,theta)

Here are a couple of interesting curves and the code for drawing them. First is a lemniscate.
trange = (-pi,pi)
X = cos(t)/(1+sin(t)^2)
Y = sin(t)*cos(t)/(1+sin(t)^2)
f = (X,Y)
draw(5*f,t)

Next is a cardioid.

9
r = (1+cos(t))/2
u = (cos(t),sin(t))
xrange = (-1,1)
yrange = (-1,1)
trange = (0,2*pi)
draw(r*u,t)

1.7 Complex numbers



When Eigenmath starts up, it defines the symbol i as i = −1. Other than that, there is nothing
special about i. It is just a regular symbol that can be redefined and used for some other purpose if
need be.
Complex quantities can be entered in either rectangular or polar form.
a+i*b
a + ib
exp(i*pi/3)
1
exp( iπ)
3
Converting to rectangular or polar coordinates causes simplification of mixed forms.
A = 1+i
B = sqrt(2)*exp(i*pi/4)
A-B
1 + i − 21/2 exp( 14 iπ)
rect(last)
0
Rectangular complex quantities, when raised to a power, are multiplied out.
(a+i*b)^2
a2 − b2 + 2iab
When a and b are numerical and the power is negative, the evaluation is done as follows.
n 
a − ib n
 
−n a − ib
(a + ib) = = 2
(a + ib)(a − ib) a + b2

10
Of course, this causes i to be removed from the denominator. Here are a few examples.
1/(2-i)
2 1
+ i
5 5
(-1+3i)/(2-i)
−1 + i
The absolute value of a complex number returns its magnitude.
abs(3+4*i)
5
Since symbols can have complex values, the absolute value of a symbolic expression is not computed.
abs(a+b*i)
abs(a + ib)

The result is not a2 + b2 because that would assume that a and b are real. For example, suppose
that a = 0 and b = i. Then
|a + ib| = | − 1| = 1
and p √
a2 + b2 = −1 = i
Hence p
|a + ib| =
6 a2 + b2 for some a, b ∈ C

The mag function can be used instead of abs. It treats symbols like a and b as real.
mag(a+b*i)
(a2 + b2 )1/2

The imaginary unit can be changed from i to j by defining j = −1.
j = sqrt(-1)
sqrt(-4)
2j

1.8 Linear algebra

The function dot is used to multiply vectors and matrices. Let

   
1 2 x1
A= and x =
3 4 x2

The following example computes Ax.


A = ((1,2),(3,4))
x = (x1,x2)
dot(A,x)

11
 
x1 + 2x2
3x1 + 4x2
The following example shows how to use dot and inv to solve for the vector X in AX = B.
A = ((3,7),(1,-9))
B = (16,-22)
X = dot(inv(A),B)
X
 5

X = 4117
17

The dot function can have more than two arguments. For example, dot(A, B, C) can be used for the
dot product of three tensors.
Square brackets are used for component access. Index numbering starts with 1.
A = ((a,b),(c,d))
A[1,2] = -A[1,1]
A
 
a −a
c d
The following example demonstrates the relation A−1 = adj A/ det A.
A = ((a,b),(c,d))
inv(A)
 d b 
ad−bc − ad−bc
c a
− ad−bc ad−bc

adj(A)
 
d −b
−c a
det(A)
ad − bc
inv(A) - adj(A)/det(A)
 
0 0
0 0
Sometimes a calculation will be simpler if it can be reorganized to use adj instead of inv. The main idea
is to try to prevent the determinant from appearing as a divisor. For example, suppose for matrices A
and B you want to check that
A − B −1 = 0
Depending on the complexity of det B, the software may not be able to find a simplification that yields
zero. Should that occur, the following alternative formulation can be tried.

(det B) · A − adj B = 0

The adjunct of a matrix is related to the cofactors as follows.

12
A = ((a,b),(c,d))
C = ((0,0),(0,0))
C[1,1] = cofactor(A,1,1)
C[1,2] = cofactor(A,1,2)
C[2,1] = cofactor(A,2,1)
C[2,2] = cofactor(A,2,2)
C
 
d −c
C=
−b a
adj(A) - transpose(C)
 
0 0
0 0

2 Calculus

2.1 Derivative

d(f, x) returns the derivative of f with respect to x. The x can be omitted for expressions in x.
d(x^2)
2x
The following table summarizes the various ways to obtain multiderivatives.
∂2f
d(f,x,x) d(f,x,2)
∂x2

∂2f
d(f,x,y)
∂x ∂y

∂ m+n+··· f
d(f,x,...,y,...) d(f,x,m,y,n,...)
∂xm ∂y n · · ·

2.2 Gradient

The gradient of f is obtained by using a vector for x in d(f, x).


r = sqrt(x^2+y^2)
d(r,(x,y))
x
" #
(x2 +y 2 )1/2
y
(x2 +y 2 )1/2

The f in d(f, x) can be a tensor function. Gradient raises the rank by one.
F = (x+2y,3x+4y)
X = (x,y)
d(F,X)

13
 
1 2
3 4

2.3 Template functions

The function f in d(f ) does not have to be defined. It can be a template function with just a name
and an argument list. Eigenmath checks the argument list to figure out what to do. For example,
d(f (x), x) evaluates to itself because f depends on x. However, d(f (x), y) evaluates to zero because f
does not depend on y.
d(f(x),x)
d(f (x), x)
d(f(x),y)
0
d(f(x,y),y)
d(f (x, y), y)
d(f(),t)
d(f (), t)
As the final example shows, an empty argument list causes d(f ) to always evaluate to itself, regardless
of the second argument.
Template functions are useful for experimenting with differential forms. For example, let us check the
identity
div(curl F ) = 0
for an arbitrary vector function F .
F = (F1(x,y,z),F2(x,y,z),F3(x,y,z))
curl(U) = (d(U[3],y)-d(U[2],z),d(U[1],z)-d(U[3],x),d(U[2],x)-d(U[1],y))
div(U) = d(U[1],x)+d(U[2],y)+d(U[3],z)
div(curl(F))
0

2.4 Integral

integral(f, x) returns the integral of f with respect to x. The x can be omitted for expressions in x.
The argument list can be extended for multiple integrals.
integral(x^2)
1 3
x
3
integral(x*y,x,y)
1 2 2
x y
4

14
def int(f, x, a, b, . . .) computes the definite integral of f with respect to x evaluated from a to b. The
argument list can be extended for multiple integrals. The following example computes the√integral of
f = x2 over the domain of a semicircle. For each x along the abscissa, y ranges from 0 to 1 − x2 .
defint(x^2,y,0,sqrt(1-x^2),x,-1,1)
1
π
8
As an alternative, the eval function can be used to compute a definite integral step by step.
I = integral(x^2,y)
I = eval(I,y,sqrt(1-x^2))-eval(I,y,0)
I = integral(I,x)
eval(I,x,1)-eval(I,x,-1)
1
π
8
Here is a useful trick. Difficult integrals involving sine and cosine can often be solved by using
exponentials. Trigonometric simplifications involving powers and multiple angles turn into simple
algebra in the exponential domain. For example, the definite integral
Z 2π
sin4 t − 2 cos3 (t/2) sin t dt

0

can be solved as follows.


f = sin(t)^4-2*cos(t/2)^3*sin(t)
f = circexp(f)
defint(f,t,0,2*pi)
16 3
− + π
5 4
Here is a check of the result.
g = integral(f,t)
f-d(g,t)
0
The fundamental theorem of calculus is a formal expression of the inverse relation between integrals
and derivatives. Z b
f 0 (x) dx = f (b) − f (a)
a
Here is an Eigenmath demonstration of the fundamental theorem of calculus.
xrange = (-1,1)
yrange = (-1,1)
f = d(x^2/2)
draw(f,x)

15
f = integral(d(x^2/2))
draw(f,x)

The first graph shows that f 0 (x) is antisymmetric, therefore the total area under the curve from −1 to
1 sums to zero. The second graph shows that f (1) = f (−1). Hence for f (x) = 21 x2 we have
Z 1
f 0 (x) dx = f (1) − f (−1) = 0
−1

2.5 Arc length

Let g(t) be a function that draws a curve. The arc length from g(a) to g(b) is given by
Z b
|g 0 (t)| dt
a

where |g 0 (t)| is the length of the tangent vector at g(t). The integral sums over all of the tangent lengths
to arrive at the total length from a to b. For example, let us measure the length of the following curve.
xrange = (0,1)
yrange = (0,1)
draw(x^2)

16
A suitable g(t) for the arc is
g(t) = (t, t2 ), 0≤t≤1
Hence one Eigenmath solution for computing the arc length is
x = t
y = t^2
g = (x,y)
defint(abs(d(g,t)),t,0,1)
1 1
log(2 + 51/2 ) + 51/2
4 2
float
1.47894

As expected, the result is greater than 2 ≈ 1.414, the length of the diagonal from (0, 0) to (1, 1).
The result seems rather complicated given that we started with a simple parabola. Let us inspect
|g 0 (t)| to see why.
g
 
t
g= 2
t
d(g,t)
 
1
2t
abs(d(g,t))
(4t2 + 1)1/2
The following script does a discrete computation of the arc length by dividing the curve into 100 pieces.
g(t) = (t,t^2)
h(k) = abs(g(k/100.0) - g((k-1)/100.0))
sum(k,1,100,h(k))
1.47894
As expected, the discrete result matches the analytic result.
Find the length of the curve y = x3/2 from the origin to x = 43 .

17
x = t
y = x^(3/2)
g = (x,y)
defint(abs(d(g,x)),x,0,4/3)
56
27
Because of the way t is substituted for x, the following code yields the same result.
g = (t,t^(3/2))
defint(abs(d(g,t)),t,0,4/3)
56
27

2.6 Line integrals

There are two different kinds of line integrals, one for scalar fields and one for vector fields. The
following table shows how both are based on the calculation of arc length.
Abstract form Computable form
Z Z b
Arc length ds |g 0 (t)| dt
C a

Z Z b
Line integral, scalar field f ds f (g(t)) |g 0 (t)| dt
C a

Z Z b
Line integral, vector field (F · u) ds F (g(t)) · g 0 (t) dt
C a

For the vector field form, the symbol u is the unit tangent vector
g 0 (t)
u=
|g 0 (t)|
The length of the tangent vector cancels with ds as follows.
g 0 (t)
Z Z b   Z b
0
(F · u) ds = F (g(t)) · 0 |g (t)| dt = F (g(t)) · g 0 (t) dt
C a |g (t)| a

Evaluate Z Z
x ds and x dx
C C
where C is a straight line from (0, 0) to (1, 1).
What a difference the measure makes. The first integral is over a scalar field and the second is over a
vector field. This can be understood when we recall that
ds = |g 0 (t)| dt
R
Hence for C x ds we have

18
x = t
y = t
g = (x,y)
defint(x*abs(d(g,t)),t,0,1)
1
21/2
R
For C x dx we have
x = t
y = t
g = (x,y)
F = (x,0)
defint(dot(F,d(g,t)),t,0,1)
1
2
The following line integral problems are from Advanced Calculus, Fifth Edition by Wilfred Kaplan.
Evaluate y 2 dx along the straight line from (0, 0) to (2, 2).
R

x = 2t
y = 2t
g = (x,y)
F = (y^2,0)
defint(dot(F,d(g,t)),t,0,1)
8
3
z dx + x dy + y dz along the path x = 2t + 1, y = t2 , z = 1 + t3 , 0 ≤ t ≤ 1.
R
Evaluate
x = 2t+1
y = t^2
z = 1+t^3
g = (x,y,z)
F = (z,x,y)
defint(dot(F,d(g,t)),t,0,1)
163
30

2.7 Surface area

Let S be a surface parameterized by x and y. That is, let S = (x, y, z) where z = f (x, y). The
tangent lines at a point on S form a tiny parallelogram. The area a of the parallelogram is given by
the magnitude of the cross product.
∂S ∂S
a= ×
∂x ∂y
By summing over all the parallelograms we obtain the total surface area A. Hence
ZZ ZZ
A= dA = a dx dy

The following example computes the surface area of a unit disk parallel to the xy plane.

19
z = 2
S = (x,y,z)
a = abs(cross(d(S,x),d(S,y)))
defint(a,y,-sqrt(1-x^2),sqrt(1-x^2),x,-1,1)
π
The result is π, the area of a unit circle, which is what we expect. The following example computes
the surface area of z = x2 + 2y over a unit square.
z = x^2+2y
S = (x,y,z)
a = abs(cross(d(S,x),d(S,y)))
defint(a,x,0,1,y,0,1)
3 5
+ log(5)
2 8
The following exercise is from Multivariable Mathematics by Williamson and Trotter, p. 598. Find the
area of the spiral ramp defined by
 
u cos v
S =  u sin v  , 0 ≤ u ≤ 1, 0 ≤ v ≤ 3π
v
x = u*cos(v)
y = u*sin(v)
z = v
S = (x,y,z)
a = abs(cross(d(S,u),d(S,v)))
defint(a,u,0,1,v,0,3pi)
3 3π
π log(1 + 21/2 ) + 1/2
2 2
float
10.8177

2.8 Surface integrals

A surface integral is like adding up all the wind on a sail. In other words, we want to compute
ZZ
F · n dA

where F · n is the amount of wind normal to a tiny parallelogram dA. The integral sums over the
entire area of the sail. Let S be the surface of the sail parameterized by x and y. (In this model, the z
direction points downwind.) By the properties of the cross product we have the following for the unit
normal n and for dA.
∂S ∂S
∂x × ∂y

∂S ∂S
n = dA = × dx dy
∂S ∂S
∂x × ∂y ∂x ∂y

Hence ZZ ZZ  
∂S ∂S
F · n dA = F· × dx dy
∂x ∂y

20
The following exercise is from Advanced Calculus by Wilfred Kaplan, p. 313. Evaluate the surface
integral
ZZ
F · n dσ
S

where F = xy 2 zi − 2x3 j + yz 2 k, S is the surface z = 1 − x2 − y 2 , x2 + y 2 ≤ 1 and n is upper.


Note that the surface intersects the xy plane in a circle. By the right hand rule, crossing x into y yields
n pointing upwards hence  
∂S ∂S
n dσ = × dx dy
∂x ∂y
The following Eigenmath code computes the surface integral. The symbols f and h are used as
temporary variables.
z = 1-x^2-y^2
F = (x*y^2*z,-2*x^3,y*z^2)
S = (x,y,z)
f = dot(F,cross(d(S,x),d(S,y)))
h = sqrt(1-x^2)
defint(f,y,-h,h,x,-1,1)
1
π
48

2.9 Green’s theorem

Green’s theorem tells us that

I ZZ  
∂Q ∂P
P dx + Q dy = − dx dy
∂x ∂y

In other words, a line integral and a surface integral can yield the same result.
Example 1. The following exercise is from Advanced Calculus by Wilfred Kaplan, p. 287. Evaluate
(2x3 − y 3 ) dx + (x3 + y 3 ) dy around the circle x2 + y 2 = 1 using Green’s theorem.
H

It turns out that Eigenmath cannot solve the double integral over x and y directly. Polar coordinates
are used instead.
P = 2x^3-y^3
Q = x^3+y^3
f = d(Q,x)-d(P,y)
x = r*cos(theta)
y = r*sin(theta)
defint(f*r,r,0,1,theta,0,2pi)
3
π
2
The def int integrand is f ∗r because r dr dθ = dx dy.

21
Now let us try computing the line integral side of Green’s theorem and see if we get the same result.
We need to use the trick of converting sine and cosine to exponentials so that Eigenmath can find a
solution.
x = cos(t)
y = sin(t)
P = 2x^3-y^3
Q = x^3+y^3
f = P*d(x,t)+Q*d(y,t)
f = circexp(f)
defint(f,t,0,2pi)
3
π
2
Example 2. Compute both sides of Green’s theorem for F = (1 − y, x) over the disk x2 + y 2 ≤ 4.
First compute the line integral along the boundary of the disk. Note that the radius of the disk is 2.
-- Line integral
P = 1-y
Q = x
x = 2*cos(t)
y = 2*sin(t)
defint(P*d(x,t)+Q*d(y,t),t,0,2pi)

-- Surface integral
x = quote(x) --clear x
y = quote(y) --clear y
h = sqrt(4-x^2)
defint(d(Q,x)-d(P,y),y,-h,h,x,-2,2)

-- Try computing the surface integral using polar coordinates.
f = d(Q,x)-d(P,y) --do before change of coordinates
x = r*cos(theta)
y = r*sin(theta)
defint(f*r,r,0,2,theta,0,2pi)

defint(f*r,theta,0,2pi,r,0,2) --try integrating over theta first

In this case, Eigenmath solved both forms of the polar integral. However, in cases where Eigenmath
fails to solve a double integral, try changing the order of integration.

2.10 Stokes’ theorem

Stokes’ theorem says that in typical problems a surface integral can be computed using a line integral.
(There is some fine print regarding continuity and boundary conditions.) This is a useful theorem

22
because usually the line integral is easier to compute. In rectangular coordinates the equivalence
between a line integral on the left and a surface integral on the right is
I ZZ
P dx + Q dy + R dz = (curl F) · n dσ
S

where F = (P, Q, R). For S parametrized by x and y we have


 
∂S ∂S
n dσ = × dx dy
∂x ∂y

Example: Let F = (y, z, x) and let S be the part of the paraboloid z = 4 − x2 − y 2 that is above the xy
plane. The perimeter of the paraboloid is the circle x2 + y 2 = 2. The following script computes both
the line and surface integrals. It turns out that we need to use polar coordinates for the line integral
so that defint can succeed.
-- Surface integral
z = 4-x^2-y^2
F = (y,z,x)
S = (x,y,z)
f = dot(curl(F),cross(d(S,x),d(S,y)))
x = r*cos(theta)
y = r*sin(theta)
defint(f*r,r,0,2,theta,0,2pi)
-- Line integral
x = 2*cos(t)
y = 2*sin(t)
z = 4-x^2-y^2
P = y
Q = z
R = x
f = P*d(x,t)+Q*d(y,t)+R*d(z,t)
f = circexp(f)
defint(f,t,0,2pi)
This is the result when the script runs. Both the surface integral and the line integral yield the same
result.
−4π
−4π

3 Examples

3.1 François Viète

François Viète was the first to discover an exact formula for π. Here is his formula.
√ √ √
p q p
2 2 2+ 2 2+ 2+ 2
= × × × ···
π 2 2 2

23

Let a0 = 0 and an = 2 + an−1 . Then we can write
2 a1 a2 a3
= × × × ···
π 2 2 2
Solving for π we have

2 2 2 Y 2
π =2× × × × ··· = 2
a1 a2 a3 ak
k=1

Let us now use Eigenmath to compute π according to Viète’s formula. Of course, we cannot calculate
all the way out to infinity, we have to stop somewhere. It turns out that nine factors are just enough
to get six digits of accuracy.
a(n)=test(n=0,0,sqrt(2+a(n-1)))
float(2*product(k,1,9,2/a(k)))
3.14159
The function a(n) calls itself n times so overall there are 54 calls to a(n). By using a different algorithm
with temporary variables, we can get the answer in just nine steps.
a = 0
b = 2
for(k,1,9,a=sqrt(2+a),b=b*2/a)
float(b)
3.14159

3.2 Curl in tensor form

The curl of a vector function can be expressed in tensor form as


∂Fk
curl F = ijk
∂xj

where ijk is the Levi-Civita tensor. The following script demonstrates that this formula is equivalent
to computing curl the old fashioned way.

24
-- Define epsilon
epsilon = zero(3,3,3)
epsilon[1,2,3] = 1
epsilon[2,3,1] = 1
epsilon[3,1,2] = 1
epsilon[3,2,1] = -1
epsilon[1,3,2] = -1
epsilon[2,1,3] = -1
-- F is a generic vector function
F = (FX(),FY(),FZ())
-- A is the curl of F
A = outer(epsilon,d(F,(x,y,z)))
A = contract(A,3,4) --sum across k
A = contract(A,2,3) --sum across j
-- B is the curl of F computed the old fashioned way
BX = d(F[3],y)-d(F[2],z)
BY = d(F[1],z)-d(F[3],x)
BZ = d(F[2],x)-d(F[1],y)
B = (BX,BY,BZ)
-- Are A and B equal? Subtract to find out.
A-B
Here is the result when the script runs.
 
0
0
0
The following is a variation on the previous script. The product ijk ∂Fk /∂xj is computed in just one
line of code. In addition, the outer product and the contraction across k are now computed with a dot
product.
F = (FX(),FY(),FZ())
epsilon = zero(3,3,3)
epsilon[1,2,3] = 1
epsilon[2,3,1] = 1
epsilon[3,1,2] = 1
epsilon[3,2,1] = -1
epsilon[1,3,2] = -1
epsilon[2,1,3] = -1
A = contract(dot(epsilon,d(F,(x,y,z))),2,3)
BX = d(F[3],y)-d(F[2],z)
BY = d(F[1],z)-d(F[3],x)
BZ = d(F[2],x)-d(F[1],y)
B = (BX,BY,BZ)
-- Are A and B equal? Subtract to find out.
A-B
This is the result when the script runs.

25
 
0
0
0

3.3 Quantum harmonic oscillator

For total energy E, kinetic energy K and potential energy V we have

E =K +V

The corresponding formula for a quantum harmonic oscillator is

d2 ψ
(2n + 1)ψ = − + x2 ψ
dx2
where n is an integer and represents the quantization of energy values. The solution to the above
equation is
ψn (x) = exp(−x2 /2)Hn (x)
where Hn (x) is the nth Hermite polynomial in x. The following Eigenmath code checks E = K + V
for n = 7.
n = 7
psi = exp(-x^2/2)*hermite(x,n)
E = (2*n+1)*psi
K = -d(psi,x,x)
V = x^2*psi
E-K-V
0

3.4 Hydrogen wavefunctions

Hydrogen wavefunctions ψ are solutions to the differential equation


ψ 2ψ
2
= ∇2 ψ +
n r
where n is an integer representing the quantization of total energy and r is the radial distance of the
electron. The Laplacian operator in spherical coordinates is

∂2
   
2 1 ∂ 2 ∂ 1 ∂ ∂ 1
∇ = 2 r + 2 sin θ +
r ∂r ∂r r sin θ ∂θ ∂θ r2 sin2 θ ∂φ2

The general form of ψ is

|m|
ψ = rl e−r/n L2l+1
n−l−1 (2r/n)Pl (cos θ)eimφ

where L is a Laguerre polynomial, P is a Legendre polynomial and l and m are integers such that

1 ≤ l ≤ n − 1, −l ≤ m ≤ l

26
The general form can be expressed as the product of a radial wavefunction R and a spherical harmonic
Y.

|m|
ψ = RY, R = rl e−r/n L2l+1
n−l−1 (2r/n), Y = Pl (cos θ)eimφ

The following script checks E = K + V for n, l, m = 7, 3, 1.


laplacian(f) = 1/r^2*d(r^2*d(f,r),r)+
1/(r^2*sin(theta))*d(sin(theta)*d(f,theta),theta)+
1/(r*sin(theta))^2*d(f,phi,phi)
n = 7
l = 3
m = 1
R = r^l*exp(-r/n)*laguerre(2*r/n,n-l-1,2*l+1)
Y = legendre(cos(theta),l,abs(m))*exp(i*m*phi)
psi = R*Y
E = psi/n^2
K = laplacian(psi)
V = 2*psi/r
simplify(E-K-V)
This is the result when the script runs.
0

3.5 Space shuttle and Corvette

The space shuttle accelerates from zero to 17,000 miles per hour in 8 minutes. A Corvette accelerates
from zero to 60 miles per hour in 4.5 seconds. The following script compares the two.
vs = 17000*"mile"/"hr"
ts = 8*"min"/(60*"min"/"hr")
as = vs/ts
as
vc = 60*"mile"/"hr"
tc = 4.5*"sec"/(3600*"sec"/"hr")
ac = vc/tc
ac
"Time for Corvette to reach orbital velocity:"
vs/ac
vs/ac*60*"min"/"hr"
Here is the result when the script runs. It turns out that the space shuttle accelerates more than twice
as fast as a Corvette.
127500 mile
as =
(hr)2
48000 mile
ac =
(hr)2
Time for Corvette to reach orbital velocity:

27
0.354167 hr
21.25 min

3.6 Avogadro’s constant

There is a proposal to define Avogadro’s constant as exactly 84446886 to the third power. (Fox, Ronald
and Theodore Hill. “An Exact Value for Avogadro’s Number.” American Scientist 95 (2007): 104–107.)
The proposed number in the article is actually (84446888)3 . In a subsequent addendum the authors
reduced it to 844468863 to make the number divisible by 12. (See www.physorg.com/news109595312.html.)
This number corresponds to an ideal cube of atoms with 84,446,886 atoms along each edge. Let us check
the difference between the proposed value and the measured value of (6.0221415 ± 0.0000010) × 1023
atoms.
A = 84446886^3
B = 6.0221415*10^23
A-B
−5.17173 × 1016
0.0000010*10^23
1 × 1017
We see that the proposed value is within the experimental error. Just for the fun of it, let us factor
the proposed value.
factor(A)
23 × 33 × 16673 × 84433

3.7 Zero to the zero power

The following example draws a graph of the function f (x) = |xx |. The graph shows why the convention
00 = 1 makes sense.
f(x) = abs(x^x)
xrange = (-2,2)
yrange = (-2,2)
draw(f,x)

28
We can see how 00 = 1 results in a continuous line through x = 0. Now let us see how xx behaves in
the complex plane.
f(t) = (real(t^t),imag(t^t))
xrange = (-2,2)
yrange = (-2,2)
trange = (-4,2)
draw(f,t)

3.8 Euler’s identity

It is easy to “believe” that eiπ = −1 by looking at Taylor series expansions.


First, consider the Taylor series expansion of ey .

y2 y3 y4 y5 y6 y7
ey = 1 + y + + + + + + + ···
2! 3! 4! 5! 6! 7!
Next, substitute ix for y.

(ix)2 (ix)3 (ix)4 (ix)5 (ix)6 (ix)7


eix = 1 + ix + + + + + + + ···
2! 3! 4! 5! 6! 7!
x2 x3 x4 x5 x6 x7
= 1 + ix − −i + +i − − i + ···
2! 3! 4! 5! 6! 7!
Next, collect the real and imaginary terms.

x2 x4 x6 x3 x5 x7
   
ix
e = 1− + − + ··· + i x − + − + ···
2! 4! 6! 3! 5! 7!
= cos x + i sin x

Finally, substitute π for x.


eiπ = cos π + i sin π = −1
The following script checks the identity eix = cos x + i sin x for order n.
n = 7
E = taylor(e^y,y,n)
E = eval(E,y,i*x)
C = taylor(cos(x),x,n)
S = taylor(sin(x),x,n)
test(E=C+i*S,"true","false")

29
4 Built-in functions

abs

abs(x) returns the absolute value or vector length of x. The mag function should be used for complex
x.
P = (x,y)
abs(P)
(x2 + y 2 )1/2

adj

adj(m) returns the adjunct of matrix m.

and

and(a, b, . . .) returns the logical “and” of predicate expressions.

arccos

arccos(x) returns the inverse cosine of x.

arccosh

arccosh(x) returns the inverse hyperbolic cosine of x.

arcsin

arcsin(x) returns the inverse sine of x.

arcsinh

arcsinh(x) returns the inverse hyperbolic sine of x.

arctan

arcttan(x) returns the inverse tangent of x.

30
arctanh

arctanh(x) returns the inverse hyperbolic tangent of x.

arg

arg(z) returns the angle of complex z.

ceiling

ceiling(x) returns the smallest integer not less than x.

check

check(x) In a script, if the predicate x is true then continue, else stop.

choose
n
choose(n, k) returns
k

circexp

circexp(x) returns expression x with circular functions converted to exponential forms. Sometimes this
will simplify an expression.

coeff

coeff(p, x, n) returns the coefficient of xn in polynomial p.

cofactor

cofactor(m, i, j) returns of the cofactor of matrix m with respect to row i and column j.

conj

conj(z) returns the complex conjugate of z.

31
contract

contract(a, i, j) returns tensor a summed over indices i and j. If i and j are omitted then indices 1
and 2 are used. contract(m) is equivalent to the trace of matrix m.

cos

cos(x) returns the cosine of x.

cosh

cosh(x) returns the hyperbolic cosine of x.

cross

cross(u, v) returns the cross product of vectors u and v.

curl

curl(u) returns the curl of vector u.

d(f, x) returns the derivative of f with respect to x.

defint

defint(f, x, a, b, . . .) returns the definite integral of f with respect to x evaluated from a to b. The
argument list can be extended for multiple integrals. For example, d(f, x, a, b, y, c, d).

deg

deg(p, x) returns the degree of polynomial p in x.

denominator

denominator(x) returns the denominator of expression x.

32
det

det(m) returns the determinant of matrix m.

do

do(a, b, . . .) evaluates the argument list from left to right. Returns the result of the last argument.

dot

dot(a, b, . . .) returns the dot product of tensors.

draw

draw(f, x) draws the function f with respect to x.

erf

erf(x) returns the error function of x.

erfc

erf(x) returns the complementary error function of x.

eval

eval(f, x, n) returns f evaluated at x = n.

exp

exp(x) returns ex .

expand

expand(r, x) returns the partial fraction expansion of the ratio of polynomials r in x.


expand(1/(x^3+x^2),x)
1 1 1
2
− +
x x x+1

33
expcos

expcos(x) returns the cosine of x in exponential form.


expcos(x)
1 1
exp(−ix) + exp(ix)
2 2

expsin

expsin(x) returns the sine of x in exponential form.


expsin(x)
1 1
i exp(−ix) − i exp(ix)
2 2

factor

factor(n) factors the integer n.


factor(12345)
3 × 5 × 823
f actor(p, x) factors polynomial p in x. The last argument can be omitted for polynomials in x. The
argument list can be extended for multivariate polynomials. For example, factor(p, x, y) factors p over
x and then over y.
factor(125*x^3-1)
(5x − 1)(25x2 + 5x + 1)

factorial

Example:
10!
3628800

filter

filter(f, a, b, . . .) returns f with terms involving a, b, etc. removed.


1/a+1/b+1/c
1 1 1
+ +
a b c
filter(last,a)

34
1 1
+
b c

float

float(x) converts x to a floating point value.


sum(n,0,20,(-1/2)^n)
699051
1048576
float(last)
0.666667

floor

floor(x) returns the largest integer not greater than x.

for

for(i, j, k, a, b, . . .) For i equals j through k evaluate a, b, etc.


x = 0
y = 2
for(k,1,9,x=sqrt(2+x),y=2*y/x)
float(y)
3.14159

gcd

gcd(a, b, . . .) returns the greatest common divisor.

hermite

hermite(x, n) returns the nth Hermite polynomial in x.

hilbert

hilbert(n) returns a Hilbert matrix of order n.

35
imag

imag(z) returns the imaginary part of complex z.

inner

inner(a, b, . . .) returns the inner product of tensors. Same as the dot product.

integral

integral(f, x) returns the integral of f with respect to x.

inv

inv(m) returns the inverse of matrix m.

isprime

isprime(n) returns 1 if n is prime, zero otherwise.


isprime(2^53-111)
1

laguerre

laguerre(x, n, a) returns the nth Laguerre polynomial in x. If a is omitted then a = 0 is used.

lcm

lcm(a, b, . . .) returns the least common multiple.

leading

leading(p, x) returns the leading coefficient of polynomial p in x.


leading(5x^2+x+1,x)
5

36
legendre

legendre(x, n, m) returns the nth Legendre polynomial in x. If m is omitted then m = 0 is used.

log

log(x) returns the natural logarithm of x.

mag

mag(z) returns the magnitude of complex z.

mod

mod(a, b) returns the remainder of a divided by b.

not

not(x) negates the result of predicate expression x.

nroots

nroots(p, x) returns all of the roots, both real and complex, of polynomial p in x. The roots are
computed numerically. The coefficients of p can be real or complex.

numerator

numerator(x) returns the numerator of expression x.

or

or(a, b, . . .) returns the logical “or” of predicate expressions.

outer

outer(a, b, . . .) returns the outer product of tensors.

37
Example 1.      
1 1 1 0
⊗ =
0 0 0 0
outer((1,0),(1,0))
 
1 0
0 0

Example 2. From the identity X


|Ai = |iihi|Ai
i
it follows that X
|iihi| = I
i
For a two-state system with basis vectors
 √   √ 
1/√ 2 1/ √2
|1i = , and |2i =
i/ 2 −i/ 2
P
the following code computes |iihi|.
X1 = (1/sqrt(2),i/sqrt(2))
X2 = (1/sqrt(2),-i/sqrt(2))
outer(conj(X1),X1) + outer(conj(X2),X2)
 
1 0
0 1

The following code uses a different approach. First, tensor T is computed such that
 
|1ih1| |1ih2|
T =
|2ih1| |2ih2|
Then contraction is used to sum the diagonal elements.
X1 = (1/sqrt(2),i/sqrt(2))
X2 = (1/sqrt(2),-i/sqrt(2))
X = (X1,X2)
T = outer(conj(X),X)
T = transpose(T,2,3)
contract(T)
 
1 0
0 1
-- verify components of T
T[1,1] == outer(conj(X1),X1)
T[1,2] == outer(conj(X1),X2)
T[2,1] == outer(conj(X2),X1)
T[2,2] == outer(conj(X2),X2)
1
1
1
1

38
polar

polar(z) converts complex z to polar form.

prime

prime(n) returns the nth prime number, 1 ≤ n ≤ 10,000.

print

print(a, b, . . .) evaluates expressions and prints the results.. Useful for printing from inside a “for” loop.

product
k
Y
product(i, j, k, f ) returns f
i=j

quote

quote(x) returns expression x unevaluated.

quotient

quotient(p, q, x) returns the quotient of polynomials in x.

rank

rank(a) returns the number of indices that tensor a has. A scalar has no indices so its rank is zero.

rationalize

rationalize(x) puts everything over a common denominator.


rationalize(a/b+b/a)
a2 + b2
ab

39
real

real(z) returns the real part of complex z.

rect

rect(z) returns complex z in rectangular form.

roots

roots(p, x) returns the values of x such that the polynomial p(x) = 0. The polynomial should be
factorable over integers.

simplify

simplify(x) returns x in a simpler form.

sin

sin(x) returns the sine of x.

sinh

sinh(x) returns the hyperbolic sine of x.

sqrt

sqrt(x) returns the square root of x.

stop

In a script, it does what it says.

subst

subst(a, b, c) substitutes a for b in c and returns the result.

40
sum
k
X
sum(i, j, k, f ) returns f
i=j

tan

tan(x) returns the tangent of x.

tanh

tanh(x) returns the hyperbolic tangent of x.

taylor

taylor(f, x, n, a) returns the Taylor expansion of f of x at a. The argument n is the degree of the
expansion. If a is omitted then a = 0 is used.
taylor(1/cos(x),x,4)
5 4 1 2
x + x +1
24 2

test

test(a, b, c, d, . . .) If a is true then b is returned else if c is true then d is returned, etc. If the number
of arguments is odd then the last argument is returned when all else fails.

transpose

transpose(a, i, j) returns the transpose of tensor a with respect to indices i and j. If i and j are omitted
then 1 and 2 are used. Hence a matrix can be transposed with a single argument.
A = ((a,b),(c,d))
transpose(A)
 
a c
b d

unit

unit(n) returns an n × n identity matrix.


unit(2)

41
 
1 0
0 1

zero

zero(i, j, . . .) returns a null tensor with dimensions i, j, etc. Useful for creating a tensor and then
setting the component values.

42
5 Syntax

Math Eigenmath Alternate form and/or comment

−a -a

a+b a+b

a−b a-b

ab a*b a b with a space in between

a
a/b
b
a
a/b/c
bc

a2 a^2

a a^(1/2) sqrt(a)

1
√ a^(-1/2) 1/sqrt(a)
a

a(b + c) a*(b+c) a (b+c) with a space in between

f (a) f(a)
 
a
b (a,b,c)
c
 
a b
((a,b),(c,d))
c d

T 12 T[1,2] tensor component access

2 km 2*"km" units of measure are quoted

43
6 Tricks

1. The Eigenmath result field can be copied to the pasteboard by click, drag, then release.
2. The last result is stored in the symbol last.
3. In a script, setting trace=1 causes each line to be printed just before it is evaluated. Useful for
debugging.
4. Use contract(A) to get the mathematical trace of matrix A.
5. Calculations in a script can span multiple lines. The trick is to arrange things so the parser will
keep going. For example, if a calculation ends with a plus sign, the parser will go to the next
line to get another term. Also, the parser will keep going when it expects a close parenthesis.
6. Normally a function body is not evaluated when a function is defined. However, in some cases
it is required that the function body be the result of something. The trick is to use eval. For
example, the following code causes the function body to be a sixth order Taylor series expansion
of cos(x).
f(x) = eval(taylor(cos(x),x,6))
7. Use binding to see the unevaluated binding of a symbol.
binding(f)
8. This is how to clear a symbol.
f = quote(f)

44

You might also like