Eigenmath
Eigenmath
1
Contents
1 Introduction 5
2 Syntax 6
3 Symbols 8
4 Function definitions 10
5 Arithmetic 11
6 Complex numbers 12
7 Draw 13
8 Linear algebra 16
9 Component arithmetic 17
10 Quantum computing 18
11 Derivative 21
12 Template functions 22
13 Laplacian 23
14 Integral 24
15 Arc length 25
16 Line integral 26
17 Surface area 28
18 Surface integral 29
19 Green’s theorem 30
20 Stokes’s theorem 32
21 Feature index 33
22 Tricks 53
2
BSD 2-Clause License
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3
Commands are entered in the following field.
Multiple commands can be put together in a script. Scripts are run by clicking the Run
button.
Scripts go here
After a script runs, all of the results are available in command mode.
Note: Times New Roman and Times New Roman Italic fonts need to be the standard Mac
fonts that include special symbols and Greek letters. See the following link for correcting
font problems.
support.apple.com/guide/font-book/restore-fonts-that-came-with-your-mac-fb34862/mac
4
1 Introduction
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 cer-
tain Hindu calculator who in exactly two seconds could find the seventeenth root
of, say, 3529471145760275132301897342055866171392 (I am not sure I have got
this right; anyway the root was 212).
Example 1. Compute 21217 . (User input is shown in blue, results are shown in black.)
212^17
3529471145760275132301897342055866171392
N = 212^17
N
N = 3529471145760275132301897342055866171392
N^(1/17)
212
N > 2^64
5
2 Syntax
Arithmetic operators have the expected precedence of multiplication and division before
addition and subtraction. Subexpressions in parentheses have highest precedence.
Math Eigenmath Comment
a
a/b division
b
a
a/b/c division is left-associative
bc
a2 a^2 power
√
a sqrt(a) square root, also a^(1/2)
π pi
e exp(1) natural number
Parentheses are required around negative exponents. For example,
10^(-3)
instead of
6
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. Hence the following syntax is
required.
x^(-1/2)
In general, parentheses are always required when the exponent is an expression. For example,
x^1/2 is evaluated as (x1 )/2 which is probably not the desired result.
x^1/2
1
2
x
x^(1/2)
x1/2
7
3 Symbols
Symbols are defined with 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 = 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
xi = 1/2
xi
1
ξ= 2
Amu = 2.0
Amu
Aµ = 2.0
The following example shows how a symbol is scanned to find Greek letters.
alphamunu = 1
alphamunu
αµν = 1
Symbol definitions are evaluated serially until a terminal symbol is reached. The following
example sets A = B followed by B = C. Then when A is evaluated, the result is C.
A = B
B = C
A
8
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)
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.
A = quote(A)
A
9
4 Function definitions
The syntax for defining functions is
where arg-list is a comma separated list of zero to nine symbols that receive arguments.
Unlike symbol definitions, expr is not evaluated when function-name is defined. Instead,
expr is evaluated when function-name is used in a subsequent computation. The scope of
function arguments is the function definition expr.
Function definitions cannot be nested. In other words, function definition expr cannot contain
another function definition.
f(x) = sin(x)/x
f(pi/2)
2
π
After a user function is defined, expr can be recalled using the binding function.
binding(f)
sin(x)
x
To define a local symbol for use inside expr, extend the argument list. In the following
example, argument y is used as a local symbol. Note that function L is called without
supplying an argument for y.
h(f,x,a) = abs(eval(f,x,a))
h(cos(y),y,0)
10
5 Arithmetic
Big integer arithmetic is used so that numerical values can exceed machine size.
2^64
18446744073709551616
212^17
3529471145760275132301897342055866171392
1/2 + 1/3
5
6
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.
ε = 1.0 × 10−6
11
6 Complex numbers
√
Symbol i is initialized to −1.
Complex quantities can be entered in either rectangular or polar form.
a + i b
a + ib
exp(1/3 i pi)
exp 13 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 n
−n a − ib a − ib
(a + ib) = =
(a + ib)(a − ib) a2 + b 2
Here are a few examples.
1/(2 - i)
2
5
+ 15 i
(-1 + 3 i)/(2 - i)
−1 + i
The absolute value of a complex number returns its magnitude.
abs(3 + 4 i)
5
√
The imaginary unit can be changed from i to j by defining j = −1.
j = sqrt(-1)
sqrt(-4)
2j
12
7 Draw
draw(f,x) draws a graph of function f of x.
draw(x^2,x)
The vectors xrange and yrange control the scale of the graph.
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=(-pi,pi). In the following example, draw varies
theta over the default range −π to +π.
xrange = (-10,10)
yrange = (-10,10)
f = 5 (cos(theta),sin(theta))
draw(f,theta)
13
In the following example, trange is reduced to draw a quarter circle instead of a full circle.
trange = (0,pi/2)
f = 5 (cos(theta),sin(theta))
draw(f,theta)
Draw a lemniscate.
trange = (-pi,pi)
X = cos(t) / (1 + sin(t)^2)
Y = sin(t) cos(t) / (1 + sin(t)^2)
f = 5 (X,Y)
draw(f,t)
14
Draw a cardioid.
r = (1 + cos(t)) / 2
u = (cos(t),sin(t))
f = r u
xrange = (-1,1)
yrange = (-1,1)
trange = (0,2pi)
draw(f,t)
15
8 Linear algebra
dot(a,b,...) returns the inner product of vectors, matrices, and higher rank tensors. Also
known as the matrix product. Arguments are evaluated from right to left for maximum
efficiency when the rightmost argument is a vector.
A = ((a11,a12),(a21,a22))
X = (x1,x2)
dot(A,X)
" #
a11 x1 + a12 x2
a21 x1 + a22 x2
A = ((3,7),(1,-9))
B = (16,-22)
X = dot(inv(A),B)
X
" 5#
−
X = 4117
17
A = ((a,b),(c,d))
inv(A) == adj(A) / det(A)
16
9 Component arithmetic
Tensor plus scalar adds scalar to each tensor component.
A = ((a,b),(c,d))
A + 10
" #
a + 10 b + 10
c + 10 d + 10
A = ((a,b),(c,d))
A A
" #
a2 b 2
c2 d2
A = ((a,b),(c,d))
A^2
" #
a2 b 2
c2 d 2
17
10 Quantum computing
n
A quantum computer can be simulated by applying rotations to a unit vector u ∈ C2 where
C is the set of complex numbers and n is the number of qubits. The dimension is 2n because
a register with n qubits has 2n eigenstates. (Recall that an eigenstate is the output of a
quantum computer.) Quantum operations are “rotations” because they preserve |u| = 1.
Mathematically, a rotation of u is equivalent to the product Ru where R is a 2n × 2n matrix.
Eigenstates |j⟩ are represented by the following vectors. (Each vector has 2n elements.)
|0⟩ = (1, 0, 0, . . . , 0)
|1⟩ = (0, 1, 0, . . . , 0)
|2⟩ = (0, 0, 1, . . . , 0)
..
.
|2n − 1⟩ = (0, 0, 0, . . . , 1)
A quantum computer algorithm is a sequence of rotations applied to the initial state |0⟩. (The
sequence could be combined into a single rotation by associativity of matrix multiplication.)
Let ψf be the final state of the quantum computer after all the rotations have been applied.
Like any other state, ψf is a linear combination of eigenstates.
n −1
2X
ψf = cj |j⟩, cj ∈ C, |ψf | = 1
j=0
The last step is to measure ψf and get a result. Measurement rotates ψf to an eigenstate
|j⟩. The measurement result is |j⟩. The probability Pj of getting a specific result |j⟩ is
Pj = |cj |2 = cj c∗j
Note that if ψf is already an eigenstate then no rotation occurs. (The probability of observing
a different eigenstate is zero.) Since the measurement result is always an eigenstate, the
coefficients cj cannot be observed. However, the same calculation can be run multiple times
to obtain a probability distribution of results. The probability distribution is an estimate of
|cj |2 for each |j⟩ in ψf .
Unlike a real quantum computer, in a simulation the final state ψf , or any other state, is
available for inspection. Hence there is no need to simulate the measurement process. The
probability distribution of the result can be computed directly as
P = ψf ψf∗
where ψf ψf∗ is the Hadamard (element-wise) product of vector ψf and its complex conjugate.
Result P is a vector such that Pj is the probability of eigenstate |j⟩ and
n −1
2X
Pj = 1
j=0
18
Note: Eigenmath index numbering begins with 1 hence P[1] is the probability of |0⟩, P[2]
is the probability of |1⟩, etc.
The Eigenmath function rotate(u, s, k, . . .) rotates vector u and returns the result. Vector
u is required to have 2n elements where n is an integer from 1 to 15. Arguments s, k, . . . are
a sequence of rotation codes where s is an upper case letter and k is a qubit number from 0
to n − 1. Rotations are evaluated from left to right. The available rotation codes are
C, k Control prefix
H, k Hadamard
P, k, ϕ Phase modifier (use ϕ = 14 π for T rotation)
Q, k Quantum Fourier transform
V, k Inverse quantum Fourier transform
W, k, j Swap bits
X, k Pauli X
Y, k Pauli Y
Z, k Pauli Z
Control prefix C, k modifies the next rotation code so that it is a controlled rotation with
k as the control qubit. Use two or more prefixes to specify multiple control qubits. For
example, C, k, C, j, X, m is a Toffoli rotation. Fourier rotations Q, k and V, k are applied to
qubits 0 through k. (Q and V ignore any control prefix.)
Example: Verify the following truth table for quantum operator CNOT where qubit 0 is the
control and qubit 1 is the target. (Target is inverted when control is set.)
ket00 = (1,0,0,0)
ket01 = (0,1,0,0)
ket10 = (0,0,1,0)
19
ket11 = (0,0,0,1)
U(ket00) == ket00
U(ket01) == ket11
U(ket10) == ket10
U(ket11) == ket01
Here are some useful Eigenmath code snippets for setting up a simulation and computing
the result.
1. Initialize ψ = |0⟩.
n = 4 -- number of qubits (example)
N = 2^n -- number of eigenstates
psi = zero(N)
psi[1] = 1
2. Compute the probability distribution for state ψ.
P = psi conj(psi)
Hence
P[1] = probability that |0⟩ will be the result
P[2] = probability that |1⟩ will be the result
P[3] = probability that |2⟩ will be the result
..
.
P[N] = probability that |N − 1⟩ will be the result
3. (Only for macOS) Draw a probability distribution.
xrange = (0,N)
yrange = (0,1)
draw(P[ceiling(x)],x)
4. Compute an expectation value.
sum(k,1,N, (k - 1) P[k])
5. Make the high order qubit “don’t care.”
for(k,1,N/2, P[k] = P[k] + P[k + N/2])
Hence for N = 16
P[1] = probability that the result will be |0⟩ or |8⟩
P[2] = probability that the result will be |1⟩ or |9⟩
P[3] = probability that the result will be |2⟩ or |10⟩
..
.
P[8] = probability that the result will be |7⟩ or |15⟩
20
11 Derivative
d(f,x) returns the derivative of f with respect to x.
d(x^2,x)
2x
f = 1 / (x + y)
d(f,x,y)
2
(x + y)3
d(sin(x),x,x)
− sin(x)
d(sin(x),x,2)
− sin(x)
r = sqrt(x^2 + y^2)
d(r,(x,y))
x
(x2 + y 2 )1/2
y
2
(x + y )2 1/2
The f in d(f,x) can be a vector or higher rank function. Gradient increases rank by one.
F = (x^2,y^2)
X = (x,y)
d(F,X)
" #
2x 0
0 2y
21
12 Template functions
Function f in d(f,x) does not have to be defined, it can be a template function with
just a name and an argument list. The argument list determines the result. 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)
d(f(x,y),y)
d(f(),t)
d(f (), t)
Template functions are useful for working with differential forms. For example, show that
∇ · (∇ × F) = 0
F = (Fx(),Fy(),Fz())
div(curl(F))
22
13 Laplacian
The Laplacian ∇2 is the divergence of the gradient of scalar function f .
∂ 2f ∂ 2f ∂ 2f
∇2 f = ∇ · ∇f = + +
∂x2 ∂y 2 ∂z 2
div(grad(f()))
A = (Ax(),Ay(),Az())
div(grad(A)) - curl(curl(A)) == d(A,x,x) + d(A,y,y) + d(A,z,z)
Show that
∇ · ∇A = ∇(∇ · A)
div(grad(A)) == grad(div(A))
23
14 Integral
integral(f,x) returns the integral of f with respect to x.
integral(x^2,x)
1 3
3
x
f = x y
integral(f,x,y)
1 2 2
4
xy
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. Integrals involving sine and cosine can often be solved using exponen-
tials. For example, the definite integral
Z 2π
sin4 t − 2 cos3 (t/2) sin t dt
0
24
15 Arc length
Let g(t) be a parametric function that draws a curve in Rn . The arc length from g(a) to
g(b) is given by
Z b
|g ′ (t)| dt
a
′
where |g (t)| is the length of the tangent vector at g(t).
g = (t,t^2)
defint(abs(d(g,t)),t,0,1)
1
2
51/2 − 14 log(2) + 14 log(2 51/2 + 4)
float
1.47894
√
As expected, the result is greater than 2 ≈ 1.414, the length of a straight line from (0, 0)
to (1, 1).
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
Example 2. Find the length of the curve y = x3/2 from the origin to x = 34 .
g = (t,t^(3/2))
defint(abs(d(g,t)),t,0,4/3)
56
27
25
16 Line integral
There are two 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.
Z Z b
Line integral, scalar field f ds f (g(t)) |g ′ (t)| dt
C a
Z Z b
Line integral, vector field (F · u) ds F (g(t)) · g ′ (t) dt
C a
ds = |g ′ (t)| dt
g ′ (t)
u=
|g ′ (t)|
x = t
y = t
g = (x,y)
defint(x abs(d(g,t)), t, 0, 1)
1
21/2
R
Example 2. Evaluate C
x dx where C is a straight line from (0, 0) to (1, 1).
We have x dx = (F · u) ds hence
26
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.
R
Example 3. Evaluate y 2 dx along the straight line from (0, 0) to (2, 2).
The following solution parametrizes x and y so that the endpoint (2, 2) corresponds to t = 1.
x = 2 t
y = 2 t
g = (x,y)
F = (y^2,0)
defint(dot(F,d(g,t)), t, 0, 1)
8
3
R
Example 4. Evaluate z dx + x dy + y dz along the path x = 2t + 1, y = t2 , z = 1 + t3 ,
0 ≤ t ≤ 1.
x = 2 t + 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
27
17 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
Z Z Z Z
A= dA = a dx dy
The following example computes the surface area of a unit disk parallel to the xy plane.
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)
5 3
8
log(5) + 2
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 = circexp(abs(cross(d(S,u),d(S,v))))
defint(a,u,0,1,v,0,3pi)
3π
+ 32 π log 21/2 + 1
21/2
float
10.8177
28
18 Surface integral
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
The following exercise is from Advanced Calculus by Wilfred Kaplan, p. 313. Evaluate the
surface integral ZZ
F · n dσ
S
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 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
π
29
19 Green’s theorem
This is Green’s theorem.
I ZZ
∂Q ∂P
(P dx + Q dy) = − dx dy
∂x ∂y
In words, a line integral and a surface integral can yield the same result.
Example H1. 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.
Use polar coordinates to solve the surface integral.
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, 2 pi)
3
2
π
The defint integrand is f r because r dr dθ = dx dy.
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 the 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, 2 pi)
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)
30
8π
-- 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)
8π
8π
8π
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.
31
20 Stokes’s theorem
Stokes’s theorem equates a surface integral of the curl of a function with a line integral of
the same function. In rectangular coordinates the equivalence is
ZZ I
(curl F) · n dσ = (P dx + Q dy + R dz)
S
For 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 surface and line integrals. Polar coordinates are used 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)
z = quote(z) -- clear z for use by curl
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, 2 pi)
"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, 2 pi)
This is the result when the script runs. Both the surface integral and the line integral yield
the same result.
Surface integral
−4π
Line integral
−4π
32
21 Feature index
abs(x)
Returns the absolute value or vector length of x.
X = (x,y,z)
abs(X)
1/2
(x2 + y 2 + z 2 )
adj(m)
Returns the adjunct of matrix m. Adjunct is equal to determinant times inverse.
A = ((a,b),(c,d))
adj(A) == det(A) inv(A)
and(a, b, . . .)
Returns 1 if all arguments are true (nonzero). Returns 0 otherwise.
and(1=1,2=2)
arccos(x)
Returns the arc cosine of x.
arccos(1/2)
1
3
π
arccosh(x)
Returns the arc hyperbolic cosine of x.
arcsin(x)
Returns the arc sine of x.
arcsin(1/2)
1
6
π
33
arcsinh(x)
Returns the arc hyperbolic sine of x.
arctan(y, x)
Returns the arc tangent of y over x. If x is omitted then x = 1 is used.
arctan(1,0)
1
2
π
arctanh(x)
Returns the arc hyperbolic tangent of x.
arg(z)
Returns the angle of complex z.
arg(2 - 3i)
arctan(−3, 2)
binding(s)
The result of evaluating a symbol can differ from the symbol’s binding. For example, the
result may be expanded. The binding function returns the actual binding of a symbol.
p = quote((x + 1)^2)
p
p = x2 + 2x + 1
binding(p)
(x + 1)2
ceiling(x)
Returns the smallest integer greater than or equal to x.
ceiling(1/2)
34
check(x)
If x is true (nonzero) then continue, else stop. Expression x can include the relational
operators =, ==, <, <=, >, >=. Use the not function to test for inequality.
A = exp(i pi)
B = -1
check(A == B) -- stop here if A not equal to B
choose(n, k)
Returns the binomial coefficient n choose k.
2598960
circexp(x)
Returns expression x with circular and hyperbolic functions converted to exponentials.
circexp(cos(x) + i sin(x))
exp(ix)
clear
Clears all symbol definitions.
clock(z)
Returns complex z in polar form with base of negative 1 instead of e.
clock(2 - 3i)
131/2 (−1)arctan(−3,2)/π
cofactor(m, i, j)
Returns the cofactor of matrix m for row i and column j.
A = ((a,b),(c,d))
cofactor(A,1,2) == adj(A)[2,1]
35
conj(z)
Returns the complex conjugate of z.
conj(2 - 3i)
2 + 3i
contract(a, i, j)
Returns tensor a summed over indices i and j. If i and j are omitted then 1 and 2 are used.
The expression contract(m) computes the trace of matrix m.
A = ((a,b),(c,d))
contract(A)
a+d
cos(x)
Returns the cosine of x.
cos(pi/4)
1
21/2
cosh(x)
Returns the hyperbolic cosine of x.
circexp(cosh(x))
1
2
exp(−x) + 21 exp(x)
cross(u, v)
Returns the cross product of vectors u and v.
curl(v)
Returns the curl of vector v with respect to symbols x, y, and z.
36
d(f, x, . . .)
Returns the partial derivative of f with respect to x and any additional arguments.
d(sin(x),x)
cos(x)
d(sin(x),x,x)
− sin(x)
A numeric argument n computes the nth derivative with respect to the previous symbol.
d(sin(x y),x,2,y,2)
Argument f can be a tensor of any rank. Argument x can be a vector. When x is a vector
the result is the gradient of f .
F = (f(),g(),h())
X = (x,y,z)
d(F,X)
d(f (), x) d(f (), y) d(f (), z)
d(g(), x) d(g(), y) d(g(), z)
Symbol d can be used as a variable name. Doing so does not conflict with function d.
Symbol d can be redefined as a different function. The function derivative, a synonym for
d, can be used to obtain a partial derivative.
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 as shown in the following example.
f = (1 + cos(theta)^2) sin(theta)
-- integrate over theta then over phi
defint(f, theta, 0, pi, phi, 0, 2 pi)
16
3
π
37
denominator(x)
Returns the denominator of expression x.
denominator(a/b)
b
det(m)
Returns the determinant of matrix m.
A = ((a,b),(c,d))
det(A)
ad − bc
dim(a, n)
Returns the dimension of the nth index of tensor a. Index numbering starts with 1.
A = ((1,2),(3,4),(5,6))
dim(A,1)
3
div(v)
Returns the divergence of vector v with respect to symbols x, y, and z.
do(a, b, . . .)
Evaluates each argument from left to right. Returns the result of the final argument.
do(A=1,B=2,A+B)
3
dot(a, b, . . .)
Returns the dot product of vectors, matrices, and tensors. Also known as the matrix product.
Arguments are evaluated from right to left. The following example solves for X in AX = B.
A = ((1,2),(3,4))
B = (5,6)
X = dot(inv(A),B)
X
" #
−4
9
2
38
eigenvec(m)
Returns eigenvectors for matrix m. Matrix m is required to be numerical, real, and symmet-
ric. The return value is a matrix with each column an eigenvector. Eigenvalues are obtained
as shown.
A = ((1,2,3),(2,6,4),(3,4,5))
Q = eigenvec(A)
D = dot(transpose(Q),A,Q) -- eigenvalues on the diagonal of D
dot(Q,D,transpose(Q))
1 2 3
2 6 4
3 4 5
eval(f, x, a, y, b, . . .)
Returns f evaluated with x replaced by a, y replaced by b, etc. All arguments can be
expressions.
f = sqrt(x^2 + y^2)
eval(f,x,3,y,4)
P(2,-1)
− 21 cos(θ) sin(θ)
exp(x)
Returns the exponential of x.
exp(i pi)
−1
39
expcos(z)
Returns the cosine of z in exponential form.
expcos(z)
1
2
exp(iz) + 12 exp(−iz)
expcosh(z)
Returns the hyperbolic cosine of z in exponential form.
expcosh(z)
1
2
exp(−z) + 12 exp(z)
expsin(z)
Returns the sine of z in exponential form.
expsin(z)
− 12 i exp(iz) + 21 i exp(−iz)
expsinh(z)
Returns the hyperbolic sine of z in exponential form.
expsinh(z)
− 21 exp(−z) + 12 exp(z)
exptan(z)
Returns the tangent of z in exponential form.
exptan(z)
i i exp(2iz)
−
exp(2iz) + 1 exp(2iz) + 1
exptanh(z)
Returns the hyperbolic tangent of z in exponential form.
exptanh(z)
1 exp(2z)
− +
exp(2z) + 1 exp(2z) + 1
40
factorial(n)
Returns the factorial of n. The expression n! can also be used.
20!
2432902008176640000
float(x)
Returns expression x with rational numbers and integers converted to floating point values.
The symbol pi and the natural number are also converted.
float(212^17)
3.52947 × 1039
floor(x)
Returns the largest integer less than or equal to x.
floor(1/2)
for(i, j, k, a, b, . . .)
For i equals j through k evaluate a, b, etc.
for(k,1,3,A=k,print(A))
A=1
A=2
A=3
Note: The original value of i is restored after for completes. If symbol i is used for index
variable i then the imaginary unit is overridden in the scope of for.
grad(f )
Returns the gradient d(f,(x,y,z)).
grad(f())
d(f (), x)
d(f (), y)
d(f (), z)
41
hadamard(a, b, . . .)
Returns the Hadamard (element-wise) product.
X = (a,b,c)
hadamard(X,X)
2
a
2
b
c2
i
√
Symbol i is initialized to the imaginary unit −1.
exp(i pi)
−1
Note: It is ok to clear or redefine i and use the symbol for something else.
imag(z)
Returns the imaginary part of complex z.
imag(2 - 3i)
−3
infixform(x)
Converts expression x to a string and returns the result.
p = (x + 1)^2
infixform(p)
x^2 + 2 x + 1
inner(a, b, . . .)
Returns the inner product of vectors, matrices, and tensors. Also known as the matrix
product.
A = ((a,b),(c,d))
B = (x,y)
inner(A,B)
" #
ax + by
cx + dy
Note: inner and dot are the same function.
42
integral(f, x)
Returns the integral of f with respect to x.
integral(x^2,x)
1 3
3
x
inv(m)
Returns the inverse of matrix m.
A = ((1,2),(3,4))
inv(A)
" #
−2 1
3
2
− 21
j
Set j=sqrt(-1) to use j for the imaginary unit instead of i.
j = sqrt(-1)
1/sqrt(-1)
−j
kronecker(a, b, . . .)
Returns the Kronecker product of vectors and matrices.
A = ((1,2),(3,4))
B = ((a,b),(c,d))
kronecker(A,B)
a b 2a 2b
c d 2c 2d
3a 3b 4a 4b
3c 3d 3c 4d
43
last
The result of the previous calculation is stored in last.
212^17
3529471145760275132301897342055866171392
last^(1/17)
212
212^17
3529471145760275132301897342055866171392
float
3.52947 × 1039
log(x)
Returns the natural logarithm of x.
log(x^y)
y log(x)
mag(z)
Returns the magnitude of complex z. Function mag treats undefined symbols as real while
abs does not.
mag(x + i y)
(x2 + y 2 )1/2
minor(m, i, j)
Returns the minor of matrix m for row i and column j.
A = ((1,2,3),(4,5,6),(7,8,9))
minor(A,1,1) == det(minormatrix(A,1,1))
44
minormatrix(m, i, j)
Returns a copy of matrix m with row i and column j removed.
A = ((1,2,3),(4,5,6),(7,8,9))
minormatrix(A,1,1)
" #
5 6
8 9
noexpand(x)
Evaluates expression x without expanding products of sums.
x+1
not(x)
Returns 0 if x is true (nonzero). Returns 1 otherwise.
not(1=1)
nroots(p, x)
Returns the approximate roots of polynomials with real or complex coefficients. Multiple
roots are returned as a vector.
p = x^5 - 1
nroots(p,x)
1
−0.809017 + 0.587785 i
−0.809017 − 0.587785 i
0.309017 + 0.951057 i
0.309017 − 0.951057 i
numerator(x)
Returns the numerator of expression x.
numerator(a/b)
45
or(a, b, . . .)
Returns 1 if at least one argument is true (nonzero). Returns 0 otherwise.
or(1=1,2=2)
outer(a, b, . . .)
Returns the outer product of vectors, matrices, and tensors.
A = (a,b,c)
B = (x,y,z)
outer(A,B)
ax ay az
bx by bz
cx cy cz
pi
Symbol for π.
exp(i pi)
−1
polar(z)
Returns complex z in polar form.
polar(x - i y)
power
Use ^ to raise something to a power. Use parentheses for negative powers.
x^(-2)
1
x2
46
print(a, b, . . .)
Evaluate expressions and print the results. Useful for printing from inside a for loop.
for(j,1,3,print(j))
j=1
j=2
j=3
product(i, j, k, f )
For i equals j through k evaluate f . Returns the product of all f .
product(j,1,3,x + j)
x3 + 6x2 + 11x + 6
The original value of i is restored after product completes. If symbol i is used for index
variable i then the imaginary unit is overridden in the scope of product.
product(y)
Returns the product of components of y.
y = (1,2,3,4)
product(y)
24
quote(x)
Returns expression x without evaluating it first.
quote((x + 1)^2)
(x + 1)2
rank(a)
Returns the number of indices that tensor a has.
A = ((a,b),(c,d))
rank(A)
47
rationalize(x)
Returns expression x with everything over a common denominator.
rationalize(1/a + 1/b + 1/2)
2a + ab + 2b
2ab
Note: rationalize returns an unexpanded expression. If the result is assigned to a symbol,
evaluating the symbol will expand the result. Use binding to retrieve the unexpanded
expression.
f = rationalize(1/a + 1/b + 1/2)
binding(f)
2a + ab + 2b
2ab
real(z)
Returns the real part of complex z.
real(2 - 3i)
2
rect(z)
Returns complex z in rectangular form.
rect(exp(i x))
cos(x) + i sin(x)
roots(p, x)
Returns the rational roots of a polynomial. Multiple roots are returned as a vector.
p = (x + 1) (x - 2)
roots(p,x)
" #
−1
2
If no roots are found then nil is returned. A nil result is not printed so the following
example uses infixform to print nil as a string.
p = x^2 + 1
infixform(roots(p,x))
nil
48
rotate(u, s, k, . . .)
Rotates vector u and returns the result. Vector u is required to have 2n elements where n is
an integer from 1 to 15. Arguments s, k, . . . are a sequence of rotation codes where s is an
upper case letter and k is a qubit number from 0 to n − 1. Rotations are evaluated from left
to right. See the section on quantum computing for a list of rotation codes.
psi = (1,0,0,0)
rotate(psi,H,0)
1
21/2
1
21/2
0
0
run(x)
Run script x where x evaluates to a filename string. Useful for importing function libraries.
run("EVA2.txt")
For Eigenmath installed from the Mac App Store, run files need to be put in the directory
~/Library/Containers/eigenmath/Data/
simplify(x)
Returns expression x in a simpler form.
simplify(sin(x)^2 + cos(x)^2)
sin(x)
Returns the sine of x.
sin(pi/4)
1
21/2
49
sinh(x)
Returns the hyperbolic sine of x.
circexp(sinh(x))
− 12 exp(−x) + 12 exp(x)
sqrt(x)
Returns the square root of x.
sqrt(10!)
720 71/2
stop
In a script, it does what it says.
sum(i, j, k, f )
For i equals j through k evaluate f . Returns the sum of all f .
sum(j,1,5,x^j)
x5 + x4 + x3 + x2 + x
The original value of i is restored after sum completes. If symbol i is used for index variable
i then the imaginary unit is overridden in the scope of sum.
sum(y)
Returns the sum of components of y.
y = (1,2,3,4)
sum(y)
10
tan(x)
Returns the tangent of x.
simplify(tan(x) - sin(x)/cos(x))
50
tanh(x)
Returns the hyperbolic tangent of x.
circexp(tanh(x))
1 exp(2x)
− +
exp(2x) + 1 exp(2x) + 1
test(a, b, c, d, . . .)
If argument a is true (nonzero) then b is returned, else if c is true then d is returned, etc. If the
number of arguments is odd then the final argument is returned if all else fails. Expressions
can include the relational operators =, ==, <, <=, >, >=. Use the not function to test for
inequality. (The equality operator == is available for contexts in which = is the assignment
operator.)
A = 1
B = 1
test(A=B,"yes","no")
yes
trace
Set trace=1 in a script to print the script as it is evaluated. Useful for debugging.
trace = 1
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
Note: The argument list can be extended for multiple transpose operations. Arguments
are evaluated from left to right. For example, transpose(A,1,2,2,3) is equivalent to
transpose(transpose(A,1,2),2,3)
51
tty
Set tty=1 to show results in string format. Set tty=0 to turn off. Can be useful when
displayed results exceed window size.
tty = 1
(x + 1)^2
x^2 + 2 x + 1
unit(n)
Returns an n by n identity matrix.
unit(3)
1 0 0
0 1 0
0 0 1
zero(i, j, . . .)
Returns a null tensor with dimensions i, j, etc. Useful for creating a tensor and then setting
component values.
A = zero(3,3)
for(k,1,3,A[k,k]=k)
A
1 0 0
A = 0 2 0
0 0 3
52
22 Tricks
1. Use == to test for equality. In effect, A==B is equivalent to simplify(A-B)==0.
2. In a script, line breaking is allowed where the scanner needs something to complete an
expression. For example, the scanner will automatically go to the next line after an
operator.
3. Setting trace=1 in a script causes each line to be printed just before it is evaluated.
Useful for debugging.
8. Use float(pi) to get the floating point value of π. Set pi=float(pi) to evaluate
expressions with a numerical value for π. Set pi=quote(pi) to make π symbolic
again.
9. Assign strings to unit names so they are printed normally. For example, setting
meter="meter" causes the symbol meter to be printed as meter instead of meter .
10. Use expsin and expcos instead of sin and cos. Trigonometric simplifications occur
automatically when exponentials are used.
Aϕ1 = a1 ϕ1
Aϕ2 = a2 ϕ2
Verify that
a1 + a2 a1 − a2 (E1 − E2 )t
Z
∗
⟨A⟩ = ψ Aψ dx = + cos
2 2 ℏ
R R
Note: Because Rϕ1 and ϕ2 are normalized we have |ϕ1 |2 = |ϕ2 |2 = 1. By orthogo-
nality we have ϕ∗1 ϕ2 = 0. Hence the integral can be accomplished with eval.
1
See exercise 4-10 of Quantum Mechanics by Richard Fitzpatrick.
53
psi = (phi1 + phi2) / 2 exp(-i E1 t / hbar) +
(phi1 - phi2) / 2 exp(-i E2 t / hbar)
A = conj(psi) Apsi
54