In [1]:
##
A = randn(5,5)
A = A+A'
Ainit = copy(A)

Out[1]:
5×5 Array{Float64,2}:
-0.597004   1.69975    0.450669   1.87282   -1.33448
1.69975   -0.899741   1.32614    1.36228   -1.78448
0.450669   1.32614    1.12733   -0.649644  -0.474519
1.87282    1.36228   -0.649644   3.47961    4.0889
-1.33448   -1.78448   -0.474519   4.0889    -0.546019
In [2]:
## Example of the deflation method
λs,X = eig(A)
eval = rand(1:size(A,2))
x = X[:,eval] # just an eigenvector
# build the householder matrix H
xe1 = zeros(size(A,1))
xe1[1] = norm(x)
v = xe1-x
H = eye(size(A)...) - 2*v*v'/(v'*v)
# apply it
display(H*A*H')
display(λs[eval])

5×5 Array{Float64,2}:
3.23145       3.19189e-15  -5.82867e-15  -4.18415e-15  -2.88658e-15
3.10862e-15  -2.75071       0.0958526     0.336754     -0.534267
-5.68989e-15   0.0958526     0.39517      -1.4486        0.37995
-4.21885e-15   0.336754     -1.4486        3.07228       4.74938
-3.16414e-15  -0.534267      0.37995       4.74938      -1.38401    
3.2314491081893175
WARNING: imported binding for eval overwritten in module Main

In [3]:
##
eigvals((H*A*H')[2:end,2:end])

Out[3]:
4-element Array{Float64,1}:
-4.83993
-2.55704
0.459341
6.27036 
In [4]:
## The subspace method coverges
X = eye(size(A)...)
D = diagm(sort(diag(X'*A*X)))
for i = 1:50
Y = A*X
D = diagm(sort(diag(X'*Y)))
X,R = qr(Y)
end
@show [diag(D) eigvals(A)]

[diag(D) eigvals(A)] = [-4.83993 -4.83993; -2.55704 -2.55704; 0.459341 0.459341; 3.23145 3.23145; 6.27036 6.27036]

In [5]:
##
X = eye(size(A)...)
A = copy(Ainit)
for i = 1:10
Q,R = qr(A)
A = R*Q
D = diagm(sort(diag(A)))
X = X*Q
end
@show [diag(D) eigvals(Ainit)]

[diag(D) eigvals(Ainit)] = [-4.59284 -4.83993; -2.42407 -2.55704; 0.459341 0.459341; 3.09415 3.23145; 6.0276 6.27036]

Out[5]:
5×2 Array{Float64,2}:
-4.59284   -4.83993
-2.42407   -2.55704
0.459341   0.459341
3.09415    3.23145
6.0276     6.27036 
In [6]:
##
A = copy(Ainit)
for i = 1:10
Q,R = qr(A)
A = R*Q
D = diagm(sort(diag(A)))
X = X*Q
display(A)
end

5×5 Array{Float64,2}:
-0.403048   0.877387  -1.16785    -3.47202   -0.546792
0.877387  -2.0484    -2.07752    -1.23005   -0.52261
-1.16785   -2.07752    2.0874     -3.31245    0.0848015
-3.47202   -1.23005   -3.31245     2.54067   -0.858685
-0.546792  -0.52261    0.0848015  -0.858685   0.387559 
5×5 Array{Float64,2}:
0.1712     -0.84412   -0.123737    4.59117    -0.0676178
-0.84412    -2.47071    2.31464     0.173501   -0.052632
-0.123737    2.31464    3.44787    -1.78478    -0.0442329
4.59117     0.173501  -1.78478     0.956856    0.0639253
-0.0676178  -0.052632  -0.0442329   0.0639253   0.458967 
5×5 Array{Float64,2}:
1.23885      3.16959     -0.893947    -4.01742      0.00664858
3.16959     -1.48738     -2.64146     -0.404622    -0.00599066
-0.893947    -2.64146      2.64091     -0.577235     0.00783553
-4.01742     -0.404622    -0.577235    -0.287547     0.00370208
0.00664858  -0.00599066   0.00783553   0.00370208   0.459346  
5×5 Array{Float64,2}:
2.53914      -4.48533      -0.406231    2.46279      -0.000571865
-4.48533      -1.25592       1.91624     0.0345346    -0.000709129
-0.406231      1.91624       1.84244     1.8998       -0.0011788
2.46279       0.0345346     1.8998     -1.02082       0.000243218
-0.000571865  -0.000709129  -0.0011788   0.000243218   0.459341   
5×5 Array{Float64,2}:
3.70292      4.47955      0.0080984    -1.23143      4.58691e-5
4.47955     -2.14302     -1.22149       0.30262     -7.68729e-5
0.0080984   -1.22149      2.01879      -2.10218      0.000178967
-1.23143      0.30262     -2.10218      -1.47385      4.22684e-5
4.58691e-5  -7.68729e-5   0.000178967   4.22684e-5   0.459341   
5×5 Array{Float64,2}:
4.58965     -3.92726      0.11213      0.560083    -3.54652e-6
-3.92726     -3.06596      0.819477     0.288043    -7.84978e-6
0.11213      0.819477     2.39048      1.90547     -2.68994e-5
0.560083     0.288043     1.90547     -1.80934      1.04811e-5
-3.54652e-6  -7.84978e-6  -2.68994e-5   1.04811e-5   0.459341  
5×5 Array{Float64,2}:
5.20655     3.25446      0.0986352   -0.24329      2.6849e-7
3.25446    -3.72914     -0.564616     0.199046    -7.76757e-7
0.0986352  -0.564616     2.68487     -1.61697      3.96675e-6
-0.24329     0.199046    -1.61697     -2.05744      2.37939e-6
2.6849e-7  -7.76757e-7   3.96675e-6   2.37939e-6   0.459341  
5×5 Array{Float64,2}:
5.61194     -2.61907      0.0650997    0.103063    -2.00677e-8
-2.61907     -4.15986      0.389056     0.121199    -7.55418e-8
0.0650997    0.389056     2.884        1.33191     -5.76653e-7
0.103063     0.121199     1.33191     -2.23125      4.92849e-7
-2.00677e-8  -7.55418e-8  -5.76653e-7   4.92849e-7   0.459341  
5×5 Array{Float64,2}:
5.86873      2.0726       0.038366    -0.0430196    1.48815e-9
2.0726      -4.42857     -0.266072     0.0693601   -7.27365e-9
0.038366    -0.266072     3.01259     -1.07966      8.30812e-8
-0.0430196    0.0693601   -1.07966     -2.3479       9.64427e-8
1.48815e-9  -7.27365e-9   8.30812e-8   9.64427e-8   0.459341  
5×5 Array{Float64,2}:
6.0276       -1.62386       0.0213678    0.0177962   -1.09824e-10
-1.62386      -4.59284       0.180599     0.0383847   -6.96252e-10
0.0213678     0.180599      3.09415      0.867038    -1.19058e-8
0.0177962     0.0383847     0.867038    -2.42407      1.82471e-8
-1.09824e-10  -6.96253e-10  -1.19058e-8   1.82471e-8   0.459341