I am trying to Implement Hessenberg factorization in the following code.
function GivensRotation(a::Float64, b::Float64)
# Calculate the Given's rotation that rotates [a;b] to [r;0]:
c = 0.; s = 0.; r = 0.
if ( b == 0. )
c = sign(a)
s = 0.
r = abs(a)
elseif ( a == 0. )
c = 0.
s = -sign(b)
r = abs(b)
elseif ( abs(a) .> abs(b) )
t = b/a
u = sign(a)*abs(sqrt(1+t^2))
c = 1/u
s = -c*t
r = a*u
else
t = a/b
u = sign(b)*abs(sqrt(1+t^2))
s = -1/u
c = -s*t
r = b*u
end
return (c, s, r)
end
function HessenbergQR( R::Matrix )
# Compute the QR factorization of an upper-Hessenberg matrix:
n = size(R, 1)
Q = Matrix(I,(n,n))
# Convert R from upper-hessenberg form to upper-triangular form using n-1 Givens rotations:
for k = 1:n-1
(c, s, r) = GivensRotation(R[k,k], R[k+1,k])
# Compute G*R[k:k+1,:] and Q[:,k:k+1]*G', where G = [c -s ; s c]
for j = 1:n
newrow = c*R[k,j] - s*R[k+1,j]
R[k+1,j] = s*R[k,j] + c*R[k+1,j]
R[k,j] = newrow
newcol = c*Q[j,k] - s*Q[j,k+1]
Q[j,k+1] = s*Q[j,k] + c*Q[j,k+1]
Q[j,k] = newcol
end
end
return (Q, R)
end
H = triu(rand(3,3),-1)
HessenbergQR( H )
I got this error
InexactError: Bool(-0.7006778795038133)
any tips ?