Exemple #1
0
 def test_exp(self):
     self.assertEqual(imath.exp(0), interval[1])
     self.assertEqual(imath.exp(1), interval[math.e,
                                             helpers.nudge(math.e, +1)])
     self.assertEqual(
         imath.exp(interval([-fpu.infinity, 0], [1, fpu.infinity])),
         interval([0, 1], [math.e, fpu.infinity]))
Exemple #2
0
 def test_cosh(self):
     self.assertEqual(imath.cosh(0), interval[1])
     assert imath.cosh(2) in (imath.exp(2) + imath.exp(-2))/2
     assert imath.cosh(interval[1, 2]) == interval.hull((imath.cosh(1), imath.cosh(2)))
     assert imath.cosh(interval[-2, -1])  == imath.cosh(interval[1, 2])
     assert imath.cosh(interval[-2, 1]) == interval.hull((interval[1], imath.cosh(2)))
     assert imath.cosh(interval[-1, 2]) == interval.hull((interval[1], imath.cosh(2)))
Exemple #3
0
    def test_exp(self):
        z = interval[-100, 100].newton(lambda x: imath.exp(x) + x,
                                       lambda x: imath.exp(x) + 1)
        assert z == interval[-0.56714329040978395,
                             helpers.nudge(-0.56714329040978395, +1)]

        w = interval[-100, 100].newton(lambda x: imath.exp(-x) * x + 1,
                                       lambda x: imath.exp(-x) * (1 - x))
        assert z == w
Exemple #4
0
 def test_cosh(self):
     self.assertEqual(imath.cosh(0), interval[1])
     assert imath.cosh(2) in (imath.exp(2) + imath.exp(-2)) / 2
     assert imath.cosh(interval[1, 2]) == interval.hull(
         (imath.cosh(1), imath.cosh(2)))
     assert imath.cosh(interval[-2, -1]) == imath.cosh(interval[1, 2])
     assert imath.cosh(interval[-2, 1]) == interval.hull(
         (interval[1], imath.cosh(2)))
     assert imath.cosh(interval[-1, 2]) == interval.hull(
         (interval[1], imath.cosh(2)))
Exemple #5
0
 def test_log(self):
     self.assertEqual(imath.log(1), interval[0])
     self.assertEqual(imath.log(0), interval[-fpu.infinity])
     self.assertEqual(imath.log(imath.exp(1)), interval[helpers.nudge(1, -1), helpers.nudge(1, +1)])
     self.assertEqual(imath.log(0), interval[-fpu.infinity])
     self.assertEqual(imath.log(interval[0, 1]), interval[-fpu.infinity, 0])
     self.assertEqual(imath.log(interval[-1, 1]), interval[-fpu.infinity, 0])
     self.assertEqual(imath.log(interval[-2, -1]), interval())
Exemple #6
0
 def test_log(self):
     assert imath.log(1)                == interval[0]
     assert imath.log(0)                == interval[-fpu.infinity]
     assert imath.log(imath.exp(1))     == interval[helpers.nudge(1, -1), helpers.nudge(1, +1)]
     assert imath.log(0)                == interval[-fpu.infinity]
     assert imath.log(interval[0, 1])   == interval[-fpu.infinity, 0]
     assert imath.log(interval[-1, 1])  == interval[-fpu.infinity, 0]
     assert imath.log(interval[-2, -1]) == interval()
Exemple #7
0
 def test_log(self):
     assert imath.log(1) == interval[0]
     assert imath.log(0) == interval[-fpu.infinity]
     assert imath.log(imath.exp(1)) == interval[helpers.nudge(1, -1),
                                                helpers.nudge(1, +1)]
     assert imath.log(0) == interval[-fpu.infinity]
     assert imath.log(interval[0, 1]) == interval[-fpu.infinity, 0]
     assert imath.log(interval[-1, 1]) == interval[-fpu.infinity, 0]
     assert imath.log(interval[-2, -1]) == interval()
Exemple #8
0
 def test_log(self):
     self.assertEqual(imath.log(1), interval[0])
     self.assertEqual(imath.log(0), interval[-fpu.infinity])
     self.assertEqual(imath.log(imath.exp(1)),
                      interval[helpers.nudge(1, -1),
                               helpers.nudge(1, +1)])
     self.assertEqual(imath.log(0), interval[-fpu.infinity])
     self.assertEqual(imath.log(interval[0, 1]), interval[-fpu.infinity, 0])
     self.assertEqual(imath.log(interval[-1, 1]), interval[-fpu.infinity,
                                                           0])
     self.assertEqual(imath.log(interval[-2, -1]), interval())
Exemple #9
0
    def __fwd_eval(self, n_id, box):
        n = self.dag[n_id]

        fwd = self.__fwd
        rec = self.__fwd_eval

        if n[0] == '+':
            rec(n[1], box)
            rec(n[2], box)
            fwd[n_id] = fwd[n[1]] + fwd[n[2]]
        elif n[0] == '-':
            rec(n[1], box)
            rec(n[2], box)
            fwd[n_id] = fwd[n[1]] - fwd[n[2]]
        elif n[0] == '*':
            rec(n[1], box)
            rec(n[2], box)
            fwd[n_id] = fwd[n[1]] * fwd[n[2]]
        elif n[0] == '/':
            rec(n[1], box)
            rec(n[2], box)
            fwd[n_id] = fwd[n[1]] / fwd[n[2]]

        elif n[0] == '^':
            rec(n[1], box)
            i = self.dag[n[2]][1]
            fwd[n_id] = fwd[n[1]]**i

        elif n[0] == 'exp':
            rec(n[1], box)
            fwd[n_id] = imath.exp(fwd[n[1]])

        elif n[0] == 'sqrt':
            rec(n[1], box)
            fwd[n_id] = imath.sqrt(fwd[n[1]])

        elif n[0] == 'sin':
            rec(n[1], box)
            fwd[n_id] = imath.sin(fwd[n[1]])

        elif n[0] == 'cos':
            rec(n[1], box)
            fwd[n_id] = imath.cos(fwd[n[1]])

        elif n[0] == 'C':
            fwd[n_id] = interval[n[1]]
        elif n[0] == 'V':
            fwd[n_id] = box[n[1]]
        else:
            print('unsupported node: ' + n)
            assert (False)
