sympy: nifty, but... (was: How about adding rational fraction to Python?)

Mensanator mensanator at aol.com
Sun Mar 2 16:19:12 EST 2008


On Mar 1, 12:29 pm, "Anand Patil" <anand.prabhakar.pa... at gmail.com>
wrote:
> Not sure if this is common knowledge yet but Sympy,http://code.google.com/p/sympy, has a rational type.

I hadn't heard of this before, thanks for the link.

Very nifty, lots of goodies not found in gmpy (although
it seems to lack a modular inverse function and the linear
congruence solver that can be derived from it making it
of no value to me).

Alas, it's written in Python. Who writes a math library
in Python?

Nevertheless, I thought I would try out the Rational numbers.

Once I figured out how to use them, I converted my Polynomial
Finder by Newton's Forward Difference Method program to use
sympy instead of gmpy.

I have a test case where I create 1 66 degree polynomial where
the coefficients are large rationals. The polynomial was
calculated flawlessly

<partial output>
## sympy
##          Term0: [66, -66, 66, -66, 66, -66, 66, -66, 66, -66, 66,
-66, 66, -66, 66, -66, 66, -66, 66, -66, 66, -66, 66, -66, 66, -66,
66, -66, 66, -66, 66, -66, 66, -66, 66, -66, 66, -66, 66, -66, 66,
-66, 66, -66, 66, -66, 66, -66, 66, -66, 66, -66, 66, -66, 66, -66,
66, -66, 66, -66, 66, -66, 66, -66, 66, -66, 66, 0]
##
##            Seq: [66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 66]
##
##    The Polynomial:
##
##
##        1
##
-------------------------------------------------------------------------------------------
*  n**66
##
8247650592082470666723170306785496252186258551345437492922123134388955774976000000000000000
##
##
##        -67
##
------------------------------------------------------------------------------------------
*  n**65
##
249928805820680929294641524448045340975341168222589014937034034375422902272000000000000000
##
##
##        67
##
---------------------------------------------------------------------------------------
*  n**64
##
230703513065243934733515253336657237823391847590082167634185262500390371328000000000000

But because they are calculated using Python,
it took 175 seconds compared to 0.2 seconds
for gmpy to do the same polynomial.

So, I'll keep it around for it's neat features
that gmpy doesn't have, but it won't replace gmpy
for any serious work.


>
> In [2]: from sympy import *
>
> In [3]: Rational(21,4)
> Out[3]: 21/4
>
> In [4]: Rational(21,4)+Rational(3,4)
> Out[4]: 6




More information about the Python-list mailing list