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.Sun, 30 Sep 2012 04:12:13 -0500numerical evaluation with variableshttp://ask.sagemath.org/question/9346/numerical-evaluation-with-variables/Hello,
a few days ago I started working with sage. So very soon I run into into questions.
Here is one of them:
I defined an equation:
sage: x, y = var('x, y')
sage: eq = y == (sqrt(5) + 3) * x^3 + e^pi * x^2 + sqrt(2) * x + 1/27
And I want to get from that something like:
y == 5.236 * x^3 + 23.141 * x^2 + 1.414 * x + 0.037
the numerical_approx() seems to be designed for another purpose. How do I get my desired result?
Sun, 30 Sep 2012 01:00:20 -0500http://ask.sagemath.org/question/9346/numerical-evaluation-with-variables/Answer by DSM for <p>Hello,</p>
<p>a few days ago I started working with sage. So very soon I run into into questions.
Here is one of them:</p>
<p>I defined an equation:</p>
<p>sage: x, y = var('x, y')</p>
<p>sage: eq = y == (sqrt(5) + 3) * x^3 + e^pi * x^2 + sqrt(2) * x + 1/27 </p>
<p>And I want to get from that something like:</p>
<p>y == 5.236 * x^3 + 23.141 * x^2 + 1.414 * x + 0.037</p>
<p>the numerical_approx() seems to be designed for another purpose. How do I get my desired result?</p>
http://ask.sagemath.org/question/9346/numerical-evaluation-with-variables/?answer=14086#post-id-14086One way is to use `_convert` to coerce the symbolic coefficients to the Real Field:
sage: var("x y")
(x, y)
sage: eq = y == (sqrt(5) + 3) * x^3 + e^pi * x^2 + sqrt(2) * x + 1/27
sage: eq._convert(RR)
y == 5.23606797749979*x^3 + 23.1406926327793*x^2 + 1.41421356237310*x + 0.0370370370370370
There's a ticket -- trac #12577 -- which proposes to expose this (by which I mean get rid of the initial _). Unfortunately the `convert` method without an underscore already exists, and doesn't seem to be really useful, but we could use something more explicit like `change_coefficient_ring`. Actually, it could even take a function argument as well as a ring, and could be used for quick transformations without needing to implement an expression walker..Sun, 30 Sep 2012 03:17:13 -0500http://ask.sagemath.org/question/9346/numerical-evaluation-with-variables/?answer=14086#post-id-14086Comment by planimeterer for <p>One way is to use <code>_convert</code> to coerce the symbolic coefficients to the Real Field:</p>
<pre><code>sage: var("x y")
(x, y)
sage: eq = y == (sqrt(5) + 3) * x^3 + e^pi * x^2 + sqrt(2) * x + 1/27
sage: eq._convert(RR)
y == 5.23606797749979*x^3 + 23.1406926327793*x^2 + 1.41421356237310*x + 0.0370370370370370
</code></pre>
<p>There's a ticket -- trac #12577 -- which proposes to expose this (by which I mean get rid of the initial _). Unfortunately the <code>convert</code> method without an underscore already exists, and doesn't seem to be really useful, but we could use something more explicit like <code>change_coefficient_ring</code>. Actually, it could even take a function argument as well as a ring, and could be used for quick transformations without needing to implement an expression walker..</p>
http://ask.sagemath.org/question/9346/numerical-evaluation-with-variables/?comment=18966#post-id-18966Thank you, that is exactly what I needed.Sun, 30 Sep 2012 04:12:13 -0500http://ask.sagemath.org/question/9346/numerical-evaluation-with-variables/?comment=18966#post-id-18966