コード例 #1
0
    def test_qr_with_padding(self, m_shape, b_shape):
        """Tests qr_blocked with padding"""
        np.set_printoptions(precision=2)
        np.random.seed(8)

        m2b_ds = random_array(m_shape, b_shape)

        (q, r) = qr(m2b_ds, mode='full')

        q = q.collect()
        r = r.collect()

        # check if Q matrix is orthogonal
        self.assertTrue(np.allclose(q.dot(q.T), np.identity(m_shape[0])))
        # check if R matrix is upper triangular
        self.assertTrue(np.allclose(np.triu(r), r))
        # check if the product Q * R is the original matrix
        self.assertTrue(np.allclose(q.dot(r), m2b_ds.collect()))

        (q, r) = qr(m2b_ds, mode="economic")

        q = q.collect()
        r = r.collect()
        m2b = m2b_ds.collect()

        # check if Q matrix is orthogonal
        self.assertTrue(np.allclose(q.T.dot(q), np.identity(m_shape[1])))
        # check if R matrix is upper triangular
        self.assertTrue(np.allclose(np.triu(r), r))
        # check if the product Q * R is the original matrix
        self.assertTrue(np.allclose(q.dot(r), m2b))
        # check the dimensions of Q
        self.assertTrue(q.shape == (m_shape[0], m_shape[1]))
        # check the dimensions of R
        self.assertTrue(r.shape == (m_shape[1], m_shape[1]))
コード例 #2
0
    def test_economic_overwrite(self):
        """Tests economic qr with overwrite = True"""
        np.set_printoptions(precision=2)
        np.random.seed(8)
        m_size = 25
        n_size = 10
        b_size = 5
        m2b_ds = random_array((m_size, n_size), (b_size, b_size))
        (q, r) = qr(m2b_ds, mode="economic", overwrite_a=True)

        q = q.collect()
        r = r.collect()
        m2b = m2b_ds.collect()

        # check if arrays original and r arrays are different
        # as it is not possible to overwrite the original matrix in the
        # economic mode
        self.assertFalse(np.array_equal(m2b, r))

        # check if Q matrix is orthogonal
        self.assertTrue(np.allclose(q.T.dot(q), np.identity(n_size)))
        # check if R matrix is upper triangular
        self.assertTrue(np.allclose(np.triu(r), r))
        # check if the product Q * R is the original matrix
        self.assertTrue(np.allclose(q.dot(r), m2b))
        # check the dimensions of Q
        self.assertTrue(q.shape == (m_size, n_size))
        # check the dimensions of R
        self.assertTrue(r.shape == (n_size, n_size))
コード例 #3
0
    def test_exceptions(self):
        m2b_ds = random_array((100, 100), (10, 10))
        with self.assertRaises(ValueError):
            # unsupported mode
            qr(m2b_ds, mode="x")

        m2b_ds = random_array((50, 100), (10, 10))
        with self.assertRaises(ValueError):
            # m > n is required for matrices m x n
            qr(m2b_ds)

        m2b_ds = random_array((100, 100), (10, 5))
        with self.assertRaises(ValueError):
            # Square blocks are required
            qr(m2b_ds)

        m2b_ds = random_array((100, 100), (10, 10))
        m2b_ds = m2b_ds[:, 0:5]
        with self.assertRaises(ValueError):
            qr(m2b_ds)
コード例 #4
0
    def test_special_cases(self):
        """Tests special cases not covered in other tests"""
        np.set_printoptions(precision=2)
        np.random.seed(8)

        # testing a block transpose
        a = None
        a_type = [[ZEROS]]
        a_transposed_type, a = _transpose_block(a, a_type)
        self.assertEqual(a_type, a_transposed_type)
        self.assertIsNone(a)

        a = None
        a_type = [[IDENTITY]]
        a_transposed_type, a = _transpose_block(a, a_type)
        self.assertEqual(a_type, a_transposed_type)
        self.assertIsNone(a)

        # testing array validation
        a = random_array((10, 10), (2, 5))
        with self.assertRaises(ValueError):
            _validate_ds_array(a)

        # testing transpose dot
        a = np.random.rand(3, 2)
        b = np.random.rand(2, 5)
        a_dot_b_transposed = compss_wait_on(
            _dot_task(a, b, transpose_result=True))
        self.assertTrue(
            np.allclose(np.transpose(np.dot(a, b)), a_dot_b_transposed))

        # testing qr of zeros or identity
        b_size = (5, 5)
        a = None
        q, r = _qr_task(a, np.full((1, 1), ZEROS), b_size)
        q = compss_wait_on(q)
        r = compss_wait_on(r)
        q_numpy, r_numpy = qr_numpy(np.zeros(b_size), mode='reduced')
        self.assertTrue(np.allclose(q_numpy, q))
        self.assertTrue(np.allclose(r_numpy, r))
        q, r = _qr_task(a, np.full((1, 1), IDENTITY), b_size)
        q = compss_wait_on(q)
        r = compss_wait_on(r)
        q_numpy, r_numpy = qr_numpy(np.identity(b_size[0]), mode='reduced')
        self.assertTrue(np.allclose(q_numpy, q))
        self.assertTrue(np.allclose(r_numpy, r))
