Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #129 from invenia/aa/Cholesky
Browse files Browse the repository at this point in the history
Allow constructing Cholesky objects directly
  • Loading branch information
iamed2 authored Jan 30, 2019
2 parents 2fc91f4 + 8dd0a06 commit 570a48d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/sensitivities/linalg/factorization/cholesky.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import LinearAlgebra.BLAS: gemv, gemv!, gemm!, trsm!, axpy!, ger!
import LinearAlgebra: cholesky
import LinearAlgebra: cholesky, Cholesky
import Base: getproperty

Base.@deprecate chol(X) cholesky(X).U
Expand Down Expand Up @@ -31,6 +31,26 @@ function ∇(::typeof(getproperty), ::Type{Arg{1}}, p, y, ȳ, C::Cholesky, x::S
end
end

@explicit_intercepts(
Cholesky,
Tuple{AbstractMatrix{<:∇Scalar}, Union{Char, Symbol}, Integer},
[true, false, false],
)
function (
::Type{Cholesky},
::Type{Arg{1}},
p,
C::Cholesky,
::Union{UpperTriangular, LowerTriangular},
X::Union{UpperTriangular, LowerTriangular},
uplo::Union{Char, Symbol},
info::Integer,
)
# We aren't doing any actual computation if we've constructed a Cholesky object
# directly, so just pass through this call and return the sensitivies
return
end

"""
level2partition(A::AbstractMatrix, j::Int, upper::Bool)
Expand Down
23 changes: 23 additions & 0 deletions test/sensitivities/linalg/factorization/cholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,27 @@

@test_throws ArgumentError (X->cholesky(X).info)(X_)
end

let
X_ = Matrix{Float64}(I, 5, 5)
X = Leaf(Tape(), X_)
U = cholesky(X).U
C = Cholesky(U, 'U', 0)
@test C isa Branch{<:Cholesky}
@test getfield(C, :f) == LinearAlgebra.Cholesky
@test unbox(C) == Cholesky(UpperTriangular(X_), 'U', 0)
# Ensure we can still directly extract the .U field
UU = C.U
@test UU isa Branch{<:UpperTriangular}
# And access .L as well
LL = C.L
@test LL isa Branch{<:LowerTriangular}
# Make sure that computing the Cholesky and already having the Cholesky
# produce the same results
expected = Matrix(0.5I, 5, 5)
@test (X->det(cholesky(X).U))(X_)[1] expected
@test (X->det(cholesky(X).L))(X_)[1] expected
@test (X->det(Cholesky(cholesky(X).U, :U, 0).U))(X_)[1] expected
@test (X->det(Cholesky(cholesky(X).L, 'L', 0).U))(X_)[1] expected
end
end

0 comments on commit 570a48d

Please sign in to comment.