Skip to content

Instantly share code, notes, and snippets.

@GordStephen
Created November 29, 2015 04:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GordStephen/be274faa22417d6f26a1 to your computer and use it in GitHub Desktop.
Save GordStephen/be274faa22417d6f26a1 to your computer and use it in GitHub Desktop.
Hack to add cholupdate functionality from qrupdate in Julia
function cholupdate!(S::Matrix{Float64}, u::Vector{Float64}, downdate::Bool=false, checks::Bool=true)
n = size(S,1)
if checks
@assert size(S,2) == n
@assert length(u) == n
@assert all(diag(S) .!= 0)
end #if
u, w = copy(u), zeros(n)
status = Ref{Int32}(0)
if downdate
ccall( (:dch1dn_, "libqrupdate"), Void,
(Ref{Int32}, Ptr{Float64}, Ref{Int32}, Ptr{Float64}, Ptr{Float64}, Ref{Int32}),
n, S, n, u, w, status)
else
ccall( (:dch1up_, "libqrupdate"), Void,
(Ref{Int32}, Ptr{Float64}, Ref{Int32}, Ptr{Float64}, Ptr{Float64}),
n, S, n, u, w)
end #if
status = status[]
if status == 0
return S
elseif status == 1
error("Update violates positive semi-definiteness")
elseif status == 2
error("Input matrix is singular")
else
error("An unspecified error occurred")
end #if
end #cholupdate!
function cholupdate!(S::Matrix{Float64}, U::Matrix{Float64},
downdate::Bool=false, checks::Bool=true)
n = size(S,1)
if checks
@assert size(S, 2) == n
@assert size(U, 1) == n
@assert all(diag(S) .!= 0)
end #if
for i in 1:size(U, 2)
cholupdate!(S, U[:,i], downdate, false)
end #for
return S
end #cholupdate!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment