Fix recurrence order in dawson's exponential-sum branch - #50
Open
gaoflow wants to merge 1 commit into
Open
Conversation
The |x| >= 0.2 branch advanced d1, d2 and e1 at the top of the loop, one step ahead of the Numerical Recipes for-loop post-clause. Every term was therefore shifted by one recurrence step, so dawson was wrong for all |x| >= 0.2 (wrong sign around x = 0.5, orders of magnitude off for large x). faddeeva's imaginary part near the real axis inherited this, since it delegates to dawson via im_w_of_x. Advance the recurrence after accumulating each term, matching NR. Add differential tests against mpmath covering both dawson branches, the branch boundary and asymptotic tail, and faddeeva on the real axis.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
dawson(x)returns wrong values for every|x| >= 0.2, andfaddeeva(x, y)'simaginary part is wrong near the real axis. Both come from one bug in the
exponential-sum branch of
dawson.Root cause
src/dawson.rs, the|x| >= 0.2branch, ports this Numerical Recipes loop:The increments are in the
forpost-clause, so they run after each iterationbody. The port moved them to the top of the body, so the first iteration already
uses
d1+2,d2-2,e1*e2instead of the initialisedd1,d2,e1. Everyterm is shifted one recurrence step, so the whole branch sums the wrong series.
faddeevainherits this: for|y| < 1e-8*|x|the imaginary part isim_w_of_x(x) = 2/sqrt(pi) * dawson(x)(src/faddeeva.rs:280), so thereal-axis imaginary part is wrong too. The real part
exp(-x^2)is unaffected.Impact (before), oracle = mpmath
sqrt(pi)/2 * exp(-x^2) * erfi(x)dawson(x)beforefaddeeva(0.5, 0.0).imwas-1.12845vs0.47893.Fix
Move the three recurrence increments after the
sum +=accumulation, matchingthe NR post-clause. No change is needed in
faddeeva.rs.Why it went unnoticed / tests
dawson_testonly checkedx = 0.14(the|x| < 0.2series branch, which wasalways correct), and
faddeeva_testonly samplesRe(z) == Im(z), so thenear-real-axis branch that calls
dawsonwas never exercised.Added differential tests against mpmath (agreement < 6e-8, within the documented
~2e-7 accuracy):
dawsonseries branch|x| < 0.2(still correct after the change)dawsonexponential-sum branch: boundaryx = 0.2, sign region, asymptotictail to
x = 10, and negativesD(-x) = -D(x)across both branchesfaddeevaimaginary part on the real axis (y = 0andy = 1e-12*x), withreal part
exp(-x^2)I also checked the other NR recurrence loops in the crate (
betacf,erfccheb, the Bessel routines); none share this post-clause pattern, so theissue is localised to
dawson.