Skip to content

Commit

Permalink
Fix type instability in logdet(::Matrix{Complex64})
Browse files Browse the repository at this point in the history
Ref: #10188

1. Adds pi*im in a type stable fashion when there are an odd number of
pivots

2. Uses mod2pi instead of mod(_, 2pi)
  • Loading branch information
jiahao committed Feb 13, 2015
1 parent 24c64b8 commit 56ab950
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
7 changes: 5 additions & 2 deletions base/linalg/lu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,12 @@ end

function logdet{T<:Complex,S}(A::LU{T,S})
n = chksquare(A)
s = sum(log(diag(A.factors))) + (bool(sum(A.ipiv .!= 1:n) % 2) ? complex(0,pi) : 0)
s = sum(log(diag(A.factors)))
if bool(sum(A.ipiv .!= 1:n) % 2)
s = Complex(real(s), imag(s)+π)
end
r, a = reim(s)
a = pi-mod(pi-a,2pi) #Take principal branch with argument (-pi,pi]
a = π-mod2pi-a) #Take principal branch with argument (-pi,pi]
complex(r,a)
end

Expand Down
1 change: 1 addition & 0 deletions test/linalg/lu.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@test logdet(Complex64[1.0f0 0.5f0; 0.5f0 -1.0f0]) === 0.22314355f0 + 3.1415927f0im
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ end
if "linalg" in tests
# specifically selected case
filter!(x -> x != "linalg", tests)
prepend!(tests, ["linalg1", "linalg2", "linalg3", "linalg4", "linalg/lapack", "linalg/triangular", "linalg/tridiag", "linalg/pinv", "linalg/givens"])
prepend!(tests, ["linalg1", "linalg2", "linalg3", "linalg4", "linalg/lapack", "linalg/triangular", "linalg/tridiag", "linalg/pinv", "linalg/givens", "linalg/lu"])
end

net_required_for = ["socket", "parallel"]
Expand Down

0 comments on commit 56ab950

Please sign in to comment.