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.Sat, 08 Jan 2022 22:24:01 +0100Simplify function numericallyhttps://ask.sagemath.org/question/60538/simplify-function-numerically/ When I have a function that is exact, such as:
x = var("x")
fnc = sqrt(2) * x
print(fnc)
And I want to resolve/approximate all those parts of the function which can be resolved numerically, how can I do this?
fnc.n()
does not work because it throws a TypeError, similarly does
float(fnc)
I've also tried all .simplify() variants, but neither do what I want. I simply want the output to be:
print(magic_function(fnc))
1.4142 * x
How can this be done?Mon, 03 Jan 2022 19:06:07 +0100https://ask.sagemath.org/question/60538/simplify-function-numerically/Comment by itecMemory for <p>When I have a function that is exact, such as:</p>
<pre><code>x = var("x")
fnc = sqrt(2) * x
print(fnc)
</code></pre>
<p>And I want to resolve/approximate all those parts of the function which can be resolved numerically, how can I do this?</p>
<pre><code>fnc.n()
</code></pre>
<p>does not work because it throws a TypeError, similarly does</p>
<pre><code>float(fnc)
</code></pre>
<p>I've also tried all .simplify() variants, but neither do what I want. I simply want the output to be:</p>
<pre><code>print(magic_function(fnc))
1.4142 * x
</code></pre>
<p>How can this be done?</p>
https://ask.sagemath.org/question/60538/simplify-function-numerically/?comment=60568#post-id-60568It does indeed! I was using an ancient 2017 version of Sagemath. Once I upgraded to 9.2, it worked like advertised. Thanks for the help!
EDIT: Sorry, no, it doesn't work. Wasn't the sage version. Try this:
expr = 1.5*2^x/log(2)
print(SubstituteNumericalApprox()(expr))
gives
1.50000000000000*2.00000000000000^x/log(2.00000000000000)^1.00000000000000
i.e. log(2) is not resolved.Fri, 07 Jan 2022 12:14:26 +0100https://ask.sagemath.org/question/60538/simplify-function-numerically/?comment=60568#post-id-60568Comment by rburing for <p>When I have a function that is exact, such as:</p>
<pre><code>x = var("x")
fnc = sqrt(2) * x
print(fnc)
</code></pre>
<p>And I want to resolve/approximate all those parts of the function which can be resolved numerically, how can I do this?</p>
<pre><code>fnc.n()
</code></pre>
<p>does not work because it throws a TypeError, similarly does</p>
<pre><code>float(fnc)
</code></pre>
<p>I've also tried all .simplify() variants, but neither do what I want. I simply want the output to be:</p>
<pre><code>print(magic_function(fnc))
1.4142 * x
</code></pre>
<p>How can this be done?</p>
https://ask.sagemath.org/question/60538/simplify-function-numerically/?comment=60554#post-id-60554I just tried it and it does work on your example. On what expression does it not work?Wed, 05 Jan 2022 23:03:39 +0100https://ask.sagemath.org/question/60538/simplify-function-numerically/?comment=60554#post-id-60554Comment by itecMemory for <p>When I have a function that is exact, such as:</p>
<pre><code>x = var("x")
fnc = sqrt(2) * x
print(fnc)
</code></pre>
<p>And I want to resolve/approximate all those parts of the function which can be resolved numerically, how can I do this?</p>
<pre><code>fnc.n()
</code></pre>
<p>does not work because it throws a TypeError, similarly does</p>
<pre><code>float(fnc)
</code></pre>
<p>I've also tried all .simplify() variants, but neither do what I want. I simply want the output to be:</p>
<pre><code>print(magic_function(fnc))
1.4142 * x
</code></pre>
<p>How can this be done?</p>
https://ask.sagemath.org/question/60538/simplify-function-numerically/?comment=60551#post-id-60551Thanks for the answer, but this doesn't work. The ExpressionTreeWalker walks coefficients/factors (e.g., the "2" in this case) but it doesn't resolve sqrt(2).Wed, 05 Jan 2022 09:12:09 +0100https://ask.sagemath.org/question/60538/simplify-function-numerically/?comment=60551#post-id-60551Comment by rburing for <p>When I have a function that is exact, such as:</p>
<pre><code>x = var("x")
fnc = sqrt(2) * x
print(fnc)
</code></pre>
<p>And I want to resolve/approximate all those parts of the function which can be resolved numerically, how can I do this?</p>
<pre><code>fnc.n()
</code></pre>
<p>does not work because it throws a TypeError, similarly does</p>
<pre><code>float(fnc)
</code></pre>
<p>I've also tried all .simplify() variants, but neither do what I want. I simply want the output to be:</p>
<pre><code>print(magic_function(fnc))
1.4142 * x
</code></pre>
<p>How can this be done?</p>
https://ask.sagemath.org/question/60538/simplify-function-numerically/?comment=60546#post-id-60546See [is it possible to round numbers in symbolic expression](https://ask.sagemath.org/question/46059/is-it-possible-to-round-numbers-in-symbolic-expression/#46061).Tue, 04 Jan 2022 10:21:40 +0100https://ask.sagemath.org/question/60538/simplify-function-numerically/?comment=60546#post-id-60546Answer by rburing for <p>When I have a function that is exact, such as:</p>
<pre><code>x = var("x")
fnc = sqrt(2) * x
print(fnc)
</code></pre>
<p>And I want to resolve/approximate all those parts of the function which can be resolved numerically, how can I do this?</p>
<pre><code>fnc.n()
</code></pre>
<p>does not work because it throws a TypeError, similarly does</p>
<pre><code>float(fnc)
</code></pre>
<p>I've also tried all .simplify() variants, but neither do what I want. I simply want the output to be:</p>
<pre><code>print(magic_function(fnc))
1.4142 * x
</code></pre>
<p>How can this be done?</p>
https://ask.sagemath.org/question/60538/simplify-function-numerically/?answer=60581#post-id-60581I've updated [my answer to "is it possible to round numbers in symbolic expression"](https://ask.sagemath.org/question/46059/is-it-possible-to-round-numbers-in-symbolic-expression/#46061) to handle functions (such as logarithms) that can be evaluated numerically:
sage: SubstituteNumericalApprox(digits=5)(1.5*2^x/log(2))
2.1640*2.0000^xSat, 08 Jan 2022 17:54:18 +0100https://ask.sagemath.org/question/60538/simplify-function-numerically/?answer=60581#post-id-60581Answer by Emmanuel Charpentier for <p>When I have a function that is exact, such as:</p>
<pre><code>x = var("x")
fnc = sqrt(2) * x
print(fnc)
</code></pre>
<p>And I want to resolve/approximate all those parts of the function which can be resolved numerically, how can I do this?</p>
<pre><code>fnc.n()
</code></pre>
<p>does not work because it throws a TypeError, similarly does</p>
<pre><code>float(fnc)
</code></pre>
<p>I've also tried all .simplify() variants, but neither do what I want. I simply want the output to be:</p>
<pre><code>print(magic_function(fnc))
1.4142 * x
</code></pre>
<p>How can this be done?</p>
https://ask.sagemath.org/question/60538/simplify-function-numerically/?answer=60586#post-id-60586FWIW :
sage: (x*sqrt(2)).n()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
[ Snip... ]
TypeError: cannot evaluate symbolic expression numerically
indeed... But :
sage: (x*sqrt(2))._sympy_().n()._sage_()
1.41421356237310*x
Nevertheless, `rbunrning`'s answer to this (close) [question](https://ask.sagemath.org/question/46059/is-it-possible-to-round-numbers-in-symbolic-expression/) is highly relevant
HTH,Sat, 08 Jan 2022 22:24:01 +0100https://ask.sagemath.org/question/60538/simplify-function-numerically/?answer=60586#post-id-60586