Ask Your Question
1

Implementing new CF class advice

asked 2016-02-26 00:29:45 -0500

mirgee gravatar image

updated 2016-02-26 03:22:38 -0500

My goal is to implement Gosper's algorithm for algebraic operations on continued fractions into Sage. To begin with, I would like to implement a functionality to perform a homographic transformation

$$x \rightarrow \frac{ax + b}{cx + d}$$

where $x$ is a CF.

The easiest way to do this is probably to create a new class in sage.rings.continued_fraction that would on inicialization accept the integer constants $a, b, c, d$ and a descendant of ContinuedFraction_base class. The most important method of this class would be next() or _iter_(), which would read the input $x$ until it can output new term using Gosper's algorithm.

How do I create an instance of this class with proper arguments, when for example $(3*x + 1)/2$, where $x$ is a CF, is called from Sage? (Instead of _mul_() being called, than _add_() and so on...)

Simplifying using inbuilt Sage methods and then evaluating the expression as a string using regular expressions somehow (like this) might be a solution, but I am not sure if the best one.

Thank you very much for any advice on this!

EDIT: I have found this promising link, but haven't found anything useful in there so far...

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
1

answered 2016-02-26 03:44:53 -0500

tmonteil gravatar image

updated 2016-02-26 04:13:00 -0500

Preparsing as your first link is mostyl impossible because the expression (3∗x+1)/2 could appear with various types of x, and it would be very hard from the parser to infer how to deal with it with just knowing a small string.

The problem is that you do not want to override the addition and multiplication, but if you want to write (3∗x+1)/2, the interpreter will start by doing 3*x as a multiplication. So what you have to do is to define the action of a homography (taken as a whole) on a continued fraction.

For this, you can either define an apply_homography(self, matrix) method in the ContinuedFraction_* classes where your algorithm can be applied (the highest in the hierarchy is the best to avoid repetitions).

Otherwise, if you have many methods to develop about homographies (not only Gosper algorithm), you can create a Homography class and define a product h * cf to be understood as the action of the homography h on the continued fraction cf.

edit flag offensive delete link more

Comments

This is great advice, thank you.

mirgee gravatar imagemirgee ( 2016-02-26 07:50:40 -0500 )edit

What would be the proper way to implement Homography class? Where should I put it?

mirgee gravatar imagemirgee ( 2016-02-29 07:50:16 -0500 )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: 2016-02-26 00:29:45 -0500

Seen: 131 times

Last updated: Feb 26 '16