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, 06 Dec 2017 11:36:03 +0100How can I get mpmath to work with sage variables?https://ask.sagemath.org/question/39921/how-can-i-get-mpmath-to-work-with-sage-variables/ In Sage, the following code produces error messages ending with "TypeError: cannot evaluate symbolic expression numerically":
from mpmath import *
var('a')
print sqrt(a)
If I remove the line "from mpmath import *", it prints "sqrt(a)" as expected.
How can I get mpmath to work with sage variables?
Sun, 03 Dec 2017 00:10:43 +0100https://ask.sagemath.org/question/39921/how-can-i-get-mpmath-to-work-with-sage-variables/Answer by tmonteil for <p>In Sage, the following code produces error messages ending with "TypeError: cannot evaluate symbolic expression numerically":</p>
<pre><code>from mpmath import *
var('a')
print sqrt(a)
</code></pre>
<p>If I remove the line "from mpmath import *", it prints "sqrt(a)" as expected.</p>
<p>How can I get mpmath to work with sage variables?</p>
https://ask.sagemath.org/question/39921/how-can-i-get-mpmath-to-work-with-sage-variables/?answer=39934#post-id-39934To complement @vdelecroix answer, the main issue is that, when you write:
from mpmath import *
you import hundreds of functions into the namespace, overwriting the ones that are imported by Sage, this includes `sqrt`.
If you want to use both Sage's symbolics and `mpmath` numerics, you have to be more parsimonious and only import what you need. For example, if the functions you want to import from `mpmath` are `acos` and `atan`, you can write:
from mpmath import acos, atan
Also, perhaps a more long-term solution, if there is something that works better with `mpmath` than with the Sage's default (or that seems absent from Sage), you should advertise it so that we use mpmath for such a feature in Sage.
Mon, 04 Dec 2017 09:32:53 +0100https://ask.sagemath.org/question/39921/how-can-i-get-mpmath-to-work-with-sage-variables/?answer=39934#post-id-39934Comment by B r u n o for <p>To complement <a href="/users/87/vdelecroix/">@vdelecroix</a> answer, the main issue is that, when you write:</p>
<pre><code>from mpmath import *
</code></pre>
<p>you import hundreds of functions into the namespace, overwriting the ones that are imported by Sage, this includes <code>sqrt</code>.</p>
<p>If you want to use both Sage's symbolics and <code>mpmath</code> numerics, you have to be more parsimonious and only import what you need. For example, if the functions you want to import from <code>mpmath</code> are <code>acos</code> and <code>atan</code>, you can write:</p>
<pre><code>from mpmath import acos, atan
</code></pre>
<p>Also, perhaps a more long-term solution, if there is something that works better with <code>mpmath</code> than with the Sage's default (or that seems absent from Sage), you should advertise it so that we use mpmath for such a feature in Sage.</p>
https://ask.sagemath.org/question/39921/how-can-i-get-mpmath-to-work-with-sage-variables/?comment=39938#post-id-39938As a complement: If you ant to use both `mpmath`'s `sqrt` and Sage's one, you have three options: Either you use `from mpmath import sqrt as mpsqrt` and call `mpsqrt`, or you use `import mpmath` and call `mpmath.sqrt`, or you use `import mpmath as mp` and call `mp.sqrt`. (The `as ...` is the way to define an alias, so you can use anything in place of `mpsqrt` or `mp` in my examples.) In all cases, `sqrt` still refers to Sage's function.Mon, 04 Dec 2017 10:44:29 +0100https://ask.sagemath.org/question/39921/how-can-i-get-mpmath-to-work-with-sage-variables/?comment=39938#post-id-39938Answer by vdelecroix for <p>In Sage, the following code produces error messages ending with "TypeError: cannot evaluate symbolic expression numerically":</p>
<pre><code>from mpmath import *
var('a')
print sqrt(a)
</code></pre>
<p>If I remove the line "from mpmath import *", it prints "sqrt(a)" as expected.</p>
<p>How can I get mpmath to work with sage variables?</p>
https://ask.sagemath.org/question/39921/how-can-i-get-mpmath-to-work-with-sage-variables/?answer=39924#post-id-39924`mpmath` is a library intended to compute functions to high precision. If you try to use it on a symbolic variable it will fail. The input must be numeric (and Sage numbers work well)
sage: import mpmath
sage: mpmath.sqrt(5.3)
mpf('2.3021728866442674')
sage: mpmath.sqrt(pi)
mpf('1.7724538509055159')
Sun, 03 Dec 2017 12:13:49 +0100https://ask.sagemath.org/question/39921/how-can-i-get-mpmath-to-work-with-sage-variables/?answer=39924#post-id-39924Comment by vdelecroix for <p><code>mpmath</code> is a library intended to compute functions to high precision. If you try to use it on a symbolic variable it will fail. The input must be numeric (and Sage numbers work well)</p>
<pre><code>sage: import mpmath
sage: mpmath.sqrt(5.3)
mpf('2.3021728866442674')
sage: mpmath.sqrt(pi)
mpf('1.7724538509055159')
</code></pre>
https://ask.sagemath.org/question/39921/how-can-i-get-mpmath-to-work-with-sage-variables/?comment=39970#post-id-39970Yes: do not use `from mpmath import *` but say explicitely what you need to import.Wed, 06 Dec 2017 11:36:03 +0100https://ask.sagemath.org/question/39921/how-can-i-get-mpmath-to-work-with-sage-variables/?comment=39970#post-id-39970Comment by millermj for <p><code>mpmath</code> is a library intended to compute functions to high precision. If you try to use it on a symbolic variable it will fail. The input must be numeric (and Sage numbers work well)</p>
<pre><code>sage: import mpmath
sage: mpmath.sqrt(5.3)
mpf('2.3021728866442674')
sage: mpmath.sqrt(pi)
mpf('1.7724538509055159')
</code></pre>
https://ask.sagemath.org/question/39921/how-can-i-get-mpmath-to-work-with-sage-variables/?comment=39933#post-id-39933I'm attempting to do a mix of symbolic and high-precision-numeric computations in a loop. It appears (from my example above) that merely importing mpmath precludes doing **any** symbolic computations involving sqrt **anywhere** in that session.
Specifically, it appears that the "ordinary" sqrt works with symbolic variables, but that the mpmath sqrt does not. Is there a workaround?Mon, 04 Dec 2017 05:12:16 +0100https://ask.sagemath.org/question/39921/how-can-i-get-mpmath-to-work-with-sage-variables/?comment=39933#post-id-39933