def test_odeintw_complex(): z0 = 0.25 + 0.5j t = np.linspace(0, 1, 5) zsol = odeintw(system1_complex, z0, t) zv = zsol.view(np.float64) sol = odeintw(system1_real, [z0.real, z0.imag], t) assert_allclose(zv, sol)
def test_odeint_array(): t = np.linspace(0, 1, 5) a0 = np.array([[1, -2], [3, -4]]) asol = odeintw(system2_array, a0, t) avec = asol.reshape(-1, 4) sol = odeintw(system2_vector, a0.ravel(), t) assert_allclose(avec, sol)
def test_system3_banded(): c = np.array([[-20+1j, 5-1j, 0, 0], [ 0, -0.1, 1+2.5j, 0], [ 0, 0, -1, 0.5], [ 0, 0, 0, -5+10j]]) common_kwargs = dict(args=(c,), full_output=True, atol=1e-12, rtol=1e-10, mxstep=1000) z0 = np.arange(1, 5.0) + 0.5j t = np.linspace(0, 250, 11) sol0, info0 = odeintw(system3_func, z0, t, Dfun=system3_jac, **common_kwargs) sol1, info1 = odeintw(system3_func, z0, t, Dfun=system3_bjac_cols, ml=0, mu=1, col_deriv=True, **common_kwargs) sol2, info2 = odeintw(system3_func, z0, t, Dfun=system3_bjac_rows, ml=0, mu=1, **common_kwargs) assert_allclose(sol0, sol1) assert_allclose(sol0, sol2) # The same code paths should have been followed in computing # sol1 and sol2, so the number of jacobian evaluations should # be the same. assert_array_equal(info1['nje'], info2['nje'])
def test_odeint_array(): t = np.linspace(0, 1, 5) a0 = np.array([[1, -2], [3, -4]]) asol = odeintw(system2_array, a0, t) avec = asol.reshape(-1, 4) sol = odeintw(system2_vector, a0.ravel(), t) yield assert_allclose, avec, sol
def test_odeintw_complex(): z0 = 0.25 + 0.5j t = np.linspace(0, 1, 5) zsol = odeintw(system1_complex, z0, t) zv = zsol.view(np.float64) sol = odeintw(system1_real, [z0.real, z0.imag], t) yield assert_allclose, zv, sol
def test_system3_banded(): c = np.array([[-20+1j, 5-1j, 0, 0], [ 0, -0.1, 1+2.5j, 0], [ 0, 0, -1, 0.5], [ 0, 0, 0, -5+10j]]) common_kwargs = dict(args=(c,), full_output=True, atol=1e-12, rtol=1e-10, mxstep=1000) z0 = np.arange(1,5.0) + 0.5j t = np.linspace(0, 250, 11) sol0, info0 = odeintw(system3_func, z0, t, Dfun=system3_jac, **common_kwargs) sol1, info1 = odeintw(system3_func, z0, t, Dfun=system3_bjac_cols, ml=0, mu=1, col_deriv=True, **common_kwargs) sol2, info2 = odeintw(system3_func, z0, t, Dfun=system3_bjac_rows, ml=0, mu=1, **common_kwargs) yield assert_allclose, sol0, sol1 yield assert_allclose, sol0, sol2 # The same code paths should have been followed in computing # sol1 and sol2, so the number of jacobian evaluations should # be the same. yield assert_array_equal, info1['nje'], info2['nje']
def test_system3_tfirst(): z0 = np.array([1.0, 2+3j, -4j, 5]) t = np.linspace(0, 250, 11) common_kwargs = dict(atol=1e-12, rtol=1e-10, mxstep=1000) sol = odeintw(system3_func, z0, t, Dfun=system3_jac, args=(C,), **common_kwargs) sol_tfirst = odeintw(system3_func_tfirst, z0, t, Dfun=system3_jac_tfirst, args=(C,), tfirst=True, **common_kwargs) assert_allclose(sol, sol_tfirst)
def test_system3(): c = np.array([[-20+1j, 5-1j, 0, 0], [ 0, -0.1, 1+2.5j, 0], [ 0, 0, -1, 0.5], [ 0, 0, 0, -5+10j]]) z0 = np.arange(1,5.0) + 0.5j t = np.linspace(0, 250, 11) common_kwargs = dict(full_output=True, atol=1e-12, rtol=1e-10, mxstep=1000) sol0, info0 = odeintw(system3_funcz, z0, t, Dfun=system3_jac, args=(c,), **common_kwargs) nje0 = info0['nje'] x0 = z0.view(np.float64) sol1, info1 = odeint(system3_func, x0, t, Dfun=system3_jac, args=(_complex_to_real_jac(c),), **common_kwargs) nje1 = info1['nje'] # Using assert_array_equal here is risky. The system definitions have # been defined so the call to odeint in odeintw follows the same # code path as the call to odeint above. Still, floating point operations # aren't necessarily deterministic. yield assert_array_equal, sol0.view(np.float64), sol1 yield assert_array_equal, nje0, nje1
def test_system3(): c = np.array([[-20 + 1j, 5 - 1j, 0, 0], [0, -0.1, 1 + 2.5j, 0], [0, 0, -1, 0.5], [0, 0, 0, -5 + 10j]]) z0 = np.arange(1, 5.0) + 0.5j t = np.linspace(0, 250, 11) common_kwargs = dict(full_output=True, atol=1e-12, rtol=1e-10, mxstep=1000) sol0, info0 = odeintw(system3_funcz, z0, t, Dfun=system3_jac, args=(c, ), **common_kwargs) nje0 = info0['nje'] x0 = z0.view(np.float64) sol1, info1 = odeint(system3_func, x0, t, Dfun=system3_jac, args=(_complex_to_real_jac(c), ), **common_kwargs) nje1 = info1['nje'] # Using assert_array_equal here is risky. The system definitions have # been defined so the call to odeint in odeintw follows the same # code path as the call to odeint above. Still, floating point operations # aren't necessarily deterministic. yield assert_array_equal, sol0.view(np.float64), sol1 yield assert_array_equal, nje0, nje1
def test_complex_simple_scalar_integration(): # The exact solution is z0 + k*t**2 def sys(z, t, k): return 2*k*t def sys_tfirst(t, z, k): return 2*k*t k = 3 z0 = 1+2j, t = np.array([0, 0.5, 1]) sol = odeintw(sys, z0, t, args=(k,)) assert_allclose(sol, z0 + k*t.reshape(-1, 1)**2) sol = odeintw(sys_tfirst, z0, t, args=(k,), tfirst=True) assert_allclose(sol, z0 + k*t.reshape(-1, 1)**2)
def test_tfirst_system1(): z0 = 0.25 + 0.5j t = np.linspace(0, 1, 5) sol_zfirst = odeintw(system1_complex, z0, t) sol_tfirst = odeintw(system1_complex_tfirst, z0, t, tfirst=True) assert_allclose(sol_tfirst, sol_zfirst)