Ask Your Question

Coercion on continued fractions

asked 2016-04-17 14:49:52 +0200

mirgee gravatar image

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, 2, 3 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!

edit retag flag offensive close merge delete


kcrisman gravatar imagekcrisman ( 2016-04-19 16:23:14 +0200 )edit

It 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. 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)

A.Alharbi gravatar imageA.Alharbi ( 2016-04-21 12:57:08 +0200 )edit

@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....

mirgee gravatar imagemirgee ( 2016-04-26 17:00:45 +0200 )edit

@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.

A.Alharbi gravatar imageA.Alharbi ( 2016-04-27 20:07:54 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2016-04-20 19:39:06 +0200

vdelecroix gravatar image

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

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.

edit flag offensive delete link more


"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?

mirgee gravatar imagemirgee ( 2016-04-20 20:43:04 +0200 )edit

Indeed, doing continued_fraction(3*x) is probably faster than 3 * continued_fraction(x) as it is intended to be done for #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)

Stepan gravatar imageStepan ( 2016-04-22 13:55:08 +0200 )edit

Sure. 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)
vdelecroix gravatar imagevdelecroix ( 2016-04-27 02:51:53 +0200 )edit

I 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?

mirgee gravatar imagemirgee ( 2016-04-27 15:48:01 +0200 )edit

It would be better if you push your code on the corresponding trac ticket and we continue discussing there.

vdelecroix gravatar imagevdelecroix ( 2016-04-28 14:59:18 +0200 )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



Asked: 2016-04-17 14:49:52 +0200

Seen: 413 times

Last updated: Apr 20 '16