GMP(GNU Multiple Precision Arithmetic Library,即GNU高精度算術運算庫),它是一個開源的高精度運算庫,其中不但有普通的整數、實數、浮點數的高精度運算,還有隨機數生成,尤其是提供了非常完備的數論中的運算接口,比如Miller-Rabin素數測試算法、大素數生成、歐幾里德算法、求域中元素的逆、Jacobi符號、legendre符號等。
gmpy2是Python的一個擴展庫,是對GMP的封裝,它的前身是gmpy,經過其作者的調整和封裝,使得gmpy2的使用大大簡化。
官方文檔:https://gmpy2.readthedocs.io/en/latest/
-------
1、windows上安裝gmpy2
在windows上直接安裝wheel文件就方便多了。
https://pypi.org/project/gmpy2/#files
這里面有python2.6、2.7、3.2、3.3、3.4版本的wheel文件,下載后用pip安裝即可。(暫時還找不到3.5版本)
后來也發現有一個gmpy2-2.0.8.win-amd64-py2.7.exe文件。
-= 2018.01補充 =-
現在gmpy2 2.1.0a1里可以支持3.4+
2、linux上安裝gmpy2
gmpy2是依賴GMP、MPFR、MPC三個庫,故此在linux上安裝前得先安裝這3個庫。
為了后續安裝的方便,先建立2個文件夾。
mkdir -p $HOME/src mkdir -p $HOME/static
-= 2018.04補充 =-
測試有沒有安裝m4模塊:
man m4
如果出現No manual entry for m4,就說明沒有安裝m4模塊。
如果沒安裝m4模塊,在編譯GMP時候會報錯checking for suitable m4... configure: error: No usable m4 in $PATH or /usr/5bin (see config.log for reasons).
安裝m4:
GNU M4 is an implementation of the traditional Unix macro processor. It is mostly SVR4 compatible although it has some extensions (for example, handling more than 9 positional parameters to macros). GNU M4 also has built-in functions for including files, running shell commands, doing arithmetic, etc.
當前最新的是1.4.18
v=1.4.18 cd $HOME/src wget http://ftp.gnu.org/gnu/m4/m4-${v}.tar.gz tar xf m4-${v}.tar.gz && cd m4-${v} ./configure -prefix=/usr/local make && make check && make install
3、安裝GMP
GMP(The GNU Multiple Precision Arithmetic Library) is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating-point numbers.
https://gmplib.org/
當前最新的是6.1.2
v=6.1.2 cd $HOME/src wget https://gmplib.org/download/gmp/gmp-${v}.tar.bz2 tar -jxvf gmp-${v}.tar.bz2 && cd gmp-${v} ./configure --prefix=$HOME/static --enable-static --disable-shared --with-pic make && make check && make install
4、安裝MPFR
The MPFR library is a C library for multiple-precision floating-point computations with correct rounding.
http://www.mpfr.org/mpfr-current/#download
當前最新的是4.0.1 (請自己訪問官網,替換成最新的版本號)
v=4.0.1 cd $HOME/src wget http://www.mpfr.org/mpfr-current/mpfr-${v}.tar.bz2 tar -jxvf mpfr-${v}.tar.bz2 && cd mpfr-${v} ./configure --prefix=$HOME/static --enable-static --disable-shared --with-pic --with-gmp=$HOME/static make && make check && make install
-= 2018.04補充 =-
如果mpfr.org下載太慢,可以換為
wget http://ftp.gnu.org/gnu/mpfr/mpfr-${v}.tar.bz2
5、安裝MPC
GNU MPC is a C library for the arithmetic of complex numbers with arbitrarily high precision and correct rounding of the result.
http://www.multiprecision.org/mpc/download.html (這里最新是1.0.3)
但當mpfr版本為4.x以上會報錯Makefile:532: recipe for target 'mul.lo' failed
在ftp://ftp.gnu.org/gnu/mpc/ 可以找到更新的1.1.0版本
v=1.1.0 cd $HOME/src wget ftp://ftp.gnu.org/gnu/mpc/mpc-${v}.tar.gz tar -zxvf mpc-${v}.tar.gz && cd mpc-${v} ./configure --prefix=$HOME/static --enable-static --disable-shared --with-pic --with-gmp=$HOME/static --with-mpfr=$HOME/static make && make check && make install
6、安裝gmpy2
github項目:https://github.com/aleaxit/gmpy
現在新的版本(2-2.1.0b1以上版本)在執行python setup.py build_ext --static=$HOME/static install
會報錯error: option --static must not have an argument
解決法子1:
找releases版本(2-2.1.0a1以下版本)來安裝
v=2-2.1.0a1 cd $HOME/src wget https://github.com/aleaxit/gmpy/releases/download/gmpy${v}/gmpy${v}.tar.gz tar xf gmpy${v}.tar.gz && cd gmpy${v} python setup.py build_ext --static=$HOME/static install
解決法子2:
因為新版本的setup.py修改了不少,故得采用以下法子:
python setup.py build_ext --static-dir=$HOME/static install
安裝后,命令行進入python模式后,輸入import gmpy2沒報錯就成功了。
-= 2018.04補充 =-
如果使用wget下載時候一直卡在:
Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.216.227.24|:443...
可以嘗試:
echo 52.216.16.16 github-production-release-asset-2e65be.s3.amazonaws.com >>/etc/hosts /etc/init.d/networking restart
-= 2018.05補充 =-
如果報錯fatal error: Python.h: 沒有那個文件或目錄
apt-get install python-dev
7、gmpy2使用方法
這里pcat就介紹找一百以內的素數,更多gmpy2的用法自己搜索吧,或者以后補充(←_←)。
import gmpy2 for i in xrange(100+1): if gmpy2.is_prime(i): print i