Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,18 @@ public static Complex Erfc(Complex z)
if (re.IsZero && im.IsZero)
return Integer.One;
var precision = context.Precision.ToInt32Checked();
var work = Working(context, GuardDigits(re, im, precision) + (re.IsNegative ? 0 : Cancelled(re, im)));
(EDecimal Re, EDecimal Im) erfc;
if (!re.IsNegative && SquaredModulus(re, im).CompareTo(Reach(precision)) > 0)
erfc = ErfcByTheAsymptoticSeries(re, im, work);
// The asymptotic series is erfc itself and cancels nothing, so it does without the
// digits 1 - erf(z) would lose. Those grow as Re(z^2): 4343 of them at z = 100,
// where erfc(100) took over 20 s. https://github.com/asc-community/AngouriMath/issues/1606
// On the imaginary axis it is 1 - erf(z) instead, where erf(i y) is i erfi(y) exactly:
// the asymptotic series leaves out the 1 there, below its precision beside erfi(y),
// and answered erfc(20 i) with a real part of 0.
if (!re.IsNegative && !re.IsZero && SquaredModulus(re, im).CompareTo(Reach(precision)) > 0)
erfc = ErfcByTheAsymptoticSeries(re, im, Working(context, GuardDigits(re, im, precision)));
else
{
var work = Working(context, GuardDigits(re, im, precision) + (re.IsNegative ? 0 : Cancelled(re, im)));
var (erfRe, erfIm) = Erf(re, im, work, extraDigits: 0);
erfc = (EDecimal.One.Subtract(erfRe, work), erfIm.Negate());
}
Expand Down
21 changes: 21 additions & 0 deletions Sources/Tests/UnitTests/Convenience/ErrorFunctionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public sealed class ErrorFunctionTest
[InlineData("erfc(10)", "2.08848758376254475700078629495778861156081812e-45", "0")]
[InlineData("erfc(-1)", "1.842700792949714869341220635082609259296067", "0")]
[InlineData("erfc(15 + 5 * i)", "4.3823936244741368125841545115653530204851946e-89", "2.25866204017490621858266035895561905217636073e-89")]
[InlineData("erfc(16)", "2.32848575157153069336487285457344259753439695e-113", "0")]
[InlineData("erfc(16 + 3 * i)", "-6.63976210744169686093175089357492626279041806e-110", "-1.73191535469740949550705185233821524194290283e-109")]
[InlineData("erfc(14 - 9 * i)", "1.24364721743380983428549812322004008428400814e-52", "3.64858214804222874123198985548113095364186296e-52")]
[InlineData("erfc(20 * i)", "1", "-1.47479753962878620244773315313183512459925928e+172")]
[InlineData("erfc(2 + i)", "-0.00360634272565175091291182820541914235532928537", "0.0112590060288150250764009156316482248536651599")]
[InlineData("erfi(1)", "1.65042575879754287602533772956136244389567987", "0")]
[InlineData("erfi(10)", "1524307422708669699360546614726544062463812.07", "0")]
Expand Down Expand Up @@ -80,6 +84,23 @@ private static void Close(EDecimal expected, EDecimal actual)
public void ExactValues(string expression, string expected)
=> Assert.Equal(expected.ToEntity(), expression.ToEntity().InnerSimplified);

/// <summary>
/// Far out on the right erfc is below what the default precision represents, and it is
/// its asymptotic series, which cancels nothing. It was carried with the <c>Re(z^2)/ln 10</c>
/// digits that <c>1 - erf(z)</c> would lose: 4343 at 100, where it took about 25 s, and
/// 43430 at 1000, where it did not return in 30.
/// <a href="https://github.com/asc-community/AngouriMath/issues/1606">#1606</a>
/// </summary>
[Theory]
[InlineData("erfc(1000)")]
[InlineData("erfc(1000 + i)")]
public void FarOutOnTheRightItIsZeroAtOnce(string expression)
{
var evaluation = System.Threading.Tasks.Task.Run(() => MathS.FromString(expression, useCache: false).EvalNumerical());
Assert.True(evaluation.Wait(System.TimeSpan.FromSeconds(30)), $"{expression} did not return");
Assert.True(evaluation.Result.RealPart.EDecimal.IsZero && evaluation.Result.ImaginaryPart.EDecimal.IsZero, $"{expression} = {evaluation.Result}");
}

[Theory]
[InlineData("erf(x)", "2 / sqrt(pi) * e ^ (-x ^ 2)")]
[InlineData("erfc(x)", "-2 / sqrt(pi) * e ^ (-x ^ 2)")]
Expand Down
Loading