问题
I want to have a numpy array with mpz/mpfr values. Because my code:
import numpy as np
import gmpy2
A=np.ones((5,5));
print A/gmpy2.mpfr(1);
generates:
RuntimeWarning: invalid value encountered in divide
print A/gmpy2.mpfr(1);
[[1.0 1.0 1.0 1.0 1.0]
[1.0 1.0 1.0 1.0 1.0]
[1.0 1.0 1.0 1.0 1.0]
[1.0 1.0 1.0 1.0 1.0]
[1.0 1.0 1.0 1.0 1.0]]
Which as I can understand is the impossibility to convert gmpy mpfr to numpy float64. So how can I get a numpy array with mpfr values in the first place?
Thanks.
回答1:
You will need to create your array with dtype=object
, and then you can use any python type inside your array. I don't have gmpy2 installed, but the following example should show how it works:
In [3]: a = np.ones((5, 5), dtype=object)
In [5]: import fractions
In [6]: a *= fractions.Fraction(3, 4)
In [7]: a
Out[7]:
array([[3/4, 3/4, 3/4, 3/4, 3/4],
[3/4, 3/4, 3/4, 3/4, 3/4],
[3/4, 3/4, 3/4, 3/4, 3/4],
[3/4, 3/4, 3/4, 3/4, 3/4],
[3/4, 3/4, 3/4, 3/4, 3/4]], dtype=object)
Having a numpy array of dtype=object
can be a liitle misleading, because the powerful numpy machinery that makes operations with the standard dtypes super fast, is now taken care of by the default object's python operators, which means that the speed will not be there anymore:
In [12]: b = np.ones((5, 5)) * 0.75
In [13]: %timeit np.sum(a)
1000 loops, best of 3: 1.25 ms per loop
In [14]: %timeit np.sum(b)
10000 loops, best of 3: 23.9 us per loop
回答2:
I believe this is a bug in one of the two libraries. I also believe it is fixed.
Input:
import sys
import numpy as np
import gmpy2
print(sys.version)
print(np.__version__)
print(gmpy2.version)
A=np.ones((5,5));
print(A/gmpy2.mpfr(1))
Output:
3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:15:05) [MSC v.1600 32 bit (Intel)]
1.9.1
2.0.5
[[mpfr('1.0') mpfr('1.0') mpfr('1.0') mpfr('1.0') mpfr('1.0')]
[mpfr('1.0') mpfr('1.0') mpfr('1.0') mpfr('1.0') mpfr('1.0')]
[mpfr('1.0') mpfr('1.0') mpfr('1.0') mpfr('1.0') mpfr('1.0')]
[mpfr('1.0') mpfr('1.0') mpfr('1.0') mpfr('1.0') mpfr('1.0')]
[mpfr('1.0') mpfr('1.0') mpfr('1.0') mpfr('1.0') mpfr('1.0')]]
Either Numpy didn't properly say what to do when it encountered an unknown type, or gmpy2 didn't specify how to get divided by something (__rdiv__).
It is not necessary to specify the dtype
of an ndarray
unless you intend to write over its elements. Operations like multiplication will result in a new ndarray
, and Numpy will figure out what dtype
to use.
来源:https://stackoverflow.com/questions/15307589/numpy-array-with-mpz-mpfr-values