Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Help with sympy, please

Reply
Thread Tools

Help with sympy, please

 
 
Dick Moores
Guest
Posts: n/a
 
      11-18-2007
from __future__ import division
Here's what I'm trying to do, but using sympy:
=============================
from math import e
n = 1
prod = 1
k = 0
while k < 1000:
k += 1
term = (e**(1.0/n))/(e**(1.0/(n+1)))
prod *= term
n += 2
print prod, term
================================
Output:
1.99950018746 1.00000025013 (the limit is 2)


============================
from sympy import *
from sympy import Rational as R
from sympy.numerics import *

prec = 50
Float.setdps(prec)
e = evalf(E)
n = 1
k = 0
prod = evalf(R(1,1))
while k < 1000:
k += 1
n = evalf(R(n,1))
term = (e**(1/n))/(e**(1/(n+1)))
prod *= term
n += 2
print prod, term
===========================

This gets:
Traceback (most recent call last):
File "E:\PythonWork\Untitled 5.py", line 20, in <module>
term = (e**(1/n))/(e**(1/(n+1)))
TypeError: unsupported operand type(s) for /: 'int' and 'Float'

Thanks,

Dick Moores

 
Reply With Quote
 
 
 
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      11-18-2007
On Sun, 18 Nov 2007 13:02:01 -0800, Dick Moores <(E-Mail Removed)>
declaimed the following in comp.lang.python:

>
> This gets:
> Traceback (most recent call last):
> File "E:\PythonWork\Untitled 5.py", line 20, in <module>
> term = (e**(1/n))/(e**(1/(n+1)))
> TypeError: unsupported operand type(s) for /: 'int' and 'Float'
>

Seems self-explanatory... try using 1.0 rather than 1
--
Wulfraed Dennis Lee Bieber KD6MOG
http://www.velocityreviews.com/forums/(E-Mail Removed) (E-Mail Removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (E-Mail Removed))
HTTP://www.bestiaria.com/
 
Reply With Quote
 
 
 
 
Dick Moores
Guest
Posts: n/a
 
      11-19-2007
At 03:42 PM 11/18/2007, Dennis Lee Bieber wrote:
>On Sun, 18 Nov 2007 13:02:01 -0800, Dick Moores <(E-Mail Removed)>
>declaimed the following in comp.lang.python:
>
> >
> > This gets:
> > Traceback (most recent call last):
> > File "E:\PythonWork\Untitled 5.py", line 20, in <module>
> > term = (e**(1/n))/(e**(1/(n+1)))
> > TypeError: unsupported operand type(s) for /: 'int' and 'Float'
> >

> Seems self-explanatory... try using 1.0 rather than 1


term = (e**(1.0/n))/(e**(1.0/(n+1)))
TypeError: unsupported operand type(s) for /: 'float' and 'Float'

Dick

 
Reply With Quote
 
Fredrik Johansson
Guest
Posts: n/a
 
      11-19-2007
On Nov 19, 2007 1:05 AM, Dick Moores <(E-Mail Removed)> wrote:
> At 03:42 PM 11/18/2007, Dennis Lee Bieber wrote:
> >On Sun, 18 Nov 2007 13:02:01 -0800, Dick Moores <(E-Mail Removed)>
> >declaimed the following in comp.lang.python:
> >
> > >
> > > This gets:
> > > Traceback (most recent call last):
> > > File "E:\PythonWork\Untitled 5.py", line 20, in <module>
> > > term = (e**(1/n))/(e**(1/(n+1)))
> > > TypeError: unsupported operand type(s) for /: 'int' and 'Float'
> > >

> > Seems self-explanatory... try using 1.0 rather than 1

>
> term = (e**(1.0/n))/(e**(1.0/(n+1)))
> TypeError: unsupported operand type(s) for /: 'float' and 'Float'


Hi Dick, I recognize you from python-list, where you had a question
about mpmath.

Your code still won't work if you convert the numbers to Floats
because the Float type in sympy.numerics does not implement ** for
fractional numbers. You could use the exp function in
sympy.numerics.functions instead to compute e**x.