コード例 #5
0
    def test_qr_r(self, m_size, n_size, b_size):
        """Tests qr_blocked r mode"""
        np.set_printoptions(precision=2)
        np.random.seed(8)

        shape = (m_size * b_size, n_size * b_size)
        m2b_ds = random_array(shape, (b_size, b_size))

        r = qr(m2b_ds, mode="r")
        _, r_full = qr(m2b_ds, mode="full")

        r = r.collect()
        r_full = r_full.collect()

        # check if R matrix is upper triangular
        self.assertTrue(np.allclose(np.triu(r), r))
        # check if R matrix is the same as when the full mode is applied
        self.assertTrue(np.allclose(r, r_full))
コード例 #6
0
    def test_exceptions(self):
        """Tests exceptions thrown by utility functions"""
        np.random.seed(8)

        a = random_array((20, 20), (6, 6))
        with self.assertRaises(NotImplementedError):
            pad(a, ((2, 0), (0, 0)))

        with self.assertRaises(NotImplementedError):
            pad(a, ((0, 0), (2, 0)))

        with self.assertRaises(NotImplementedError):
            pad(a, ((0, 8), (0, 0)))

        with self.assertRaises(NotImplementedError):
            pad(a, ((0, 0), (0, 8)))

        with self.assertRaises(ValueError):
            remove_last_columns(a, 8)
コード例 #7
0
    def test_qr(self, m_size, n_size, b_size):
        """Tests qr_blocked full mode"""
        np.set_printoptions(precision=2)
        np.random.seed(8)

        shape = (m_size * b_size, n_size * b_size)
        m2b_ds = random_array(shape, (b_size, b_size))

        (q, r) = qr(m2b_ds)

        q = q.collect()
        r = r.collect()
        m2b = m2b_ds.collect()

        # check if Q matrix is orthogonal
        self.assertTrue(np.allclose(q.dot(q.T), np.identity(m_size * b_size)))
        # check if R matrix is upper triangular
        self.assertTrue(np.allclose(np.triu(r), r))
        # check if the product Q * R is the original matrix
        self.assertTrue(np.allclose(q.dot(r), m2b))
コード例 #8
0
    def test_pad_with_zeros(self, shape, block_size):
        """Tests dislib.data.util.pad_last_blocks_with_zeros"""
        np.random.seed(8)

        a = random_array(shape, block_size)
        a_original = a.copy()

        bottom = 0 if shape[0] % block_size[0] == 0 else block_size[
            0] - shape[0] % block_size[0]
        right = 0 if shape[1] % block_size[1] == 0 else block_size[
            1] - shape[1] % block_size[1]

        pad_last_blocks_with_zeros(a)

        a_np = a.collect()
        # check if original content is the same
        self.assertTrue(
            np.allclose(a_np[0:shape[0], 0:shape[1]], a_original.collect()))
        # check if bottom rows are added correctly
        self.assertTrue(
            np.allclose(a_np[shape[0]:shape[0] + bottom, :],
                        np.full((bottom, shape[1] + right), 0)))
        # check if right columns are added correctly
        self.assertTrue(
            np.allclose(a_np[0:shape[0] + bottom, shape[1]:shape[1] + right],
                        np.full((shape[0] + bottom, right), 0)))
        # check changed bottom blocks
        for i in range(len(a._blocks[-1])):
            self.assertTrue(a._blocks[-1][i].shape == a._reg_shape)
        # check changed right blocks
        for i in range(len(a._blocks)):
            self.assertTrue(a._blocks[i][-1].shape == a._reg_shape)
        # check top-left shape
        if shape[0] < block_size[0]:
            self.assertTrue(a._top_left_shape[0] == a._reg_shape[0])
        if shape[1] < block_size[1]:
            self.assertTrue(a._top_left_shape[1] == a._reg_shape[1])
コード例 #9
0
    def test_compute_bottom_right_shape(self, shape, block_size,
                                        desired_br_shape):
        np.random.seed(8)
        a = random_array(shape, block_size)

        self.assertTrue(compute_bottom_right_shape(a), desired_br_shape)