sn6uv / gmpy_cffi Goto Github PK
View Code? Open in Web Editor NEWLicense: Other
License: Other
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
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.
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.
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.gmpy2_mpz_bitops.h
from the 2.1 branch. So there may be other functions/methods to add.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?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.
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.