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, 20 Feb 2013 10:34:55 +0100Evaluate expression with unknownshttps://ask.sagemath.org/question/9816/evaluate-expression-with-unknowns/I'm trying to evaluate an integral that comes out with a crazy long result. I'm not going to paste it here because it really is quite long, which is essentially the problem. The result actually only has a few instances of unknowns in it, 90% of it's length comes from un-evaluated constants (like 2^(1/7), log(11.5), stuff like that). So it sort of looks like:
f(x) = (x* 2* pi* log(5)* 6^1.5) / (3^4*pi^2+x)
except it spans 10 lines.
If I could get sage to just express all of that stuff as a solid number, then the resulting expression wouldn't be so prohibitively long (I think it would actually evaluate out to something similar to the example I gave, number*x/(number+x) ). But numerical_approx() won't take anything with unknowns in it, so I can't just plug that expression into n().
How does one evaluate the knowns in an expression that contains unknowns?
Thank you ahead of time for your help!Mon, 18 Feb 2013 14:59:38 +0100https://ask.sagemath.org/question/9816/evaluate-expression-with-unknowns/Answer by twch for <p>I'm trying to evaluate an integral that comes out with a crazy long result. I'm not going to paste it here because it really is quite long, which is essentially the problem. The result actually only has a few instances of unknowns in it, 90% of it's length comes from un-evaluated constants (like 2^(1/7), log(11.5), stuff like that). So it sort of looks like:</p>
<p>f(x) = (x* 2* pi* log(5)* 6^1.5) / (3^4*pi^2+x)</p>
<p>except it spans 10 lines. </p>
<p>If I could get sage to just express all of that stuff as a solid number, then the resulting expression wouldn't be so prohibitively long (I think it would actually evaluate out to something similar to the example I gave, number*x/(number+x) ). But numerical_approx() won't take anything with unknowns in it, so I can't just plug that expression into n().</p>
<p>How does one evaluate the knowns in an expression that contains unknowns?</p>
<p>Thank you ahead of time for your help!</p>
https://ask.sagemath.org/question/9816/evaluate-expression-with-unknowns/?answer=14556#post-id-14556Mh interesting question. I'm sure that I will not answer it completely satisfactory, but for the example that you have given, the following works:
f= (x* 2* pi* log(5)* 6^1.5)/(3^4*pi^2+x)
R = RR['x']
FR=Frac(R)
FR(f)
returns
148.621271129886*x/(x + 799.437956488238)
The idea behind it is to convert the symbolic expression to the fractional field of polynomials with coefficiants in the real numbers. This however fails as soon as the expression you like to simplify is not a rational function anymore, i.e. as soon as some squareroots or trigonometric functions appear. So what else can be done?
Mon, 18 Feb 2013 15:57:09 +0100https://ask.sagemath.org/question/9816/evaluate-expression-with-unknowns/?answer=14556#post-id-14556Answer by burcin for <p>I'm trying to evaluate an integral that comes out with a crazy long result. I'm not going to paste it here because it really is quite long, which is essentially the problem. The result actually only has a few instances of unknowns in it, 90% of it's length comes from un-evaluated constants (like 2^(1/7), log(11.5), stuff like that). So it sort of looks like:</p>
<p>f(x) = (x* 2* pi* log(5)* 6^1.5) / (3^4*pi^2+x)</p>
<p>except it spans 10 lines. </p>
<p>If I could get sage to just express all of that stuff as a solid number, then the resulting expression wouldn't be so prohibitively long (I think it would actually evaluate out to something similar to the example I gave, number*x/(number+x) ). But numerical_approx() won't take anything with unknowns in it, so I can't just plug that expression into n().</p>
<p>How does one evaluate the knowns in an expression that contains unknowns?</p>
<p>Thank you ahead of time for your help!</p>
https://ask.sagemath.org/question/9816/evaluate-expression-with-unknowns/?answer=14559#post-id-14559You need the `._convert()` method:
sage: f = (x* 2* pi* log(5)* 6^1.5)/(3^4*pi^2+x)
sage: f._convert(RR)
148.621271129886*x/(x + 799.437956488238)
Tue, 19 Feb 2013 10:17:14 +0100https://ask.sagemath.org/question/9816/evaluate-expression-with-unknowns/?answer=14559#post-id-14559Comment by kcrisman for <p>You need the <code>._convert()</code> method:</p>
<pre><code>sage: f = (x* 2* pi* log(5)* 6^1.5)/(3^4*pi^2+x)
sage: f._convert(RR)
148.621271129886*x/(x + 799.437956488238)
</code></pre>
https://ask.sagemath.org/question/9816/evaluate-expression-with-unknowns/?comment=18209#post-id-18209@burcin - should this be a non-underscore function, then?Wed, 20 Feb 2013 10:34:55 +0100https://ask.sagemath.org/question/9816/evaluate-expression-with-unknowns/?comment=18209#post-id-18209Comment by twch for <p>You need the <code>._convert()</code> method:</p>
<pre><code>sage: f = (x* 2* pi* log(5)* 6^1.5)/(3^4*pi^2+x)
sage: f._convert(RR)
148.621271129886*x/(x + 799.437956488238)
</code></pre>
https://ask.sagemath.org/question/9816/evaluate-expression-with-unknowns/?comment=18217#post-id-18217nice! this really seems to solve the question.Tue, 19 Feb 2013 10:41:53 +0100https://ask.sagemath.org/question/9816/evaluate-expression-with-unknowns/?comment=18217#post-id-18217Comment by adamhg for <p>You need the <code>._convert()</code> method:</p>
<pre><code>sage: f = (x* 2* pi* log(5)* 6^1.5)/(3^4*pi^2+x)
sage: f._convert(RR)
148.621271129886*x/(x + 799.437956488238)
</code></pre>
https://ask.sagemath.org/question/9816/evaluate-expression-with-unknowns/?comment=18214#post-id-18214Aha! This is exactly what I needed, thank you. Thank you @tobias-weich as well, you are right though my final expression wasn't just a polynomial or a division of polynomials.Tue, 19 Feb 2013 17:11:29 +0100https://ask.sagemath.org/question/9816/evaluate-expression-with-unknowns/?comment=18214#post-id-18214Comment by twch for <p>You need the <code>._convert()</code> method:</p>
<pre><code>sage: f = (x* 2* pi* log(5)* 6^1.5)/(3^4*pi^2+x)
sage: f._convert(RR)
148.621271129886*x/(x + 799.437956488238)
</code></pre>
https://ask.sagemath.org/question/9816/evaluate-expression-with-unknowns/?comment=18213#post-id-18213you might then mark burcin's answer as a solution by clicking on the check mark in order to show to the others, that this question is solved.Tue, 19 Feb 2013 18:00:59 +0100https://ask.sagemath.org/question/9816/evaluate-expression-with-unknowns/?comment=18213#post-id-18213