Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

efficient algorithm to compute continued fraction of a sum of continued fractions #19120

Closed
staroste opened this issue Sep 1, 2015 · 53 comments

Comments

@staroste
Copy link
Contributor

staroste commented Sep 1, 2015

Implement a method which, taking continued fraction x as an input, computes the continued fraction of (ax+b)/(cx+d) using Gosper's algorithm. Call this method with proper arguments upon

sage: 3*x
sage: x+1

(overlaps with the following TODO item of sage/rings/continued_fraction.py:
Add Gosper’s algorithm to compute the continued fraction of (ax + b)/(cx + d) knowing the one of x (see Gosper (1972, http://www.inwap.com/pdp10/hbaker/hakmem/cf.html), Knuth (1998, TAOCP vol 2, Exercise 4.5.3.15), Fowler (1999). See also Liardet, P. and Stambul, P. “Algebraic Computation with Continued Fractions.” J. Number Th. 73, 92-121, 1998.)

CC: @videlec @staroste

Component: number theory

Keywords: continued_fraction

Author: Miroslav Kovar

Branch/Commit: 8cafbb4

Reviewer: Vincent Delecroix, Frédéric Chapoton

Issue created by migration from https://trac.sagemath.org/ticket/19120

@sagetrac-mirgee
Copy link
Mannequin

sagetrac-mirgee mannequin commented Apr 29, 2016

Branch: u/mirgee/my_sage_1

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 29, 2016

Commit: 3fcc597

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 29, 2016

Branch pushed to git repo; I updated commit sha1. Last 10 new commits:

171498bSolved the big bug with the negative value of infinite continued fraction! :)
7836c3eDon't know what changed. Backing up.
7c6b445Added a few lines to check for problem with singularity.
56c76e2Latest version... Don't know what changed :)
3ab71abDone some code refactoring, tried to make the code simpler and more readable. I hope I didn't break it.
2bd5194Of course I broke something. Bug for infinite fractions fixed and code shortened.
0dece4cAdded a test and fixed associated bugs (hopefully).
e246b63Added a test and fixed associated bugs (hopefully).
f9714f8Added check for arguments' validity.
3fcc597Moved Gosper iterator.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 29, 2016

Changed commit from 3fcc597 to 51f21d5

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 29, 2016

Branch pushed to git repo; I updated commit sha1. New commits:

51f21d5Better check coefficients using Integer.

@videlec
Copy link
Contributor

videlec commented Apr 29, 2016

comment:4

Hello,

Some remarks:

  1. You would better edit the description of this ticket to explain what you want to do.

  2. Your branch does not merge cleanly with the last beta. The reason is that you added a line in lazy_list.pyx: from sage.rings.all import Infinity. It is always better to base your work on the latest available version of the source code. You can consult the developer manual.

  3. The commit message should reflect what is inside the commit. Not your mood at the time you did the commit.

  4. The file with gosper algorithm should not be in sage/misc/. Put it in the same directory as continued_fraction.py. And you would better call it continued_fraction_gosper.py.

  5. In continued_fraction.py you added the following in the comparison code

@@ -530,6 +530,8 @@ class ContinuedFraction_base(SageObject):
             if a == ZZ_0 and b == ZZ_0 and i:  # rational case
                 return 0
             i += 1
+            if i == 300:
+                return 0

I understand why you did it but it is definitely not a solution. What you can do is to raise a RuntimeError after a certain limit.

  1. All methods must be documented as explained in details in the developer manual.

Vincent

@videlec
Copy link
Contributor

videlec commented Apr 29, 2016

comment:5

And last but not the least: it would be very nice to have Gosper algorithm within Sage!!

@sagetrac-mirgee
Copy link
Mannequin

sagetrac-mirgee mannequin commented Apr 30, 2016

comment:6

Hi,

thanks for the great input.

As for 5.: We need a way to compare instances of _infinite, and even though this is not a valid test, if enough terms are the same, the two continued fractions could be regarded as equal for most practical purposes. Another option would be to compare values, as in _real, but the problem is that

  1. value() method is by documentation optional,

  2. in case of _infinite, this would delegate the computation of value to RLF, which itself may use comparison,

  3. this solution would effectively do the same thing - compute the value of the operands with certain precision and then compare the intervals.

What do you think?

@sagetrac-mirgee

This comment has been minimized.

@sagetrac-mirgee

This comment has been minimized.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 30, 2016

Changed commit from 51f21d5 to fa1173a

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 30, 2016

Branch pushed to git repo; I updated commit sha1. New commits:

fa1173aMoved and renamed the continued_fraction_gosper. Added some documentation.

@videlec
Copy link
Contributor

videlec commented Apr 30, 2016

comment:10

Replying to @sagetrac-mirgee:

Hi,

thanks for the great input.

As for 5.: We need a way to compare instances of _infinite, and even though this is not a valid test, if enough terms are the same, the two continued fractions could be regarded as equal for most practical purposes. Another option would be to compare values, as in _real, but the problem is that

  1. value() method is by documentation optional,

  2. in case of _infinite, this would delegate the computation of value to RLF, which itself may use comparison,

  3. this solution would effectively do the same thing - compute the value of the operands with certain precision and then compare the intervals.

What do you think?

Everything would be better than an silent approximate comparison. If equality can not be decided just raise an error (see Python exceptions). I think that in that case an ArithmeticError would make sense.

If you want to check equality up to some precision, you should do

sage: abs(cf1 - cf2) < 2**-100

or

sage: R = RealIntervalField(256)
sage: R(cf1).overlaps(R(cf2))

@videlec
Copy link
Contributor

videlec commented Apr 30, 2016

comment:11

For the sake of this ticket I would not implement something like 1+cf or 3*cf which needs some work. Having a method apply_homography would be enough for a first ticket. In order to include this first part in Sage you need to

  • make it compatible with the last version of Sage source code (see 2. in [comment:4]). By the way why did you make this change in lazy_list.pyx?

  • add doctests to each method or function

@sagetrac-mirgee
Copy link
Mannequin

sagetrac-mirgee mannequin commented Apr 30, 2016

comment:12

The change in lazy_list() is unnecessary and can be deleted. Personally I don't think it is necessary to add doctests for the simple helper functions, but I will comply with the standards and add them, no problem. I will also look into the the comparison.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 3, 2016

Branch pushed to git repo; I updated commit sha1. New commits:

fa492afAdded more doctests in continued_fraction_gosper.py.
fea8eb9Removed unnecessary comments.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 3, 2016

Changed commit from fa1173a to fea8eb9

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 4, 2016

Changed commit from fea8eb9 to 92b8aa0

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 4, 2016

Branch pushed to git repo; I updated commit sha1. New commits:

92b8aa0Added more unit tests, replaced overwritten method quotients() with get preperiod length, so that all doctests pass. Added a few more doctests. Now all methods are accompanied with doctests.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 8, 2016

Changed commit from 92b8aa0 to e063f2f

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 8, 2016

Branch pushed to git repo; I updated commit sha1. New commits:

e063f2fUsing a set of tuples instead of list of dictionaries for state tracking, should improve performance. Using lazy_list instead of Word for instances of infinite continued fractions.

@fchapoton
Copy link
Contributor

comment:16

rebase, squashed, py3-compatible


New commits:

3308c92Implement Gosper algorithm for homographic action on continued fractions

@fchapoton
Copy link
Contributor

Changed commit from e063f2f to 3308c92

@fchapoton
Copy link
Contributor

Changed branch from u/mirgee/my_sage_1 to public/ticket/19120

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 12, 2019

Branch pushed to git repo; I updated commit sha1. New commits:

031088ffix for py2: also define next

@sagetrac-mirgee
Copy link
Mannequin

sagetrac-mirgee mannequin commented Sep 24, 2020

comment:25

Replying to @videlec:

Author? (name in the git log is John Doe <miroslav.kovar@concur.com>)

I am the author: Miroslav Kovar (miroslavkovar@protonmail.com)

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Sep 24, 2020

Changed commit from fe2cd08 to 81e0693

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Sep 24, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

81e0693fixes, documentation, copyright, optimization

@videlec
Copy link
Contributor

videlec commented Sep 24, 2020

comment:27

Replying to @sagetrac-mirgee:

Replying to @videlec:

Author? (name in the git log is John Doe <miroslav.kovar@concur.com>)

I am the author: Miroslav Kovar (miroslavkovar@protonmail.com)

Thanks. I put your name and mail in the copyright header of the file.

@videlec
Copy link
Contributor

videlec commented Sep 24, 2020

Author: Miroslav Kovar

@videlec
Copy link
Contributor

videlec commented Sep 24, 2020

Reviewer: Vincent Delecroix

@fchapoton
Copy link
Contributor

comment:29

patchbot plugins complain

@fchapoton
Copy link
Contributor

comment:30

Le reference [LS1998] a une allure bizarre, avec un P isolé

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Sep 25, 2020

Changed commit from 81e0693 to 3323f09

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Sep 25, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

3323f09enoding, import, references

@fchapoton
Copy link
Contributor

comment:32

Merci. Pyflakes est encore pas content:

src/sage/rings/continued_fraction_gosper.py:37:1 'sage.rings.real_mpfr.RR' imported but unused

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Sep 26, 2020

Changed commit from 3323f09 to 7161eaf

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Sep 26, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

7161eafremove unused import

@videlec
Copy link
Contributor

videlec commented Sep 26, 2020

comment:34

There is something wrong in the way infinity is handled. In the situation where we apply x -> 1 / (qx - p) to x=p/q Gosper iterator is empty. But currently continued_fraction([]) is wrongly 0.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Sep 26, 2020

Changed commit from 7161eaf to 8cafbb4

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Sep 26, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

8cafbb4fix infinities in continued fraction and do not forward values

@videlec
Copy link
Contributor

videlec commented Sep 26, 2020

comment:36

fixed

@fchapoton
Copy link
Contributor

comment:37

ok, this seems to be ready for a positive review ?

@videlec
Copy link
Contributor

videlec commented Sep 28, 2020

Changed reviewer from Vincent Delecroix to Vincent Delecroix, Frédéric Chapoton

@vbraun
Copy link
Member

vbraun commented Nov 7, 2020

Changed branch from public/ticket/19120 to 8cafbb4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants