Ejemplo n.º 1
0
class TestIntervalSubtraction(RelaxationTestCase):

    @parameterized.expand([
        [Interval(0, 0), Interval(0, 0), Interval(0, 0)],
        [Interval(1, 1), Interval(1, 1), Interval(0, 0)],
        [Interval(1, 1), Interval(-2, -2), Interval(3, 3)],
        [Interval(-2, 5), Interval(1, 2), Interval(-4, 4)],
    ])
    def test_float_intervals(self, a, b, expected):
        self.assertEqual(expected, a - b)

    @parameterized.expand(zip(sample_intervals(), sample_intervals()))
    def test_ndarray_intervals(self, a, b):
        self.assertAlmostEqualNumpy(a.lower_bound - b.upper_bound, (a - b).lower_bound)
        self.assertAlmostEqualNumpy(a.upper_bound - b.lower_bound, (a - b).upper_bound)
        self.assertSound(a - b, [a, b], lambda p: p[0] - p[1])

    @parameterized.expand([
        [0, Interval(0, 0), Interval(0, 0)],
        [1, Interval(1, 1), Interval(0, 0)],
        [-1, Interval(-2, -2), Interval(1, 1)],
        [2, Interval(-2, 5), Interval(-3, 4)],
    ])
    def test_with_constants(self, a, b, expected):
        self.assertEqual(expected, a - b)

    @parameterized.expand(zip(np.random.uniform(-100, 100, (100, 100)), sample_intervals()))
    def test_with_constant_ndarray(self, a, b):
        self.assertAlmostEqualNumpy(a - b.upper_bound, (a - b).lower_bound)
        self.assertAlmostEqualNumpy(a - b.lower_bound, (a - b).upper_bound)
        self.assertSound(a - b, [b], lambda b: a - b)

        self.assertAlmostEqualNumpy(b.lower_bound - a, (b - a).lower_bound)
        self.assertAlmostEqualNumpy(b.upper_bound - a, (b - a).upper_bound)
        self.assertSound(b - a, [b], lambda b: b - a)

    def test_broadcast(self):
        a = Interval(-1, 1)
        b = np.random.uniform(-100, 100, (100,))

        self.assertAlmostEqualNumpy(-b - 1, (a - b).lower_bound)
        self.assertAlmostEqualNumpy(-b + 1, (a - b).upper_bound)

        self.assertAlmostEqualNumpy(b - 1, (b - a).lower_bound)
        self.assertAlmostEqualNumpy(b + 1, (b - a).upper_bound)

    @parameterized.expand([
        [Interval(0, 0), Interval(0, 0)],
        [Interval(1, 1), Interval(-1, -1)],
        [Interval(-2, 2), Interval(-2, 2)],
        [Interval(-2, 5), Interval(-5, 2)],
    ])
    def test_interval_inversion(self, a, expected):
        self.assertEqual(expected, -a)
Ejemplo n.º 2
0
class TestIntervalSquare(RelaxationTestCase):

    @parameterized.expand([
        [Interval(0, 0), Interval(0, 0)],
        [Interval(1, 1), Interval(1, 1)],
        [Interval(-2, -2), Interval(4, 4)],
        [Interval(2, 3), Interval(4, 9)],
        [Interval(-3, -2), Interval(4, 9)],
        [Interval(-2, 3), Interval(0, 9)]
    ])
    def test_single_interval(self, a: Interval, expected: Interval):
        self.assertAlmostEqual(expected.lower_bound, iv.square(a).lower_bound)
        self.assertAlmostEqual(expected.upper_bound, iv.square(a).upper_bound)
        self.assertSound(iv.square(a), [a], np.square)

    @parameterized.expand([
        [0, 0],
        [1, 1],
        [-1, 1],
        [2, 4],
        [-3, 9],
    ])
    def test_single_scalar(self, a: float, expected: float):
        self.assertAlmostEqual(expected, iv.square(a))

    @parameterized.expand([[i] for i in sample_intervals()])
    def test_nd_interval(self, a):
        self.assertSound(iv.square(a), [a], np.square)

    @parameterized.expand([[i] for i in np.random.uniform(-100, 100, (100, 100))])
    def test_nd_array(self, a):
        self.assertAlmostEqualNumpy(np.square(a), iv.square(a))
