Пример #1
0
    def test_multiply_larger(self):
        data = np.array([[[2, 1, 2],
                         [3, 7, 4]],
                        [[1, 1, 3],
                         [4, 9, 10]]])
        af = DiscreteFactor([(0, 2), (3, 2), (12, 3)], data=data)
        a = DiscreteBelief(af)

        data = np.array([[2, 3, 1],
                         [5, 1, 7]])
        b = DiscreteFactor([(0, 2), (12, 3)], data=data)
        data = np.array([[[2 * 2, 1 * 3, 2 * 1],
                          [3 * 2, 7 * 3, 4 * 1]],
                         [[1 * 5, 1 * 1, 3 * 7],
                         [4 * 5, 9 * 1, 10 * 7]]])
        c = DiscreteFactor([(0, 2), (3, 2), (12, 3)], data=data)

        multiply(a, b)

        print a.data
        print c.data
        print a.data.shape
        print c.data.shape
        self.assertEqual(a.variables, c.variables)
        self.assertEqual(a.axis_to_variable, c.axis_to_variable)
        assert_array_almost_equal(a.data, c.data)
Пример #2
0
    def test_divide_small(self):
        a = DiscreteFactor([(0, 2), (1, 2)], data=np.array([[1.0, 2], [5, 6]]))
        b = DiscreteFactor([(1, 2)], data=np.array([2.0, 3]))
        data = np.array([[1.0 / 2.0, 2.0 / 3.0],
                         [5.0 / 2.0, 6.0 / 3.0]])
        c = DiscreteFactor([(0, 2), (1, 2)], data=data)
        multiply(a, b, divide=True)

        print a.data
        print c.data
        print a.data.shape
        print c.data.shape
        self.assertEqual(a.variables, c.variables)
        self.assertEqual(a.axis_to_variable, c.axis_to_variable)
        assert_array_almost_equal(a.data, c.data)
Пример #3
0
    def test_multiply_small_a(self):
        data = np.array([[1, 2],
                         [5, 6]], dtype='float64')
        af = DiscreteFactor([(0, 2), (1, 2)], data=data)
        a = DiscreteBelief(af)

        data = np.array([2, 3])
        e = DiscreteFactor([(0, 2)], data=data)
        data = np.array([[1 * 2, 2 * 2],
                         [5 * 3, 6 * 3]])
        f = DiscreteFactor([(0, 2), (1, 2)], data=data)

        multiply(a, e)

        print 'a', a.data
        print 'e', e.data
        print
        print f.data
        print a.data.shape
        print f.data.shape
        self.assertEqual(a.variables, f.variables)
        self.assertEqual(a.axis_to_variable, f.axis_to_variable)
        assert_array_almost_equal(a.data, f.data)
Пример #4
0
    def test_multiply_small_inplace(self):
        data = np.array([[1, 2],
                         [5, 6]])
        af = DiscreteFactor([(0, 2), (1, 2)], data=data)
        a = DiscreteBelief(af)

        data = np.array([2, 3])
        bf = DiscreteFactor([(1, 2)], data=data)
        b = DiscreteBelief(bf)

        data = np.array([[2, 6],
                         [10, 18]])
        c = DiscreteFactor([(0, 2), (1, 2)], data=data)

        multiply(a, b)

        print a.data
        print c.data
        print a.data.shape
        print c.data.shape
        print af.data
        self.assertEqual(a.variables, c.variables)
        self.assertEqual(a.axis_to_variable, c.axis_to_variable)
        assert_array_almost_equal(a.data, c.data)