# pi and e not evaluated when i use my own classes

This question might be related to this question: http://ask.sagemath.org/question/966/...

I have a symbolic calculation and I do evaluate it numerically. Within one of my classes (within my notebook; no external files) I evaluate this term

-1/e^(1/10*abs(-0.0555555555556*pi - 26714619/51021164)) + 1


within a for loop.

I am getting the following error:

File "", line 1, in <module> File "/tmp/tmpkItECC/___code___.py", line 113, in <module> __temp=MuObject.calcValue((mu_enlacement[i])(**param_dict)) File "/tmp/tmpJuxeqk/___code___.py", line 97, in calcValue self._result[self._k,_sage_const_0 ]=R(self._value(theta_1=item[_sage_const_0 ],mu_t=item[_sage_const_1 ],mu_j=item[_sage_const_1 ])) File "parent.pyx", line 988, in sage.structure.parent.Parent.__call__ (sage/structure/parent.c:7326) File "coerce_maps.pyx", line 156, in sage.structure.coerce_maps.NamedConvertMap._call_ (sage/structure/coerce_maps.c:4235)
File "expression.pyx", line 862, in sage.symbolic.expression.Expression._mpfr_ (sage/symbolic/expression.cpp:5117)
File "expression.pyx", line 796, in sage.symbolic.expression.Expression._eval_self (sage/symbolic/expression.cpp:4912) TypeError: Cannot evaluate symbolic expression to a numeric value.

If I copy paste the expression

print
(-1/e^(1/10*abs(-0.0555555555556*pi - 26714619/51021164)) + 1).n()


to my notebook it evaluates fine to:

0.0674319647572598


I even tried to replace pi and e using a dictionary but I can't get it working.

Any ideas what i am doing wrong?

Markus P.S.: I can make the notebook public if usefull

edit retag close merge delete

The problem seems to be related to what is imported. Another version where the object calculating the values are created earlier works fine. I imported matplotlib as plt and numpy as np so no modules of these should be conflicting, right?

( 2011-12-22 09:36:16 -0500 )edit
1

I'm not sure what's happening. If you make the notebook public, or simply make a small self-contained example (always useful!) showing the problem, we can have a look.

( 2011-12-22 15:01:01 -0500 )edit
2

def expr(): return -1/exp(1/10*abs(-0.0555555555556*n(pi) - 26714619/51021164)) + 1 (I'm often using workarounds of this type. Does it work for you?)

( 2011-12-22 20:35:00 -0500 )edit

@achrzesz: if this is an answer, make it an answer and get karma (as well as make it easier to search for)!

( 2011-12-23 05:10:36 -0500 )edit

@kcrisman As DSM stated without a self-contained code snippet it is difficult to give a definite answer. My guess is that to obtain a numerical result, the symbolic arguments pi,e should be replaced by n(pi),n(e) or RR(pi),RR(e)

( 2011-12-23 21:03:47 -0500 )edit

Sort by » oldest newest most voted

I don't know why this is happening, but here are some workarounds: one option is to replace

self._value(theta_1=item[0],mu_t=item[1],mu_j=item[1])


by

self._value(theta_1=item[0],mu_t=item[1],mu_j=item[1], e=e, pi=pi)


A second option: right after the line

self._value=self._value(**self.th1_dict)(**self.pre10_dict)(**self.constants).trig_reduce().simplify() #set dictionary values


self._value = self._value(**self.constants)


By the way, you can also replace the "constants" dictionary with

    self.constants={
"pi": pi,
"e": e
}


to give better precision.

more

Thanks a million time for this indeed helpfull answer. BTW: that behaviour seems like a bug to me. After adding e=e... in the first of the if loops all the others also work fine. Looks like sage forgets about the constants. Should I try to file a bug here?

( 2012-01-01 05:28:14 -0500 )edit

File a bug if you can come up with a small example which displays the behavior. Your current worksheet is too complicated to figure out exactly where the bug lies.

( 2012-01-01 18:22:11 -0500 )edit

Hi,

I have the same problem using 'pi' in Sage shell or Sage scripts : computing "vector([cos(pi), sin(pi)])" will give a numerical answer but when using "random()2pi", we have a symbolic answer like "0.2456412311*pi" and so on.

So for now I will use another variable PI=n(pi) and this will give numerical values as I want. Not a real problem, I just have to add a short affectation but maybe there is a way to specify once for all we want numerical values instead of symbolic expression. In the case I have to use more constants like pi,e, etc...

edit: prefer methods to functions :

When using n(pi) and then a loop with increment n will overwrite n() function. So it's better to use pi.n() and not n(pi).

bigduke

more

2

1. Instead of n(pi), I recommend using RDF.pi(), which will make your code faster. 2. If you are not going to need the symbolic pi, you can call the numerical version pi instead of PI, just write pi = RDF.pi() at the beginning of your code. 3. If you are always using 2*pi, why not define two_pi = 2 * RDF.pi() and use two_pi all along.

( 2014-07-31 08:27:52 -0500 )edit
1

or tau = 2*RDF.pi() ;) See http://www.tauday.com/tau-manifesto

( 2014-07-31 09:13:14 -0500 )edit

Thanks for these advices, especially RDF.pi(). And yes, good habit to use constant instead of useless (repetitive) calculation like 2*pi in a loop ;) I do it even if I didn't specify. @niles Thanks for the link, interesting ! I agree with the main statement, rotation in a plane goes from 0 to 2pi. But Archimede found pi reasoning on area. And circular area is pi.R^2. Of course we could define it by tau.R^2/2 :) anyway, to me, a constant is a constant ! EDIT: @niles Again thank you for the link, "as a physicist" I like table 3. It makes sense to me :)

( 2014-07-31 13:02:34 -0500 )edit

By the way, if you want to make this a new question feel free! This question is far more straightforward than the original one...

( 2014-08-01 20:36:34 -0500 )edit

@kcrisman You mean like "numerical values VS symbolic values" ? I have my anwser now thanks to this thread but like you said, maybe more straightforward for other people.

( 2014-08-02 05:14:36 -0500 )edit