The fractions
module can do that
>>> from fractions import Fraction
>>> Fraction(98, 42)
Fraction(7, 3)
There's a recipe over here for a numpy gcd. Which you could then use to divide your fraction
>>> def numpy_gcd(a, b):
... a, b = np.broadcast_arrays(a, b)
... a = a.copy()
... b = b.copy()
... pos = np.nonzero(b)[0]
... while len(pos) > 0:
... b2 = b[pos]
... a[pos], b[pos] = b2, a[pos] % b2
... pos = pos[b[pos]!=0]
... return a
...
>>> numpy_gcd(np.array([98]), np.array([42]))
array([14])
>>> 98/14, 42/14
(7, 3)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…