File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed
src/rulesets/SparseArrays Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
16
16
SparseInverseSubset = " dc90abb0-5640-4711-901d-7e5b23a2fada"
17
17
Statistics = " 10745b16-79ce-11e8-11f9-7d13ad32a3b2"
18
18
StructArrays = " 09ab397b-f2b6-538f-b94a-2f83cf4a842a"
19
+ SuiteSparse = " 4607b0f0-06f3-5cda-b6b1-a6196a1729e9"
19
20
20
21
[compat ]
21
22
Adapt = " 3.4.0"
Original file line number Diff line number Diff line change @@ -50,6 +50,40 @@ function rrule(::typeof(findnz), v::AbstractSparseVector)
50
50
return (I, V), findnz_pullback
51
51
end
52
52
53
+ if VERSION < v " 1.7"
54
+ #=
55
+ This method for `logabsdet(F::UmfpackLU)` is required to calculate the (log)determinants
56
+ of sparse matrices, but was not defined prior to Julia v1.7. In order fo the rrules
57
+ for the determinants of sparse matrices below to work, they need to be able to
58
+ compute the primals as well, so this import from the future is included. For more
59
+ recent versions of Julia, this definition lives in:
60
+ julia/stdlib/SuiteSparse/src/umfpack.jl
61
+ =#
62
+ using SuiteSparse. UMFPACK: _signperm, UmfpackLU
63
+
64
+ for itype in (:Int32 , :Int64 )
65
+ @eval begin
66
+ function LinearAlgebra. logabsdet (F:: UmfpackLU{T, $itype} ) where {T<: Union{Float64,ComplexF64} }
67
+ n = checksquare (F)
68
+ issuccess (F) || return log (zero (real (T))), zero (T)
69
+ U = F. U
70
+ Rs = F. Rs
71
+ p = F. p
72
+ q = F. q
73
+ s = _signperm (p)* _signperm (q)* one (real (T))
74
+ P = one (T)
75
+ abs_det = zero (real (T))
76
+ @inbounds for i in 1 : n
77
+ dg_ii = U[i, i] / Rs[i]
78
+ P *= sign (dg_ii)
79
+ abs_det += log (abs (dg_ii))
80
+ end
81
+ return abs_det, s * P
82
+ end
83
+ end
84
+ end
85
+ end
86
+
53
87
54
88
function rrule (:: typeof (logabsdet), x:: SparseMatrixCSC )
55
89
F = cholesky (x)
You can’t perform that action at this time.
0 commit comments