You can also convert the entire array to an object
array of Fraction
objects, by abusing the element-wise conversion of numpy arrays under arithmetic operations. (Note: this requires the original array to be an integer array, since arithmetic between float
s and Fractions
produce float
s.)
>>> A = np.array([[-1, 1],[-2, -1]])
array([[-1, 1],
[-2, -1]])
>>>
>>> A.dtype
dtype('int64')
>>>
>>> A = A + Fraction()
>>> A
array([[Fraction(-1, 1), Fraction(1, 1)],
[Fraction(-2, 1), Fraction(-1, 1)]], dtype=object)
With the array in this format, any further arithmetic performed will be over elements of type Fraction
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…