def test_product(self): self.assertRaises(TypeError, ph.Product, cr.Function, cr.Function) # only Placeholders allowed p1 = ph.Product(self.input, self.test_funcs) p2 = ph.Product(self.test_funcs, self.field_var) # test single argument call p3 = ph.Product(self.test_funcs) self.assertTrue(p3.b_empty) res = ut.evaluate_placeholder_function(p3.args[0], np.pi/2) self.assertTrue(np.allclose(res, [1, 0])) # test automated evaluation of Product with Scaled function p4 = ph.Product(self.field_var, self.scale_funcs) self.assertTrue(isinstance(p4.args[0], ph.Placeholder)) res = ut.evaluate_placeholder_function(p4.args[0], 0) self.assertTrue(np.allclose(res, self.scale(0)*np.array([self.ini_funcs[0](0), self.ini_funcs[1](0)]))) self.assertEqual(p4.args[1], None) self.assertTrue(p4.b_empty) # test automated simplification of cascaded products p5 = ph.Product(ph.Product(self.field_var, self.scale_funcs), ph.Product(self.test_funcs, self.scale_funcs)) self.assertFalse(p5.b_empty) p6 = ph.Product(ph.Product(self.field_var_dz, self.scale_funcs), ph.Product(self.test_funcs, self.scale_funcs)) self.assertFalse(p6.b_empty) res = ut.evaluate_placeholder_function(p5.args[0], 0) self.assertTrue(np.allclose(res, self.scale(0)*np.array([self.ini_funcs[0](0), self.ini_funcs[1](0)]))) res1 = ut.evaluate_placeholder_function(p5.args[0], 1) self.assertTrue(np.allclose(res1, self.scale(1)*np.array([self.ini_funcs[0](1), self.ini_funcs[1](1)]))) res2 = ut.evaluate_placeholder_function(p5.args[1], 0) self.assertTrue(np.allclose(res2, self.scale(0)*np.array([self.t_funcs[0](0), self.t_funcs[1](0)]))) res3 = ut.evaluate_placeholder_function(p5.args[1], 1) self.assertTrue(np.allclose(res3, self.scale(0)*np.array([self.t_funcs[0](1), self.t_funcs[1](1)]))) # test methods self.assertEqual(p1.get_arg_by_class(ph.Input), [self.input]) self.assertEqual(p1.get_arg_by_class(ph.TestFunction), [self.test_funcs]) self.assertEqual(p2.get_arg_by_class(ph.TestFunction), [self.test_funcs]) self.assertEqual(p2.get_arg_by_class(ph.FieldVariable), [self.field_var])
def test_eval(self): eval_values = np.array(list(range(10))) res = ut.evaluate_placeholder_function(self.funcs, eval_values) self.assertTrue(np.allclose(self.psi(eval_values), res))
def test_product(self): self.assertRaises(TypeError, ph.Product, cr.Function, cr.Function) # only Placeholders allowed p1 = ph.Product(self.input, self.test_funcs) p2 = ph.Product(self.test_funcs, self.field_var) # test single argument call p3 = ph.Product(self.test_funcs) self.assertTrue(p3.b_empty) res = ut.evaluate_placeholder_function(p3.args[0], np.pi / 2) self.assertTrue(np.allclose(res, [1, 0])) # test automated evaluation of Product with Scaled function p4 = ph.Product(self.field_var, self.scale_funcs) self.assertTrue(isinstance(p4.args[0], ph.Placeholder)) res = ut.evaluate_placeholder_function(p4.args[0], 0) self.assertTrue( np.allclose( res, self.scale(0) * np.array([self.ini_funcs[0](0), self.ini_funcs[1](0)]))) self.assertEqual(p4.args[1], None) self.assertTrue(p4.b_empty) # test automated simplification of cascaded products p5 = ph.Product(ph.Product(self.field_var, self.scale_funcs), ph.Product(self.test_funcs, self.scale_funcs)) self.assertFalse(p5.b_empty) p6 = ph.Product(ph.Product(self.field_var_dz, self.scale_funcs), ph.Product(self.test_funcs, self.scale_funcs)) self.assertFalse(p6.b_empty) res = ut.evaluate_placeholder_function(p5.args[0], 0) self.assertTrue( np.allclose( res, self.scale(0) * np.array([self.ini_funcs[0](0), self.ini_funcs[1](0)]))) res1 = ut.evaluate_placeholder_function(p5.args[0], 1) self.assertTrue( np.allclose( res1, self.scale(1) * np.array([self.ini_funcs[0](1), self.ini_funcs[1](1)]))) res2 = ut.evaluate_placeholder_function(p5.args[1], 0) self.assertTrue( np.allclose( res2, self.scale(0) * np.array([self.t_funcs[0](0), self.t_funcs[1](0)]))) res3 = ut.evaluate_placeholder_function(p5.args[1], 1) self.assertTrue( np.allclose( res3, self.scale(0) * np.array([self.t_funcs[0](1), self.t_funcs[1](1)]))) # test methods self.assertEqual(p1.get_arg_by_class(ph.Input), [self.input]) self.assertEqual(p1.get_arg_by_class(ph.TestFunction), [self.test_funcs]) self.assertEqual(p2.get_arg_by_class(ph.TestFunction), [self.test_funcs]) self.assertEqual(p2.get_arg_by_class(ph.FieldVariable), [self.field_var])