CS 421: Assignment 2 - Cornell Computer Science

CS 421: Assignment 2
Due: Wednesday, October 1, 2003 (In Lecture or in Upson 4130 by 4pm)
Do not submit work unless you have adhered to the principles of academic integrity as descibed on the course website:
http://www.cs.cornell.edu/cv/CS421/421home.htm
Points will be deducted for poorly commented code, redundant computation that seriously effects efficiency, and failure
to use features of Matlab that are part of the course syllabus. In particular, use vector operations whenever possible.
Test scripts (as mentioned below) will be available on the course website by Monday, September 22.
Problem 1 (5 pts) Stationary Vectors
Suppose 0 ≤ θ1 < · · · < θn < 2π and define
vi =
cos(θi )
sin(θi )
.
Imagine a Markov process where the probability of traveling from vj to vi in a given time step is

 0
if i = j
1/dij
pij =
otherwise

1/d1j + · · · + 1/dj−1,j + 1/dj+1,j + · · · + 1/dn,j
where dij = vi − vj 2 . Note that P = (pij ) is a stochastic matrix and that in this particular Markov model,
you are more likely to travel to nearer points.
We wish to determine a nonvero vector v such that P v = v. In this case we say that v is a stationary
vector. Finding a stationary vector is tantamount to finding a nonzero vector in the nullspace of A = I − P .
To appreciate that this is possible, just observe that the vector of all ones is a null vector for AT . (If AT is
singular the same can be said of A.) For the matrix P above, it turns out that there is a unique stationary
vector v with vi ≥ 0 and v1 + ... + vn = 1. Think of vi as the fraction of people at point i and that when the
fractions are so distributed, a steady state has been reached.
One way to compute the normalized stationary vector is to use the SVD, for if A ∈ IRn×n is singular and
A = U ΣV T is its SVD, then σn = 0 and AV (:, n) = 0. A cheaper method is to compute the LU factorization
A = LU . It can be shown that A has a stable LU factorization with the property that all the uii are nonzero
except unn. Figure out how to compute a null vector v of an upper triangular matrix that has this property.
It follows that Av = (LU )v = L(U v) = 0.
Referring to the matrix P above as P (θ), complete the following functions so that they perform as specified:
function P = SetUp(theta)
% theta is a column n-vector with 0 <= theta(1) < ... < theta(n) < 2pi
% P is the n-by-n stochastic matrix P(theta)
%
%
%
%
function
theta is
v is the
sigma is
Uses the
[v,sigma] = StatVec1(theta)
a column n-vector with 0 <= theta(1) < ... < theta(n) < 2pi
normalized stationary vector for P(theta)
the column n-vector of the singuar values of P(theta).
SVD method
%
%
%
%
function
theta is
v is the
Ucond is
Uses the
[v,Ucond] = StatVec2(theta)
a column n-vector with 0 <= theta(1) < ... < theta(n) < 2pi
normalized stationary vector for P(theta)
the condition of U(1:,n-1,1:n-1) where (I - P(theta)) = LU
LU method
Test your implementations with P1. Feel free to use cond and SVD and LU.
Problem 2 (5 pts) Losing Positive Definiteness
Here are three properties of a symmetric positive definite matrix A ∈ IRn×n:
• A is nonsingular but not necessarily well-conditioned.
• akk > 0, for k = 1:n.
• A(1:k, 1:k) is positive definite for k = 1:n.
It follows that if we reduce the value of akk enough, then A(1:k, 1:k) will no longer be positive definite.
Complete the following function so that it performs as speciifed
function d = PosDefD(A)
% A is an n-by-n symmetric positive definite matrix.
% d is a column n-vector with the property that A(1:k,1:k) - d(k)ek*ek’
% is singular where ek is the last column of eye(k,k).
Test your implementation with P2. Hint: Think about the computation of gkk in the Cholesky factorization
A = GGT . Make use of the Matlab function chol that returns GT .
Problem 3 (5 pts) Vectorizing Multiple Systems
Matlab supports multidimensional arrays. Thus, A = rand(n,p,m) is a random n-by-p-by-m array. The
“slices” A(:,:,1),...,A(:,:,m) are n-by-p matrices. The computation v = A(2,1,:)./A(1,1,:) produces
the 1-by-1-by-m array with v(1,1,k) = A(2,1,k)/A(1,1,k), k=1:m. This problem is about vectorizing the
solution of multiple linear systems which are “stacked” inside a 3D array. Here is a non-vectorized solution:
function X = Solve(A)
% A is a n-by-(n+1)-by-m array such that A(:,1:n,k) is nonsingular, k=1:m
% X is a n-by-m with the property that A(:,1:n,k)X(:,k) = A(:,n+1,k), k=1:m
[n,np1,m] = size(A);
X = zeros(n,m);
for k=1:m
X(:,k) = A(:,1:n,k)\A(:,n+1,k);
end
The trouble with this is that for small n and very large m it isn’t efficiently vectorized. Your task is to develop
an implementation of Solve that addresses this shortcoming.
The starting point is this implementation of a single-matrix linear system solve that is equivalent to x =
A(1:n,1:n)\A(:,n+1) if we ignore pivoting:
% Reduce A to upper triangular and apply updates to A(:,n+1)...
for k=1:n-1
for i=k+1:n
tau = A(i,k)/A(k,k);
for j=k:n+1
A(i,j) = A(i,j) - tau*A(k,j);
end
end
end
2
% Solve the upper triangular system...
x = zeros(n,1);
for i = n:-1:1
x(i) = b(i);
for j=i+1:n
x(i) = x(i) - A(i,j)*x(j);
end
x(i) = x(i)/A(i,i);
end
To develop the proper implementation of Solve, you will have to vectorize the scalar floating point operations
so that they become vector operations of length m. After you figure this out, incorporate pivoting. Test your
solution with P3.
Problem 4 (5 pts) Circumscribing Tetrahedrons
Consider a sphere (the Earth) with radius R = 3950 centered at (0,0,0). Assume that a point on the surface
with latitude θ and longitude φ has (x, y, z) coordinates specified by
R ∗ (− cos(θ) sin(φ), − cos(θ) cos(φ), sin(θ)).
If we pick four surface points, then the four tangent planes at those points may define a tetrahedron and
that tetrahedron may circumscribe the Earth. (Think: If we have a circumscribing tetrahedron and we put
a satellite at every vertex, then everyone on the planet is in view of a satellite.) We define the diameter of
a circumscribing tetrahedron to be the distance from (0,0,0) to the vertex that is “farthest out in space.”
Complete the following function so that it performs as specified
%
%
%
%
%
%
%
%
%
%
%
%
%
function D = Tetra(theta,phi)
theta and phi are column n-vectors that satisfy
with -90 <= theta(i) <= 90 and -180 <= phi(i) <= 180.
Let C(i) be the point on the sphere of radius 3950 with
latitude theta(i) and longitude phi(i), both in degrees.
Let T(i) be the tangent plane to the sphere at C(i) and assume
that any three members of {T(1),...,T(n)} intersect at a single point.
D is an n-by-n-by-n-by-n array. If p, q, r, and s are indices that satisfy
n >= p > q > r > s >= 1 then D(p,q,r,s) = -1 if the tangent planes
T(p), T(q), T(r), and T(s) do NOT define a circumscribing tetrahedron,
Otherwise D(p,q,r,s) specifies its diameter.
For your information, given a point p = (px, py , pz ) and a nonzero normal vector n = (nx , ny , nz ), the set
C = { (x, y, z) | nx (x − px) + ny (y − py ) + nz (z − pz ) = 0}
defines a plane. For each tetrahedron you will have to solve four 3-by-3 linear systems, one for each of the
vertices. Test your program by running it on the script P4.
3