def test_merge_two_equations(self): x, y = initAdArrays([np.array([1]), np.array([2])]) z1 = exp(x) + y z2 = exp(y) + x z = concatenate((z1, z2)) val = np.array([np.exp(1) + 2, np.exp(2) + 1]) J = np.array([[np.exp(1), 1], [1, np.exp(2)]]) self.assertTrue(np.allclose(z.val, val) and np.allclose(z.full_jac().A, J))
def test_exp_sparse_jac(self): val = np.array([1, 2, 3]) J = sps.csc_matrix(np.array([[3, 2, 1], [5, 6, 1], [2, 3, 5]])) a = Ad_array(val, J) b = af.exp(a) jac = np.dot(np.diag(np.exp(val)), J.A) self.assertTrue(np.all(b.val == np.exp(val)) and np.all(b.jac == jac))
def test_exp_vector(self): val = np.array([1, 2, 3]) J = np.array([[3, 2, 1], [5, 6, 1], [2, 3, 5]]) a = Ad_array(val, sps.csc_matrix(J)) b = af.exp(a) jac = np.dot(np.diag(np.exp(val)), J) self.assertTrue(np.all(b.val == np.exp(val)) and np.all(b.jac == jac)) self.assertTrue(np.all(J == np.array([[3, 2, 1], [5, 6, 1], [2, 3, 5]])))
def test_exp_scalar_times_ad_var(self): val = np.array([1, 2, 3]) J = sps.diags(np.array([1, 1, 1])) a = Ad_array(val, J) c = 2 b = af.exp(c * a) jac = c * sps.diags(np.exp(c * val)) * J self.assertTrue( np.allclose(b.val, np.exp(c * val)) and np.allclose(b.jac.A, jac.A) ) self.assertTrue(np.all(a.val == [1, 2, 3]) and np.all(a.jac.A == J.A))
def test_exp_scalar_times_ad_var(self): val = np.array([1, 2, 3]) J = sps.diags(np.array([1, 1, 1])) a, _, _ = initAdArrays([val, val, val]) c = 2 b = af.exp(c * a) zero = sps.csc_matrix((3, 3)) jac = sps.hstack([c * sps.diags(np.exp(c * val)) * J, zero, zero]) jac_a = sps.hstack([J, zero, zero]) self.assertTrue( np.allclose(b.val, np.exp(c * val)) and np.allclose(b.jac.A, jac.A) ) self.assertTrue(np.all(a.val == [1, 2, 3]) and np.all(a.jac.A == jac_a.A))
def test_exp_advar(self): a = Ad_array(2, 3) b = af.exp(a) self.assertTrue(b.val == np.exp(2) and b.jac == 3 * np.exp(2)) self.assertTrue(a.val == 2 and a.jac == 3)
def test_exp_scalar(self): a = Ad_array(1, 0) b = af.exp(a) self.assertTrue(b.val == np.exp(1) and b.jac == 0) self.assertTrue(a.val == 1 and a.jac == 0)