예제 #1
0
 def test_sqrt(self):
     a = Leaf(9.0)
     c = sqrt(a)
     res = sparse_derivative([c], [a])[0][0].value
     self.assertEqual(res, 1.0 / 6.0)
     del c
     self.assertEqual(a.node.ref_count, 1)
예제 #2
0
    def test_empanadina(self):
        def func(x):
            y = x**6
            J = 6 * x**5
            return y, J

        a = Leaf(2.0)
        b = sqrt(a)

        y = empanadina(func, b)
        self.assertAlmostEqual(y.value, 8.0)
        self.assertAlmostEqual(Derivative([y], [a])[0, 0].value, 12.0)
예제 #3
0
 def _do_calcs(self, a, b):
     av, bv = a.value, b.value
     self.assertEqual((a * b).value, av * bv)
     self.assertEqual((a / b).value, av / bv)
     self.assertEqual((a - b).value, av - bv)
     self.assertEqual((-a).value, -av)
     self.assertEqual((a ** b).value, av ** bv)
     self.assertEqual(sin(a).value, math.sin(av))
     self.assertEqual(cos(a).value, math.cos(av))
     self.assertEqual(tan(a).value, math.tan(av))
     a.value = av = 0.6
     self.assertEqual(asin(a).value, math.asin(av))
     self.assertEqual(acos(a).value, math.acos(av))
     self.assertEqual(atan(a).value, math.atan(av))
     self.assertEqual(sinh(a).value, math.sinh(av))
     self.assertEqual(cosh(a).value, math.cosh(av))
     self.assertEqual(tanh(a).value, math.tanh(av))
     self.assertEqual(sqrt(a).value, math.sqrt(av))
     self.assertEqual(log(a).value, math.log(av))
     self.assertEqual(exp(a).value, math.exp(av))
     self.assertEqual(squ(a).value, av*av)
     self.assertEqual(inv(a).value, 1.0 / av)
예제 #4
0
    def test_empanada(self):
        from math import sin as fsin, cos as fcos, exp as fexp, sqrt as fsqrt

        def func(x):
            a, b = x
            c = fexp(a + b)
            y = [a, a * fsin(b), c]
            J = [[1.0, 0.0], [fsin(b), a * fcos(b)], [c, c]]
            return y, J

        x = Leaf(2.0)
        z = [x * x, sqrt(x)]
        y = empanada(func, z, dim_out=3)

        dydx = Derivative(y, [x]).value
        dydx = [entry[0] for entry in dydx]

        ref = [
            4.0, 4 * fsin(fsqrt(2)) + fsqrt(2) * fcos(fsqrt(2)),
            (4 + 0.5 / fsqrt(2)) * fexp(4 + fsqrt(2))
        ]
        for r1, r2 in zip(dydx, ref):
            self.assertAlmostEqual(r1, r2)