def test_direct_evaluation(self): l0 = FunctionWrapper( "LinearBackground", A0=0, A1=2) l1 = FunctionWrapper( "LinearBackground", A0=5, A1=-1) ws = CreateWorkspace(DataX=[0,1,2,3,4], DataY=[5,5,5,5]) c = CompositeFunctionWrapper(l0, l1) cws = EvaluateFunction(c,"ws", OutputWorkspace='out') cvals = cws.readY(1) self.assertAlmostEqual(cvals[0], 5.5) self.assertAlmostEqual(cvals[1], 6.5) self.assertAlmostEqual(cvals[2], 7.5) self.assertAlmostEqual(cvals[3], 8.5) p = ProductFunctionWrapper(l0, l1) pws = EvaluateFunction(p,"ws", OutputWorkspace='out') pvals = pws.readY(1) self.assertAlmostEqual(pvals[0], 4.5) self.assertAlmostEqual(pvals[1], 10.5) self.assertAlmostEqual(pvals[2], 12.5) self.assertAlmostEqual(pvals[3], 10.5) sq = Polynomial(attributes={'n': 2}, A0=0, A1=0.0, A2=1.0) sqws = EvaluateFunction(sq,"ws", OutputWorkspace='out') sqvals = sqws.readY(1) self.assertAlmostEqual(sqvals[0], 0.25) self.assertAlmostEqual(sqvals[1], 2.25) self.assertAlmostEqual(sqvals[2], 6.25)
def test_attributes(self): testhelpers.assertRaisesNothing(self, FunctionWrapper, "Polynomial", attributes={'n': 3}, A0=4, A1=3, A2=2, A3=1) testhelpers.assertRaisesNothing(self, FunctionWrapper, "Polynomial", n=3, A0=4, A1=3, A2=2, A3=1) p = Polynomial(n=3, A0=1, A1=2, A2=4, A3=3) self.assertEqual(p['n'],3) p['n'] = 4 self.assertEqual(p['n'],4)
def test_evaluation_by_1D_numpy_array(self): import numpy as np a = np.array([0, 1, 3]) p = Polynomial(n=4, A0=1, A1=1, A2=1, A3=1, A4=1) result = p(a) self.assertAlmostEqual(result[0],1.0) self.assertAlmostEqual(result[1],5.0) self.assertAlmostEqual(result[2],121.0)
def test_evaluation_by_workspace(self): ws = CreateWorkspace(DataX=[0,1,2,3], DataY=[5,5,5]) sq = Polynomial(n=2, A0=0, A1=0, A2=1) outWs = sq(ws) sqvals = outWs.readY(1) self.assertAlmostEqual(sqvals[0], 0.25) self.assertAlmostEqual(sqvals[1], 2.25) self.assertAlmostEqual(sqvals[2], 6.25)
def test_evaluation_with_parameters_set(self): p = Polynomial(n=2) result = p([0, 1, 2], 0.0, 0.5, 0.5) self.assertAlmostEqual(result[0], 0.0) self.assertAlmostEqual(result[1], 1.0) self.assertAlmostEqual(result[2], 3.0)
def test_evaluation_by_list_of_values(self): p = Polynomial(n=4, A0=1, A1=1, A2=1, A3=1, A4=1) result = p([0,1,3]) self.assertAlmostEqual(result[0],1.0) self.assertAlmostEqual(result[1],5.0) self.assertAlmostEqual(result[2],121.0)
def test_evaluation_by_single_value(self): p = Polynomial(n=4, A0=1, A1=1, A2=1, A3=1, A4=1) self.assertAlmostEqual(p(0),1.0) self.assertAlmostEqual(p(1),5.0) self.assertAlmostEqual(p(2),31.0)