Ejemplo n.º 3
0
class TestIntervalCosine(RelaxationTestCase):

    @parameterized.expand([
        [Interval(0, 0), Interval(1, 1)],
        [Interval(np.pi / 2, np.pi / 2), Interval(0, 0)],
        [Interval(-np.pi / 2, -np.pi / 2), Interval(0, 0)],
        [Interval(np.pi * 5 / 2, np.pi * 5 / 2), Interval(0, 0)],
        [Interval(np.pi * 9 / 2, np.pi * 9 / 2), Interval(0, 0)],
        [Interval(0, np.pi / 2), Interval(0, 1)]
    ])
    def test_single_interval(self, a: Interval, expected: Interval):
        self.assertAlmostEqual(expected.lower_bound, iv.cos(a).lower_bound)
        self.assertAlmostEqual(expected.upper_bound, iv.cos(a).upper_bound)
        self.assertSound(iv.cos(a), [a], np.cos)

    @parameterized.expand([
        [0, 1],
        [np.pi / 2, 0],
        [-np.pi / 2, 0],
        [np.pi * 5 / 2, 0],
        [np.pi * 3, -1],
    ])
    def test_single_scalar(self, a: float, expected: float):
        self.assertAlmostEqual(expected, iv.cos(a))

    @parameterized.expand([[i] for i in sample_intervals()])
    def test_nd_interval(self, a):
        self.assertSound(iv.cos(a), [a], np.cos)

    @parameterized.expand([[i] for i in np.random.uniform(-100, 100, (100, 100))])
    def test_nd_array(self, a):
        self.assertAlmostEqualNumpy(np.cos(a), iv.cos(a))
Ejemplo n.º 4
0
class TestIntervalSine(RelaxationTestCase):

    @parameterized.expand([
        [Interval(0, 0), Interval(0, 0)],
        [Interval(np.pi / 2, np.pi / 2), Interval(1, 1)],
        [Interval(-np.pi / 2, -np.pi / 2), Interval(-1, -1)],
        [Interval(np.pi * 5 / 2, np.pi * 5 / 2), Interval(1, 1)],
        [Interval(np.pi * 9 / 2, np.pi * 9 / 2), Interval(1, 1)],
        [Interval(0, np.pi / 2), Interval(0, 1)]
    ])
    def test_single_interval(self, a, expected):
        self.assertEqual(expected, iv.sin(a))
        self.assertSound(iv.sin(a), [a], np.sin)

    @parameterized.expand([
        [0, 0],
        [np.pi / 2, 1],
        [-np.pi / 2, -1],
        [np.pi * 5 / 2, 1],
        [np.pi * 9 / 2, 1],
    ])
    def test_single_scalar(self, a, expected):
        self.assertAlmostEqual(expected, iv.sin(a))

    @parameterized.expand([[i] for i in sample_intervals()])
    def test_nd_interval(self, a):
        self.assertSound(iv.sin(a), [a], np.sin)

    @parameterized.expand([[i] for i in np.random.uniform(-100, 100, (100, 100))])
    def test_nd_array(self, a):
        self.assertAlmostEqualNumpy(np.sin(a), iv.sin(a))