Exemple #10
0
 def test_exp(self):
     self.assertEqual(imath.exp(0), interval[1])
     self.assertEqual(imath.exp(1), interval[math.e, helpers.nudge(math.e, +1)])
     self.assertEqual(imath.exp(interval([-fpu.infinity, 0], [1, fpu.infinity])),
                      interval([0, 1], [math.e, fpu.infinity]))
Exemple #11
0
    def test_exp(self):
        z = interval[-100, 100].newton(lambda x: imath.exp(x) + x, lambda x: imath.exp(x) + 1)
        assert z == interval[-0.56714329040978395, helpers.nudge(-0.56714329040978395, +1)]

        w = interval[-100, 100].newton(lambda x: imath.exp(-x)*x + 1, lambda x: imath.exp(-x)*(1-x))
        assert z == w
Exemple #12
0
 def test_sinh(self):
     self.assertEqual(imath.sinh(0), interval[0])
     assert imath.sinh(1) in ((imath.exp(1) - imath.exp(-1))/2)
Exemple #13
0
 def test_exp(self):
     assert imath.exp(0) == interval[1]
     assert imath.exp(1) == interval[math.e, helpers.nudge(math.e, +1)]
     assert (
         imath.exp(interval([-fpu.infinity, 0], [1, fpu.infinity])) ==
         interval([0, 1], [math.e, fpu.infinity]))
Exemple #14
0
 def test_sinh(self):
     assert imath.sinh(0) ==  interval[0]
     assert imath.sinh(1) in ((imath.exp(1) - imath.exp(-1)) / 2)
Exemple #15
0
 def test_expm1(self):
     assert imath.expm1(0) == interval[0]
     assert imath.expm1(1) in (imath.exp(1) - 1)
Exemple #16
0
 def test_expm1(self):
     assert imath.expm1(0) == interval[0]
     assert imath.expm1(1) in (imath.exp(1) - 1)
Exemple #17
0
 def test_expm1(self):
     self.assertEqual(imath.expm1(0), interval[0])
     assert imath.expm1(1) in (imath.exp(1) - 1)
Exemple #18
0
 def test_sinh(self):
     assert imath.sinh(0) == interval[0]
     assert imath.sinh(1) in ((imath.exp(1) - imath.exp(-1)) / 2)
Exemple #19
0
 def test_sinh(self):
     self.assertEqual(imath.sinh(0), interval[0])
     assert imath.sinh(1) in ((imath.exp(1) - imath.exp(-1)) / 2)
Exemple #20
0
 def test_expm1(self):
     self.assertEqual(imath.expm1(0), interval[0])
     assert imath.expm1(1) in (imath.exp(1) - 1)
Exemple #21
0
    def __eval(self, n_id, box):
        n = self.__dag[n_id]

        rec = self.__eval

        if n[0] == '+':
            v1 = rec(n[1], box)
            v2 = rec(n[2], box)
            return v1 + v2
        elif n[0] == '-':
            v1 = rec(n[1], box)
            v2 = rec(n[2], box)
            return v1 - v2
        elif n[0] == '*':
            v1 = rec(n[1], box)
            v2 = rec(n[2], box)
            return v1 * v2
        elif n[0] == '/':
            v1 = rec(n[1], box)
            v2 = rec(n[2], box)
            return v1 / v2

        elif n[0] == '^':
            v = rec(n[1], box)
            i = self.__dag[n[2]][1]
            return v**i

        elif n[0] == 'exp':
            v = rec(n[1], box)
            return imath.exp(v)

        elif n[0] == 'log':
            v = rec(n[1], box)
            return imath.log(v)

        elif n[0] == 'sqrt':
            v = rec(n[1], box)
            return imath.sqrt(v)

        elif n[0] == 'sin':
            v = rec(n[1], box)
            return imath.sin(v)

        elif n[0] == 'cos':
            v = rec(n[1], box)
            return imath.cos(v)

        elif n[0] == 'tan':
            v = rec(n[1], box)
            return imath.tan(v)

        elif n[0] == 'asin':
            v = rec(n[1], box)
            return imath.asin(v)

        elif n[0] == 'acos':
            v = rec(n[1], box)
            return imath.acos(v)

        elif n[0] == 'atan':
            v = rec(n[1], box)
            return imath.atan(v)

        elif n[0] == 'sinh':
            v = rec(n[1], box)
            return imath.sinh(v)

        elif n[0] == 'cosh':
            v = rec(n[1], box)
            return imath.cosh(v)

        elif n[0] == 'tanh':
            v = rec(n[1], box)
            return imath.tanh(v)

        elif n[0] == 'C':
            return interval[n[1]]
        elif n[0] == 'V':
            return box[n[1]]
        else:
            print('unsupported node: ' + str(n))
            assert (False)
Exemple #22
0
def pow(x, n):
    return imath.exp(n * imath.log(x))
Exemple #23
0
 def test_exp(self):
     assert imath.exp(0) == interval[1]
     assert imath.exp(1) == interval[math.e, helpers.nudge(math.e, +1)]
     assert (imath.exp(interval([-fpu.infinity, 0],
                                [1, fpu.infinity])) == interval(
                                    [0, 1], [math.e, fpu.infinity]))