From fad19d68d3fad851dcb808174d2485c98ef1676f Mon Sep 17 00:00:00 2001 From: Alexander Voigt Date: Wed, 21 Feb 2024 11:57:40 +0100 Subject: [PATCH] Improve performance of complex logarithm (#26) by using real functions when possible --------- Co-authored-by: Alexander Voigt --- src/Log.jl | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Log.jl b/src/Log.jl index 0374f60..a3f1367 100644 --- a/src/Log.jl +++ b/src/Log.jl @@ -9,7 +9,13 @@ end # returns complex logarithm of z; # handles case when imag(z) == -0.0 function clog(z::Complex) - log(convert_minus_0(z)) + if iszero(imag(z)) && real(z) > zero(real(z)) + Complex(log(real(z)), zero(real(z))) + elseif iszero(imag(z)) + Complex(log(real(-z)), convert(typeof(real(z)),pi)) + else + log(z) + end end # returns logarithm of x @@ -20,7 +26,11 @@ end # returns complex logarithm of 1+z; # handles case when imag(z) == -0.0 function clog1p(z::Complex) - log1p(convert_minus_0(z)) + if iszero(imag(z)) && real(z) > -one(real(z)) + Complex(log1p(real(z)), zero(real(z))) + else + log1p(convert_minus_0(z)) + end end # returns |ln(x)|^2 for all x