Ask Your Question

Numerical values VS symbolic values ?

asked 2014-08-03 06:05:57 -0500

bigduke gravatar image

This question might be related with link text

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.123456789pi". In a for loop with this expression I obtain at the end something like "0.123456789pi + 0.987654321pi + ..." 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" )

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2014-08-03 10:07:05 -0500

Using numerical values (first case in your question)

a. My first suggestion is to work in RDF, and define pi = RDF.pi(). On the one hand, elements in RDF are close to machine floats, which will usually make your code faster than using RR. On the other hand, they have a wealth of methods you can apply to them, contrary to floats that you get by using pi.n().

b. Personally I would name the numerical value the same as the symbolic constant, eg pi = RDF.pi().
This way the same code can work in both numerical and symbolic setting. Do reset('pi') to revert to the default.

c. If you are always using 2*pi, why not define two_pi = 2 * RDF.pi() and use two_pi all along.

Finally, I don't know much about numpy, scipy, and sympy, but maybe they can help with your question.

edit flag offensive delete link more


Hi there, thanks for sharing ! Indeed Sage is more powerful for literal/symbolic expression. When you have any variable (like x=var('x')) you can do any symbolic operation. Then evaluate a numerical value by affecting x with any value ( x=1 ) is straightforward. Now the reset() function is great, you can go back to literal expression with reset('x'). As I remember in sympy, it's not possible to do this so easily. Good job Sage !

bigduke gravatar imagebigduke ( 2014-08-03 15:20:36 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-08-03 06:05:57 -0500

Seen: 223 times

Last updated: Aug 03 '14