Skip to content

Commit 9d3a957

Browse files
committed
efficiency
1 parent ce1e1bd commit 9d3a957

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/internal_rules.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -924,8 +924,10 @@ end
924924

925925
function cofactor(A)
926926
cofA = zeros(eltype(A), size(A))
927-
minorAij = zeros(eltype(A), size(A, 1) - 1, size(A, 2) - 1)
927+
minorAij = similar(A, size(A, 1) - 1, size(A, 2) - 1)
928928
for i in 1:size(A, 1), j in 1:size(A, 2)
929+
fill!(minorAij, zero(eltype(A)))
930+
929931
# build minor matrix
930932
for k in 1:size(A, 1), l in 1:size(A, 2)
931933
if !(k == i || l == j)
@@ -935,13 +937,12 @@ function cofactor(A)
935937
end
936938
end
937939
@inbounds cofA[i, j] = (-1)^(i - 1 + j - 1) * det(minorAij)
938-
minorAij .= zero(eltype(A))
939940
end
940941
return cofA
941942
end
942-
# partial derivative of the determinant is the matrix of
943-
# cofactors
944-
EnzymeRules.@easy_rule(LinearAlgebra.det(A), (cofactor(A),))
943+
944+
# partial derivative of the determinant is the matrix of cofactors
945+
EnzymeRules.@easy_rule(LinearAlgebra.det(A::AbstractMatrix), (cofactor(A),))
945946

946947
function EnzymeRules.forward(
947948
config::EnzymeRules.FwdConfig,

0 commit comments

Comments
 (0)