Giter Site home page Giter Site logo

gmpy_cffi's People

Contributors

sn6uv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

gmpy_cffi's Issues

Some comments

I'm still building PyPy so I haven't tried the code yet.

I looked at the code and I have a couple of comments based on my experiences with gmpy/gmpy2.

When deleting an object, you should check the size of the object before deciding to save it. When I first added caching to gmpy, I didn't make the size check and consumed all memory when someone began working with very large numbers.

When converting a PyLong to an mpz, I access the internal structure - fast but not portable. Have you looked at the int.to_bytes method available in Python 3.x? It might be faster than repeated divmod. If you don't want to be version dependent, have you tried a combination of & and right shift?

I'll try to provide some additional comments after PyPy is running.

I'm impressed by how clean the CFFI code looks.

Case

gmpy_cffi doesn't appear to have set_context

I have some code working on CPython with gmpy2, using mpfr: https://stromberg.dnsalias.org/svn/pybenoit/trunk

Sadly, when I try to run it on Pypy3 with gmpy_cffi, I get:

Traceback (most recent call last):
  File "./pybenoit", line 227, in <module>
    main()
  File "./pybenoit", line 185, in main
    mp.set_context(mp.context())
AttributeError: module 'gmpy_cffi' has no attribute 'set_context'

Is there a way to set a context with a caller-specified precision, using gmpy_cffi? I tried eliminating the set_context, but then mp.get_context().precision = 100 fails with:

Traceback (most recent call last):
  File "./pybenoit", line 227, in <module>
    main()
  File "./pybenoit", line 186, in main
    mp.get_context().precision = 100
AttributeError: module 'gmpy_cffi' has no attribute 'get_context'

I'm using CPython 3.9,2, Pypy3 7.3.9, and:

$ dpkg -l | egrep -i 'libgmp-dev|libmpfr-dev|libmpc-dev'
below cmd output started 2023 Sun Sep 24 10:08:36 AM PDT
ii  libgmp-dev:amd64                              2:6.2.1+dfsg-1+deb11u1
ii  libmpc-dev:amd64                              1.2.0-1
ii  libmpfr-dev:amd64                             4.1.0-3

Thanks! I'm kind of excited about the potential of gmpy_cffi.

Some missing functions from gmpy2

I've got some gmpy2 code that uses the popcount function, which isn't wrapped by gmpy_cffi. From a quick look, hamdist, bit_set, bit_clear, bit_flip, bit_test, bit_scan0, and bit_scan1 are also missing, both the free module functions and the mpz methods.

All I personally need is the popcount function, but I figured it was worth adding all the missing functions and methods. I've got an implementation on my fork, but it's not ready for commit yet, for a few reasons.

  • The copy I have lying around on an old system has mpz.bit_popcount and mpz.bit_hamdist methods, but both the 2.0 and 2.1 gmpy2 source only have free functions popcount and hamdist, so I need to figure out what's going on there.
  • I haven't written any tests for the new functions, because I can't get the tests running. (I get different errors with three different CPython and PyPy 3 builds on my Mac.)
  • I didn't exactly do a detailed survey of what was missing; I just took a quick look through gmpy2_mpz_bitops.h from the 2.1 branch. So there may be other functions/methods to add.
  • No docstrings. But then much of gmpy_cffi doesn't seem to have them. I guess the idea is that you develop with gmpy2 in CPython and then only use gmpy_cffi as a replacement if you happen to be running under PyPy?
  • I want to sleep on one of the implementation decisions: I put the basic functionality in the mpz methods, and then wrote the free functions as return mpz(n).bit_foo(m). In gmpy2, by contrast, there are parallel implementations for, e.g., GMPy_MPZ_bit_scan0_method and GMPy_MPZ_bit_scan0_function. I think that's just because it's a pain to abstract out the common bits with the C API, but I want to make sure there's not a more relevant reason.

When I get a chance, I'll fix all that and create a PR.

mpz/mpq should implement numbers.Integral/Rational

Right now, for example, numerator/denominator attributes are missing for mpz. Also, classes aren't properly registered.

In [6]: gmpy_cffi.mpz(2)
Out[6]: mpz(2)

In [7]: _6.numerator
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-e929b9596bbc> in <module>()
----> 1 _6.numerator

AttributeError: 'mpz' object has no attribute 'numerator'

In [8]: isinstance(_6, numbers.Integral)
Out[8]: False

In [9]: isinstance(gmpy_cffi.mpq(2), numbers.Rational)
Out[9]: False

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.