Repository navigation
ContinuedFraction of a quadratic irrational is its periodic form and takes exactly n terms, of another exact real the terms a doubled-precision check vouches for; FromContinuedFraction reads a period back - #571
Open
enumeratio wants to merge 1 commit into
Open
enumeratio wants to merge 1 commit into
enumeratio wants to merge 1 commit into
Conversation
… …, [period]] and takes exactly n terms, of another exact real the terms a doubled-precision check vouches for (ExponentialE, 40 had a wrong last term), and FromContinuedFraction reads a trailing period back as the quadratic irrational; add regression tests. Fix cortex-js#570
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.
ContinuedFraction(Sqrt(2))is[1, [2]],ContinuedFraction(ExponentialE, 40)is its 40 terms, andFromContinuedFraction([1, [2]])isSqrt(2). Fixes #570Rules (
numerics/continued-fractions.ts, new;library/number-theory.ts):Add,Multiply,Divide,Negate,Subtract,Sqrt,Root(·, 2)andPower(·, 1/2)of rationals and radical literals, expands by the recurrencea = ⌊(P + √D)/Q⌋,P' = aQ − P,Q' = (D − P'²)/Q, withQ | D − P²ensured at the start so the state stays integral. The first repeated state(P, Q)closes the period (Lagrange's theorem), and the first term is always in the leading part:[1, [2]],[1, [1]]for the golden ratio,[1, 7, [1, 2, 1, 8, 13, 8]]for(3 + √7)/5, as Wolfram writes them. With a countnthe result is exactlynflat terms. A sum or product of different radicals is not one quadratic field and goes the way of the next rule.ExponentialE,Pi,Exp(1/2)) is computed at2n + 30digits (_withTransientPrecision), the terms common to both ends of[v − ε, v + ε]are kept less the last (a terminating end writes it two ways), and the samenterms must come out of twice the digits. Not reached within 20000 digits, or the two disagree: unevaluated.ndefaults to 20..N()reads the operand as written: it had already made it a float, which has no terms past its precision. The result of.N()is the result ofevaluate(), floated the way the wrapper does for a float operand.FromContinuedFractionreads a last element that is a list as the period[p₀, …, p_{k−1}]: the matrix of the period gives the tailyas the positive root ofC y² + (D − A) y − B = 0, and the leading terms are folded back (x = a₀ + 1/(a₁ + …)). An empty period, a non-positive period term and a non-integer term decline..N(), and a float term, give a float.MAX_CONTINUED_FRACTION_TERMS(100000) on a period or a count,MAX_CERTIFIED_DIGITSon the precision. Past a cap the call is unevaluated, not truncated.The signature is
(real, integer?) -> list<integer | list<integer>>. A rational, a perfect square and an inexact operand are unchanged (flat list). A negative quadratic irrational uses the floor expansion, as a negative rational does (-√2is[-2, 1, 1, [2]]); Wolfram's signed terms are not followed.isqrtinnumerics/diophantine.tsis exported for the recurrence. The compile targets have no lowering forContinuedFraction, so nothing changes there.Tests:
test/compute-engine/continued-fraction-periodic.test.ts(new): ten quadratic irrationals against Wolfram underevaluate()and.N(),nterms, E to 40 and Pi to 30 terms, the cap, andFromContinuedFraction(the same four forms,.N(), declines, and the round trip for every expansion above). Pinned tests changed:wester.test.ts,ContinuedFraction(Sqrt(23))was twenty terms ending…, 1, 4, now[4, [1, 3, 1, 8]](and ten terms with a count); the result type of the broadcastContinuedFractionrow inbroadcast-collection-valued-results.test.ts,list<list<integer>>tolist<list<integer | list<integer>>>.Overlap: #521, #522, #543, #545 and #547 edit
number-theory.ts(other heads; the helpers sit beforecanEnumerateacceptance, and the CHANGELOG and docs entries will need a rebase); #493 and others edit the same docs tables.Left out: a float operand's own last term is a truncation of its approximant (
ContinuedFraction(2.718281828459045, 40)still ends…, 12, 2);{1, 2, {}}(an empty period) is declined where Wolfram answers3/2.