Write function for use by both mpmath and sage types

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

MvG gravatar image

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?

edit retag flag offensive close merge delete

Comments

1

Unfortunately, we just haven't done it. See http://trac.sagemath.org/sage_trac/ticket/13608 for one example of this problem. But in theory we should be able to have Sage coercion with mpmath types.

kcrisman gravatar imagekcrisman ( 2012-10-31 07:28:11 -0500 )edit