Introduction To Numerical Methods With Examples in Javascript
Introduction To Numerical Methods With Examples in Javascript
D.V. Fedorov
Department of Physics and Astronomy
Aarhus University
8000 Aarhus C, Denmark
ii
Permission is granted to copy and redistribute this work under the terms of
either the GNU General Public License1 , version 3 or later, as published by the
Free Software Foundation, or the Creative Commons Attribution Share Alike
License2 , version 3 or later, as published by the Creative Commons corporation.
This work is distributed in the hope that it will be useful, but without any
warranty. No responsibility is assumed by the author and the publisher for any
damage from any use of any methods, instructions or ideas contained in the
material herein.
1 http://en.wikipedia.org/wiki/GPL
2 http://en.wikipedia.org/wiki/CC-BY-SA
iii
Preface
This book evolved from lecture notes developed over several years of teaching
numerical methods at the University of Aarhus. It contains short descriptions of
the most common numerical methods together with program examples written
in Javascript. The latter was chosen simply because the it seems concise and
intuitive to me. The program examples are not tested or optimized in any way
other than to fit on one page of the book.
The text of the book is free as in freedom. You are permitted to copy and
redistribute the book in original or modified form either gratis or for a fee.
However, you must attribute the original author(s) and pass the same freedoms
to all recipients of your copies3 .
2010
Dmitri Fedorov
1 Linear equations 1
1.1 Triangular systems and back-substitution . . . . . . . . . . . . . 1
1.2 Reduction to triangular form . . . . . . . . . . . . . . . . . . . . 2
1.2.1 LU decomposition . . . . . . . . . . . . . . . . . . . . . . 2
1.2.2 QR decomposition . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Determinant of a matrix . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Matrix inverse . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 JavaScript implementations . . . . . . . . . . . . . . . . . . . . . 4
2 Interpolation 5
2.1 Polynomial interpolation . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Spline interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.1 Linear interpolation . . . . . . . . . . . . . . . . . . . . . 7
2.2.2 Quadratic spline . . . . . . . . . . . . . . . . . . . . . . . 7
2.2.3 Cubic spline . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 Other forms of interpolation . . . . . . . . . . . . . . . . . . . . . 9
4 Numerical integration 15
4.1 Classical quadratures with equally spaced abscissas . . . . . . . . 15
4.2 Quadratures with optimized abscissas . . . . . . . . . . . . . . . 16
4.3 Reducing the error by subdividing the interval . . . . . . . . . . 17
4.4 Adaptive quadratures . . . . . . . . . . . . . . . . . . . . . . . . 17
4.5 Gauss-Kronrod quadratures . . . . . . . . . . . . . . . . . . . . . 17
4.6 Integrals over infinite intervals . . . . . . . . . . . . . . . . . . . 18
4.6.1 Infinite intervals . . . . . . . . . . . . . . . . . . . . . . . 18
4.6.2 Half-infinite intervals . . . . . . . . . . . . . . . . . . . . . 19
v
vi CONTENTS
7 Nonlinear equations 31
7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
7.2 Newton’s method . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
7.3 Broyden’s quasi-Newton method . . . . . . . . . . . . . . . . . . 32
7.4 Javascript implementation . . . . . . . . . . . . . . . . . . . . . . 32
8 Optimization 35
8.1 Downhill simplex method . . . . . . . . . . . . . . . . . . . . . . 35
8.2 Javascript implementation . . . . . . . . . . . . . . . . . . . . . . 36
Linear equations
where x1 , x2 , . . . , xn are the unknown variables, A11 , A12 , . . . , Amn are the (con-
stant) coefficients of the system, and b1 , b2 , . . . , bm are the (constant) right-hand
side terms.
The system can be written in matrix form as
Ax = b . (1.2)
. .
where A = {Aij } is the m × n matrix of the coefficients, x = {xj } is the size-n
.
column-vector of the unknown variables, and b = {bi } is the size-m column-
vector of right-hand side terms.
Systems of linear equations occur regularly in applied mathematics. There-
fore the computational algorithms for finding solutions of linear systems are an
important part of numerical methods.
A system of non-linear equations can often be approximated by a linear
system, a helpful technique (called linearization) in creating a mathematical
model of an otherwise a more complex system.
If m = n, the matrix A is called square. A square system has a unique
solution if A is invertible.
1
2 CHAPTER 1. LINEAR EQUATIONS
For the lower triangular system the equivalent procedure is called forward sub-
stitution.
Note that a diagonal matrix – that is, a square matrix in which the elements
outside the main diagonal are all zero – is also a triangular matrix.
1.2.1 LU decomposition
LU decomposition is a factorization of a square matrix into a product of a lower
triangular matrix L and an upper triangular matrix U ,
A = LU . (1.5)
1.2.2 QR decomposition
QR decomposition is a factorization of a matrix into a product of an orthogonal
matrix Q, such that QT Q = 1 (where T denotes transposition), and a right
triangular matrix R,
A = QR . (1.7)
QR-decomposition can be used to convert the linear system Ax = b into the
triangular form
Rx = QT b, (1.8)
which can be solved directly by back-substitution.
1.3. DETERMINANT OF A MATRIX 3
Gram-Schmidt orthogonalization
Gram-Schmidt orthogonalization is an algorithm for orthogonalization of a set
of vectors in a given inner product space. It takes a linearly independent set
of vectors A = {a1 , . . . , am } and generates an orthogonal set Q = {q1 , . . . , qm }
which spans the same subspace as A. The algorithm is given as
f o r i = 1 to m
qi ← ai /kai k ( n o r m a l i z a t i o n )
f o r j = i + 1 to m
aj ← aj − haj , qi iqi ( o r t h o g o n a l i z a t i o n )
p
where ha, bi is the inner product of two vectors, and kak = ha, ai is the
vector’s norm. This variant of the algorithm, where all remaining vectors aj are
made orthogonal to qi as soon as the latter is calculated, is considered to be
numerically stable and is referred to as stabilized or modified.
Stabilized Gram-Schmidt orthogonalization can be used to compute QR de-
composition of a matrix A by orthogonalization of its column-vectors ai with
the inner product
Xn
ha, bi = aT b ≡ (a)k (b)k , (1.9)
k=1
where n is the length of column-vectors a and b, and (a)k is the kth element of
the column-vector.
i n p u t : m a tr i x A = {a1 , . . . , am } ( d e s t r o y e d )
o u tp u t : m a t r i c e s R , Q = {q1 , . . . , qm } : A = QR
for i = 1 . . . m
Rii = (aTi ai )
1/2
qi = ai /Rii
for j = i + 1 . . . m
Rij = qT i aj
aj = aj − qi Rij
function i n v e r s e (A) { // c a l c u l a t e s i n v e r s e o f m at r i x A
var [ Q,R]= q r d e c (A) ;
return [ q r b a c k (Q, R , [ ( k == i ? 1 : 0 ) f o r ( k i n A) ] ) f o r ( i i n A) ] ; } //
end i n v e r s e
Chapter 2
Interpolation
In practice one often meets a situation where the function of interest, f (x), is
only given as a discrete set of n tabulated points, {xi , yi = f (xi ) | i = 1 . . . n},
as obtained for example by sampling, experimentation, or expensive numerical
calculations.
Interpolation means constructing a (smooth) function, called interpolating
function, which passes exactly through the given points and hopefully approx-
imates the tabulated function in between the tabulated points. Interpolation
is a specific case of curve fitting in which the fitting function must go exactly
through the data points.
The interpolating function can be used for different practical needs like esti-
mating the tabulated function between the tabulated points and estimating the
derivatives and integrals involving the tabulated function.
function p i n t e r p ( x , y , z ) {
f o r ( var s =0 , i =0; i <x . l e n g t h ; i ++){
f o r ( var p=1 ,k =0; k<x . l e n g t h ; k++)
i f ( k!= i ) p∗=( z−x [ k ] ) / ( x [ i ]−x [ k ] )
s+=y [ i ] ∗ p}
return s }
5
6 CHAPTER 2. INTERPOLATION
1.0
0.8
0.6
0.4
y
0.2
0.0
−0.2
−0.4
−6 −4 −2 0 2 4 6
x
Figure 2.1: Lagrange interpolating polynomial, solid line, showing the Runge
phenomenon: large oscillations at the end-points. Dashed line shows a quadratic
spline.
Si (xi ) = yi , i = 1...n − 1
Si (xi+1 ) = yi+1 , i = 1...n − 1 , (2.3)
Si (x) = yi + bi (x − xi ) + ci (x − xi )2 + di (x − xi )3 , (2.11)
which automatically satisfies the upper half of continuity conditions (2.3). The
other half of continuity conditions (2.3) and the continuity of the first and second
derivatives (2.4) give
where
hi = xi+1 − xi . (2.13)
8 CHAPTER 2. INTERPOLATION
The set of equations (2.12) is a set of 3n − 5 linear equations for the 3(n − 1)
unknown coefficients {ai , bi , ci | i = 1, . . . , n − 1}. Therefore two more equations
should be added to the set to find the coefficients. If the two extra equations
are also linear, the total system is linear and can be easily solved.
The spline is called natural if the extra conditions are vanishing second
derivative at the end-points,
which gives
c1 = 0 ,
cn−1 + 3dn−1 hn−1 = 0 . (2.15)
where pi ≡ ∆y hi . The natural conditions (2.15) and the third equation in (2.12)
i
then produce the following tridiagonal system of n linear equations for the n
coefficients bi ,
2b1 + b2 = 3p1 ,
hi hi hi
bi + (2 + 2)bi+1 + bi+2 = 3(pi + pi+1 ) , i = 1, n − 2
hi+1 hi+1 hi+1
bn−1 + 2bn = 3pn−1 , (2.17)
This system can be solved by one run of Gauss elimination and then a run
of back-substitution. After a run of Gaussian elimination the system becomes
D̃1 Q1 0 0 ...
b1 B̃1
0 D̃2 Q2 0 ... .. ..
. .
0 0 D̃3 Q3 ...
= , (2.22)
.. .. .. .. ..
.. ..
. .
. . . . .
... ... 0 0 D̃n bn B̃n
where
D̃1 = D1 ; D̃i = Di − Qi−1 /Di−1 , i = 2, . . . , n (2.23)
and
B̃1 = B1 ; B̃i = Bi − Bi−1 /Di−1 , i = 2, . . . , n (2.24)
The triangular system (2.22) can be solved by a run of back-substitution,
1 1
bn = B̃n ; bi = (B̃i − Qi bi+1 ) , i = n − 1, . . . , 1 . (2.25)
D̃n D̃i
where the constants a, b, c, d are obtained from the condition that the inter-
polating function is equal the tabulated values at the nearest four tabulated
points.
10 CHAPTER 2. INTERPOLATION
Chapter 3
The problem (3.2) is called the linear least-squares problem and the vector c
that minimizes kAc − bk2 is called the least-squares solution.
kAc − bk2 = kQRc − bk2 = kRc − QT bk2 + k(1 − QQT )bk2 ≥ k(1 − QQT )bk2 .
(3.3)
11
12 CHAPTER 3. LINEAR LEAST SQUARES
The term k(1 − QQT )bk2 is independent of the variables c and can not be
reduced by their variations. However, the term kRc − QT bk2 can be reduced
down to zero by solving the m × m system of linear equations
Rc − QT b = 0 . (3.4)
The system is right-triangular and can be readily solved by back-substitution.
Thus the solution to the linear least-squares problem (3.2) is given by the
solution of the triangular system (3.4).
The objective of the least-squares fit is to minimize the square deviation, called
χ2 , between the fitting function and the experimental data,
n 2
2
X F (xi ) − yi
χ = . (3.6)
i=1
∆yi
Individual deviations from experimental points are weighted with their inverse
errors in order to promote contributions from the more precise measurements.
Minimization of χ2 with respect to the coefficiendt ck in (3.5) is apparently
equivalent to the least-squares problem (3.2) where
fk (xi ) yi
Aik = , bi = . (3.7)
∆yi ∆yi
If QR = A is the QR-decomposition of the matrix A, the formal least-squares
solution is
c = R−1 QT b . (3.8)
However in practice it is better to back-substitute the system Rc = QT b.
In a good experiment the deviations δyi are statistically independent and dis-
tributed normally with the standard deviations ∆yi . The deviations (3.9) are
then also distributed normally with variances,
X ∂ck 2 X 2
∂ck
hδck δck i = ∆yi = . (3.10)
∂yi ∂bi
i i
3.4. JAVASCRIPT IMPLEMENTATION 13
The standard errors in the fitting coefficients are then given as the square roots
of variances, v
uX ∂ck 2
u
p
∆ck = hδck δck i = t . (3.11)
i
∂bi
Covariances hδck δcq i are measures of to what extent the coefficients ck and cq
change together if the measured values yi are varied. The normalized covari-
ances,
hδck δcq i
p (3.13)
hδck δck ihδcq δcq i
are called correlations.
Using (3.12) and (3.8) the covariance matrix can be calculated as
T
∂c ∂c
Σ= = R−1 (R−1 )T = (RT R)−1 = (AT A)−1 . (3.14)
∂b ∂b
The square roots of the diagonal elements of this matrix provide the estimates
of the errors of the fitting coefficients and the (normalized) off-diagonal elements
are the estimates of their correlations.
Numerical integration
The abscissas xi and the weights wi in (4.1) are chosen such that the quadrature
is particularly well suited for the given class class of functions to integrate. Dif-
ferent quadratures use different strategies of choosing the abscissas and weights.
The weights wi can then be determined by solving the linear system (4.3).
If the functions to be integrated exactly are chosen as polynomials {1, x, x2 , . . . , xn−1 },
the quadrature is called Newton-Cotes quadrature. An n-point Newton-Cotes
15
16 CHAPTER 4. NUMERICAL INTEGRATION
quadrature can integrate exactly the first n terms of the function’s Taylor ex-
pansion
∞
X f (k) (a) k
f (a + t) = t . (4.4)
k!
k=0
f (n) (a) n
The nth order term n! t
will not be integrated exactly by an n-point quadra-
ture and will then result in the quadrature’s error2
1
Z h (n)
f (a) n f (n) (a) n+1
ǫn ≈ t dt = h . (4.6)
0 n! n!(n + 1)
If the function is smooth and the interval h is small enough the Newton-Cotes
quadrature can give a good approximation.
Here are several examples of closed and open classical quadratures:
Z h
f (x)dx ≈ 21 h [f (0) + f (h)] , (4.7)
0
Z h
1
f (0) + 4f ( 21 h) + f (h) ,
f (x)dx ≈ 6h (4.8)
0
Z h
1
1
f ( 3 h) + f ( 32 h) ,
f (x)dx ≈ 2h (4.9)
0
Z h
1
2f ( 61 h) + f ( 26 h) + f ( 46 h) + 2f ( 65 h) .
f (x)dx ≈ 6h (4.10)
0
2 Actually the error is often one order in h higher due to symmetry of the the polynomials
where δ is the absolute accuracy goal and ǫ is the relative accuracy goal of the
integration.
Otherwise the interval is subdivided into two half-intervals and the procedure
applies recursively to subintervals √with the same relative accuracy goal ǫ and
rescaled absolute accuracy goal δ/ 2.
The reuse of the function evaluations made at the previous step of adaptive
integration is very important for the efficiency of the algorithm. The equally-
spaced abscissas naturally provide for such a reuse.
var x = [ 1 / 6 , 2 / 6 , 4 / 6 , 5 / 6 ] ; // a b s c i s s a s
var w= [ 2 / 6 , 1 / 6 , 1 / 6 , 2 / 6 ] ; // w e i g h t s o f h i g h e r o r d e r q u a d r a t u r e
var v = [ 1 / 4 , 1 / 4 , 1 / 4 , 1 / 4 ] ; // w e i g h t s o f l o w e r o r d e r q u a d r a t u r e
var p = [ 1 , 0 , 0 , 1 ] ; // shows t h e new p o i n t s a t each r e c u r s i o n
var n=x . l e n g t h , h=b−a ;
i f ( t y p e o f ( o l d f s )==”u n d e f i n e d ” ) // f i r s t c a l l ?
f s =[ f ( a+x [ i ] ∗ h ) f o r ( i i n x ) ] ; // f i r s t c a l l : p o p u l a t e o l d f s
e l s e { // r e c u r s i v e c a l l : o l d f s ar e g i v e n
f s = new Array ( n ) ;
f o r ( var k=0 , i =0; i <n ; i ++){
i f ( p [ i ] ) f s [ i ]= f ( a+x [ i ] ∗ h ) ; // new p o i n t s
else f s [ i ]= o l d f s [ k ++];}} // r e u s e o f o l d p o i n t s
i f ( e r r <t o l ) // ar e we done ?
return [ q4 , e r r ] // yes , r e t u r n i n t e g r a l and e r r o r
e l s e { // t o o b i g e r r or , p r e p a r i n g t h e r e c u r s i o n
a c c /=Math . s q r t ( 2 . ) // r e s c a l e t h e a b s o l u t e ac c u r ac y g o a l
var mid=(a+b ) /2
var l e f t =[ f s [ i ] f o r ( i i n f s ) i f ( i < n / 2 ) ] // s t o r e t h e l e f t p o i n t s
var r g h t =[ f s [ i ] f o r ( i i n f s ) i f ( i >=n / 2 ) ] // s t o r e t h e r i g h t p o i n t s
var [ q l , e l ]= adapt ( f , a , mid , eps , acc , l e f t ) // d i s p a t c h two r e c u r s i v e
calls
var [ qr , e r ]= adapt ( f , mid , b , eps , acc , r g h t )
return [ q l+qr , Math . s q r t ( e l ∗ e l+e r ∗ e r ) ] // r e t u r n t h e grand
estimates
}
}
Alternatively,
Z +∞ 1
dt 1−t 1−t
Z
f (x)dx = f +f − . (4.17)
−∞ 0 t2 t t
Similarly,
a 0
t −1
Z Z
f (x)dx = f a− dt , (4.20)
−∞ −1 1+t (1 + t)2
Alternatively,
+∞ 1
1 − t dt
Z Z
f (x)dx = f a+ (4.21)
a 0 t t2
b 1
1 − t dt
Z Z
f (x)dx = f b− (4.22)
−∞ 0 t t2
20 CHAPTER 4. NUMERICAL INTEGRATION
Chapter 5
Monte Carlo integration is a cubature where the points, at which the integrand
is evaluated, are chosen randomly. Typically no assumptions are made about
the smoothness of the integrand, not even that it is continuous.
Plain Monte Carlo algorithm distributes points uniformly throughout the
integration region using either uncorrelated pseudo-random or correlated quasi-
random sequences of points.
Adaptive algorithms, such as VEGAS and MISER, distribute points non-
uniformly in an attempt to reduce integration error. They use correspondingly
importance and stratified sampling.
21
22 CHAPTER 5. MONTE CARLO INTEGRATION
where
V
∆Vi = (5.7)
N ρ(xi )
5.4. STRATIFIED SAMPLING 23
0.8
y 0.6
0.4
0.2
0
0 0.2 0.4 0.6 0.8 1
x
1 1
0.8 0.8
0.6 0.6
0.4 0.4
0.2 0.2
0 0
0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1
of the error. The error estimation is actually not trivial. In practice one can
employ two different sequences and use their difference as the error estimate.
Quasi-random sequences can be roughly divided into lattice rules and digital
nets (see e.g. arXiv:1003.4785 [math.NA] and references therein).
Ordinary differential
equations
6.1 Introduction
Many scientific problems can be formulated in terms of a system of ordinary
differential equations (ODE),
k = f (x0 , y0 ) . (6.4)
k0 = f (x0 , y0 ) ,
k1/2 = f (x0 + 12 h, y0 + 21 hk0 ) ,
k = k1/2 , (6.5)
27
28 CHAPTER 6. ORDINARY DIFFERENTIAL EQUATIONS
Similarly, one can use the two-step approximation (6.9) as a predictor, and
then improve it by one order with a correction step, namely
The coefficient d can be found from the condition ȳ ¯ ′ (x1 ) = f̄1 , where f̄1 =
f (x1 , ȳ(x1 )),
f̄1 − y0′ − 2c · (x1 − x0 )
d= . (6.14)
2(x1 − x0 )(x1 − x−1 ) + (x1 − x0 )2
Equation (6.13) gives a better estimate, y1 = ȳ¯ (x1 ), of the function at the point
x1 .
In this context the formula (6.9) is referred to as predictor, and (6.13) as
corrector. The difference between the two gives an estimate of the error.
where p is the order of the algorithm used. It is better to pick formulas where
the full-step and two half-step calculations share the evaluations of the function
f (x, y).
Another possibility is to make the same step with two methods of different
orders, the difference between the solutions providing an estimate of the error.
In a predictor-corrector method the correction itself can serve as the estimate
of the error.
In practice one uses the current values of the function y in the estimate of
the tolerance, r
hi
τi = (ǫkyi k + δ) (6.19)
b−a
The step is accepted if the error is smaller than tolerance. The next step-size
can be estimated according to the empirical prescription
τ Power
hnew = hold × × Safety, (6.20)
e
where Power ≈ 0.25, Safety ≈ 0.95. If the error ei is larger than tolerance τi
the step is rejected and a new step with the new step size (6.20) is attempted.
Nonlinear equations
7.1 Introduction
Non-linear equations or root-finding is a problem of finding a set of n variables
{x1 , . . . , xn } which satisfy n equations
fi (x1 , ..., xn ) = 0 , i = 1, . . . , n , (7.1)
where the functions fi are generally non-linear.
31
32 CHAPTER 7. NONLINEAR EQUATIONS
(J + δJ)δx = δf , (7.7)
δJ = c δxT , (7.8)
where the unknown vector c can be found by substituting (7.8) into (7.7), which
gives
δf − Jδx T
δJ = δx . (7.9)
kδxk2
f o r ( i i n x ) f o r ( k i n x ) { // c a l c u l a t e J ac ob i an
x [ k]+=dx
J [ k ] [ i ]=( f s [ i ] ( x )+m i n u s f x [ i ] ) / dx
x [ k]−=dx }
var [Q,R]= q r d e c ( J ) , Dx=q r b a c k (Q, R, m i n u s f x ) // Newton ’ s s t e p
var s=2
do{ // s i m p l e b a c k t r a c k i n g l i n e s e a r c h
s=s / 2 ;
var z =[x [ i ]+ s ∗Dx [ i ] f o r ( i i n x ) ]
var m i n u s f z =[− f s [ i ] ( z ) f o r ( i i n x ) ]
} while ( norm ( m i n u s f z )>(1−s / 2 ) ∗norm ( m i n u s f x ) && s > 1 . / 1 2 8 )
m i n u s f x=m i n u s f z ; x=z ; // s t e p done
} while ( norm ( m i n u s f x )>a c c )
return x ;
} // end newton
34 CHAPTER 7. NONLINEAR EQUATIONS
Chapter 8
Optimization
35
36 CHAPTER 8. OPTIMIZATION
Eigenvalues and
eigenvectors
9.1 Introduction
A non-zero column-vector v is called an eigenvector of a matrix A with an
eigenvalue λ, if
Av = λv . (9.1)
If an n × n matrix A is real and symmetric, AT = A, then it has n real
eigenvalues λ1 , . . . , λn , and its (orthogonalized) eigenvectors V = {v1 , . . . , vn }
form a full basis,
V V T = V TV = 1 , (9.2)
in which the matrix is diagonal,
λ1 0 ··· 0
..
0 λ2 .
V T AV =
.
. (9.3)
.. ..
.
0 ··· λn
A → QT AQ , (9.4)
A → S −1 AS , (9.5)
37
38 CHAPTER 9. EIGENVALUES AND EIGENVECTORS
The orthogonal matrix J(p, q) which eliminates the element Apq is called the
Jacobi rotation matrix. It is equal identity matrix except for the four elements
with indices pp, pq, qp, and qq,
1
..
. 0
cos φ · · · sin φ ← row p
. .
J(p, q) =
.. .. ..
. (9.7)
.
← row q
− sin φ · · · cos φ
..
0 .
1
Or explicitly,
A′ij = Aij ∀ i 6= p, q ∧ j 6= p, q
A′pi = A′ip = cApi − sAqi ∀ i 6= p, q ;
A′qi = A′iq = sApi + cAqi ∀ i 6= p, q ;
A′pp = c2 App − 2scApq + s2 Aqq ;
A′qq = s2 App + 2scApq + c2 Aqq ;
A′pq = A′qp = sc(App − Aqq ) + (c2 − s2 )Apq , (9.9)
where c ≡ cos φ, s ≡ sin φ. The angle φ is chosen such that after rotation the
matrix element A′pq is zeroed,
Aqq − App
cot(2φ) = ⇒ A′pq = 0 . (9.10)
2Apq
rows and columns with indices equal to p and q. However, after the Jacobi
rotation the sum of squares of all off-diagonal elemens is reduced. The algorithm
repeatedly performs rotations until the off-diagonal elements become sufficiently
small.
The convergence of the Jacobi method can be proved for two strategies for
choosing the order in which the elements are zeroed:
1. Classical method: with each rotation the largest of the remaining off-
diagonal elements is zeroed.
2. Cyclic method: the off-diagonal elements are zeroed in strict order, e.g.
row after row.
Vij → Vij , j 6= p, q
Vip → cVip − sViq (9.11)
Viq → sVip + cViq
Alternatively, if only one (or few) eigenvector vk is needed, one can instead
solve the (singular) system (A − λk )v = 0.
The iteration converges to the eigenvector of the largest eigenvalue. The eigen-
value can be estimated using the Rayleigh quotient
(A − λ)xi+1 = xi (9.16)
function r o t a t e ( p , q , A,V) { // J a c o b i r o t a t i o n e l i m i n a t i n g A pq .
// Only upper t r i a n g l e o f A i s u pdat e d .
// The m at r i x o f e i g e n v e c t o r s V i s a l s o u pdat e d .
i f ( q<p ) [ p , q ] = [ q , p ]
var n=A. l e n g t h , app = A[ p ] [ p ] , aqq = A[ q ] [ q ] , apq = A[ q ] [ p ] ;
var p h i =0.5∗ Math . a ta n 2 (2 ∗ apq , aqq−app ) ; // c o u l d be done b e t t e r
var c=Math . c o s ( p h i ) , s=Math . s i n ( p h i ) ;
A[ p ] [ p ] = c ∗ c ∗ app + s ∗ s ∗ aqq − 2 ∗ s ∗ c ∗ apq ;
A[ q ] [ q ] = s ∗ s ∗ app + c ∗ c ∗ aqq + 2 ∗ s ∗ c ∗ apq ;
A[ q ] [ p ] = 0 ;
9.4. JAVASCRIPT IMPLEMENTATION 41
10.1 Introduction
When calculating an eigenvalue of a matrix A using the power method, one starts
with an initial random vector b and then computes iteratively the sequence
Ab, A2 b, . . . , An−1 b normalising and storing the result in b on each iteration.
The sequence converges to the eigenvector of the largest eigenvalue of A.
The set of vectors
Kn = b, Ab, A2 b, . . . , An−1 b ,
(10.1)
where n < rank(A), is called the order-n Krylov matrix, and the subspace
spanned by these vectors is called the order-n Krylov subspace. The vectors are
not orthogonal but can be made so e.g. by Gram-Schmidt orthogonalisation.
For the same reason that An−1 b approximates the dominant eigenvector one
can expect that the other orthogonalised vectors approximate the eigenvectors
of the n largest eigenvalues.
Krylov subspaces are the basis of several successful iterative methods in
numerical linear algebra, in particular: Arnoldi and Lanczos methods for finding
one (or a few) eigenvalues of a matrix; and GMRES (Generalised Minimum
RESidual) method for solving systems of linear equations.
These methods are particularly suitable for large sparse matrices as they
avoid matrix-matrix operations but rather multiply vectors by matrices and
work with the resulting vectors and matrices in Krylov subspaces of modest
sizes.
43
44 CHAPTER 10. POWER METHOD AND KRYLOV SUBSPACES
The DFT represent the amplitude and phase of the different sinusoidal com-
ponents in the input data xn .
The DFT is widely used in different fields, like spectral analysis, data com-
pression, solution of partial differential equations and others.
45
46 CHAPTER 11. FAST FOURIER TRANSFORM
11.1.1 Applications
Data compression
Several lossy (that is, with certain loss of data) image and sound compression
methods employ DFT as an approximation for the Fourier series. The signal
is discretized and transformed, and then the Fourier coefficients of high/low
frequencies, which are assumed to be unnoticeable, are discarded. The decom-
pressor computes the inverse transform based on this reduced number of Fourier
coefficients.
where c(even) and c(odd) are the DFTs of the even- and odd-numbered sub-sets
of x.
11.3. MULTIDIMENSIONAL DFT 47
11.4 C implementation
#i n c l u d e <complex . h>
#i n c l u d e <tgmath . h>
#d e f i n e PI 3 . 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5 0 2 8 8
v o i d d f t ( i n t N, complex ∗ x , complex ∗ c , i n t s i g n ) {
complex w = cexp ( s i g n ∗2∗ PI ∗ I /N) ;
f o r ( i n t k =0;k<N; k++){
complex sum=0; f o r ( i n t n=0;n<N; n++) sum+=x [ n ] ∗ cpow (w, n∗k ) ;
c [ k]=sum/ s q r t (N) ; }
}
v o i d f f t ( i n t N, complex ∗ x , complex ∗ c , i n t s i g n ) {
i f (N%2==0){
complex w = exp ( s i g n ∗2∗ PI ∗ I /N) ;
i n t M=N/ 2 ;
complex xo [M] , co [M] , xe [M] , c e [M] ; //VLA: c om pi l e w i t h −s t d=c99
f o r ( i n t m=0;m<M;m++){ xo [m]=x [ 2 ∗m+ 1 ] ; xe [m]=x [ 2 ∗m] ; }
f f t (M, xo , co , s i g n ) ; f f t (M, xe , ce , s i g n ) ;
f o r ( i n t k =0; k<M; k++) c [ k ]=( c e [ k]+cpow (w, k ) ∗ co [ k ] ) / s q r t ( 2 ) ;
f o r ( i n t k=M; k<N; k++) c [ k ]=( c e [ k−M]+cpow (w, k ) ∗ co [ k−M] ) / s q r t ( 2 ) ;
}
e l s e d f t (N, x , c , s i g n ) ;
}
48 CHAPTER 11. FAST FOURIER TRANSFORM
Index
Arnoldi iteration, 43
back substitution, 1
backtracking line search, 32
cubature, 15
Danielson-Lanczos lemma, 47
eigenvalue, 37
eigenvector, 37
forward substitution, 2
GMRES, 44
Jacobi rotation, 38
Jacobian matrix, 32
Krylov matrix, 43
Krylov subspace, 43
Lanczos iteration, 44
line search, 32
LU decomposition, 2
matrix diagonalization, 37
Newton-Cotes quadrature, 15
orthogonal transformation, 37
QR decomposition, 2
quadrature, 15
Rayleigh quotient, 40
root-finding, 31
similarity transformation, 37
triangular system, 1
49