def test_beta(): np.random.seed(1234) b = np.r_[np.logspace(-200, 200, 4), np.logspace(-10, 10, 4), np.logspace(-1, 1, 4), -1, -2.3, -3, -100.3, -10003.4] a = b ab = np.array(np.broadcast_arrays(a[:, None], b[None, :])).reshape(2, -1).T old_dps, old_prec = mpmath.mp.dps, mpmath.mp.prec try: mpmath.mp.dps = 400 assert_func_equal(sc.beta, lambda a, b: float(mpmath.beta(a, b)), ab, vectorized=False, rtol=1e-10) assert_func_equal( sc.betaln, lambda a, b: float(mpmath.log(abs(mpmath.beta(a, b)))), ab, vectorized=False, rtol=1e-10) finally: mpmath.mp.dps, mpmath.mp.prec = old_dps, old_prec
def test_beta(): np.random.seed(1234) b = np.r_[np.logspace(-200, 200, 4), np.logspace(-10, 10, 4), np.logspace(-1, 1, 4), -1, -2.3, -3, -100.3, -10003.4] a = b ab = np.array(np.broadcast_arrays(a[:,None], b[None,:])).reshape(2, -1).T old_dps, old_prec = mpmath.mp.dps, mpmath.mp.prec try: mpmath.mp.dps = 400 assert_func_equal(sc.beta, lambda a, b: float(mpmath.beta(a, b)), ab, vectorized=False, rtol=1e-10) assert_func_equal( sc.betaln, lambda a, b: float(mpmath.log(abs(mpmath.beta(a, b)))), ab, vectorized=False, rtol=1e-10) finally: mpmath.mp.dps, mpmath.mp.prec = old_dps, old_prec
def check(self): np.random.seed(1234) num_args = len(self.arg_spec) # Generate values for the arguments ms = np.asarray([1.5 if isinstance(arg, ComplexArg) else 1.0 for arg in self.arg_spec]) ms = (self.n**(ms/sum(ms))).astype(int) + 1 argvals = [] for arg, m in zip(self.arg_spec, ms): argvals.append(arg.values(m)) argarr = np.array(np.broadcast_arrays(*np.ix_(*argvals))).reshape(num_args, -1).T # Check old_dps, old_prec = mpmath.mp.dps, mpmath.mp.prec try: if self.dps is not None: dps_list = [self.dps] else: dps_list = [20] if self.prec is not None: mpmath.mp.prec = self.prec # Proper casting of mpmath input and output types. Using # native mpmath types as inputs gives improved precision # in some cases. if np.issubdtype(argarr.dtype, np.complexfloating): pytype = complex mptype = lambda x: mpmath.mpc(complex(x)) else: mptype = lambda x: mpmath.mpf(float(x)) def pytype(x): if abs(x.imag) > 1e-16*(1 + abs(x.real)): return np.nan else: return float(x.real) # Try out different dps until one (or none) works for j, dps in enumerate(dps_list): mpmath.mp.dps = dps try: assert_func_equal(self.scipy_func, lambda *a: pytype(self.mpmath_func(*map(mptype, a))), argarr, vectorized=False, rtol=self.rtol, atol=self.atol, ignore_inf_sign=self.ignore_inf_sign, nan_ok=True) break except AssertionError: if j >= len(dps_list)-1: reraise(*sys.exc_info()) finally: mpmath.mp.dps, mpmath.mp.prec = old_dps, old_prec
def test_in_interval(self, interval, expected_rtol, uniform_random_points): left, right = interval points = (right - left) * uniform_random_points + left assert_func_equal(log_ndtr_ndtri_exp, lambda y: y, points, rtol=expected_rtol, nan_ok=True)
def test_very_small_arg(self, test_input, uniform_random_points): scale = test_input points = scale * (0.5 * uniform_random_points + 0.5) assert_func_equal(log_ndtr_ndtri_exp, lambda y: y, points, rtol=1e-14, nan_ok=True)
def test_extreme(self): assert_func_equal( log_ndtr_ndtri_exp, lambda y: y, [-np.finfo(float).max, -np.finfo(float).min], rtol=1e-12, nan_ok=True )
def check(self): np.random.seed(1234) # Generate values for the arguments num_args = len(self.arg_spec) m = int(self.n**(1. / num_args)) + 1 argvals = [] for arg in self.arg_spec: argvals.append(arg.values(m)) argarr = np.array(np.broadcast_arrays(*np.ix_(*argvals))).reshape( num_args, -1).T # Check old_dps, old_prec = mpmath.mp.dps, mpmath.mp.prec try: if self.dps is not None: dps_list = [self.dps] else: dps_list = [20] if self.prec is not None: mpmath.mp.prec = self.prec # Proper casting of mpmath input and output types. Using # native mpmath types as inputs gives improved precision # in some cases. if np.issubdtype(argarr.dtype, np.complexfloating): pytype = complex mptype = lambda x: mpmath.mpc(complex(x)) else: mptype = lambda x: mpmath.mpf(float(x)) def pytype(x): if abs(x.imag) > 1e-16 * (1 + abs(x.real)): return np.nan else: return float(x.real) # Try out different dps until one (or none) works for j, dps in enumerate(dps_list): mpmath.mp.dps = dps try: assert_func_equal( self.scipy_func, lambda *a: pytype(self.mpmath_func(*map(mptype, a))), argarr, vectorized=False, rtol=self.rtol, atol=self.atol, nan_ok=True) break except AssertionError: if j >= len(dps_list) - 1: reraise(*sys.exc_info()) finally: mpmath.mp.dps, mpmath.mp.prec = old_dps, old_prec
def check(self): np.random.seed(1234) # Generate values for the arguments argarr = get_args(self.arg_spec, self.n) # Check old_dps, old_prec = mpmath.mp.dps, mpmath.mp.prec try: if self.dps is not None: dps_list = [self.dps] else: dps_list = [20] if self.prec is not None: mpmath.mp.prec = self.prec # Proper casting of mpmath input and output types. Using # native mpmath types as inputs gives improved precision # in some cases. if np.issubdtype(argarr.dtype, np.complexfloating): pytype = mpc2complex def mptype(x): return mpmath.mpc(complex(x)) else: def mptype(x): return mpmath.mpf(float(x)) def pytype(x): if abs(x.imag) > 1e-16*(1 + abs(x.real)): return np.nan else: return mpf2float(x.real) # Try out different dps until one (or none) works for j, dps in enumerate(dps_list): mpmath.mp.dps = dps try: assert_func_equal(self.scipy_func, lambda *a: pytype(self.mpmath_func(*map(mptype, a))), argarr, vectorized=False, rtol=self.rtol, atol=self.atol, ignore_inf_sign=self.ignore_inf_sign, distinguish_nan_and_inf=self.distinguish_nan_and_inf, nan_ok=self.nan_ok, param_filter=self.param_filter) break except AssertionError: if j >= len(dps_list)-1: # reraise the Exception tp, value, tb = sys.exc_info() if value.__traceback__ is not tb: raise value.with_traceback(tb) raise value finally: mpmath.mp.dps, mpmath.mp.prec = old_dps, old_prec
def check(self): np.random.seed(1234) # Generate values for the arguments argarr = get_args(self.arg_spec, self.n) # Check old_dps, old_prec = mpmath.mp.dps, mpmath.mp.prec try: if self.dps is not None: dps_list = [self.dps] else: dps_list = [20] if self.prec is not None: mpmath.mp.prec = self.prec # Proper casting of mpmath input and output types. Using # native mpmath types as inputs gives improved precision # in some cases. if np.issubdtype(argarr.dtype, np.complexfloating): pytype = mpc2complex def mptype(x): return mpmath.mpc(complex(x)) else: def mptype(x): return mpmath.mpf(float(x)) def pytype(x): if abs(x.imag) > 1e-16*(1 + abs(x.real)): return np.nan else: return mpf2float(x.real) # Try out different dps until one (or none) works for j, dps in enumerate(dps_list): mpmath.mp.dps = dps try: assert_func_equal(self.scipy_func, lambda *a: pytype(self.mpmath_func(*map(mptype, a))), argarr, vectorized=False, rtol=self.rtol, atol=self.atol, ignore_inf_sign=self.ignore_inf_sign, distinguish_nan_and_inf=self.distinguish_nan_and_inf, nan_ok=self.nan_ok, param_filter=self.param_filter) break except AssertionError: if j >= len(dps_list)-1: reraise(*sys.exc_info()) finally: mpmath.mp.dps, mpmath.mp.prec = old_dps, old_prec
def test_erf_complex(): # need to increase mpmath precision for this test old_dps, old_prec = mpmath.mp.dps, mpmath.mp.prec try: mpmath.mp.dps = 70 x1, y1 = np.meshgrid(np.linspace(-10, 1, 11), np.linspace(-10, 1, 11)) x2, y2 = np.meshgrid(np.logspace(-10, 0.8, 11), np.logspace(-10, 0.8, 11)) points = np.r_[x1.ravel(), x2.ravel()] + 1j * np.r_[y1.ravel(), y2.ravel()] # note that the global accuracy of our complex erf algorithm is limited # roughly to 2e-8 assert_func_equal(sc.erf, lambda x: complex(mpmath.erf(x)), points, vectorized=False, rtol=2e-8) finally: mpmath.mp.dps, mpmath.mp.prec = old_dps, old_prec
def test_erf_complex(): # need to increase mpmath precision for this test old_dps, old_prec = mpmath.mp.dps, mpmath.mp.prec try: mpmath.mp.dps = 70 x1, y1 = np.meshgrid(np.linspace(-10, 1, 31), np.linspace(-10, 1, 11)) x2, y2 = np.meshgrid(np.logspace(-80, .8, 31), np.logspace(-80, .8, 11)) points = np.r_[x1.ravel(),x2.ravel()] + 1j*np.r_[y1.ravel(),y2.ravel()] assert_func_equal(sc.erf, lambda x: complex(mpmath.erf(x)), points, vectorized=False, rtol=1e-13) assert_func_equal(sc.erfc, lambda x: complex(mpmath.erfc(x)), points, vectorized=False, rtol=1e-13) finally: mpmath.mp.dps, mpmath.mp.prec = old_dps, old_prec
def test_erf_complex(): # need to increase mpmath precision for this test old_dps, old_prec = mpmath.mp.dps, mpmath.mp.prec try: mpmath.mp.dps = 70 x1, y1 = np.meshgrid(np.linspace(-10, 1, 11), np.linspace(-10, 1, 11)) x2, y2 = np.meshgrid(np.logspace(-10, .8, 11), np.logspace(-10, .8, 11)) points = np.r_[x1.ravel(),x2.ravel()] + 1j*np.r_[y1.ravel(),y2.ravel()] # note that the global accuracy of our complex erf algorithm is limited # roughly to 2e-8 assert_func_equal(sc.erf, lambda x: complex(mpmath.erf(x)), points, vectorized=False, rtol=2e-8) finally: mpmath.mp.dps, mpmath.mp.prec = old_dps, old_prec
def test_erf_complex(): # need to increase mpmath precision for this test old_dps, old_prec = mpmath.mp.dps, mpmath.mp.prec try: mpmath.mp.dps = 70 x1, y1 = np.meshgrid(np.linspace(-10, 1, 31), np.linspace(-10, 1, 11)) x2, y2 = np.meshgrid(np.logspace(-80, .8, 31), np.logspace(-80, .8, 11)) points = np.r_[x1.ravel(), x2.ravel()] + 1j * np.r_[y1.ravel(), y2.ravel()] assert_func_equal(sc.erf, lambda x: complex(mpmath.erf(x)), points, vectorized=False, rtol=1e-13) assert_func_equal(sc.erfc, lambda x: complex(mpmath.erfc(x)), points, vectorized=False, rtol=1e-13) finally: mpmath.mp.dps, mpmath.mp.prec = old_dps, old_prec
def test_ellip_norm(): def G01(h2, k2): return 4*pi def G11(h2, k2): return 4*pi*h2*k2/3 def G12(h2, k2): return 4*pi*h2*(k2 - h2)/3 def G13(h2, k2): return 4*pi*k2*(k2 - h2)/3 def G22(h2, k2): res = (2*(h2**4 + k2**4) - 4*h2*k2*(h2**2 + k2**2) + 6*h2**2*k2**2 + sqrt(h2**2 + k2**2 - h2*k2)*(-2*(h2**3 + k2**3) + 3*h2*k2*(h2 + k2))) return 16*pi/405*res def G21(h2, k2): res = (2*(h2**4 + k2**4) - 4*h2*k2*(h2**2 + k2**2) + 6*h2**2*k2**2 + sqrt(h2**2 + k2**2 - h2*k2)*(2*(h2**3 + k2**3) - 3*h2*k2*(h2 + k2))) return 16*pi/405*res def G23(h2, k2): return 4*pi*h2**2*k2*(k2 - h2)/15 def G24(h2, k2): return 4*pi*h2*k2**2*(k2 - h2)/15 def G25(h2, k2): return 4*pi*h2*k2*(k2 - h2)**2/15 def G32(h2, k2): res = (16*(h2**4 + k2**4) - 36*h2*k2*(h2**2 + k2**2) + 46*h2**2*k2**2 + sqrt(4*(h2**2 + k2**2) - 7*h2*k2)*(-8*(h2**3 + k2**3) + 11*h2*k2*(h2 + k2))) return 16*pi/13125*k2*h2*res def G31(h2, k2): res = (16*(h2**4 + k2**4) - 36*h2*k2*(h2**2 + k2**2) + 46*h2**2*k2**2 + sqrt(4*(h2**2 + k2**2) - 7*h2*k2)*(8*(h2**3 + k2**3) - 11*h2*k2*(h2 + k2))) return 16*pi/13125*h2*k2*res def G34(h2, k2): res = (6*h2**4 + 16*k2**4 - 12*h2**3*k2 - 28*h2*k2**3 + 34*h2**2*k2**2 + sqrt(h2**2 + 4*k2**2 - h2*k2)*(-6*h2**3 - 8*k2**3 + 9*h2**2*k2 + 13*h2*k2**2)) return 16*pi/13125*h2*(k2 - h2)*res def G33(h2, k2): res = (6*h2**4 + 16*k2**4 - 12*h2**3*k2 - 28*h2*k2**3 + 34*h2**2*k2**2 + sqrt(h2**2 + 4*k2**2 - h2*k2)*(6*h2**3 + 8*k2**3 - 9*h2**2*k2 - 13*h2*k2**2)) return 16*pi/13125*h2*(k2 - h2)*res def G36(h2, k2): res = (16*h2**4 + 6*k2**4 - 28*h2**3*k2 - 12*h2*k2**3 + 34*h2**2*k2**2 + sqrt(4*h2**2 + k2**2 - h2*k2)*(-8*h2**3 - 6*k2**3 + 13*h2**2*k2 + 9*h2*k2**2)) return 16*pi/13125*k2*(k2 - h2)*res def G35(h2, k2): res = (16*h2**4 + 6*k2**4 - 28*h2**3*k2 - 12*h2*k2**3 + 34*h2**2*k2**2 + sqrt(4*h2**2 + k2**2 - h2*k2)*(8*h2**3 + 6*k2**3 - 13*h2**2*k2 - 9*h2*k2**2)) return 16*pi/13125*k2*(k2 - h2)*res def G37(h2, k2): return 4*pi*h2**2*k2**2*(k2 - h2)**2/105 known_funcs = {(0, 1): G01, (1, 1): G11, (1, 2): G12, (1, 3): G13, (2, 1): G21, (2, 2): G22, (2, 3): G23, (2, 4): G24, (2, 5): G25, (3, 1): G31, (3, 2): G32, (3, 3): G33, (3, 4): G34, (3, 5): G35, (3, 6): G36, (3, 7): G37} def _ellip_norm(n, p, h2, k2): func = known_funcs[n, p] return func(h2, k2) _ellip_norm = np.vectorize(_ellip_norm) def ellip_normal_known(h2, k2, n, p): return _ellip_norm(n, p, h2, k2) # generate both large and small h2 < k2 pairs np.random.seed(1234) h2 = np.random.pareto(0.5, size=1) k2 = h2 * (1 + np.random.pareto(0.5, size=h2.size)) points = [] for n in range(4): for p in range(1, 2*n+2): points.append((h2, k2, n*np.ones(h2.size), p*np.ones(h2.size))) points = np.array(points) assert_func_equal(ellip_normal, ellip_normal_known, points, rtol=1e-12)
def test_wrightomega_real_versus_complex(): x = np.linspace(-500, 500, 1001) results = sc.wrightomega(x + 0j).real assert_func_equal(sc.wrightomega, results, x, atol=0, rtol=1e-14)
def test_ellip_harm(): def E01(h2, k2, s): return 1 def E11(h2, k2, s): return s def E12(h2, k2, s): return sqrt(abs(s*s - h2)) def E13(h2, k2, s): return sqrt(abs(s*s - k2)) def E21(h2, k2, s): return s*s - 1/3*((h2 + k2) + sqrt(abs((h2 + k2)*(h2 + k2)-3*h2*k2))) def E22(h2, k2, s): return s*s - 1/3*((h2 + k2) - sqrt(abs((h2 + k2)*(h2 + k2)-3*h2*k2))) def E23(h2, k2, s): return s * sqrt(abs(s*s - h2)) def E24(h2, k2, s): return s * sqrt(abs(s*s - k2)) def E25(h2, k2, s): return sqrt(abs((s*s - h2)*(s*s - k2))) def E31(h2, k2, s): return s*s*s - (s/5)*(2*(h2 + k2) + sqrt(4*(h2 + k2)*(h2 + k2) - 15*h2*k2)) def E32(h2, k2, s): return s*s*s - (s/5)*(2*(h2 + k2) - sqrt(4*(h2 + k2)*(h2 + k2) - 15*h2*k2)) def E33(h2, k2, s): return sqrt(abs(s*s - h2))*(s*s - 1/5*((h2 + 2*k2) + sqrt(abs((h2 + 2*k2)*(h2 + 2*k2) - 5*h2*k2)))) def E34(h2, k2, s): return sqrt(abs(s*s - h2))*(s*s - 1/5*((h2 + 2*k2) - sqrt(abs((h2 + 2*k2)*(h2 + 2*k2) - 5*h2*k2)))) def E35(h2, k2, s): return sqrt(abs(s*s - k2))*(s*s - 1/5*((2*h2 + k2) + sqrt(abs((2*h2 + k2)*(2*h2 + k2) - 5*h2*k2)))) def E36(h2, k2, s): return sqrt(abs(s*s - k2))*(s*s - 1/5*((2*h2 + k2) - sqrt(abs((2*h2 + k2)*(2*h2 + k2) - 5*h2*k2)))) def E37(h2, k2, s): return s * sqrt(abs((s*s - h2)*(s*s - k2))) assert_equal(ellip_harm(5, 8, 1, 2, 2.5, 1, 1), ellip_harm(5, 8, 1, 2, 2.5)) known_funcs = {(0, 1): E01, (1, 1): E11, (1, 2): E12, (1, 3): E13, (2, 1): E21, (2, 2): E22, (2, 3): E23, (2, 4): E24, (2, 5): E25, (3, 1): E31, (3, 2): E32, (3, 3): E33, (3, 4): E34, (3, 5): E35, (3, 6): E36, (3, 7): E37} point_ref = [] def ellip_harm_known(h2, k2, n, p, s): for i in range(h2.size): func = known_funcs[(int(n[i]), int(p[i]))] point_ref.append(func(h2[i], k2[i], s[i])) return point_ref np.random.seed(1234) h2 = np.random.pareto(0.5, size=30) k2 = h2*(1 + np.random.pareto(0.5, size=h2.size)) s = np.random.pareto(0.5, size=h2.size) points = [] for i in range(h2.size): for n in range(4): for p in range(1, 2*n+2): points.append((h2[i], k2[i], n, p, s[i])) points = np.array(points) assert_func_equal(ellip_harm, ellip_harm_known, points, rtol=1e-12)
def test_ellip_norm(): def G01(h2, k2): return 4 * pi def G11(h2, k2): return 4 * pi * h2 * k2 / 3 def G12(h2, k2): return 4 * pi * h2 * (k2 - h2) / 3 def G13(h2, k2): return 4 * pi * k2 * (k2 - h2) / 3 def G22(h2, k2): res = (2 * (h2**4 + k2**4) - 4 * h2 * k2 * (h2**2 + k2**2) + 6 * h2**2 * k2**2 + sqrt(h2**2 + k2**2 - h2 * k2) * (-2 * (h2**3 + k2**3) + 3 * h2 * k2 * (h2 + k2))) return 16 * pi / 405 * res def G21(h2, k2): res = (2 * (h2**4 + k2**4) - 4 * h2 * k2 * (h2**2 + k2**2) + 6 * h2**2 * k2**2 + sqrt(h2**2 + k2**2 - h2 * k2) * (2 * (h2**3 + k2**3) - 3 * h2 * k2 * (h2 + k2))) return 16 * pi / 405 * res def G23(h2, k2): return 4 * pi * h2**2 * k2 * (k2 - h2) / 15 def G24(h2, k2): return 4 * pi * h2 * k2**2 * (k2 - h2) / 15 def G25(h2, k2): return 4 * pi * h2 * k2 * (k2 - h2)**2 / 15 def G32(h2, k2): res = (16 * (h2**4 + k2**4) - 36 * h2 * k2 * (h2**2 + k2**2) + 46 * h2**2 * k2**2 + sqrt(4 * (h2**2 + k2**2) - 7 * h2 * k2) * (-8 * (h2**3 + k2**3) + 11 * h2 * k2 * (h2 + k2))) return 16 * pi / 13125 * k2 * h2 * res def G31(h2, k2): res = (16 * (h2**4 + k2**4) - 36 * h2 * k2 * (h2**2 + k2**2) + 46 * h2**2 * k2**2 + sqrt(4 * (h2**2 + k2**2) - 7 * h2 * k2) * (8 * (h2**3 + k2**3) - 11 * h2 * k2 * (h2 + k2))) return 16 * pi / 13125 * h2 * k2 * res def G34(h2, k2): res = (6 * h2**4 + 16 * k2**4 - 12 * h2**3 * k2 - 28 * h2 * k2**3 + 34 * h2**2 * k2**2 + sqrt(h2**2 + 4 * k2**2 - h2 * k2) * (-6 * h2**3 - 8 * k2**3 + 9 * h2**2 * k2 + 13 * h2 * k2**2)) return 16 * pi / 13125 * h2 * (k2 - h2) * res def G33(h2, k2): res = (6 * h2**4 + 16 * k2**4 - 12 * h2**3 * k2 - 28 * h2 * k2**3 + 34 * h2**2 * k2**2 + sqrt(h2**2 + 4 * k2**2 - h2 * k2) * (6 * h2**3 + 8 * k2**3 - 9 * h2**2 * k2 - 13 * h2 * k2**2)) return 16 * pi / 13125 * h2 * (k2 - h2) * res def G36(h2, k2): res = (16 * h2**4 + 6 * k2**4 - 28 * h2**3 * k2 - 12 * h2 * k2**3 + 34 * h2**2 * k2**2 + sqrt(4 * h2**2 + k2**2 - h2 * k2) * (-8 * h2**3 - 6 * k2**3 + 13 * h2**2 * k2 + 9 * h2 * k2**2)) return 16 * pi / 13125 * k2 * (k2 - h2) * res def G35(h2, k2): res = (16 * h2**4 + 6 * k2**4 - 28 * h2**3 * k2 - 12 * h2 * k2**3 + 34 * h2**2 * k2**2 + sqrt(4 * h2**2 + k2**2 - h2 * k2) * (8 * h2**3 + 6 * k2**3 - 13 * h2**2 * k2 - 9 * h2 * k2**2)) return 16 * pi / 13125 * k2 * (k2 - h2) * res def G37(h2, k2): return 4 * pi * h2**2 * k2**2 * (k2 - h2)**2 / 105 known_funcs = { (0, 1): G01, (1, 1): G11, (1, 2): G12, (1, 3): G13, (2, 1): G21, (2, 2): G22, (2, 3): G23, (2, 4): G24, (2, 5): G25, (3, 1): G31, (3, 2): G32, (3, 3): G33, (3, 4): G34, (3, 5): G35, (3, 6): G36, (3, 7): G37 } def _ellip_norm(n, p, h2, k2): func = known_funcs[n, p] return func(h2, k2) _ellip_norm = np.vectorize(_ellip_norm) def ellip_normal_known(h2, k2, n, p): return _ellip_norm(n, p, h2, k2) # generate both large and small h2 < k2 pairs np.random.seed(1234) h2 = np.random.pareto(0.5, size=1) k2 = h2 * (1 + np.random.pareto(0.5, size=h2.size)) points = [] for n in range(4): for p in range(1, 2 * n + 2): points.append((h2, k2, np.full(h2.size, n), np.full(h2.size, p))) points = np.array(points) with suppress_warnings() as sup: sup.filter(IntegrationWarning, "The occurrence of roundoff error") assert_func_equal(ellip_normal, ellip_normal_known, points, rtol=1e-12)
def test_ellip_harm(): def E01(h2, k2, s): return 1 def E11(h2, k2, s): return s def E12(h2, k2, s): return sqrt(abs(s * s - h2)) def E13(h2, k2, s): return sqrt(abs(s * s - k2)) def E21(h2, k2, s): return s * s - 1 / 3 * ( (h2 + k2) + sqrt(abs((h2 + k2) * (h2 + k2) - 3 * h2 * k2))) def E22(h2, k2, s): return s * s - 1 / 3 * ( (h2 + k2) - sqrt(abs((h2 + k2) * (h2 + k2) - 3 * h2 * k2))) def E23(h2, k2, s): return s * sqrt(abs(s * s - h2)) def E24(h2, k2, s): return s * sqrt(abs(s * s - k2)) def E25(h2, k2, s): return sqrt(abs((s * s - h2) * (s * s - k2))) def E31(h2, k2, s): return s * s * s - (s / 5) * (2 * (h2 + k2) + sqrt(4 * (h2 + k2) * (h2 + k2) - 15 * h2 * k2)) def E32(h2, k2, s): return s * s * s - (s / 5) * (2 * (h2 + k2) - sqrt(4 * (h2 + k2) * (h2 + k2) - 15 * h2 * k2)) def E33(h2, k2, s): return sqrt(abs(s * s - h2)) * (s * s - 1 / 5 * ( (h2 + 2 * k2) + sqrt(abs((h2 + 2 * k2) * (h2 + 2 * k2) - 5 * h2 * k2)))) def E34(h2, k2, s): return sqrt(abs(s * s - h2)) * (s * s - 1 / 5 * ( (h2 + 2 * k2) - sqrt(abs((h2 + 2 * k2) * (h2 + 2 * k2) - 5 * h2 * k2)))) def E35(h2, k2, s): return sqrt(abs(s * s - k2)) * (s * s - 1 / 5 * ( (2 * h2 + k2) + sqrt(abs((2 * h2 + k2) * (2 * h2 + k2) - 5 * h2 * k2)))) def E36(h2, k2, s): return sqrt(abs(s * s - k2)) * (s * s - 1 / 5 * ( (2 * h2 + k2) - sqrt(abs((2 * h2 + k2) * (2 * h2 + k2) - 5 * h2 * k2)))) def E37(h2, k2, s): return s * sqrt(abs((s * s - h2) * (s * s - k2))) assert_equal(ellip_harm(5, 8, 1, 2, 2.5, 1, 1), ellip_harm(5, 8, 1, 2, 2.5)) known_funcs = { (0, 1): E01, (1, 1): E11, (1, 2): E12, (1, 3): E13, (2, 1): E21, (2, 2): E22, (2, 3): E23, (2, 4): E24, (2, 5): E25, (3, 1): E31, (3, 2): E32, (3, 3): E33, (3, 4): E34, (3, 5): E35, (3, 6): E36, (3, 7): E37 } point_ref = [] def ellip_harm_known(h2, k2, n, p, s): for i in range(h2.size): func = known_funcs[(int(n[i]), int(p[i]))] point_ref.append(func(h2[i], k2[i], s[i])) return point_ref np.random.seed(1234) h2 = np.random.pareto(0.5, size=30) k2 = h2 * (1 + np.random.pareto(0.5, size=h2.size)) s = np.random.pareto(0.5, size=h2.size) points = [] for i in range(h2.size): for n in range(4): for p in range(1, 2 * n + 2): points.append((h2[i], k2[i], n, p, s[i])) points = np.array(points) assert_func_equal(ellip_harm, ellip_harm_known, points, rtol=1e-12)
def check(self): np.random.seed(1234) # Generate values for the arguments if isinstance(self.arg_spec, np.ndarray): argarr = self.arg_spec.copy() else: num_args = len(self.arg_spec) ms = np.asarray([ 1.5 if isinstance(arg, ComplexArg) else 1.0 for arg in self.arg_spec ]) ms = (self.n**(ms / sum(ms))).astype(int) + 1 argvals = [] for arg, m in zip(self.arg_spec, ms): argvals.append(arg.values(m)) argarr = np.array(np.broadcast_arrays(*np.ix_(*argvals))).reshape( num_args, -1).T # Check old_dps, old_prec = mpmath.mp.dps, mpmath.mp.prec try: if self.dps is not None: dps_list = [self.dps] else: dps_list = [20] if self.prec is not None: mpmath.mp.prec = self.prec # Proper casting of mpmath input and output types. Using # native mpmath types as inputs gives improved precision # in some cases. if np.issubdtype(argarr.dtype, np.complexfloating): pytype = mpc2complex def mptype(x): return mpmath.mpc(complex(x)) else: def mptype(x): return mpmath.mpf(float(x)) def pytype(x): if abs(x.imag) > 1e-16 * (1 + abs(x.real)): return np.nan else: return mpf2float(x.real) # Try out different dps until one (or none) works for j, dps in enumerate(dps_list): mpmath.mp.dps = dps try: assert_func_equal( self.scipy_func, lambda *a: pytype(self.mpmath_func(*map(mptype, a))), argarr, vectorized=False, rtol=self.rtol, atol=self.atol, ignore_inf_sign=self.ignore_inf_sign, nan_ok=self.nan_ok, param_filter=self.param_filter) break except AssertionError: if j >= len(dps_list) - 1: reraise(*sys.exc_info()) finally: mpmath.mp.dps, mpmath.mp.prec = old_dps, old_prec