Using SymPy in a Python script

633 views
Skip to first unread message

HappyMac

unread,
Oct 15, 2010, 11:48:32 PM10/15/10
to sympy
Dear all,

I am new to SymPy and trying to learning how to use.
I have installed the latest SymPy (v. 0.6.7) and tried the following
Python script:

sympy_test.py

from sympy import Symbol
from math import *

x = Symbol('x')
i = 0
prior = 1 / sqrt(2*pi) * exp(-1*x**2 / 2)
p = 1 / (1 + exp(-1*discriminations[i]*(x - difficulties[1])))
q = 1 - p
posterior = prior * q
print posterior

But, when I ran this Python code, I got the following error message:
Traceback (most recent call last):
File "/Applications/eclipse/plugins/
org.python.pydev.debug_1.5.7.2010050621/pysrc/pydevd.py", line 978, in
<module>
debugger.run(setup['file'], None, None)
File "/Applications/eclipse/plugins/
org.python.pydev.debug_1.5.7.2010050621/pysrc/pydevd.py", line 780, in
run
execfile(file, globals, locals) #execute the script
File "/Users/yjlee/Documents/Worx/EclipseWorx/SymPyProjects/src/
sympy_test.py", line 44, in <module>
prior = 1 / sqrt(2*pi) * exp(-1*x**2 / 2)
File "/Library/Python/2.6/site-packages/sympy/core/basic.py", line
2103, in __float__
raise ValueError("Symbolic value, can't compute")
ValueError: Symbolic value, can't compute

All I was trying to do was to get a product of two functions, and I
could not figure out what I did wrong.

Could anyone tell me what I did wrong?

Thanks in advance.

Ondrej Certik

unread,
Oct 16, 2010, 12:33:03 AM10/16/10
to sy...@googlegroups.com
On Fri, Oct 15, 2010 at 8:48 PM, HappyMac <youngji...@gmail.com> wrote:
> Dear all,
>
> I am new to SymPy and trying to learning how to use.
> I have installed the latest SymPy (v. 0.6.7) and tried the following
> Python script:
>
> sympy_test.py
>
> from sympy import Symbol
> from math import *

^^^ Never ever use implicit imports (if possible). By writing this explicitly:

from math import sqrt, exp

it becomes clear, what is happening:

>
> x = Symbol('x')
> i = 0
> prior = 1 / sqrt(2*pi) * exp(-1*x**2 / 2)

You are using math.exp() here, so it needs a floating point number,
but sympy doesn't know how to convert "x" to a floating point number,
thus you get the exception below. The solution is to replace the line

from math import sqrt, exp

with

from sympy import sqrt, exp

> p = 1 / (1 + exp(-1*discriminations[i]*(x - difficulties[1])))
> q = 1 - p
> posterior = prior * q
> print posterior
>
> But, when I ran this Python code, I got the following error message:
> Traceback (most recent call last):
>  File "/Applications/eclipse/plugins/
> org.python.pydev.debug_1.5.7.2010050621/pysrc/pydevd.py", line 978, in
> <module>
>    debugger.run(setup['file'], None, None)
>  File "/Applications/eclipse/plugins/
> org.python.pydev.debug_1.5.7.2010050621/pysrc/pydevd.py", line 780, in
> run
>    execfile(file, globals, locals) #execute the script
>  File "/Users/yjlee/Documents/Worx/EclipseWorx/SymPyProjects/src/
> sympy_test.py", line 44, in <module>
>    prior = 1 / sqrt(2*pi) * exp(-1*x**2 / 2)
>  File "/Library/Python/2.6/site-packages/sympy/core/basic.py", line
> 2103, in __float__
>    raise ValueError("Symbolic value, can't compute")
> ValueError: Symbolic value, can't compute
>
> All I was trying to do was to get a product of two functions, and I
> could not figure out what I did wrong.
>
> Could anyone tell me what I did wrong?

Ondrej

HappyMac

unread,
Oct 16, 2010, 9:50:18 AM10/16/10
to sympy
Thank you so much for your help!!!

On Oct 15, 11:33 pm, Ondrej Certik <ond...@certik.cz> wrote:

Germán

unread,
Oct 6, 2011, 9:58:14 AM10/6/11
to Ondrej Certik, sy...@googlegroups.com
Thanks a lot, I had that silly error too.

On Oct 16 2010, 1:33 am, Ondrej Certik <ond...@certik.cz> wrote:

Reply all
Reply to author
Forward
0 new messages