Exemplo n.º 1
0
class MatrixMultiplicationTestCase(unittest.TestCase):
    def setUp(self):
        self.matrix_A = RowMatrix(matrix_rdd,'test_data',1000,100)
        self.matrix_A2 = RowMatrix(matrix_rdd2,'test_data',100,1000)

    def test_mat_rtimes(self):
        mat = np.random.rand(100,50)
        p = self.matrix_A.rtimes(mat)
        p_true = np.dot( A, mat )
        self.assertTrue( np.linalg.norm(p-p_true)/np.linalg.norm(p_true) < 1e-5 )

    def test_mat_ltimes(self):
        mat = np.random.rand(100,1000)
        p = self.matrix_A.ltimes(mat)
        p_true = np.dot( mat,A )
        self.assertTrue( np.linalg.norm(p-p_true)/np.linalg.norm(p_true) < 1e-5 )

    def test_atamat(self):
        mat = np.random.rand(100,20)
        p = self.matrix_A.atamat(mat)
        p_true = np.dot( A.T, np.dot(A, mat) )
        self.assertTrue( np.linalg.norm(p-p_true)/np.linalg.norm(p_true) < 1e-5 )

    def test_mat_rtimes2(self):
        mat = np.random.rand(1000,50)
        p = self.matrix_A2.rtimes(mat)
        p_true = np.dot( A2, mat )
        self.assertTrue( np.linalg.norm(p-p_true)/np.linalg.norm(p_true) < 1e-5 )

    def test_mat_ltimes2(self):
        mat = np.random.rand(50,100)
        p = self.matrix_A2.ltimes(mat)
        p_true = np.dot( mat,A2 )
        self.assertTrue( np.linalg.norm(p-p_true)/np.linalg.norm(p_true) < 1e-5 )

    def test_atamat2(self):
        mat = np.random.rand(1000,20)
        p = self.matrix_A2.atamat(mat)
        p_true = np.dot( A2.T, np.dot(A2, mat) )
        self.assertTrue( np.linalg.norm(p-p_true)/np.linalg.norm(p_true) < 1e-5 )

    def test_mat_rtimes_sub(self):
        mat = np.random.rand(99,50)
        p = self.matrix_A.rtimes(mat, (0,98))
        p_true = np.dot( A[:,:-1], mat )
        self.assertTrue( np.linalg.norm(p-p_true)/np.linalg.norm(p_true) < 1e-5 )

    def test_mat_ltimes_sub(self):
        mat = np.random.rand(100,1000)
        p = self.matrix_A.ltimes(mat, (0,98))
        p_true = np.dot( mat,A[:,:-1] )
        self.assertTrue( np.linalg.norm(p-p_true)/np.linalg.norm(p_true) < 1e-5 )

    def test_atamat_sub(self):
        mat = np.random.rand(99,50)
        p = self.matrix_A.atamat(mat, (0,98))
        p_true = np.dot( A[:,:-1].T, np.dot(A[:,:-1], mat) )
        self.assertTrue( np.linalg.norm(p-p_true)/np.linalg.norm(p_true) < 1e-5 )
Exemplo n.º 2
0
class MatrixMultiplicationTestCase(unittest.TestCase):
    def setUp(self):
        self.matrix_A = RowMatrix(matrix_rdd, 'test_data', 1000, 100)
        self.matrix_A2 = RowMatrix(matrix_rdd2, 'test_data', 100, 1000)

    def test_mat_rtimes(self):
        mat = np.random.rand(100, 50)
        p = self.matrix_A.rtimes(mat)
        p_true = np.dot(A, mat)
        self.assertTrue(
            np.linalg.norm(p - p_true) / np.linalg.norm(p_true) < 1e-5)

    def test_mat_ltimes(self):
        mat = np.random.rand(100, 1000)
        p = self.matrix_A.ltimes(mat)
        p_true = np.dot(mat, A)
        self.assertTrue(
            np.linalg.norm(p - p_true) / np.linalg.norm(p_true) < 1e-5)

    def test_atamat(self):
        mat = np.random.rand(100, 20)
        p = self.matrix_A.atamat(mat)
        p_true = np.dot(A.T, np.dot(A, mat))
        self.assertTrue(
            np.linalg.norm(p - p_true) / np.linalg.norm(p_true) < 1e-5)

    def test_mat_rtimes2(self):
        mat = np.random.rand(1000, 50)
        p = self.matrix_A2.rtimes(mat)
        p_true = np.dot(A2, mat)
        self.assertTrue(
            np.linalg.norm(p - p_true) / np.linalg.norm(p_true) < 1e-5)

    def test_mat_ltimes2(self):
        mat = np.random.rand(50, 100)
        p = self.matrix_A2.ltimes(mat)
        p_true = np.dot(mat, A2)
        self.assertTrue(
            np.linalg.norm(p - p_true) / np.linalg.norm(p_true) < 1e-5)

    def test_atamat2(self):
        mat = np.random.rand(1000, 20)
        p = self.matrix_A2.atamat(mat)
        p_true = np.dot(A2.T, np.dot(A2, mat))
        self.assertTrue(
            np.linalg.norm(p - p_true) / np.linalg.norm(p_true) < 1e-5)

    def test_mat_rtimes_sub(self):
        mat = np.random.rand(99, 50)
        p = self.matrix_A.rtimes(mat, (0, 98))
        p_true = np.dot(A[:, :-1], mat)
        self.assertTrue(
            np.linalg.norm(p - p_true) / np.linalg.norm(p_true) < 1e-5)

    def test_mat_ltimes_sub(self):
        mat = np.random.rand(100, 1000)
        p = self.matrix_A.ltimes(mat, (0, 98))
        p_true = np.dot(mat, A[:, :-1])
        self.assertTrue(
            np.linalg.norm(p - p_true) / np.linalg.norm(p_true) < 1e-5)

    def test_atamat_sub(self):
        mat = np.random.rand(99, 50)
        p = self.matrix_A.atamat(mat, (0, 98))
        p_true = np.dot(A[:, :-1].T, np.dot(A[:, :-1], mat))
        self.assertTrue(
            np.linalg.norm(p - p_true) / np.linalg.norm(p_true) < 1e-5)