Skip to content

Instantly share code, notes, and snippets.

@jwscook
Created July 9, 2025 14:13
Show Gist options
  • Save jwscook/e980fae72c83000c815946e1608ac6c2 to your computer and use it in GitHub Desktop.
Save jwscook/e980fae72c83000c815946e1608ac6c2 to your computer and use it in GitHub Desktop.
ForwardDiff erfcx with complex numbers
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
@jwscook
Copy link
Author

jwscook commented Jul 9, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment