Skip to content

Commit

Permalink
Merge pull request #283 from JuliaDiff/ox/tridet
Browse files Browse the repository at this point in the history
det and logdet for structured matrixes
  • Loading branch information
oxinabox committed Oct 16, 2020
2 parents f63f385 + 57fc1ab commit 0a555a6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ChainRules"
uuid = "082447d4-558c-5d27-93f4-14fc19e9eca2"
version = "0.7.25"
version = "0.7.26"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
21 changes: 21 additions & 0 deletions src/rulesets/LinearAlgebra/structured.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,24 @@ function rrule(::typeof(tril), A::AbstractMatrix)
end
return tril(A), tril_pullback
end

_diag_view(X) = view(X, diagind(X))
_diag_view(X::Diagonal) = parent(X) #Diagonal wraps a Vector of just Diagonal elements

function rrule(::typeof(det), X::Union{Diagonal, AbstractTriangular})
y = det(X)
s = conj!(y ./ _diag_view(X))
function det_pullback(ȳ)
return (NO_FIELDS, Diagonal(ȳ .* s))
end
return y, det_pullback
end

function rrule(::typeof(logdet), X::Union{Diagonal, AbstractTriangular})
y = logdet(X)
s = conj!(one(eltype(X)) ./ _diag_view(X))
function logdet_pullback(ȳ)
return (NO_FIELDS, Diagonal(ȳ .* s))
end
return y, logdet_pullback
end
18 changes: 18 additions & 0 deletions test/rulesets/LinearAlgebra/structured.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,22 @@
rrule_test(Op, randn(n, n), (randn(n, n), randn(n, n)), (k, nothing))
end
end

@testset "det and logdet $S" for S in (Diagonal, UpperTriangular, LowerTriangular)
@testset "$op" for op in (det, logdet)
@testset "$T" for T in (Float64, ComplexF64)
n = 5
# rand (not randn) so det will be postive, so logdet will be defined
X = S(3*rand(T, (n, n)) .+ 1)
X̄_acc = Diagonal(rand(T, (n, n))) # sensitivity is always a diagonal for these types
rrule_test(op, rand(T), (X, X̄_acc))
end
@testset "return type" begin
X = S(3*rand(6, 6) .+ 1)
_, op_pullback = rrule(op, X)
= op_pullback(2.7)[2]
@testisa Diagonal
end
end
end
end

2 comments on commit 0a555a6

@oxinabox
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/23039

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.26 -m "<description of version>" 0a555a6ecdc2bc13a39ad5fabe440972882d8d62
git push origin v0.7.26

Please sign in to comment.