Beispiel #1
0
    def testBuildEquation(self):

        from numpy import array_equal

        # simple equation
        sin = builder.getBuilder("sin")
        a = builder.ArgumentBuilder(name="a", value = 1)
        A = builder.ArgumentBuilder(name="A", value = 2)
        x = numpy.arange(0, numpy.pi, 0.1)

        beq = A*sin(a*x)
        eq = beq.getEquation()

        self.assertTrue("a" in eq.argdict)
        self.assertTrue("A" in eq.argdict)
        self.assertTrue(array_equal(eq(), 2*numpy.sin(x)))

        self.assertEqual(eq.args, [eq.A, eq.a])

        # Check the number of arguments
        self.assertRaises(ValueError, sin)

        # custom function
        def _f(a, b):
            return (a-b)*1.0/(a+b)

        f = builder.wrapFunction("f", _f, 2, 1)
        a = builder.ArgumentBuilder(name="a", value = 2)
        b = builder.ArgumentBuilder(name="b", value = 1)

        beq = sin(f(a,b))
        eq = beq.getEquation()
        self.assertEqual(eq(), numpy.sin(_f(2, 1)))

        # complex function
        sqrt = builder.getBuilder("sqrt")
        e = numpy.e
        _x = numpy.arange(0, 1, 0.05)
        x = builder.ArgumentBuilder(name="x", value = _x, const = True)
        sigma = builder.ArgumentBuilder(name="sigma", value = 0.1)
        beq = sqrt(e**(-0.5*(x/sigma)**2))
        eq = beq.getEquation()
        f = lambda x, sigma : sqrt(e**(-0.5*(x/sigma)**2))
        self.assertTrue(numpy.allclose(eq(), numpy.sqrt(e**(-0.5*(_x/0.1)**2))))

        # Equation with Equation
        A = builder.ArgumentBuilder(name="A", value = 2)
        B = builder.ArgumentBuilder(name="B", value = 4)
        beq = A + B
        eq = beq.getEquation()
        E = builder.wrapOperator("eq", eq)
        eq2 = (2*E).getEquation()
        # Make sure these evaulate to the same thing
        self.assertEqual(eq.args, [A.literal, B.literal])
        self.assertEqual(2*eq(), eq2())
        # Pass new arguments to the equation
        C = builder.ArgumentBuilder(name="C", value = 5)
        D = builder.ArgumentBuilder(name="D", value = 6)
        eq3 = (E(C, D)+1).getEquation()
        self.assertEqual(12, eq3())
        # Pass old and new arguments to the equation
        # If things work right, A has been given the value of C in the last
        # evaluation (5)
        eq4 = (3*E(A, D)-1).getEquation()
        self.assertEqual(32, eq4())
        # Try to pass the wrong number of arguments
        self.assertRaises(ValueError, E, A)
        self.assertRaises(ValueError, E, A, B, C)

        self.assertTrue(noObserversInGlobalBuilders())
        return
Beispiel #2
0
    def testBuildEquation(self):

        from numpy import array_equal

        # simple equation
        sin = builder.getBuilder("sin")
        a = builder.ArgumentBuilder(name="a", value=1)
        A = builder.ArgumentBuilder(name="A", value=2)
        x = numpy.arange(0, numpy.pi, 0.1)

        beq = A * sin(a * x)
        eq = beq.getEquation()

        self.assertTrue("a" in eq.argdict)
        self.assertTrue("A" in eq.argdict)
        self.assertTrue(array_equal(eq(), 2 * numpy.sin(x)))

        self.assertEqual(eq.args, [eq.A, eq.a])

        # Check the number of arguments
        self.assertRaises(ValueError, sin)

        # custom function
        def _f(a, b):
            return (a - b) * 1.0 / (a + b)

        f = builder.wrapFunction("f", _f, 2, 1)
        a = builder.ArgumentBuilder(name="a", value=2)
        b = builder.ArgumentBuilder(name="b", value=1)

        beq = sin(f(a, b))
        eq = beq.getEquation()
        self.assertEqual(eq(), numpy.sin(_f(2, 1)))

        # complex function
        sqrt = builder.getBuilder("sqrt")
        e = numpy.e
        _x = numpy.arange(0, 1, 0.05)
        x = builder.ArgumentBuilder(name="x", value=_x, const=True)
        sigma = builder.ArgumentBuilder(name="sigma", value=0.1)
        beq = sqrt(e**(-0.5 * (x / sigma)**2))
        eq = beq.getEquation()
        f = lambda x, sigma: sqrt(e**(-0.5 * (x / sigma)**2))
        self.assertTrue(
            numpy.allclose(eq(), numpy.sqrt(e**(-0.5 * (_x / 0.1)**2))))

        # Equation with Equation
        A = builder.ArgumentBuilder(name="A", value=2)
        B = builder.ArgumentBuilder(name="B", value=4)
        beq = A + B
        eq = beq.getEquation()
        E = builder.wrapOperator("eq", eq)
        eq2 = (2 * E).getEquation()
        # Make sure these evaulate to the same thing
        self.assertEqual(eq.args, [A.literal, B.literal])
        self.assertEqual(2 * eq(), eq2())
        # Pass new arguments to the equation
        C = builder.ArgumentBuilder(name="C", value=5)
        D = builder.ArgumentBuilder(name="D", value=6)
        eq3 = (E(C, D) + 1).getEquation()
        self.assertEqual(12, eq3())
        # Pass old and new arguments to the equation
        # If things work right, A has been given the value of C in the last
        # evaluation (5)
        eq4 = (3 * E(A, D) - 1).getEquation()
        self.assertEqual(32, eq4())
        # Try to pass the wrong number of arguments
        self.assertRaises(ValueError, E, A)
        self.assertRaises(ValueError, E, A, B, C)

        self.assertTrue(noObserversInGlobalBuilders())
        return