Beispiel #1
0
    def test_diags_vs_diag(self):
        # Check that
        #
        #    diags([a, b, ...], [i, j, ...]) == diag(a, i) + diag(b, j) + ...
        #

        np.random.seed(1234)

        for n_diags in [1, 2, 3, 4, 5, 10]:
            n = 1 + n_diags // 2 + np.random.randint(0, 10)

            offsets = np.arange(-n + 1, n - 1)
            np.random.shuffle(offsets)
            offsets = offsets[:n_diags]

            diagonals = [np.random.rand(n - abs(q)) for q in offsets]

            mat = construct.diags(diagonals, offsets)
            dense_mat = sum(
                [np.diag(x, j) for x, j in zip(diagonals, offsets)])

            assert_array_almost_equal_nulp(mat.toarray(), dense_mat)

            if len(offsets) == 1:
                mat = construct.diags(diagonals[0], offsets[0])
                dense_mat = np.diag(diagonals[0], offsets[0])
                assert_array_almost_equal_nulp(mat.toarray(), dense_mat)
Beispiel #2
0
    def test_diags(self):
        a = array([1, 2, 3, 4, 5])
        b = array([6, 7, 8, 9, 10])
        c = array([11, 12, 13, 14, 15])

        cases = []
        cases.append((a[:1], 0, (1, 1), [[1]]))
        cases.append(([a[:1]], [0], (1, 1), [[1]]))
        cases.append(([a[:1]], [0], (2, 1), [[1], [0]]))
        cases.append(([a[:1]], [0], (1, 2), [[1, 0]]))
        cases.append(([a[:1]], [1], (1, 2), [[0, 1]]))
        cases.append(([a[:2]], [0], (2, 2), [[1, 0], [0, 2]]))
        cases.append(([a[:1]], [-1], (2, 2), [[0, 0], [1, 0]]))
        cases.append(([a[:3]], [0], (3, 4), [[1, 0, 0, 0], [0, 2, 0, 0],
                                             [0, 0, 3, 0]]))
        cases.append(([a[:3]], [1], (3, 4), [[0, 1, 0, 0], [0, 0, 2, 0],
                                             [0, 0, 0, 3]]))
        cases.append(([a[:1]], [-2], (3, 5), [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0],
                                              [1, 0, 0, 0, 0]]))
        cases.append(([a[:2]], [-1], (3, 5), [[0, 0, 0, 0, 0], [1, 0, 0, 0, 0],
                                              [0, 2, 0, 0, 0]]))
        cases.append(([a[:3]], [0], (3, 5), [[1, 0, 0, 0, 0], [0, 2, 0, 0, 0],
                                             [0, 0, 3, 0, 0]]))
        cases.append(([a[:3]], [1], (3, 5), [[0, 1, 0, 0, 0], [0, 0, 2, 0, 0],
                                             [0, 0, 0, 3, 0]]))
        cases.append(([a[:3]], [2], (3, 5), [[0, 0, 1, 0, 0], [0, 0, 0, 2, 0],
                                             [0, 0, 0, 0, 3]]))
        cases.append(([a[:2]], [3], (3, 5), [[0, 0, 0, 1, 0], [0, 0, 0, 0, 2],
                                             [0, 0, 0, 0, 0]]))
        cases.append(([a[:1]], [4], (3, 5), [[0, 0, 0, 0, 1], [0, 0, 0, 0, 0],
                                             [0, 0, 0, 0, 0]]))
        cases.append(([a[:1]], [-4], (5, 3), [[0, 0, 0], [0, 0, 0], [0, 0, 0],
                                              [0, 0, 0], [1, 0, 0]]))
        cases.append(([a[:2]], [-3], (5, 3), [[0, 0, 0], [0, 0, 0], [0, 0, 0],
                                              [1, 0, 0], [0, 2, 0]]))
        cases.append(([a[:3]], [-2], (5, 3), [[0, 0, 0], [0, 0, 0], [1, 0, 0],
                                              [0, 2, 0], [0, 0, 3]]))
        cases.append(([a[:3]], [-1], (5, 3), [[0, 0, 0], [1, 0, 0], [0, 2, 0],
                                              [0, 0, 3], [0, 0, 0]]))
        cases.append(([a[:3]], [0], (5, 3), [[1, 0, 0], [0, 2, 0], [0, 0, 3],
                                             [0, 0, 0], [0, 0, 0]]))
        cases.append(([a[:2]], [1], (5, 3), [[0, 1, 0], [0, 0, 2], [0, 0, 0],
                                             [0, 0, 0], [0, 0, 0]]))
        cases.append(([a[:1]], [2], (5, 3), [[0, 0, 1], [0, 0, 0], [0, 0, 0],
                                             [0, 0, 0], [0, 0, 0]]))

        cases.append(([a[:3], b[:1]], [0, 2], (3, 3), [[1, 0, 6], [0, 2, 0],
                                                       [0, 0, 3]]))
        cases.append(([a[:2], b[:3]], [-1, 0], (3, 4), [[6, 0, 0, 0],
                                                        [1, 7, 0, 0],
                                                        [0, 2, 8, 0]]))
        cases.append(([a[:4], b[:3]], [2, -3], (6, 6), [[0, 0, 1, 0, 0, 0],
                                                        [0, 0, 0, 2, 0, 0],
                                                        [0, 0, 0, 0, 3, 0],
                                                        [6, 0, 0, 0, 0, 4],
                                                        [0, 7, 0, 0, 0, 0],
                                                        [0, 0, 8, 0, 0, 0]]))

        cases.append(([a[:4], b, c[:4]], [-1, 0, 1], (5, 5), [[6, 11, 0, 0, 0],
                                                              [1, 7, 12, 0, 0],
                                                              [0, 2, 8, 13, 0],
                                                              [0, 0, 3, 9, 14],
                                                              [0, 0, 0, 4,
                                                               10]]))
        cases.append(([a[:2], b[:3], c], [-4, 2,
                                          -1], (6, 5), [[0, 0, 6, 0, 0],
                                                        [11, 0, 0, 7, 0],
                                                        [0, 12, 0, 0, 8],
                                                        [0, 0, 13, 0, 0],
                                                        [1, 0, 0, 14, 0],
                                                        [0, 2, 0, 0, 15]]))

        # too long arrays are OK
        cases.append(([a], [0], (1, 1), [[1]]))
        cases.append(([a[:3], b], [0, 2], (3, 3), [[1, 0, 6], [0, 2, 0],
                                                   [0, 0, 3]]))
        cases.append((np.array([[1, 2, 3],
                                [4, 5, 6]]), [0, -1], (3, 3), [[1, 0, 0],
                                                               [4, 2, 0],
                                                               [0, 5, 3]]))

        # scalar case: broadcasting
        cases.append(([1, -2, 1], [1, 0, -1], (3, 3), [[-2, 1, 0], [1, -2, 1],
                                                       [0, 1, -2]]))

        for d, o, shape, result in cases:
            err_msg = "%r %r %r %r" % (d, o, shape, result)
            assert_equal(construct.diags(d, o, shape=shape).toarray(),
                         result,
                         err_msg=err_msg)

            if shape[0] == shape[1] and hasattr(
                    d[0], '__len__') and len(d[0]) <= max(shape):
                # should be able to find the shape automatically
                assert_equal(construct.diags(d, o).toarray(),
                             result,
                             err_msg=err_msg)
Beispiel #3
0
 def test_diags_empty(self):
     x = construct.diags([])
     assert_equal(x.shape, (0, 0))
Beispiel #4
0
 def test_diags_one_diagonal(self):
     d = list(range(5))
     for k in range(-5, 6):
         assert_equal(
             construct.diags(d, k).toarray(),
             construct.diags([d], [k]).toarray())
Beispiel #5
0
 def test_diags_dtype(self):
     x = construct.diags([2.2], [0], shape=(2, 2), dtype=int)
     assert_equal(x.dtype, int)
     assert_equal(x.toarray(), [[2, 0], [0, 2]])
Beispiel #6
0
 def test_diags_default(self):
     a = array([1, 2, 3, 4, 5])
     assert_equal(construct.diags(a).toarray(), np.diag(a))