ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 29 Apr 2016 10:14:37 +0200Coercion on continued fractionshttps://ask.sagemath.org/question/33131/coercion-on-continued-fractions/Currently, when user types
sage: x = continued_fraction(pi)
sage: y = 3*x
an TypeError is raised saying
TypeError: unsupported operand parent(s) for '*': 'Integer Ring' and '<class 'sage.rings.continued_fraction.ContinuedFraction_real'>'
I understand that Sage tries to find common parent of Integer element and `ContinuedFraction_real` and fails. It seems that continued fractions in Sage are not implemented as elements of any particular field, and are derived directly from `SageObject`.
I am implementing an algorithm for arithmetical operations on continued fractions. Is it possible to make Sage call a particular method when the user tries to perform an arithmetical binary operation, where one of the operands is a continued fraction?
I have found those [1](http://doc.sagemath.org/html/en/reference/coercion/index.html#methods-to-implement), [2](http://doc.sagemath.org/html/en/reference/coercion/sage/structure/coerce.html), [3](http://doc.sagemath.org/html/en/tutorial/tour_coercion.html) links, but it seems that the topic of coercion is rather extensive and I don't know what to look for, as I don't know what the correct solution to the problem is.
Thank you for any response!Sun, 17 Apr 2016 14:49:52 +0200https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/Comment by kcrisman for <p>Currently, when user types</p>
<pre><code>sage: x = continued_fraction(pi)
sage: y = 3*x
</code></pre>
<p>an TypeError is raised saying </p>
<pre><code>TypeError: unsupported operand parent(s) for '*': 'Integer Ring' and '<class 'sage.rings.continued_fraction.ContinuedFraction_real'>'
</code></pre>
<p>I understand that Sage tries to find common parent of Integer element and <code>ContinuedFraction_real</code> and fails. It seems that continued fractions in Sage are not implemented as elements of any particular field, and are derived directly from <code>SageObject</code>.</p>
<p>I am implementing an algorithm for arithmetical operations on continued fractions. Is it possible to make Sage call a particular method when the user tries to perform an arithmetical binary operation, where one of the operands is a continued fraction?</p>
<p>I have found those <a href="http://doc.sagemath.org/html/en/reference/coercion/index.html#methods-to-implement">1</a>, <a href="http://doc.sagemath.org/html/en/reference/coercion/sage/structure/coerce.html">2</a>, <a href="http://doc.sagemath.org/html/en/tutorial/tour_coercion.html">3</a> links, but it seems that the topic of coercion is rather extensive and I don't know what to look for, as I don't know what the correct solution to the problem is.</p>
<p>Thank you for any response!</p>
https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/?comment=33147#post-id-33147See http://trac.sagemath.org/ticket/20466Tue, 19 Apr 2016 16:23:14 +0200https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/?comment=33147#post-id-33147Comment by A.Alharbi for <p>Currently, when user types</p>
<pre><code>sage: x = continued_fraction(pi)
sage: y = 3*x
</code></pre>
<p>an TypeError is raised saying </p>
<pre><code>TypeError: unsupported operand parent(s) for '*': 'Integer Ring' and '<class 'sage.rings.continued_fraction.ContinuedFraction_real'>'
</code></pre>
<p>I understand that Sage tries to find common parent of Integer element and <code>ContinuedFraction_real</code> and fails. It seems that continued fractions in Sage are not implemented as elements of any particular field, and are derived directly from <code>SageObject</code>.</p>
<p>I am implementing an algorithm for arithmetical operations on continued fractions. Is it possible to make Sage call a particular method when the user tries to perform an arithmetical binary operation, where one of the operands is a continued fraction?</p>
<p>I have found those <a href="http://doc.sagemath.org/html/en/reference/coercion/index.html#methods-to-implement">1</a>, <a href="http://doc.sagemath.org/html/en/reference/coercion/sage/structure/coerce.html">2</a>, <a href="http://doc.sagemath.org/html/en/tutorial/tour_coercion.html">3</a> links, but it seems that the topic of coercion is rather extensive and I don't know what to look for, as I don't know what the correct solution to the problem is.</p>
<p>Thank you for any response!</p>
https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/?comment=33173#post-id-33173It might be interesting that an older version of sage supports addition and multiplication of continued fraction with each other as well with rational numbers. See William Stein's book [Elementary Number Theory: Primes, Congruences, and Secrets](http://wstein.org/ent/ent.pdf).
Another quick solution(as you mentioned) is to add sage's magical functions _add_ , _mul_, etc. to the class continued_fraction.
It might goes as (for sure it is not an optimal solution):
def _add_(self, other):
check both types
return continued_fraction(self.value + other)Thu, 21 Apr 2016 12:57:08 +0200https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/?comment=33173#post-id-33173Comment by mirgee for <p>Currently, when user types</p>
<pre><code>sage: x = continued_fraction(pi)
sage: y = 3*x
</code></pre>
<p>an TypeError is raised saying </p>
<pre><code>TypeError: unsupported operand parent(s) for '*': 'Integer Ring' and '<class 'sage.rings.continued_fraction.ContinuedFraction_real'>'
</code></pre>
<p>I understand that Sage tries to find common parent of Integer element and <code>ContinuedFraction_real</code> and fails. It seems that continued fractions in Sage are not implemented as elements of any particular field, and are derived directly from <code>SageObject</code>.</p>
<p>I am implementing an algorithm for arithmetical operations on continued fractions. Is it possible to make Sage call a particular method when the user tries to perform an arithmetical binary operation, where one of the operands is a continued fraction?</p>
<p>I have found those <a href="http://doc.sagemath.org/html/en/reference/coercion/index.html#methods-to-implement">1</a>, <a href="http://doc.sagemath.org/html/en/reference/coercion/sage/structure/coerce.html">2</a>, <a href="http://doc.sagemath.org/html/en/tutorial/tour_coercion.html">3</a> links, but it seems that the topic of coercion is rather extensive and I don't know what to look for, as I don't know what the correct solution to the problem is.</p>
<p>Thank you for any response!</p>
https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/?comment=33231#post-id-33231@triviality: But this soultion would work only for cases where the first operand is a continued fraction, wouldn't it? Or modifying all the classes whose objects a continued fraction can be added, multiplied by....Tue, 26 Apr 2016 17:00:45 +0200https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/?comment=33231#post-id-33231Comment by A.Alharbi for <p>Currently, when user types</p>
<pre><code>sage: x = continued_fraction(pi)
sage: y = 3*x
</code></pre>
<p>an TypeError is raised saying </p>
<pre><code>TypeError: unsupported operand parent(s) for '*': 'Integer Ring' and '<class 'sage.rings.continued_fraction.ContinuedFraction_real'>'
</code></pre>
<p>I understand that Sage tries to find common parent of Integer element and <code>ContinuedFraction_real</code> and fails. It seems that continued fractions in Sage are not implemented as elements of any particular field, and are derived directly from <code>SageObject</code>.</p>
<p>I am implementing an algorithm for arithmetical operations on continued fractions. Is it possible to make Sage call a particular method when the user tries to perform an arithmetical binary operation, where one of the operands is a continued fraction?</p>
<p>I have found those <a href="http://doc.sagemath.org/html/en/reference/coercion/index.html#methods-to-implement">1</a>, <a href="http://doc.sagemath.org/html/en/reference/coercion/sage/structure/coerce.html">2</a>, <a href="http://doc.sagemath.org/html/en/tutorial/tour_coercion.html">3</a> links, but it seems that the topic of coercion is rather extensive and I don't know what to look for, as I don't know what the correct solution to the problem is.</p>
<p>Thank you for any response!</p>
https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/?comment=33246#post-id-33246@mirgee Of course, you need to take of each case. I will try to implement this solution and post it here. I think if you manage to find an old version of sage then it is matter of copying and pasting.Wed, 27 Apr 2016 20:07:54 +0200https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/?comment=33246#post-id-33246Answer by vdelecroix for <p>Currently, when user types</p>
<pre><code>sage: x = continued_fraction(pi)
sage: y = 3*x
</code></pre>
<p>an TypeError is raised saying </p>
<pre><code>TypeError: unsupported operand parent(s) for '*': 'Integer Ring' and '<class 'sage.rings.continued_fraction.ContinuedFraction_real'>'
</code></pre>
<p>I understand that Sage tries to find common parent of Integer element and <code>ContinuedFraction_real</code> and fails. It seems that continued fractions in Sage are not implemented as elements of any particular field, and are derived directly from <code>SageObject</code>.</p>
<p>I am implementing an algorithm for arithmetical operations on continued fractions. Is it possible to make Sage call a particular method when the user tries to perform an arithmetical binary operation, where one of the operands is a continued fraction?</p>
<p>I have found those <a href="http://doc.sagemath.org/html/en/reference/coercion/index.html#methods-to-implement">1</a>, <a href="http://doc.sagemath.org/html/en/reference/coercion/sage/structure/coerce.html">2</a>, <a href="http://doc.sagemath.org/html/en/tutorial/tour_coercion.html">3</a> links, but it seems that the topic of coercion is rather extensive and I don't know what to look for, as I don't know what the correct solution to the problem is.</p>
<p>Thank you for any response!</p>
https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/?answer=33156#post-id-33156Indeed, a continued fraction is just a representation of a real number. It does not make much sense to mix these considerations with arithmetic. In the particular case you mentioned you would better do
sage: continued_fraction(3*pi)
[9; 2, 2, 1, 4, 1, 1, 1, 97, 4, 1, 2, 1, 2, 45, 6, 4, 9, 1, 27, ...]
However, the operation `continued_fraction(x) -> continued_fraction(3*x)` can be much faster than the above (and `3 * continued_fraction(x)` might be a shortcut for that). There is a ticket for that purpose: [#19120](http://trac.sagemath.org/ticket/19120).Wed, 20 Apr 2016 19:39:06 +0200https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/?answer=33156#post-id-33156Comment by mirgee for <p>Indeed, a continued fraction is just a representation of a real number. It does not make much sense to mix these considerations with arithmetic. In the particular case you mentioned you would better do</p>
<pre><code>sage: continued_fraction(3*pi)
[9; 2, 2, 1, 4, 1, 1, 1, 97, 4, 1, 2, 1, 2, 45, 6, 4, 9, 1, 27, ...]
</code></pre>
<p>However, the operation <code>continued_fraction(x) -> continued_fraction(3*x)</code> can be much faster than the above (and <code>3 * continued_fraction(x)</code> might be a shortcut for that). There is a ticket for that purpose: <a href="http://trac.sagemath.org/ticket/19120">#19120</a>.</p>
https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/?comment=33160#post-id-33160"It does not make much sense to mix these considerations with arithmetic." I am not sure I understand your point. Can you please explain what considerations do you mean?
"There is a ticket for that purpose: #19120" Yes, I know, this is the ticket I have been working on. Do you have any idea how to make it work, please?Wed, 20 Apr 2016 20:43:04 +0200https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/?comment=33160#post-id-33160Comment by Stepan for <p>Indeed, a continued fraction is just a representation of a real number. It does not make much sense to mix these considerations with arithmetic. In the particular case you mentioned you would better do</p>
<pre><code>sage: continued_fraction(3*pi)
[9; 2, 2, 1, 4, 1, 1, 1, 97, 4, 1, 2, 1, 2, 45, 6, 4, 9, 1, 27, ...]
</code></pre>
<p>However, the operation <code>continued_fraction(x) -> continued_fraction(3*x)</code> can be much faster than the above (and <code>3 * continued_fraction(x)</code> might be a shortcut for that). There is a ticket for that purpose: <a href="http://trac.sagemath.org/ticket/19120">#19120</a>.</p>
https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/?comment=33183#post-id-33183Indeed, doing `continued_fraction(3*x)` is probably faster than `3 * continued_fraction(x)` as it is intended to be done for [#19120](http://trac.sagemath.org/ticket/19120) - however, you need to know the value of `x` to do that! (the algorithm mirgee is working on finds its use if you don't have the value of `x`)Fri, 22 Apr 2016 13:55:08 +0200https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/?comment=33183#post-id-33183Comment by vdelecroix for <p>Indeed, a continued fraction is just a representation of a real number. It does not make much sense to mix these considerations with arithmetic. In the particular case you mentioned you would better do</p>
<pre><code>sage: continued_fraction(3*pi)
[9; 2, 2, 1, 4, 1, 1, 1, 97, 4, 1, 2, 1, 2, 45, 6, 4, 9, 1, 27, ...]
</code></pre>
<p>However, the operation <code>continued_fraction(x) -> continued_fraction(3*x)</code> can be much faster than the above (and <code>3 * continued_fraction(x)</code> might be a shortcut for that). There is a ticket for that purpose: <a href="http://trac.sagemath.org/ticket/19120">#19120</a>.</p>
https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/?comment=33236#post-id-33236Sure. But these are two distinct things (that are not clear in the OP question):
- how do we implement methods in order that **a * continued_fraction(b)** do work (= syntaxic sugar)
- how do we make an algorithm such that given a rational number **a** and a continued fracion **cf** it returns the continued fraction of **a * value(cf)** (= algorithmic problem)Wed, 27 Apr 2016 02:51:53 +0200https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/?comment=33236#post-id-33236Comment by mirgee for <p>Indeed, a continued fraction is just a representation of a real number. It does not make much sense to mix these considerations with arithmetic. In the particular case you mentioned you would better do</p>
<pre><code>sage: continued_fraction(3*pi)
[9; 2, 2, 1, 4, 1, 1, 1, 97, 4, 1, 2, 1, 2, 45, 6, 4, 9, 1, 27, ...]
</code></pre>
<p>However, the operation <code>continued_fraction(x) -> continued_fraction(3*x)</code> can be much faster than the above (and <code>3 * continued_fraction(x)</code> might be a shortcut for that). There is a ticket for that purpose: <a href="http://trac.sagemath.org/ticket/19120">#19120</a>.</p>
https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/?comment=33243#post-id-33243I have already implemented an algorithm performing a Mobius transformation on an input continued fraction. My goal is to call the corresponding method upon a * continued_fraction(x) or continued_fraction(x) * a, where a is an integer, or continued_fraction(x) + continued_fraction(y). I apologize if the wording of my question was confusing. Can you provide any advice, please?Wed, 27 Apr 2016 15:48:01 +0200https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/?comment=33243#post-id-33243Comment by vdelecroix for <p>Indeed, a continued fraction is just a representation of a real number. It does not make much sense to mix these considerations with arithmetic. In the particular case you mentioned you would better do</p>
<pre><code>sage: continued_fraction(3*pi)
[9; 2, 2, 1, 4, 1, 1, 1, 97, 4, 1, 2, 1, 2, 45, 6, 4, 9, 1, 27, ...]
</code></pre>
<p>However, the operation <code>continued_fraction(x) -> continued_fraction(3*x)</code> can be much faster than the above (and <code>3 * continued_fraction(x)</code> might be a shortcut for that). There is a ticket for that purpose: <a href="http://trac.sagemath.org/ticket/19120">#19120</a>.</p>
https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/?comment=33253#post-id-33253It would be better if you push your code on the corresponding trac ticket and we continue discussing there.Thu, 28 Apr 2016 14:59:18 +0200https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/?comment=33253#post-id-33253Comment by mirgee for <p>Indeed, a continued fraction is just a representation of a real number. It does not make much sense to mix these considerations with arithmetic. In the particular case you mentioned you would better do</p>
<pre><code>sage: continued_fraction(3*pi)
[9; 2, 2, 1, 4, 1, 1, 1, 97, 4, 1, 2, 1, 2, 45, 6, 4, 9, 1, 27, ...]
</code></pre>
<p>However, the operation <code>continued_fraction(x) -> continued_fraction(3*x)</code> can be much faster than the above (and <code>3 * continued_fraction(x)</code> might be a shortcut for that). There is a ticket for that purpose: <a href="http://trac.sagemath.org/ticket/19120">#19120</a>.</p>
https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/?comment=33257#post-id-33257Alright, done.Fri, 29 Apr 2016 10:14:37 +0200https://ask.sagemath.org/question/33131/coercion-on-continued-fractions/?comment=33257#post-id-33257