Created
July 9, 2025 14:13
-
-
Save jwscook/e980fae72c83000c815946e1608ac6c2 to your computer and use it in GitHub Desktop.
ForwardDiff erfcx with complex numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ForwardDiff | |
using SpecialFunctions | |
import SpecialFunctions.erfcx | |
function SpecialFunctions.erfcx(ϕ::Complex{<:ForwardDiff.Dual{TAG}}) where {TAG} | |
# Split input into real and imaginary parts | |
x, y = reim(ϕ) | |
# This gives the 'finite' complex part of the input | |
z = complex(ForwardDiff.value(x), ForwardDiff.value(y)) | |
# Calculate the finite part of the output | |
q = erfcx(z) | |
# split into real and imaginary parts | |
u, v = reim(q) | |
# split derivative into real and imaginary parts too | |
∂u, ∂v = reim(2 * (z * q - 1 / sqrt(pi))) | |
# Now lets deal with the infinitesimals from the real and imaginary parts of ϕ | |
px, py = ForwardDiff.partials(x), ForwardDiff.partials(y) | |
# Split again into real and imaginary parts | |
du = ForwardDiff.Dual{TAG}(u, ∂u*px - ∂v*py) | |
dv = ForwardDiff.Dual{TAG}(v, ∂v*px + ∂u*py) | |
# And combine | |
return complex(du, dv) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As per the discourse answer by Mason.