def test_trivial_noninvertible(self): def f(x): del x return 1. with self.assertRaises(ValueError): inverse(f)(1.)
def test_simple_inverse(self): def f(x): return np.exp(x) f_inv = inverse(f, 0.1) onp.testing.assert_allclose(f_inv(1.0), 0.) def f2(x): return np.exp(x) f2_inv = inverse(f2, np.zeros(2)) onp.testing.assert_allclose(f2_inv(np.ones(2)), np.zeros(2))
def test_div_inverse(self): def f(x): return x / 2. f_inv = inverse(f) onp.testing.assert_allclose(f_inv(1.0), 2.) def f2(x): return 2. / x f2_inv = inverse(f2) onp.testing.assert_allclose(f2_inv(1.0), 2.)
def test_mul_inverse(self): def f(x): return x * 2. f_inv = inverse(f) onp.testing.assert_allclose(f_inv(1.0), 0.5) def f2(x): return 2. * x f2_inv = inverse(f2) onp.testing.assert_allclose(f2_inv(1.0), 0.5)
def test_trivial_inverse(self): def f(x): return x f_inv = inverse(f) onp.testing.assert_allclose(f_inv(1.0), 1.0) def f2(x, y): return x, y f2_inv = inverse(f2) onp.testing.assert_allclose(f2_inv(1.0, 2.0), (1.0, 2.0))
def test_advanced_inverse_two(self): def f(x, y): return np.exp(x), x**2 + y f_inv = inverse(f, 0.1, 0.2) onp.testing.assert_allclose(f_inv(2.0, 2.0), (np.log(2.), 2 - np.log(2.)**2))
def test_advanced_inverse_three(self): def f(x, y, z): return np.exp(x), x**2 + y, np.exp(z + y) f_inv = inverse(f, 0., 0., 0.) onp.testing.assert_allclose( f_inv(2.0, 2.0, 2.0), (np.log(2.), 2 - np.log(2.)**2, np.log(2.0) - (2 - np.log(2.)**2)))
def test_conditional_inverse(self): def f(x, y): return x + 1., np.exp(x + 1.) + y f_inv = inverse(f, 0., 2.) onp.testing.assert_allclose(f_inv(0., 2.), (-1., 1.))
def test_noninvertible(self): def f(x, y): return x + y, x + y with self.assertRaises(ValueError): inverse(f)(1., 2.)