From 8c6c37c64394c14c9dee3b668d8402c15bcb6a03 Mon Sep 17 00:00:00 2001 From: Sarvjeet Singh Date: Sun, 21 Feb 2016 14:26:15 +0530 Subject: [PATCH] Changed logdet method for LU for better performance. Also, removed logdet for complex subtypes because the logdet for real is also sufficient for complex. Pointed out by andreasnoack in PR #14643. --- base/linalg/lu.jl | 31 ++++--------------------------- 1 file changed, 4 insertions(+), 27 deletions(-) diff --git a/base/linalg/lu.jl b/base/linalg/lu.jl index 3a0da05f53b71..1f96f52658272 100644 --- a/base/linalg/lu.jl +++ b/base/linalg/lu.jl @@ -169,7 +169,7 @@ end function logabsdet{T,S}(A::LU{T,S}) # return log(abs(det)) and sign(det) n = checksquare(A) c = 0 - P = one(real(T)) + P = one(T) abs_det = zero(real(T)) @inbounds for i = 1:n dg_ii = A.factors[i,i] @@ -179,36 +179,13 @@ function logabsdet{T,S}(A::LU{T,S}) # return log(abs(det)) and sign(det) end abs_det += log(abs(dg_ii)) end - s = (isodd(c) ? -one(real(T)) : one(real(T))) * P + s = ifelse(isodd(c), -one(real(T)), one(real(T))) * P abs_det, s end -function logdet{T<:Real,S}(A::LU{T,S}) +function logdet(A::LU) d, s = logabsdet(A) - if s < 0 - throw(DomainError()) - end - d -end - -_mod2pi(x::BigFloat) = mod(x, big(2)*π) # we don't want to export this, but we use it below -_mod2pi(x) = mod2pi(x) -function logdet{T<:Complex,S}(A::LU{T,S}) - n = checksquare(A) - s = zero(T) - c = 0 - @inbounds for i = 1:n - if A.ipiv[i] != i - c += 1 - end - s += log(A.factors[i,i]) - end - if isodd(c) - s = Complex(real(s), imag(s)+π) - end - r, a = reim(s) - a = π - _mod2pi(π - a) #Take principal branch with argument (-pi,pi] - complex(r, a) + return d + log(s) end inv!{T<:BlasFloat,S<:StridedMatrix}(A::LU{T,S}) = @assertnonsingular LAPACK.getri!(A.factors, A.ipiv) A.info