Home Precision Matlab and Python (numpy)

# Precision Matlab and Python (numpy)

Nery Neto
1#
Nery Neto Published in 2017-12-07 01:19:32Z
 I'm converting a Matlab script to Python and I am getting different results in the 10**-4 order. In matlab: f_mean=f_mean+nanmean(f); f = f - nanmean(f); f_t = gradient(f); f_tt = gradient(f_t); if n_loop==1 theta = atan2( sum(f.*f_tt), sum(f.^2) ); end theta = -2.2011167e+03  In Python: f_mean = f_mean + np.nanmean(vel) vel = vel - np.nanmean(vel) firstDerivative = np.gradient(vel) secondDerivative = np.gradient(firstDerivative) if numberLoop == 1: theta = np.arctan2(np.sum(vel * secondDerivative), np.sum([vel**2]))  Although first and secondDerivative give the same results in Python and Matlab, f_mean is slightly different: -0.0066412 (Matlab) and -0.0066414 (Python); and so theta: -0.4126186 (M) and -0.4124718 (P). It is a small difference, but in the end leads to different results in my scripts. I know some people asked about this difference, but always regarding std, which I get, but not regarding mean values. I wonder why it is.
Paul Panzer
2#
Paul Panzer Reply to 2017-12-07 11:09:49Z
 One possible source of the initial difference you describe (between means) could be numpy's use of pairwise summation which on large arrays will typically be appreciably more accurate than the naive method: a = np.random.uniform(-1, 1, (10**6,)) a = np.r_[-a, a] # so the sum should be zero a.sum() # 7.815970093361102e-14 # use cumsum to get naive summation: a.cumsum()[-1] # -1.3716805469243809e-11  Edit (thanks @sascha): for the last word and as a "provably exact" reference you could use math.fsum: import math math.fsum(a) # 0.0  Don't have matlab, so can't check what they are doing.
 You need to login account before you can post.
Processed in 0.415079 second(s) , Gzip On .