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:28:11 +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?Wed, 31 Oct 2012 13:03:05 +0100https://ask.sagemath.org/question/9489/write-function-for-use-by-both-mpmath-and-sage-types/Comment by kcrisman for <p>I've got the following function:</p>
<pre><code>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
</code></pre>
<p>I'd like to use this function with various argument typs, among them <code>RealField</code>, <code>RealIntervalField</code> and the <code>mpf</code> type from <code>mpmath</code>. The latter appears to be a requirement for using <code>mpmath.findroot</code>, as suggested by <a href="http://ask.sagemath.org/question/1934/find-roots-with-arbitrary-precision?answer=2812#2812">this answer</a>.</p>
<p>However, the code currently fails to work with <code>mpf</code>, as these numbers don't interact nicely with sage constants:</p>
<pre><code>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
</code></pre>
<p>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?</p>
<p>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?</p>
https://ask.sagemath.org/question/9489/write-function-for-use-by-both-mpmath-and-sage-types/?comment=18775#post-id-18775Unfortunately, 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.Wed, 31 Oct 2012 13:28:11 +0100https://ask.sagemath.org/question/9489/write-function-for-use-by-both-mpmath-and-sage-types/?comment=18775#post-id-18775