2013-07-08 8 views

risposta

18

Il modulo fractions può farlo

>>> from fractions import Fraction 
>>> Fraction(98, 42) 
Fraction(7, 3) 

C'è una ricetta sopra here per un gcd NumPy. Quale che potresti usare per dividere la tua frazione

>>> 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) 
+0

Grazie! Funziona alla grande! Il valore di input deve essere numeri interi, non può essere float. – LWZ

Problemi correlati