Basically, sympy.numerics is an old version of mpmath. The
sympy.numerics module is not very well integrated in SymPy, slower
than mpmath, and has a couple bugs that have subsequently been fixed
in mpmath. In sympycore (http://code.google.com/p/sympycore/), we're
using the latest version of mpmath and integrating it directly into
the symbolic engine; it will be much more robust and user-friendly. It
will hopefully not be long until we merge the improvements we've done
in the sympycore project with the main SymPy branch.

Fredrik
 
Reply With Quote
 
Dick Moores
Guest
Posts: n/a
 
      11-19-2007
At 04:26 PM 11/18/2007, Fredrik Johansson wrote:
>On Nov 19, 2007 1:05 AM, Dick Moores <(E-Mail Removed)> wrote:
>Hi Dick, I recognize you from python-list, where you had a question
>about mpmath.
>
>Your code still won't work if you convert the numbers to Floats
>because the Float type in sympy.numerics does not implement ** for
>fractional numbers. You could use the exp function in
>sympy.numerics.functions instead to compute e**x.


Thanks, Fredrik, but I get the same error using either exp or power:
============================
from __future__ import division
from sympy import *
from sympy import Rational as R
from sympy.numerics import *
from sympy.numerics.functions import power, exp

prec = 50
Float.setdps(prec)
e = evalf(E)
n = 1
m = n + 1
k = 0
n = evalf(R(n,1))
m = evalf(R(m,1))
prod = evalf(R(1,1))
prec = 50
Float.setdps(prec)
e = evalf(E)
n = 1
k = 0
prod = evalf(R(1,1))
while k < 1000:
k += 1
n = evalf(R(n,1))
term = (exp(1/n))/(exp(1/(n+1)))
prod *= term
n += 2
print prod, term
=============================
term = (exp(1/n))/(exp(1/(n+1)))
TypeError: unsupported operand type(s) for /: 'int' and 'Float'

And also if I use sympy.numerics.functions.power in that line, as
term = power(e,(1/n))/power(e,(1/(n+1)))

I get:

term = power(e,(1/n))/power(e,(1/(n+1)))
TypeError: unsupported operand type(s) for /: 'int' and 'Float'

Dick


 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      11-19-2007
On Sun, 18 Nov 2007 16:05:15 -0800, Dick Moores <(E-Mail Removed)>
declaimed the following in comp.lang.python:

> TypeError: unsupported operand type(s) for /: 'float' and 'Float'
>

<blink><blink>

What restrictive system can't even coerce a Python float to their
own internal data type?

Does that library have any functions for type conversion? Float(1)
(or Float(1.0) ) perhaps?
--
Wulfraed Dennis Lee Bieber KD6MOG
(E-Mail Removed) (E-Mail Removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (E-Mail Removed))
HTTP://www.bestiaria.com/
 
Reply With Quote
 
Dick Moores
Guest
Posts: n/a
 
      11-19-2007
At 04:26 PM 11/18/2007, Fredrik Johansson wrote:

>Basically, sympy.numerics is an old version of mpmath. The
>sympy.numerics module is not very well integrated in SymPy, slower
>than mpmath, and has a couple bugs that have subsequently been fixed
>in mpmath. In sympycore (http://code.google.com/p/sympycore/), we're
>using the latest version of mpmath and integrating it directly into
>the symbolic engine; it will be much more robust and user-friendly. It
>will hopefully not be long until we merge the improvements we've done
>in the sympycore project with the main SymPy branch.
>
>Fredrik


OK, I tried mpmath again, and to my surprise, it went well!

===================================
#!/usr/bin/env python
#coding=utf-8
from mpmath import *
mpf.dps = 50
n = 1
k = 0
prod = mpf(1)
while k < 100000:
k += 1
term = exp(1.0/n)/exp(1.0/(n+1))
prod *= term
n += 2
print prod, term
======================================
Output:
1.999995000018749963501602808084473518238915868379 7
1.000000000025000125000407479013388938680661062617 2

Thanks,

Dick


 
Reply With Quote
 
Fredrik Johansson
Guest
Posts: n/a
 
      11-19-2007
On Nov 19, 2007 2:03 AM, Dick Moores <(E-Mail Removed)> wrote:
> At 04:26 PM 11/18/2007, Fredrik Johansson wrote:
> >On Nov 19, 2007 1:05 AM, Dick Moores <(E-Mail Removed)> wrote:
> >Hi Dick, I recognize you from python-list, where you had a question
> >about mpmath.
> >
> >Your code still won't work if you convert the numbers to Floats
> >because the Float type in sympy.numerics does not implement ** for
> >fractional numbers. You could use the exp function in
> >sympy.numerics.functions instead to compute e**x.

>
> Thanks, Fredrik, but I get the same error using either exp or power:
> ============================
> from __future__ import division
> from sympy import *
> from sympy import Rational as R
> from sympy.numerics import *
> from sympy.numerics.functions import power, exp
>
> prec = 50
> Float.setdps(prec)
> e = evalf(E)
> n = 1
> m = n + 1
> k = 0
> n = evalf(R(n,1))
> m = evalf(R(m,1))
> prod = evalf(R(1,1))
> prec = 50
> Float.setdps(prec)
> e = evalf(E)
> n = 1
> k = 0
> prod = evalf(R(1,1))
> while k < 1000:
> k += 1
> n = evalf(R(n,1))
> term = (exp(1/n))/(exp(1/(n+1)))
> prod *= term
> n += 2
> print prod, term
> =============================
> term = (exp(1/n))/(exp(1/(n+1)))
> TypeError: unsupported operand type(s) for /: 'int' and 'Float'
>
> And also if I use sympy.numerics.functions.power in that line, as
> term = power(e,(1/n))/power(e,(1/(n+1)))
>
> I get:
>
> term = power(e,(1/n))/power(e,(1/(n+1)))
> TypeError: unsupported operand type(s) for /: 'int' and 'Float'


Try not using "from __future__ import division".

Also,

n = evalf(R(n,1))

won't work, because the arguments to Rational must be ints.

Fredrik
 
Reply With Quote
 
Fredrik Johansson
Guest
Posts: n/a
 
      11-19-2007
On Nov 19, 2007 2:09 AM, Dennis Lee Bieber <(E-Mail Removed)> wrote:
> On Sun, 18 Nov 2007 16:05:15 -0800, Dick Moores <(E-Mail Removed)>
> declaimed the following in comp.lang.python:
>
> > TypeError: unsupported operand type(s) for /: 'float' and 'Float'
> >

> <blink><blink>
>
> What restrictive system can't even coerce a Python float to their
> own internal data type?
>
> Does that library have any functions for type conversion? Float(1)
> (or Float(1.0) ) perhaps?


Coercion works fine; the problem is that Float implements __div__ but
not __truediv__. It works if you don't import division from
__future__. If you do that, even Float / Float division ceases to
work. That is definitely a bug, but not as bad a bug as not being able
to coerce floats at all

Fredrik
 
Reply With Quote
 
Fredrik Johansson
Guest
Posts: n/a
 
      11-19-2007
On Nov 19, 2007 2:23 AM, Dick Moores <(E-Mail Removed)> wrote:
> OK, I tried mpmath again, and to my surprise, it went well!
>
> ===================================
> #!/usr/bin/env python
> #coding=utf-8
> from mpmath import *
> mpf.dps = 50
> n = 1
> k = 0
> prod = mpf(1)
> while k < 100000:
> k += 1
> term = exp(1.0/n)/exp(1.0/(n+1))
> prod *= term
> n += 2
> print prod, term
> ======================================
> Output:
> 1.999995000018749963501602808084473518238915868379 7
> 1.000000000025000125000407479013388938680661062617 2


You're getting slightly wrong results, though, because 1.0/n and
1.0/(n+1) just performs regular float division with ~16-digit
precision when n is a Python int. You should convert either 1.0 or n
to an mpf before dividing.

Fredrik
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please please please help this guy with his open source java app casioculture@gmail.com Java 4 05-05-2005 08:24 AM
Console profile for Windows app in VC++ - PLEASE PLEASE PLEASE HELP! MuZZy C++ 7 01-07-2005 08:40 PM
Computer problems please please please help Nick Computer Support 0 06-04-2004 08:49 PM
HELP! HELP! PLEASE, PLEASE, PLEASE tpg comcntr Computer Support 11 02-15-2004 06:22 PM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments