예제 #1
0
    def test_csr_matrix_from_single_block(self):

        block_size = 2
        arr = np.arange(block_size ** 2).reshape((block_size, block_size))

        known = np.array([[0, 1], [2, 3]])
        value = sparse_mat.csr_matrix_from_blocks(arr.ravel("c"), block_size, 1)

        self.assertTrue(np.allclose(known, value.toarray()))

        # Larger block
        block_size = 3
        arr = np.arange(block_size ** 2).reshape((block_size, block_size))

        known = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
        value = sparse_mat.csr_matrix_from_blocks(arr.ravel("c"), block_size, 1)

        self.assertTrue(np.allclose(known, value.toarray()))
예제 #2
0
    def test_csr_matrix_from_array(self):

        block_size = 2
        num_blocks = 2
        arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])

        known = np.array([[1, 2, 0, 0], [3, 4, 0, 0], [0, 0, 5, 6], [0, 0, 7, 8]])
        value = sparse_mat.csr_matrix_from_blocks(arr, block_size, num_blocks)

        self.assertTrue(np.allclose(known, value.toarray()))
예제 #3
0
    def test_csr_matrix_from_two_blocks(self):

        block_size = 2
        num_blocks = 2
        full_arr = np.arange((num_blocks * block_size)**2).reshape(
            (num_blocks * block_size, num_blocks * block_size))

        arr = np.array([
            full_arr[:block_size, :block_size], full_arr[block_size:,
                                                         block_size:]
        ])

        known = np.array([[0, 1, 0, 0], [4, 5, 0, 0], [0, 0, 10, 11],
                          [0, 0, 14, 15]])
        value = sparse_mat.csr_matrix_from_blocks(arr.ravel("c"), block_size,
                                                  num_blocks)

        self.assertTrue(np.allclose(known, value.toarray()))