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.Thu, 25 Feb 2021 20:00:22 +0100Evaluate constants in symbolic expression?https://ask.sagemath.org/question/55911/evaluate-constants-in-symbolic-expression/ Hello. If I have a symbolic expression where all of the variables are known, I can use `numerical_approx` to evaluate it:
var('a b')
x = a / b
x(a=1, b=3)
> 1/3
x(a=1, b=3).numerical_approx()
> 0.333333333333333
However, if there is an unknown variable, `numerical_approx` fails:
var('a b c')
x = a / b * c
x(a=1, b=3)
> 1/3*c
x(a=1, b=3).numerical_approx()
> TypeError: cannot evaluate symbolic expression numerically
Is there a way to approximate the values of all constants / coefficients in a symbolic expression? Essentially, something like
var('a b c')
x = a / b * c
x(a=1, b=3).numerical_approx_constants()
> 0.33333333333*c
Thank you in advance.MaybE_TreeThu, 25 Feb 2021 20:00:22 +0100https://ask.sagemath.org/question/55911/Numerical values VS symbolic values ?https://ask.sagemath.org/question/23678/numerical-values-vs-symbolic-values/ This question might be related with [link text](http://ask.sagemath.org/question/8588/pi-and-e-not-evaluated-when-i-use-my-own-classes/?comment=23671#comment-23671)
I have to perform some numerical calculation using constants like pi and e. What happens is that computing a simple expression of pi is evaluated numerically (eg, cos(pi) returns -1) but when I use a random function (eg, random()*pi), I have a symbolic expression like "0.123456789*pi". In a for loop with this expression I obtain at the end something like "0.123456789*pi + 0.987654321*pi + ..." and so on.
My question aims to clarify the way to use symbolic expression (SE) and/or numerical values (NV) within a code (either in sage shell or script file). I think we have different cases to think about :
1. I want to use only NV in my code, how can I specify once for all that constants I will use will be evaluated numerically ?
2. I want to use only SE in my code, this one seems straightforward as Sage uses a preparser structure with symbolic expression.
3. I want to use both in my code, a function using constants need to return NV but also SE. Of course calculation will use NV from this function and analysis will use SE (eg, derivation, integration, series expansion, etc...).
I hope this thread will be useful. I think I know how to use case 1, for example with NV of pi as PI=RDF.pi() or PI=pi.n(). In case of random()*PI, we have indeed a numerical result, as wanted.
Case 3 is more interesting, I remember having a lots of problem with python.sympy with SE and NV. I struggled to use SE for analysis then trying to obtain NV. I'd like to see what you think about this, Sage seems more powerful than sympy about that. I read documentation but maybe I missed something. I'm not working on this case for now (so no code example...) but if needed for clarity I can dig one of my old sympy code.
(sorry for my english, it is sometimes "random" )
bigdukeSun, 03 Aug 2014 13:05:57 +0200https://ask.sagemath.org/question/23678/