Rational numbers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Manns

    #1

    Rational numbers

    Hi,

    I am starting to use rationals and since I found no batteries included,
    I tried out the mxNumber package.

    However, I get strange warnings on comparison operations
    (which however seem to yield correct results):

    ---
    $ python
    Python 2.4.3 (#1, Jan 15 2007, 15:46:19)
    [GCC 4.1.1 (Gentoo 4.1.1-r3)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.
    >>from mx.Number import *
    >>a=Rational(0, 1)
    >>a
    0/1
    >>str(a)
    '0.0'
    >>b=-500000000000000 000000000000000 000000000000000 0000000000000
    >>b
    -500000000000000 000000000000000 000000000000000 0000000000000L
    >>a==b
    __main__:1: RuntimeWarning: tp_compare didn't return -1, 0 or 1
    False
    >>>
    ---

    How do I get rid of these warnings?

    Is there any rational number library around that
    1) is comparably fast for large denominators
    2) allows deriving types from Rationals without wrapping?

    Regards

    Martin

    P.S. The respective mailing list does not like me, so that I try my
    luck here.
  • Larry Bates

    #2
    Re: Rational numbers

    Martin Manns wrote:
    Hi,
    >
    I am starting to use rationals and since I found no batteries included,
    I tried out the mxNumber package.
    >
    However, I get strange warnings on comparison operations
    (which however seem to yield correct results):
    >
    ---
    $ python
    Python 2.4.3 (#1, Jan 15 2007, 15:46:19)
    [GCC 4.1.1 (Gentoo 4.1.1-r3)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.
    >>>from mx.Number import *
    >>>a=Rational(0 ,1)
    >>>a
    0/1
    >>>str(a)
    '0.0'
    >>>b=-500000000000000 000000000000000 000000000000000 0000000000000
    >>>b
    -500000000000000 000000000000000 000000000000000 0000000000000L
    >>>a==b
    __main__:1: RuntimeWarning: tp_compare didn't return -1, 0 or 1
    False
    ---
    >
    How do I get rid of these warnings?
    >
    Is there any rational number library around that
    1) is comparably fast for large denominators
    2) allows deriving types from Rationals without wrapping?
    >
    Regards
    >
    Martin
    >
    P.S. The respective mailing list does not like me, so that I try my
    luck here.
    I quick search of Google turned up:

    Portable, powerful, and a breeze to use, Python is the popular open source object-oriented programming language used for both standalone programs and scripting applications. It is now being used by an increasing number of major organizations, including NASA and Google.Updated for Python 2.4, The Python Cookbook, 2nd Edition offers a wealth of useful code for all Python programmers, not just advanced practitioners. Like its predecessor, the new edition provides solutions to problems that Python programmers face everyday.It now includes over 200 recipes that range from simple tasks, such as working with dictionaries and list comprehensions, to complex tasks, such as monitoring a network and building a templating system. This revised version also includes new chapters on topics such as time, money, and metaprogramming.Here's a list of additional topics covered:Manipulating textSearching and sortingWorking with files and the filesystemObject-oriented programmingDealing with threads and processesSystem administrationInteracting with databasesCreating user interfacesNetwork and web programmingProcessing XMLDistributed programmingDebugging and testingAnother advantage of The Python Cookbook, 2nd Edition is its trio of authors--three well-known Python programming experts, who are highly visible on email lists and in newsgroups, and speak often at Python conferences.With scores of practical examples and pertinent background information, The Python Cookbook, 2nd Edition is the one source you need if you're looking to build efficient, flexible, scalable, and well-integrated systems.




    -Larry

    Comment

    • Martin Manns

      #3
      Re: Rational numbers

      On Fri, 23 Feb 2007 09:52:06 -0600
      Larry Bates <lbates@websafe .comwrote:
      I quick search of Google turned up:
      >
      Portable, powerful, and a breeze to use, Python is the popular open source object-oriented programming language used for both standalone programs and scripting applications. It is now being used by an increasing number of major organizations, including NASA and Google.Updated for Python 2.4, The Python Cookbook, 2nd Edition offers a wealth of useful code for all Python programmers, not just advanced practitioners. Like its predecessor, the new edition provides solutions to problems that Python programmers face everyday.It now includes over 200 recipes that range from simple tasks, such as working with dictionaries and list comprehensions, to complex tasks, such as monitoring a network and building a templating system. This revised version also includes new chapters on topics such as time, money, and metaprogramming.Here's a list of additional topics covered:Manipulating textSearching and sortingWorking with files and the filesystemObject-oriented programmingDealing with threads and processesSystem administrationInteracting with databasesCreating user interfacesNetwork and web programmingProcessing XMLDistributed programmingDebugging and testingAnother advantage of The Python Cookbook, 2nd Edition is its trio of authors--three well-known Python programming experts, who are highly visible on email lists and in newsgroups, and speak often at Python conferences.With scores of practical examples and pertinent background information, The Python Cookbook, 2nd Edition is the one source you need if you're looking to build efficient, flexible, scalable, and well-integrated systems.


      http://gmpy.sourceforge.net/
      Sorry that I did not point these out initially.

      + clnum seems to be slower and for speed may be compiled to wrap gmp so
      that it is just an additional layer between python and gmp .

      + gmpy is looking pretty unmaintained (dead) to me (newest update of
      cvs 10 months ago).

      + boost indeed is a quite nice C++ library. However, I fear that I
      would end up writing the python wrappers for operators (+ - * / min
      max cmp etc.) myself. I would like to avoid this since these operators
      should work correctly for any type (not just int and float) and I have
      little experience with verifying such generic code. The problems
      encountered in the mxNumber wrapper support this notion.

      Martin

      Comment

      • Martin Manns

        #4
        Re: Rational numbers / alternatives to mxNumber

        On Fri, 23 Feb 2007 11:39:11 -0500
        Martin Manns <mmanns@gmx.dew rote:
        + boost indeed is a quite nice C++ library. However, I fear that I
        would end up writing the python wrappers for operators (+ - * / min
        max cmp etc.) myself. I would like to avoid this since these operators
        should work correctly for any type (not just int and float) and I have
        little experience with verifying such generic code. The problems
        encountered in the mxNumber wrapper support this notion.

        I just saw the pyboost-linux-0.1.1 package and tried it out.

        ImportError: /home/mn/notes/libboost_python .so.1.34.0: undefined
        symbol: PyUnicodeUCS4_F romEncodedObjec t

        It seems to have problems with UCS2 compiled python.
        Any robust rational wrapper out there?

        Comment

        • bearophileHUGS@lycos.com

          #5
          Re: Rational numbers

          Martin Manns:
          + gmpy is looking pretty unmaintained (dead) to me (newest update of
          cvs 10 months ago).
          I have used it on Py2.5, so it seems to work anyway, and it's fast
          enough for my purposes. And probably soon some alex-shaped life will
          show up elsewhere.

          Bye,
          bearophile

          Comment

          • Marc 'BlackJack' Rintsch

            #6
            Re: Rational numbers

            In <20070223113911 .05dcc555@local host>, Martin Manns wrote:
            + gmpy is looking pretty unmaintained (dead) to me (newest update of
            cvs 10 months ago).
            What CSV activities do you expect? This package seems to be pretty
            stable. As long as there is no bug or incompatible changes in the
            underlying library I would not expect changes to that package.

            Ciao,
            Marc 'BlackJack' Rintsch

            Comment

            • mensanator@aol.com

              #7
              Re: Rational numbers

              On Feb 23, 10:39 am, Martin Manns <mma...@gmx.dew rote:
              On Fri, 23 Feb 2007 09:52:06 -0600
              >
              Larry Bates <lba...@websafe .comwrote:
              I quick search of Google turned up:
              >>
              Sorry that I did not point these out initially.
              >
              + clnum seems to be slower and for speed may be compiled to wrap gmp so
              that it is just an additional layer between python and gmp .
              >
              + gmpy is looking pretty unmaintained (dead) to me (newest update of
              cvs 10 months ago).
              Actually, gmpy is being maitained even if SourceForge isn't up to
              date.

              I got my gmpy 1.04a for Python 2.5 Windows binary from

              <http://home.comcast.ne t/~casevh>

              I haven't used the rationals all that much, but been very
              happy with them when I have.

              >
              + boost indeed is a quite nice C++ library. However, I fear that I
              would end up writing the python wrappers for operators (+ - * / min
              max cmp etc.) myself. I would like to avoid this since these operators
              should work correctly for any type (not just int and float) and I have
              little experience with verifying such generic code. The problems
              encountered in the mxNumber wrapper support this notion.
              >
              Martin

              Comment

              • casevh@comcast.net

                #8
                Re: Rational numbers

                On Feb 23, 10:34 am, "mensana...@aol .com" <mensana...@aol .comwrote:
                On Feb 23, 10:39 am, Martin Manns <mma...@gmx.dew rote:
                >
                On Fri, 23 Feb 2007 09:52:06 -0600
                >
                Larry Bates <lba...@websafe .comwrote:
                I quick search of Google turned up:
                >>
                Sorry that I did not point these out initially.
                >
                + clnum seems to be slower and for speed may be compiled to wrap gmp so
                that it is just an additional layer between python and gmp .
                >
                + gmpy is looking pretty unmaintained (dead) to me (newest update of
                cvs 10 months ago).
                I worked with Alex Martelli (gmpy's maintainer) to fix a bug found by
                mensanator. With Alex's permission, I released it as gmpy 1.04a. Alex
                has not updated cvs with the fix.

                gmpy 1.04a compiles cleanly with the latest releases of Python and
                GMP, so I consider it stable.
                >
                Actually, gmpy is being maitained even if SourceForge isn't up to
                date.
                >
                I got my gmpy 1.04a for Python 2.5 Windows binary from
                >
                <http://home.comcast.ne t/~casevh>
                >
                I haven't used the rationals all that much, but been very
                happy with them when I have.
                >
                casevh

                Comment

                • Martin Manns

                  #9
                  Re: Rational numbers

                  On 23 Feb 2007 12:00:10 -0800
                  casevh@comcast. net wrote:
                  >
                  I worked with Alex Martelli (gmpy's maintainer) to fix a bug found by
                  mensanator. With Alex's permission, I released it as gmpy 1.04a. Alex
                  has not updated cvs with the fix.
                  >
                  gmpy 1.04a compiles cleanly with the latest releases of Python and
                  GMP, so I consider it stable.
                  >

                  Actually, gmpy is being maitained even if SourceForge isn't up to
                  date.

                  I got my gmpy 1.04a for Python 2.5 Windows binary from

                  <http://home.comcast.ne t/~casevh>

                  I haven't used the rationals all that much, but been very
                  happy with them when I have.
                  >
                  casevh
                  >
                  Thank you for all the replies.

                  I think that I am going to switch to gmpy.

                  Martin

                  Comment

                  • mensanator@aol.com

                    #10
                    Re: Rational numbers

                    On Feb 23, 2:00 pm, cas...@comcast. net wrote:
                    On Feb 23, 10:34 am, "mensana...@aol .com" <mensana...@aol .comwrote:
                    >
                    >
                    >
                    >
                    >
                    On Feb 23, 10:39 am, Martin Manns <mma...@gmx.dew rote:
                    >
                    On Fri, 23 Feb 2007 09:52:06 -0600
                    >
                    Larry Bates <lba...@websafe .comwrote:
                    I quick search of Google turned up:
                    >>
                    Sorry that I did not point these out initially.
                    >
                    + clnum seems to be slower and for speed may be compiled to wrap gmp so
                    that it is just an additional layer between python and gmp .
                    >
                    + gmpy is looking pretty unmaintained (dead) to me (newest update of
                    cvs 10 months ago).
                    >
                    I worked with Alex Martelli (gmpy's maintainer) to fix a bug found by
                    mensanator. With Alex's permission, I released it as gmpy 1.04a. Alex
                    has not updated cvs with the fix.
                    >
                    gmpy 1.04a compiles cleanly with the latest releases of Python and
                    GMP, so I consider it stable.
                    Am I hallucinating? Didn't I see at least some version
                    of gmpy for Python 2.5 on SourceForge awhile back?
                    I distinctly remember thinking that I don't have to
                    direct people to your site, but SourceForge is not
                    showing anything beyond vesion 1.01 for Python 2.4.

                    >
                    Actually, gmpy is being maitained even if SourceForge isn't up to
                    date.
                    >
                    I got my gmpy 1.04a for Python 2.5 Windows binary from
                    >
                    <http://home.comcast.ne t/~casevh>
                    >
                    I haven't used the rationals all that much, but been very
                    happy with them when I have.
                    >
                    casevh

                    Comment

                    • casevh@comcast.net

                      #11
                      Re: Rational numbers

                      Am I hallucinating? Didn't I see at least some version
                      of gmpy for Python 2.5 on SourceForge awhile back?
                      I distinctly remember thinking that I don't have to
                      direct people to your site, but SourceForge is not
                      showing anything beyond vesion 1.01 for Python 2.4.
                      Alex released versions 1.02 and 1.03 as CVS updates only. I think he
                      may have made an announcement that 1.02 included alpha support for
                      Python 2.5. 1.04a is 1.03 with one additional fix. I don't think there
                      has been an official release, though.

                      casevh

                      Comment

                      • Gabriel Genellina

                        #12
                        Re: Rational numbers

                        En Fri, 23 Feb 2007 12:35:19 -0300, Martin Manns <mmanns@gmx.dee scribió:
                        I am starting to use rationals and since I found no batteries included,
                        I tried out the mxNumber package.
                        >
                        However, I get strange warnings on comparison operations
                        (which however seem to yield correct results):
                        mx.Number.Ratio nal is horribly broken. They break this rule:

                        a==b =hash(a)==hash( b)

                        so they can'b be used as dictionary keys, by example.
                        Try the other packages suggested. I've used clnum without problems.

                        --
                        Gabriel Genellina

                        Comment

                        • aleaxit@gmail.com

                          #13
                          Re: Rational numbers

                          On Feb 23, 12:00 pm, cas...@comcast. net wrote:
                          ...
                          + gmpy is looking pretty unmaintained (dead) to me (newest update of
                          cvs 10 months ago).
                          >
                          I worked withAlex Martelli(gmpy's maintainer) to fix a bug found by
                          mensanator. With Alex's permission, I released it as gmpy 1.04a. Alex
                          has not updated cvs with the fix.
                          Heh, I see why one might get that impression -- I'm in the process of
                          moving gmpy from sourceforge (where I find it harder and harder, and
                          ever more problematic, to work) to code.google.com 's new hosting
                          facility -- gmpy 1.02 prerelease (more updated than that "1.04a", and
                          particularly including your fix, Case) is already available at
                          http://code.google.com/p/gmpy/ but I have made no official
                          announcement yet (partly because what's available is yet limited:
                          sources, and binaries for Python 2.3, 2.4 and 2.5 but only for MacOSX
                          10.4 on Macs with intel processors)... building binaries for Windows
                          (not having a Windows machine or development system) or Universal
                          binaries for the Mac (due to problems building Universal versions of
                          the underlying GMP in its latest, 4.2 incarnation... I'm running out
                          of PPC-based Macs, and have none left with MaxOSX 10.3...) is much
                          more problematic for me.

                          To call this (Google Code) release 1.02, with a "1.04" (?) out from
                          another source, may be confusing, but I'd rather not "force" the
                          number upwards

                          I do have one new co-owner on the Google Code "version" of gmpy (Chip
                          Turner, once author of a similar GMP wrapper for perl, now a Python
                          convert and a colleague of mine) but I suspect that won't make the
                          building of Windows (and Universal Mac) binaries much easier. If
                          anybody who has easy access to Microsoft's MSVC++.NET (and is willing
                          to try building GMP 4.2 with/for it), or a PPC Mac with XCode
                          installed (possibly with MacOSX 10.3...), wants to volunteer to build
                          "the missing binaries" for the platforms that the current owners of
                          gmpy can't easily support, we could complete, test and release the
                          definitive 1.02, and move on with the development (I could get
                          enthusiastic about this again, if I could develop just the sources,
                          and the binaries for the one architecture I really use -- Macs w/intel
                          -- rather than strive each time with binaries for architectures that
                          are quite a pain for me...!-).

                          Anybody who's interested in helping out is welcome to mail me and/or
                          use the "wiki" and "issues" entry of the Google Code gmpy site...


                          Thanks,

                          Alex

                          Comment

                          • aleaxit@gmail.com

                            #14
                            Re: Rational numbers

                            On Feb 23, 2:52 pm, cas...@comcast. net wrote:
                            Am I hallucinating? Didn't I see at least some version
                            of gmpy for Python 2.5 on SourceForge awhile back?
                            I distinctly remember thinking that I don't have to
                            direct people to your site, but SourceForge is not
                            showing anything beyond vesion 1.01 for Python 2.4.
                            >
                            Alex released versions 1.02 and 1.03 as CVS updates only. I think he
                            may have made an announcement that 1.02 included alpha support for
                            Python 2.5. 1.04a is 1.03 with one additional fix. I don't think there
                            has been an official release, though.
                            Right: apparently sourceforce doesn't want me to do "releases" any
                            more (quite apart from the utter mess that doing a "release" on
                            sourceforce always was and still is), though I can still update the
                            cvs repository -- that's part of why I'm moving to Google Code
                            hosting.


                            Alex

                            Comment

                            • casevh@comcast.net

                              #15
                              Re: Rational numbers

                              On Feb 23, 3:27 pm, alea...@gmail.c om wrote:
                              On Feb 23, 12:00 pm, cas...@comcast. net wrote:
                              ...
                              >
                              + gmpy is looking pretty unmaintained (dead) to me (newest update of
                              cvs 10 months ago).
                              >
                              I worked withAlex Martelli(gmpy's maintainer) to fix a bug found by
                              mensanator. With Alex's permission, I released it as gmpy 1.04a. Alex
                              has not updated cvs with the fix.
                              >
                              Heh, I see why one might get that impression -- I'm in the process of
                              moving gmpy from sourceforge (where I find it harder and harder, and
                              ever more problematic, to work) to code.google.com 's new hosting
                              facility -- gmpy 1.02 prerelease (more updated than that "1.04a", and
                              particularly including your fix, Case) is already available athttp://code.google.com/p/gmpy/but I have made no official
                              announcement yet (partly because what's available is yet limited:
                              sources, and binaries for Python 2.3, 2.4 and 2.5 but only for MacOSX
                              10.4 on Macs with intel processors)... building binaries for Windows
                              (not having a Windows machine or development system) or Universal
                              binaries for the Mac (due to problems building Universal versions of
                              the underlying GMP in its latest, 4.2 incarnation... I'm running out
                              of PPC-based Macs, and have none left with MaxOSX 10.3...) is much
                              more problematic for me.
                              >
                              To call this (Google Code) release 1.02, with a "1.04" (?) out from
                              another source, may be confusing, but I'd rather not "force" the
                              number upwards
                              >
                              I do have one new co-owner on the Google Code "version" of gmpy (Chip
                              Turner, once author of a similar GMP wrapper for perl, now a Python
                              convert and a colleague of mine) but I suspect that won't make the
                              building of Windows (and Universal Mac) binaries much easier. If
                              anybody who has easy access to Microsoft's MSVC++.NET (and is willing
                              to try building GMP 4.2 with/for it), or a PPC Mac with XCode
                              installed (possibly with MacOSX 10.3...), wants to volunteer to build
                              "the missing binaries" for the platforms that the current owners of
                              gmpy can't easily support, we could complete, test and release the
                              definitive 1.02, and move on with the development (I could get
                              enthusiastic about this again, if I could develop just the sources,
                              and the binaries for the one architecture I really use -- Macs w/intel
                              -- rather than strive each time with binaries for architectures that
                              are quite a pain for me...!-).
                              >
                              Anybody who's interested in helping out is welcome to mail me and/or
                              use the "wiki" and "issues" entry of the Google Code gmpy site...
                              >
                              Thanks,
                              >
                              Alex
                              I can keep building gmpy for Windows. I actually use MINGW since
                              getting GMP compiled under MSVC is "challangin g". I should be able to
                              build new binaries for Windows this weekend. And I would be happy to
                              point everyone to a real release.

                              casevh

                              Comment

                              Working...