Ejemplo n.º 5
0
class TestIntervalAddition(RelaxationTestCase):

    @parameterized.expand([
        [Interval(0, 0), Interval(0, 0), Interval(0, 0)],
        [Interval(1, 1), Interval(1, 1), Interval(2, 2)],
        [Interval(-1, -1), Interval(-2, -2), Interval(-3, -3)],
        [Interval(-2, 5), Interval(1, 2), Interval(-1, 7)],
    ])
    def test_float_intervals(self, a, b, expected):
        self.assertEqual(expected, a + b)
        self.assertEqual(expected, b + a)

    @parameterized.expand(zip(sample_intervals(), sample_intervals()))
    def test_ndarray_intervals(self, a, b):
        self.assertAlmostEqualNumpy(a.lower_bound + b.lower_bound, (a + b).lower_bound)
        self.assertAlmostEqualNumpy(a.lower_bound + b.lower_bound, (b + a).lower_bound)
        self.assertSound(a + b, [a, b], lambda p: p[0] + p[1])

    @parameterized.expand([
        [0, Interval(0, 0), Interval(0, 0)],
        [1, Interval(1, 1), Interval(2, 2)],
        [-1, Interval(-2, -2), Interval(-3, -3)],
        [2, Interval(-2, 5), Interval(0, 7)],
    ])
    def test_with_constants(self, a, b, expected):
        self.assertEqual(expected, a + b)
        self.assertEqual(expected, b + a)

    @parameterized.expand(zip(np.random.uniform(-100, 100, (100, 100)), sample_intervals()))
    def test_with_constant_ndarray(self, a, b):
        self.assertAlmostEqualNumpy(a + b.lower_bound, (a + b).lower_bound)
        self.assertAlmostEqualNumpy(a + b.lower_bound, (b + a).lower_bound)
        self.assertSound(a + b, [b], lambda b: a + b)

    def test_broadcast_addition(self):
        a = Interval(-1, 1)
        b = np.random.uniform(-100, 100, (100,))

        self.assertAlmostEqualNumpy(b - 1, (a + b).lower_bound)
        self.assertAlmostEqualNumpy(b + 1, (a + b).upper_bound)

        self.assertAlmostEqualNumpy(b - 1, (b + a).lower_bound)
        self.assertAlmostEqualNumpy(b + 1, (b + a).upper_bound)
Ejemplo n.º 6
0
class TestIntervalMultiplication(RelaxationTestCase):

    @parameterized.expand([
        [Interval(0, 0), Interval(0, 0), Interval(0, 0)],
        [Interval(1, 1), Interval(1, 1), Interval(1, 1)],
        [Interval(-1, -1), Interval(-2, -2), Interval(2, 2)],
        [Interval(-2, 2), Interval(-2, 2), Interval(-4, 4)],
        [Interval(-2, 5), Interval(1, 2), Interval(-4, 10)],
    ])
    def test_float_intervals(self, a, b, expected):
        self.assertEqual(expected, a * b)
        self.assertEqual(expected, b * a)

    @parameterized.expand(zip(sample_intervals(), sample_intervals()))
    def test_ndarray_intervals(self, a, b):
        self.assertSound(a * b, [a, b], lambda p: p[0] * p[1])
        self.assertSound(b * a, [a, b], lambda p: p[0] * p[1])

    @parameterized.expand([
        [0, Interval(0, 0), Interval(0, 0)],
        [1, Interval(1, 1), Interval(1, 1)],
        [-1, Interval(-2, -2), Interval(2, 2)],
        [2, Interval(-2, 5), Interval(-4, 10)],
    ])
    def test_with_constants(self, a, b, expected):
        self.assertEqual(expected, a * b)
        self.assertEqual(expected, b * a)

    @parameterized.expand(zip(np.random.uniform(-100, 100, (100, 100)), sample_intervals()))
    def test_with_constant_ndarray(self, a, b):
        self.assertSound(a * b, [b], lambda b: a * b)
        self.assertSound(b * a, [b], lambda b: a * b)

    def test_broadcast_addition(self):
        a = Interval(-1., 1.)
        b = np.random.uniform(-100, 100, (100,))

        self.assertAlmostEqualNumpy(np.minimum(-b, b), (a * b).lower_bound)
        self.assertAlmostEqualNumpy(np.maximum(-b, b), (a * b).upper_bound)

        self.assertAlmostEqualNumpy(np.minimum(-b, b), (b * a).lower_bound)
        self.assertAlmostEqualNumpy(np.maximum(-b, b), (b * a).upper_bound)