@@ -793,7 +793,7 @@ function EnzymeRules.augmented_primal(
793793 return EnzymeRules. AugmentedReturn (primal, shadow, cache)
794794end
795795
796- # This is required to handle arugments that mix real and complex numbers
796+ # This is required to handle arguments that mix real and complex numbers
797797_project (:: Type{<:Real} , x) = x
798798_project (:: Type{<:Real} , x:: Complex ) = real (x)
799799_project (:: Type{<:Complex} , x) = x
@@ -922,11 +922,26 @@ function EnzymeRules.reverse(
922922 return (nothing , nothing , nothing , dα, dβ)
923923end
924924
925-
926-
927-
928-
929-
925+ function cofactor (A)
926+ cofA = zeros (eltype (A), size (A))
927+ minorAij = zeros (eltype (A), size (A, 1 ) - 1 , size (A, 2 ) - 1 )
928+ for i in 1 : size (A, 1 ), j in 1 : size (A, 2 )
929+ # build minor matrix
930+ for k in 1 : size (A, 1 ), l in 1 : size (A, 2 )
931+ if ! (k == i || l == j)
932+ ki = k < i ? k : k - 1
933+ li = l < j ? l : l - 1
934+ @inbounds minorAij[ki, li] = A[k, l]
935+ end
936+ end
937+ @inbounds cofA[i, j] = (- 1 )^ (i - 1 + j - 1 ) * det (minorAij)
938+ minorAij .= zero (eltype (A))
939+ end
940+ return cofA
941+ end
942+ # partial derivative of the determinant is the matrix of
943+ # cofactors
944+ EnzymeRules. @easy_rule (LinearAlgebra. det (A), (cofactor (A),))
930945
931946function EnzymeRules. forward (
932947 config:: EnzymeRules.FwdConfig ,
0 commit comments