ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 13 Nov 2014 12:50:09 -0600Unable to coerce to realhttp://ask.sagemath.org/question/24859/unable-to-coerce-to-real/I'm having trouble with the find_local_minimum and find_local_maximum functions on the absolute value of a complex function. For instance, the following code is logical and works mathematically, but not in Sage:
x = var('x')
find_local_maximum(abs(i*x), 1000, 3000)
But alas, I get the error:
TypeError: unable to coerce to a real number
I've also tried adding an explicit conversion to real (desperate times):
x = var('x')
find_local_maximum(abs(i*x).real(), 1000, 3000)
This gave the same error.
How do I get around this issue? And is there the possibility that this could be resolved in the Sage codebase? It seems feasible, since the plot function works just fine with the exact same function.Thu, 13 Nov 2014 11:08:45 -0600http://ask.sagemath.org/question/24859/unable-to-coerce-to-real/Answer by tmonteil for <p>I'm having trouble with the find_local_minimum and find_local_maximum functions on the absolute value of a complex function. For instance, the following code is logical and works mathematically, but not in Sage:</p>
<pre><code>x = var('x')
find_local_maximum(abs(i*x), 1000, 3000)
</code></pre>
<p>But alas, I get the error:</p>
<pre><code>TypeError: unable to coerce to a real number
</code></pre>
<p>I've also tried adding an explicit conversion to real (desperate times):</p>
<pre><code>x = var('x')
find_local_maximum(abs(i*x).real(), 1000, 3000)
</code></pre>
<p>This gave the same error.</p>
<p>How do I get around this issue? And is there the possibility that this could be resolved in the Sage codebase? It seems feasible, since the plot function works just fine with the exact same function.</p>
http://ask.sagemath.org/question/24859/unable-to-coerce-to-real/?answer=24860#post-id-24860This is clearly a bug. Looking ate the source code, it seems to be related to the fact that Sage tries to transform the smbolic expression ``abs(i*x)`` into an object which provides fast floating point evaluation of it:
sage: abs(i*x)._fast_float_()
TypeError: unable to coerce to a real number
A workaround is to provide a python function, not a symbolic expression:
sage: f = lambda x : abs(i*x)
sage: find_local_maximum(f, 1000, 3000)
(2999.999947571147, 2999.999947571147
Thu, 13 Nov 2014 11:54:45 -0600http://ask.sagemath.org/question/24859/unable-to-coerce-to-real/?answer=24860#post-id-24860Comment by foliot for <p>This is clearly a bug. Looking ate the source code, it seems to be related to the fact that Sage tries to transform the smbolic expression <code>abs(i*x)</code> into an object which provides fast floating point evaluation of it:</p>
<pre><code>sage: abs(i*x)._fast_float_()
TypeError: unable to coerce to a real number
</code></pre>
<p>A workaround is to provide a python function, not a symbolic expression:</p>
<pre><code>sage: f = lambda x : abs(i*x)
sage: find_local_maximum(f, 1000, 3000)
(2999.999947571147, 2999.999947571147
</code></pre>
http://ask.sagemath.org/question/24859/unable-to-coerce-to-real/?comment=24862#post-id-24862That works in this case. I have another problem, but I think it deserves a new question, so I'll ask it there.Thu, 13 Nov 2014 12:50:09 -0600http://ask.sagemath.org/question/24859/unable-to-coerce-to-real/?comment=24862#post-id-24862