Cubic sub-spline for data with derivatives
S(x) x∈[xi,xi+1] =Si(x)
whereSi(x)= yi +bi(x-xi) +ci(x-xi)2 +di(x-xi)3.
For each interval the three coefficients bi, ci, di are determined by the three conditions,
Si(xi+1)=yi+1,
S'i(xi)=y'i,
S'i(xi+1)=y'i+1.
See the subsection "Akima sub-spline interpolation" for the inspiration.
Si(x)= yi +bi(x-xi) +ci(x-xi)2 +di(x-xi)3 +ei(x-xi)2(x-xi+1)2 ,
and choosing the coefficients ei such that the spline has continuous second derivative.Yet another cubic sub-spline
Implement a sub-spline of this data using the following algorithm:
Si(x)= yi +bi(x-xi) +ci(x-xi)2 +di(x-xi)3,
where for each interval the three coefficients bi, ci, di are determined by the three conditions,
Si(xi+1)=yi+1,
S'i(xi)=pi,
S'i(xi+1)=pi+1.
Bi-linear interpolation on a rectilinear grid in two dimensions
The signature of the interpolating subroutine can be
double bilinear(int nx, int ny, double* x, double* y, double** F, double px, double py)
or
double bilinear(gsl_vector* x, gsl_vector* y, gsl_matrix* F, double px, double py)
Generalized eigenvalue problem
Ax=λNx,
where A is a real symmetric matrix and N is a real symmetric positive-definite matrix (all eigenvalues of a positive-definite matrix are positive).N=VDVT,
where D is the diagonal matrix with (positive) eigenvalues of the matrix N on the diagonal and V is the matrix with the corresponding eigenvectors as its columns. Now the original generalized eigenvalue problem can be represented as an ordinary eigenvalue problem (prove it analytically),By=λy,
where the real symmetric matrix B=√D-1VTAV√D-1, and y=√DVTx.Jacobi eigenvalue algorithm: several smallest (largest) eigenvalues.
Find out for how large n the full diagonalization becomes more effective.
Hessenberg factorization of a real square matrix using Jacobi transformations
If the constraints of a linear algebra problem do not allow a general matrix to be conveniently reduced to a triangular form, reduction to Hessenberg form is often the next best thing. Reduction of any matrix to a Hessenberg form can be achieved in a finite number of steps. Many linear algebra algorithms require significantly less computational effort when applied to Hessenberg matrices.
A lower Hessenberg matrix H is a square matrix that has zero elements above the first sup-diagonal, {Hij=0}j>i+1.
An upper Hessenberg matrix H is a square matrix that has zero elements above the first sub-diagonal, {Hij=0}i>j+1.
Hessenberg factorization is a representation of a square real matrix A in the form A=QHQT where Q is an orthogonal matrix and H is a Hessenberg matrix.
If A is a symmetrix matrix its Hessenberg factorization produces a tridiagonal matrix H.
Consider a Jacobi transformation with the matrix J(p,q),
A→J(p,q)TAJ(p,q)
where the rotation angle is chosen not to zero the element Ap,q but rather to zero the element Ap-1,q. Argue that the following sequence of such Jacobi transformations,
J(2,3), J(2,4), ..., J(2,n); J(3,4), ..., J(3,n); ...; J(n-1,n)
reduces the matrix to the lower Hessenberg form.
Implement this algorithm. Remember to accumulate the total transformation matrix.
Calculate the determinant of the Hessenberg matrix.
An upper n×n Hesseberg matrix H can be LU-factorized, H=LU, with the L-matrix being bidiagonal: made of a main diagonal (containing ones) and a subdiagonal with elements vi, i=1,..,n-1.
[ 1 0 ] [ h11 h12 ] = [ h11 h12 ] [ -v 1 ] [ h21 h22 ] [ -v*h11+h21 -v*h11+h22 ]If we choose v=h21/h11 then the matrix becomes upper triangular,
[ 1 0 ] [ h11 h12 ] = [ h11 h12 ] ≡ U [ -v 1 ] [ h21 h22 ] [ 0 -v*h11+h22 ]Now the inverse of the left matrix is easy:
[ 1 0 ] [ 1 0 ] = [ 1 0 ] [ -v 1 ] [ v 1 ] [ 0 1 ]Therefore the sought operation, which eliminates one sub-diagonal element of matrix H, is
[ h11 h12 ] = [ 1 0 ] [ u11 u12 ] [ h21 h22 ] [ v 1 ] [ 0 u22 ]where v=h21/h11, u11=h11, u12=h12, u22=h22-v*h11.
Now one simply has to apply this operation to all sub-diagonal elements of the upper Hessenber matrix H, which leads to the following algorithm (see A note on the stability of the LU factorization of Hessenberg matrices):
for i=1:n u1,i=h1,i endfor for i=1:n-1 vi=hi+1,i/ui,i for j=i+1:n ui+1,j=hi+1,j-vi*ui,j endfor endfor
Two-sided Jacobi algorithm for Singular Value Decomposition (SVD)
A = U D VT ,
where matrix D is diagonal with non-negative elements and matrices U and V are orghogonal. The diagonal elements of matrix D can always be chosen non-negative by multiplying the relevant columns of matrix U with (-1).SVD can be used to solve a number of problems in linear algebra.
A → JT A J
where J ≐ J(θ,p,q) is the Jacobi matrix (where the angle θ is chosen to eliminate the off-diagonal elements Apq=Aqp) to all upper off-diagonal matrix elements in cyclic sweeps until the matrix becomes diagonal.
The two-sided Jacobi SVD algorithm for general real square matrices is the same as the Jacobi eigenvalue algorithm, except that the elementary transformation is slightly different,
A → JT GT A J ,
where
tan(θ) = (Apq - Aqp)/(Aqq + App)
(check that this is correct).The matrices U and V are updated after each transformation as
Of course you should not actually multiply Jacobi matrices—which costs O(n³) operations—but rather perform updates which only cost O(n) operations.
One-sided Jacobi algorithm for Singular Value Decomposition (SVD)
A → A J(θ,p,q)
where the indices (p,q) are swept cyclicly (p=1..n, q=p+1..n) and where the angle θ is chosen such that the columns number p and q of the matrix AJ(θ,p,q) are orthogonal. One can show that the angle should be taken from the following equation,tan(2θ)=2apTaq /(aqTaq-apTap)
where ai is the i-th column of matrix A.After the iterations converge and the matrix A'=AJ (where J is the accumulation of the individual rotations) has orthogonal columns, the SVD is simply given as
A=UDVT
whereV=J, Dii=||a'i||, ui=a'i/||a'i||,
where a'i is the i-th column of matrix A' and ui us the i-th column of matrix U.Golub-Kahan-Lanczos bidiagonalization
Bidiagonalization can be used on its own to solve linear systems and ordinary least squares problems, calculate the determinant and the (pseudo-)inverse of a matrix. But it mostly is used as the first step in SVD.
Inverse iteration algorithm for eigenvalues (and eigenvectors)
Lanczos tridiagonalization algorithm
Implement the Lanczos algorithm (Lanczos interation) for real symmetric matrices.
Symmetric rank-1 update of a size-n symmetric eigenvalue problem
The matrix A to diagonalize is given in the form
A = D +uuT,
where D is a diagonal matrix and u is a column-vector.
Given the diagonal elements of the matrix D and the elements of the vector u find the eigenvalues of the matrix A using only O(n2) operations (see section "Eigenvalues of updated matrix" in the book).
Symmetric row/column update of a size-n symmetric eigenvalue problem
The matrix to diagonalize is given in the form
A = D + e(p) uT + u e(p)T
where D is a diagonal matrix with diagonal elements {dk, k=1,...,n}, u is a given column-vector, and the vector e(p) with components e(p)i=δip is a unit vector in the direction p where 1≤p≤n.
Given the diagonal elements of the matrix D, the vector u, and the integer p, calculate the eigenvalues of the matrix A using O(n2) operations (see section "Eigenvalues of updated matrix" in the book).
ODE: a two-step method
Implement a two-step stepper for solving ODE (as in the book). Use your own stepsize controlling driver.
ODE with complex-valued functions of complex variable
Generalize the ODE solver of your choice to solve ODE with complex-valued functions of complex variable along a straight line between the given start- and end-point.
Adaptive 1D integrator with random nodes
Implement an adaptive one-dimensional integrator with random abscissas. Reuse points. Note that you don't need to pass the points to the next level of recursion, only statistics.Adaptive integration of complex-valued functions
Implement an adaptive integrator which calculates the integral of a complex-valued function f(z) of a complex variable z along a straight line between two points in the complex plane.
Adaptive integrator with subdivision into three subintervals.
Implement a (one-dimensional) adaptive integrator which at each iteration subdivides the interval not into two, but into three sub-intervals.
Two-dimensional integrator
Implement a two-dimensional integrator for integrals in the form
∫abdx
∫d(x)u(x)dy
f(x,y)
which consecutively applies your favourite adaptive one-dimensional integrator along each of the two dimensions. The signature might be something like
double integ2D(double a, double b, double d(double x), double u(double x),
double f(double x, double y), double acc, double eps)
Multidimensional pseudo-random (plain Monte Carlo) vs quasi-random (Halton and/or lattice sequence) integrators
Investigate the convergence rates (of some interesting integrals in different dimensions) as function of the number of sample points.
Rootfinding: 1D complex vs. 2D real
Implement a (quasi) Newton method for rootfinding for a complex function f of complex variable z,
Hints: do it the ordinary way, just with complex numebers:
Compare the effectiveness of your complex implementation with your homework multi-dimensional implementation of real rootfinding applied to the equivalent 2D system of two real equation with two real variables x and y,
At the lectures we have constructed an ANN that can be trained to approximate a given tabulated function. Here you should build a topologically similar network that can be trained to approximate a solution to a one-dimenstional first-order ordinary differential equation,
y'=f(x,y) ,
on a given interval x∈[a,b] with a given initial condition y(x0)=y0. After training the response of the network to the input parameter x should approximate the sought solution y(x).Solve the following equations:
y'=y*(1-y), x∈[-5,5], y(0)=0.5 (logistic function)
y'=-x*y, x∈[-5,5], y(0)=1 (gaussian function)
Hints:
Fp'(x)=f(x,Fp(x)),
andFp(x0) = y0,
where Fp(x) is the response of the network to the input x, and Fp'(x) is its derivative with respect to x.double g(double x){ return exp(-x*x/2); /* a gaussian activation function (but you better use a gaussian wavelet instead) */ } double dg(double x){ return -x*exp(-x*x/2); /* its derivative */ }
void feed_forward(ann* network,double x,double* F, double* dF){ *F = ∑[i=1..n] wi*g((x-ai)/bi); *dF= ∑[i=1..n] wi*g'((x-ai)/bi)/bi; }
δ(p)=∑k=1..N |Fp'(xk) - f(xk,Fp(xk))|2 +N*|Fp(x0)-y0|2 ,
where p is the vector of parameters of the newtork; xk=a+(b-a)*(k-1)/(N-1)|k=1..N is a mesh of points spanning the interval [a,b]; {F(xk), F'(xk)} is the response of the network and its derivative at the input signal xk.