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.Wed, 31 Oct 2012 13:03:05 +0100Write function for use by both mpmath and sage typeshttps://ask.sagemath.org/question/9489/write-function-for-use-by-both-mpmath-and-sage-types/I've got the following function:
def area2(d, r):
x = (sqrt(2*r^2 - d^2) - d)/2
a2 = 2*arccos((d + x)/r)
a2 = (a2 - sin(a2))/2*r^2
return ((2*x)^2 + 4*a2) - d.parent().pi()/9
I'd like to use this function with various argument typs, among them `RealField`, `RealIntervalField` and the `mpf` type from `mpmath`. The latter appears to be a requirement for using `mpmath.findroot`, as suggested by [this answer](http://ask.sagemath.org/question/1934/find-roots-with-arbitrary-precision?answer=2812#2812).
However, the code currently fails to work with `mpf`, as these numbers don't interact nicely with sage constants:
sage: area2(mpmath.mpf(4.44), mpmath.mpf(4.74))
[ ]
a2 = _sage_const_2 *arccos((d + x)/r)
[ ]
TypeError: cannot coerce arguments: no canonical coercion from <type 'sage.libs.mpmath.ext_main.mpf'> to Symbolic Ring
So my question is this: is there a way to write this function so that it will allow operation on all the mentioned number types, without case distinctions to cater for the different types, and without loosing precision to casts?
If this is not possible, do you know of any existing bug report asking for such a feature, should I open a new ticket, or is there any reason why such an interoperability is not technically possible?MvGWed, 31 Oct 2012 13:03:05 +0100https://ask.sagemath.org/question/9489/