我尝试从 gmpy2.mpz 转换为 numpy 布尔数组,但不能完全正确。(gmpy2:https ://gmpy2.readthedocs.io )
import gmpy2
import numpy as np
x = gmpy2.mpz(int('1'*1000,2))
print("wrong conversion 1")
y = np.fromstring(gmpy2.to_binary(x), dtype=bool) # this is wrong
print(np.sum(y)) # this returns 127 instead of 1000
print("wrong conversion 2")
y = np.fromstring(gmpy2.to_binary(x), dtype=np.uint8)
print(y) # array([ 1, 1, 255 ... 255], dtype=uint8)
y_bool = np.unpackbits(y)
slow_popcount = np.sum(y_bool, dtype=int)
print(slow_popcount) # 1002. should be 1000
print("Fudging an answer. This is wrong as well.")
y = np.fromstring(gmpy2.to_binary(x)[2:], dtype=np.uint8)
# is that slicing [2:] a slow operation?
y_bool = np.unpackbits(y)
print np.sum(y_bool, dtype=int) # 1000
更多测试:
np.fromstring(gmpy2.to_binary(gmpy2.mpz(int('1'*64,2))), dtype=np.uint8)
# array([ 1, 1, 255, 255, 255, 255, 255, 255, 255, 255], dtype=uint8)
np.fromstring(gmpy2.to_binary(gmpy2.mpz(int('1'*65,2))), dtype=np.uint8)
# array([ 1, 1, 255, 255, 255, 255, 255, 255, 255, 255, 1], dtype=uint8
np.fromstring(gmpy2.to_binary(gmpy2.mpz(int('1'*66,2))), dtype=np.uint8)
# array([ 1, 1, 255, 255, 255, 255, 255, 255, 255, 255, 3], dtype=uint8)
np.fromstring(gmpy2.to_binary(gmpy2.mpz(int('1'*1024,2))), dtype=np.uint8)
# array([ 1, 1, 255 ... 255], dtype=uint8)
顺便说一句,我实际上想快速获取 gmpy2.mpz 的所有设置位的索引列表、数组或 numpy 数组。我尝试转换的实际 4,777,000 gmpy2.mpz 每个有 760,000 位,其中大约 2,000 位为 1。计算机上的 gmp 库是用 intel icc 编译的。
谢谢