Ask Your Question
2

Recovering numbers from continued fraction

asked 2019-01-18 06:02:07 +0100

kcrisman gravatar image

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?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
4

answered 2019-01-21 17:16:01 +0100

Sébastien gravatar image

It 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). If you look at the documentation 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)
edit flag offensive delete link more

Comments

I guess I didn't look hard enough!

kcrisman gravatar imagekcrisman ( 2019-02-01 03:53:44 +0100 )edit
2

answered 2019-01-18 08:10:01 +0100

Emmanuel Charpentier gravatar image

updated 2019-01-18 08:10:51 +0100

What'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.61805555555556
edit flag offensive delete link more

Comments

Hmm, I guess so and I should have thought of this, but that wasn't exactly what I meant.

kcrisman gravatar imagekcrisman ( 2019-02-01 03:54:14 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-01-18 06:02:07 +0100

Seen: 734 times

Last updated: Jan 21 '19