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.Mon, 29 Feb 2016 07:50:16 -0600Implementing new CF class advicehttp://ask.sagemath.org/question/32672/implementing-new-cf-class-advice/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](http://sunjay.ca/2014/04/27/evaluating-simple-math-expressions-using-python-and-regular-expressions/)) 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](http://doc.sagemath.org/html/en/thematic_tutorials/coercion_and_categories.html), but haven't found anything useful in there so far...Fri, 26 Feb 2016 00:29:45 -0600http://ask.sagemath.org/question/32672/implementing-new-cf-class-advice/Answer by tmonteil for <p>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</p>
<p>$$x \rightarrow \frac{ax + b}{cx + d}$$</p>
<p>where $x$ is a CF. </p>
<p>The easiest way to do this is probably to create a new class in <code>sage.rings.continued_fraction</code> that would on inicialization accept the integer constants $a, b, c, d$ and a descendant of <code>ContinuedFraction_base</code> class. The most important method of this class would be <code>next()</code> or <code>_iter_()</code>, which would read the input $x$ until it can output new term using Gosper's algorithm.</p>
<p>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 <code>_mul_()</code> being called, than <code>_add_()</code> and so on...)</p>
<p>Simplifying using inbuilt Sage methods and then evaluating the expression as a string using regular expressions somehow (like <a href="http://sunjay.ca/2014/04/27/evaluating-simple-math-expressions-using-python-and-regular-expressions/">this</a>) might be a solution, but I am not sure if the best one.</p>
<p>Thank you very much for any advice on this!</p>
<p>EDIT: I have found this promising <a href="http://doc.sagemath.org/html/en/thematic_tutorials/coercion_and_categories.html">link</a>, but haven't found anything useful in there so far...</p>
http://ask.sagemath.org/question/32672/implementing-new-cf-class-advice/?answer=32673#post-id-32673Preparsing 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`.
Fri, 26 Feb 2016 03:44:53 -0600http://ask.sagemath.org/question/32672/implementing-new-cf-class-advice/?answer=32673#post-id-32673Comment by mirgee for <p>Preparsing as your first link is mostyl impossible because the expression <code>(3∗x+1)/2</code> could appear with various types of <code>x</code>, and it would be very hard from the parser to infer how to deal with it with just knowing a small string.</p>
<p>The problem is that you do not want to override the addition and multiplication, but if you want to write <code>(3∗x+1)/2</code>, the interpreter will start by doing <code>3*x</code> 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.</p>
<p>For this, you can either define an <code>apply_homography(self, matrix)</code> method in the <code>ContinuedFraction_*</code> classes where your algorithm can be applied (the highest in the hierarchy is the best to avoid repetitions).</p>
<p>Otherwise, if you have many methods to develop about homographies (not only Gosper algorithm), you can create a <code>Homography</code> class and define a product <code>h * cf</code> to be understood as the action of the homography <code>h</code> on the continued fraction <code>cf</code>.</p>
http://ask.sagemath.org/question/32672/implementing-new-cf-class-advice/?comment=32688#post-id-32688What would be the proper way to implement Homography class? Where should I put it?Mon, 29 Feb 2016 07:50:16 -0600http://ask.sagemath.org/question/32672/implementing-new-cf-class-advice/?comment=32688#post-id-32688Comment by mirgee for <p>Preparsing as your first link is mostyl impossible because the expression <code>(3∗x+1)/2</code> could appear with various types of <code>x</code>, and it would be very hard from the parser to infer how to deal with it with just knowing a small string.</p>
<p>The problem is that you do not want to override the addition and multiplication, but if you want to write <code>(3∗x+1)/2</code>, the interpreter will start by doing <code>3*x</code> 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.</p>
<p>For this, you can either define an <code>apply_homography(self, matrix)</code> method in the <code>ContinuedFraction_*</code> classes where your algorithm can be applied (the highest in the hierarchy is the best to avoid repetitions).</p>
<p>Otherwise, if you have many methods to develop about homographies (not only Gosper algorithm), you can create a <code>Homography</code> class and define a product <code>h * cf</code> to be understood as the action of the homography <code>h</code> on the continued fraction <code>cf</code>.</p>
http://ask.sagemath.org/question/32672/implementing-new-cf-class-advice/?comment=32674#post-id-32674This is great advice, thank you.Fri, 26 Feb 2016 07:50:40 -0600http://ask.sagemath.org/question/32672/implementing-new-cf-class-advice/?comment=32674#post-id-32674