Repository navigation
Complex numbers: the parts keep their own errors, as in WMA - #2066
Merged
Merged
Conversation
MATH-257, the rest of #2047 step 6 for complex numbers. - Precision of a Complex is its accuracy (the parts' errors in quadrature) plus Log10 of its modulus, to which an exact part counts. - Plus and Times on Complex numbers with exact or PrecisionReal parts work out each part from the real rules. Times multiplies as a balanced tree and squares a pair of the same number, z^n squares from the top bit of n, and 1/z is (a - b I)/(a^2 + b^2), as WMA does. 2.`20 I keeps an exact zero real part. - A function with a complex value at real arguments (ArcSin[2.`20], Log[-1.`20], ArcCosh[0``20], ...) gives each part the error from that part of x f'[x]; a part that does not change gets 2^-54 of the error. - A negative PrecisionReal to a Rational power: x^(1/q) shares the error of the whole between its parts; x^(p/q) is x^n (x^(1/q))^r. - Log[x] no longer becomes Log[E, x], whose base added an error. - A Complex shows an inexact zero real part; N[I, 30] is 1.`30 I. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Member
|
LGTM |
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.
The rest of #2047 step 6, for complex numbers: complex arithmetic, and functions with a complex value at a real argument, now get their precision the way WMA does.
In WMA each part of a
Complexkeeps its own error.Accuracycombines the two errors in quadrature, andPrecisionis that accuracy plusLog10of the modulus; an exact part also counts toward the modulus. Before this PR, Mathics3 gave a complex result the precision of its least precise input, or machine precision, which is why1.5`20.3 + 2.5`20.3 Ihad precision 15.PlusandTimeson complex numbers with exact orPrecisionRealparts work out each part with the real rules.Timesmultiplies as a balanced tree and squares a pair of equal factors.z^nsquares starting from the top bit ofn, and1/zis(a - b I)/(a^2 + b^2). This is the order WMA uses, and it changes the last digits of the precision.2.`20 Ikeeps an exact zero real part.ArcSin[2.`20],Log[-1.`20],ArcCosh[020],ArcSec[0.520]``, …) gives each part the error from that part ofx f'[x], as #2052–#2056 do for real values. A part that does not change gets2^-54of the error, so thePipart of ``Log[-1.20]`` is 16 digits more precise than the real part, as in WMA. At a zero or a branch point, the change offwithin the error goes to the nonzero parts.PrecisionRealto aRationalpower:x^(1/q)shares the error of the whole value between its parts, andx^(p/q)isx^n (x^(1/q))^r. Checked against WMA forp/qfrom −5/2 to 7/3.Log[x]is no longer rewritten asLog[E, x]: the numerifiedEbase added an error of its own.019.4 + 2.19.7 I``).N[I, 30]is ``1.30 I``.Precision[ArcSec[0.5`20]]15.119620.057120.0571Precision[1.5`20.3 + 2.5`20.3 I]15.20.320.3ArcCosh[020]``1.5707963…`20.*I1.5707963…`20.1961*I1.5707963…`20.1961*ISqrt[-2.`20]1.4142135…`20.*I1.4142135…`20.301*I1.4142135…`20.301*IPrecision[Log[-1.`20]]15.497120.497120.4971Accuracy[Im[Log[-1.`20]]] - Accuracy[Re[Log[-1.`20]]]4.5028516.255616.2556Precision[(1.`20 + 2.`25 I) (3.`22 - 1.`18 I)]15.18.493918.4939Precision[(1.`20 + 2.`25 I)^5]15.19.304919.3049Precision[(-8.`20)^(1/3)]20.20.477120.4771Not in this PR: functions of a complex argument (
Sqrt[1.`20 + I],Exp[1.`20 I]), a negative base with an inexact exponent, andNof an exact complex number with two nonzero parts, where WMA splits the precision between the parts.Tests
test_complex_precisionandtest_complex_valued_precisionintest/builtin/atomic/test_numbers.py, next totest_real_valued_precision, with WMA 15 values. Full run on kimmy: all doctests pass. pytest under xdist (-n 4) fails a different handful of tests on each run, on master as well, because those tests depend on symbols that earlier tests in the same worker define or clear (f,F,x). With--dist loadfile, master fails 15 (test_forms.py,test_options.py,test_testing_expressions.py,test_latex.py) and this branch fails 7 (test_list.py,test_diffeqns.py,test_constructing.py). None of them is about numbers, and each of these files passes when run on its own.Benchmark from #2050 on kimmy, best of 5, ms:
Total[Table[N[1/k, 50], {k, 2000}]]Times @@ Table[N[1 + 1/k, 50], {k, 2000}]Do[x = N[k, 50] + Pi, {k, 2000}]Total[Table[1./k, {k, 2000}]]Total[Table[N[1/k + I/(k+1), 50], {k, 2000}]]Times @@ Table[N[1 + I/k, 50], {k, 500}]Table[N[1 + I/k, 30]^5, {k, 500}]Table[1 / N[1 + I/k, 30], {k, 500}]Table[ArcSin[N[1 + k, 30]], {k, 500}]Table[Log[N[-k, 30]], {k, 500}]The real cases are unchanged within noise. Complex
Times,z^5andArcSinoutside its domain are 20–35% slower: their parts are now computed from real operations, andArcSintakes more derivatives.🤖 Generated with Claude Code