Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

asked 2012-10-31 07:03:05 -0600

MvG gravatar image

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.

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?