def stable_cumsum(arr, axis=None, rtol=1e-05, atol=1e-08):
 if (np_version < (1, 9)):
  return np.cumsum(arr, axis=axis, dtype=np.float64)
 out = np.cumsum(arr, axis=axis, dtype=np.float64)
 expected = np.sum(arr, axis=axis, dtype=np.float64)
 if (not np.all(np.isclose(out.take((-1), axis=axis), expected, rtol=rtol, atol=atol, equal_nan=True))):
  warnings.warn('cumsum was found to be unstable: its last element does not correspond to sum', RuntimeWarning)
 return out

def stable_cumsum ( arr , axis = None , rtol = 1e-05 , atol = 1e-08 ) : if ( np_version < ( 1 , 9 ) ) : return np . cumsum ( arr , axis = axis , dtype = np . float64 ) out = np . cumsum ( arr , axis = axis , dtype = np . float64 ) expected = np . sum ( arr , axis = axis , dtype = np . float64 ) if ( not np . all ( np . isclose ( out . take ( ( - 1 ) , axis = axis ) , expected , rtol = rtol , atol = atol , equal_nan = True ) ) ) : warnings . warn ( 'cumsum was found to be unstable: its last element does not correspond to sum' , RuntimeWarning ) return out
use high precision for cumsum and check that final value matches sum parameters arr : array-like to be cumulatively summed as flat axis : int .