Velocity Reviews > 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

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/

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

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

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

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
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

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
>
>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

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

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'
>

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/

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
>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

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
> >
> >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

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'
> >

>
> 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

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