Ask Your Question
3

Coercion on continued fractions

asked 2016-04-17 07:49:52 -0500

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

Comments

kcrisman gravatar imagekcrisman ( 2016-04-19 09:23:14 -0500 )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 05:57:08 -0500 )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 10:00:45 -0500 )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 13:07:54 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-04-20 12:39:06 -0500

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

Comments

"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 13:43:04 -0500 )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 06:55:08 -0500 )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-26 19:51:53 -0500 )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 08:48:01 -0500 )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 07:59:18 -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

2 followers

Stats

Asked: 2016-04-17 07:49:52 -0500

Seen: 156 times

Last updated: Apr 20 '16