ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 31 Jan 2019 20:54:14 -0600Recovering numbers from continued fractionhttp://ask.sagemath.org/question/45093/recovering-numbers-from-continued-fraction/Okay, not really, since we won't type in infinitely many numbers. But it would be nice to have a way to convert `[1,1,1,1,1,1,1,1,1]` to a rational approximation of the golden ratio. I couldn't find anything built-in, but maybe I didn't look hard enough? Thu, 17 Jan 2019 23:02:07 -0600http://ask.sagemath.org/question/45093/recovering-numbers-from-continued-fraction/Answer by Emmanuel Charpentier for <p>Okay, not really, since we won't type in infinitely many numbers. But it would be nice to have a way to convert <code>[1,1,1,1,1,1,1,1,1]</code> to a rational approximation of the golden ratio. I couldn't find anything built-in, but maybe I didn't look hard enough? </p>
http://ask.sagemath.org/question/45093/recovering-numbers-from-continued-fraction/?answer=45095#post-id-45095What's wrong with this :
sage: continued_fraction([1]*12).n()
1.61805555555556
sage: continued_fraction([1]*12)._rational_()
233/144
sage: continued_fraction([1]*12)._rational_().n()
1.61805555555556Fri, 18 Jan 2019 01:10:01 -0600http://ask.sagemath.org/question/45093/recovering-numbers-from-continued-fraction/?answer=45095#post-id-45095Comment by kcrisman for <p>What's wrong with this :</p>
<pre><code>sage: continued_fraction([1]*12).n()
1.61805555555556
sage: continued_fraction([1]*12)._rational_()
233/144
sage: continued_fraction([1]*12)._rational_().n()
1.61805555555556
</code></pre>
http://ask.sagemath.org/question/45093/recovering-numbers-from-continued-fraction/?comment=45255#post-id-45255Hmm, I guess so and I should have thought of this, but that wasn't exactly what I meant.Thu, 31 Jan 2019 20:54:14 -0600http://ask.sagemath.org/question/45093/recovering-numbers-from-continued-fraction/?comment=45255#post-id-45255Answer by Sébastien for <p>Okay, not really, since we won't type in infinitely many numbers. But it would be nice to have a way to convert <code>[1,1,1,1,1,1,1,1,1]</code> to a rational approximation of the golden ratio. I couldn't find anything built-in, but maybe I didn't look hard enough? </p>
http://ask.sagemath.org/question/45093/recovering-numbers-from-continued-fraction/?answer=45132#post-id-45132It used to be that `continued_fraction` could only deal with finite continued fraction expansions. Thanks to work made in 2014 by Vincent Delecroix and other authors/reviewers, Sage deals with ultimately periodic CF expansions (i.e., [quadratic irrationals](https://en.wikipedia.org/wiki/Quadratic_irrational_number)). If you look at the [documentation](http://doc.sagemath.org/html/en/reference/diophantine_approximation/sage/rings/continued_fraction.html) of `continued_fraction?`, you will see this example:
sage: cf = continued_fraction([(1,1),(2,8)]); cf
[1; 1, (2, 8)*]
sage: cf.value()
2/11*sqrt5 + 14/11
Therefore, to construct the golden ratio as a quadratic number from the CF expansion you do:
sage: preperiod = ()
sage: period = (1,)
sage: cf = continued_fraction([preperiod, period])
sage: cf
[(1)*]
sage: cf.value()
1/2*sqrt5 + 1/2
sage: cf.value().parent()
Number Field in sqrt5 with defining polynomial x^2 - 5
sage: cf.value().n()
1.61803398874989
Conversely, starting from a quatratic irrational, you may find the preperiod and period of its CF expansion:
sage: K.<sqrt13> = NumberField(x^2-13, embedding=3.5)
sage: continued_fraction(sqrt13/6+1/5)
[0; 1, (4, 43, 30, 43, 4, 3, 2, 42, 1, 4, 1, 1080, 1, 4, 1, 42, 2, 3)*]
sage: continued_fraction(sqrt13/6+1/5).preperiod()
(0, 1)
sage: continued_fraction(sqrt13/6+1/5).period()
(4, 43, 30, 43, 4, 3, 2, 42, 1, 4, 1, 1080, 1, 4, 1, 42, 2, 3)
Mon, 21 Jan 2019 10:16:01 -0600http://ask.sagemath.org/question/45093/recovering-numbers-from-continued-fraction/?answer=45132#post-id-45132Comment by kcrisman for <p>It used to be that <code>continued_fraction</code> could only deal with finite continued fraction expansions. Thanks to work made in 2014 by Vincent Delecroix and other authors/reviewers, Sage deals with ultimately periodic CF expansions (i.e., <a href="https://en.wikipedia.org/wiki/Quadratic_irrational_number">quadratic irrationals</a>). If you look at the <a href="http://doc.sagemath.org/html/en/reference/diophantine_approximation/sage/rings/continued_fraction.html">documentation</a> of <code>continued_fraction?</code>, you will see this example:</p>
<pre><code>sage: cf = continued_fraction([(1,1),(2,8)]); cf
[1; 1, (2, 8)*]
sage: cf.value()
2/11*sqrt5 + 14/11
</code></pre>
<p>Therefore, to construct the golden ratio as a quadratic number from the CF expansion you do:</p>
<pre><code>sage: preperiod = ()
sage: period = (1,)
sage: cf = continued_fraction([preperiod, period])
sage: cf
[(1)*]
sage: cf.value()
1/2*sqrt5 + 1/2
sage: cf.value().parent()
Number Field in sqrt5 with defining polynomial x^2 - 5
sage: cf.value().n()
1.61803398874989
</code></pre>
<p>Conversely, starting from a quatratic irrational, you may find the preperiod and period of its CF expansion:</p>
<pre><code>sage: K.<sqrt13> = NumberField(x^2-13, embedding=3.5)
sage: continued_fraction(sqrt13/6+1/5)
[0; 1, (4, 43, 30, 43, 4, 3, 2, 42, 1, 4, 1, 1080, 1, 4, 1, 42, 2, 3)*]
sage: continued_fraction(sqrt13/6+1/5).preperiod()
(0, 1)
sage: continued_fraction(sqrt13/6+1/5).period()
(4, 43, 30, 43, 4, 3, 2, 42, 1, 4, 1, 1080, 1, 4, 1, 42, 2, 3)
</code></pre>
http://ask.sagemath.org/question/45093/recovering-numbers-from-continued-fraction/?comment=45254#post-id-45254I guess I didn't look hard enough!Thu, 31 Jan 2019 20:53:44 -0600http://ask.sagemath.org/question/45093/recovering-numbers-from-continued-fraction/?comment=45254#post-id-45254