Skip to content

Commit affad43

Browse files
committed
add missing _signperm
1 parent 7df39de commit affad43

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/rulesets/SparseArrays/sparsematrix.jl

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,35 @@ end
5252

5353
if VERSION < v"1.7"
5454
#=
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:
55+
The method below for `logabsdet(F::UmfpackLU)` is required to calculate the (log)
56+
determinants of sparse matrices, but was not defined prior to Julia v1.7. In order
57+
for the rrules for the determinants of sparse matrices below to work, they need to be
58+
able to compute the primals as well, so this import from the future is included. For
59+
more recent versions of Julia, this definition lives in:
6060
julia/stdlib/SuiteSparse/src/umfpack.jl
6161
=#
62-
using SuiteSparse.UMFPACK: _signperm, UmfpackLU
62+
using SuiteSparse.UMFPACK: UmfpackLU
6363

64+
# compute the sign/parity of a permutation
65+
function _signperm(p)
66+
n = length(p)
67+
result = 0
68+
todo = trues(n)
69+
while any(todo)
70+
k = findfirst(todo)
71+
todo[k] = false
72+
result += 1 # increment element count
73+
j = p[k]
74+
while j != k
75+
result += 1 # increment element count
76+
todo[j] = false
77+
j = p[j]
78+
end
79+
result += 1 # increment cycle count
80+
end
81+
return ifelse(isodd(result), -1, 1)
82+
end
83+
6484
for itype in (:Int32, :Int64)
6585
@eval begin
6686
function LinearAlgebra.logabsdet(F::UmfpackLU{T, $itype}) where {T<:Union{Float64,ComplexF64}}

0 commit comments

Comments
 (0)