In [1]:
## Lecture 27
# Three demos
In [2]:
## Demo 1: power method on matrix with geometric multiplicity > 1
A = [3 1 2.0 1.0;
     0 3 1.0 -2.0;
     0 0 1.0 -2.0;
     0 0 0 1.0]
v = rand(4) 
x = v/norm(v)
for i=1:100000
  x = A*x
  x = x/norm(x)
end 
x
UndefVarError: `norm` not defined

Stacktrace:
 [1] top-level scope
   @ In[2]:7
In [3]:
## Demo 2: QR on a non-sym matrix
A = randn(10,10)
X = copy(A)
for i=1:1000
  Q,R = qr(X)
  X = R*Q
end
X = map(x->abs(x) < 1e-10 ? 0 : x, X)
UndefVarError: `qr` not defined

Stacktrace:
 [1] top-level scope
   @ ./In[3]:5
In [4]:
## Demo 3: LU on a non-sym matrix
A = randn(10,10) 
X = copy(A)
for i=1:1000
  L,U = lu(X, NoPivot())
  X = U*L
end
X
UndefVarError: `NoPivot` not defined

Stacktrace:
 [1] top-level scope
   @ ./In[4]:5
In [5]:
## Demo 4: testing if a matrix is non-symmetric
using LinearAlgebra, SparseArrays
n = 10
Araw = SymTridiagonal(2*ones(n), -1*ones(n-1))
A = sparse(Araw)
A[1,2] = 2
Out[5]:
2
In [6]:
##
function symtest(A; ntrials=100)
  n = size(A,1)
  na_est = norm(A*normalize!(randn(n)))
  for i=1:ntrials
    v = normalize!(randn(n))
    w = normalize!(randn(n))
    @show abs(dot(v,A*w) - dot(w,A*v))
    if abs(dot(v,A*w) - dot(w,A*v)) >= n*eps(na_est)
      return false
    end
  end
  return true
end 
symtest(A)
abs(dot(v, A * w) - dot(w, A * v)) = 0.43295568786595284
Out[6]:
false