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, 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!
See http://trac.sagemath.org/ticket/20466
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)
@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 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.