def compare(value, expected_value, epsilon, mod=False): """Compares value to expected value, and works if one or both are arrays. Also allows epsilon to be an array. If mod==True, then compare(0, tau, 0) is True. """ if mod: diff = normpi(value - expected_value) else: diff = value - expected_value if hasattr(value, '__len__') or hasattr(expected_value, '__len__'): if hasattr(epsilon, '__len__'): assert (abs(diff) <= epsilon).all() else: assert max(abs(diff)) <= epsilon else: assert abs(diff) <= epsilon
def eccentric_anomaly(e, M): """ Iterates to solve Kepler's equation to find eccentric anomaly Based on the algorithm in section 8.10.2 of the Explanatory Supplement to the Astronomical Almanac, 3rd ed. """ M = normpi(M) E = M + e * sin(M) max_iters = 100 iters = 0 while iters < max_iters: dM = M - (E - e * sin(E)) dE = dM / (1 - e * cos(E)) E = E + dE if abs(dE) < 1e-14: return E iters += 1 else: raise ValueError('Failed to converge')