Пример #1
0
def test_kind():
    assert Matrix([[1, 2], [3, 4]]).kind == MatrixKind(NumberKind)
    assert Matrix([[0, 0], [0, 0]]).kind == MatrixKind(NumberKind)
    assert Matrix(0, 0, []).kind == MatrixKind(NumberKind)
    assert Matrix([[x]]).kind == MatrixKind(NumberKind)
    assert Matrix([[1, Matrix([[1]])]]).kind == MatrixKind(UndefinedKind)
    assert SparseMatrix([[1]]).kind == MatrixKind(NumberKind)
    assert SparseMatrix([[1, Matrix([[1]])]]).kind == MatrixKind(UndefinedKind)
Пример #2
0
def test_hermitian():
    x = Symbol('x')
    a = SparseMatrix([[0, I], [-I, 0]])
    assert a.is_hermitian
    a = SparseMatrix([[1, I], [-I, 1]])
    assert a.is_hermitian
    a[0, 0] = 2 * I
    assert a.is_hermitian is False
    a[0, 0] = x
    assert a.is_hermitian is None
    a[0, 1] = a[1, 0] * I
    assert a.is_hermitian is False
Пример #3
0
def test_CL_RL():
    assert SparseMatrix(((1, 2), (3, 4))).row_list() == [
        (0, 0, 1),
        (0, 1, 2),
        (1, 0, 3),
        (1, 1, 4),
    ]
    assert SparseMatrix(((1, 2), (3, 4))).col_list() == [
        (0, 0, 1),
        (1, 0, 3),
        (0, 1, 2),
        (1, 1, 4),
    ]
Пример #4
0
def PartialTrace(expr, n):  ##calculate the partial trace of the n_th photon

    dictadd = collect(expr, [HH[l1, l2, l3, l4, l5, l6]], evaluate=False)
    TermsCoeff = list(dictadd.items())

    ParticleOne = []
    ParticleTwo = []
    ## get the size of the matrix
    for ii in range(len(TermsCoeff)):
        HHList = TermsCoeff[ii][0]
        if HHList.indices[n - 1] == HHList.indices[n + 2]:
            ll = [
                HHList.indices[0], HHList.indices[1], HHList.indices[2],
                HHList.indices[3], HHList.indices[4], HHList.indices[5]
            ]
            del (
                ll[n - 1], ll[n + 1]
            )  ## because cannot del all at the same time, thus do it one by one, the index is not n+2
            ParticleOne.append(ll[0])
            ParticleTwo.append(ll[1])
    # start from 0
    Upperone = max(ParticleOne) + 1
    Lowerone = min(min(ParticleOne), 0)
    Uppertwo = max(ParticleTwo) + 1
    Lowertwo = min(min(ParticleTwo), 0)
    rangeP1 = Upperone - Lowerone
    rangeP2 = Uppertwo - Lowertwo

    Msize = (rangeP1 * rangeP2)
    SMatrix = SparseMatrix(Msize, Msize, {(0, 0): 0})

    for ii in range(len(TermsCoeff)):
        HHList = TermsCoeff[ii][0]
        if HHList.indices[n - 1] == HHList.indices[n + 2]:
            ll = [
                HHList.indices[0], HHList.indices[1], HHList.indices[2],
                HHList.indices[3], HHList.indices[4], HHList.indices[5]
            ]
            del (
                ll[n - 1], ll[n + 1]
            )  ## because cannot del all at the same time, thus do it one by one, the index is not n+2
            #       print('rest: ',ll)
            #       print('rest: ',ll[0]-Lowerone,'',ll[1]-Lowertwo, '',ll[2]-Lowerone,'',ll[3]-Lowertwo)
            Dimrow = (ll[0] - Lowerone) * rangeP2 + (ll[1] - Lowertwo)
            Dimcol = (ll[2] - Lowerone) * rangeP2 + (ll[3] - Lowertwo)
            SMatrix = SparseMatrix(
                Msize, Msize, {(Dimrow, Dimcol): TermsCoeff[ii][1]}) + SMatrix
    return SMatrix.rank()
Пример #5
0
def symchol(M):  # symbolic Cholesky
    B = SparseMatrix(M)
    t = B.row_structure_symbolic_cholesky()
    B = np.asarray(B) * 0
    for i in range(B.shape[0]):
        B[i, t[i]] = 1
    return B
Пример #6
0
    def tomatrix(self):
        """
        Converts MutableDenseNDimArray to Matrix. Can convert only 2-dim array, else will raise error.

        Examples
        ========

        >>> from sympy import MutableSparseNDimArray
        >>> a = MutableSparseNDimArray([1 for i in range(9)], (3, 3))
        >>> b = a.tomatrix()
        >>> b
        Matrix([
        [1, 1, 1],
        [1, 1, 1],
        [1, 1, 1]])
        """
        from sympy.matrices import SparseMatrix
        if self.rank() != 2:
            raise ValueError('Dimensions must be of size of 2')

        mat_sparse = {}
        for key, value in self._sparse_array.items():
            mat_sparse[self._get_tuple_index(key)] = value

        return SparseMatrix(self.shape[0], self.shape[1], mat_sparse)
Пример #7
0
def test_MatrixElement_with_values():
    x, y, z, w = symbols("x y z w")
    M = Matrix([[x, y], [z, w]])
    i, j = symbols("i, j")
    Mij = M[i, j]
    assert isinstance(Mij, MatrixElement)
    Ms = SparseMatrix([[2, 3], [4, 5]])
    msij = Ms[i, j]
    assert isinstance(msij, MatrixElement)
    for oi, oj in [(0, 0), (0, 1), (1, 0), (1, 1)]:
        assert Mij.subs({i: oi, j: oj}) == M[oi, oj]
        assert msij.subs({i: oi, j: oj}) == Ms[oi, oj]
    A = MatrixSymbol("A", 2, 2)
    assert A[0, 0].subs(A, M) == x
    assert A[i, j].subs(A, M) == M[i, j]
    assert M[i, j].subs(M, A) == A[i, j]

    assert isinstance(M[3 * i - 2, j], MatrixElement)
    assert M[3 * i - 2, j].subs({i: 1, j: 0}) == M[1, 0]
    assert isinstance(M[i, 0], MatrixElement)
    assert M[i, 0].subs(i, 0) == M[0, 0]
    assert M[0, i].subs(i, 1) == M[0, 1]

    assert M[i, j].diff(x) == Matrix([[1, 0], [0, 0]])[i, j]

    raises(ValueError, lambda: M[i, 2])
    raises(ValueError, lambda: M[i, -1])
    raises(ValueError, lambda: M[2, i])
    raises(ValueError, lambda: M[-1, i])
Пример #8
0
def test_scipy_sparse_matrix():
    if not scipy:
        skip("scipy not installed.")
    A = SparseMatrix([[x, 0], [0, y]])
    f = lambdify((x, y), A, modules="scipy")
    B = f(1, 2)
    assert isinstance(B, scipy.sparse.coo_matrix)
Пример #9
0
def test_eq():
    A = Matrix([[1]])
    B = ImmutableMatrix([[1]])
    C = SparseMatrix([[1]])
    assert A != object()
    assert A != "Matrix([[1]])"
    assert A == B
    assert A == C
Пример #10
0
def test_as_immutable():
    data = [[1, 2], [3, 4]]
    X = Matrix(data)
    assert sympify(X) == X.as_immutable() == ImmutableMatrix(data)

    data = {(0, 0): 1, (0, 1): 2, (1, 0): 3, (1, 1): 4}
    X = SparseMatrix(2, 2, data)
    assert sympify(X) == X.as_immutable() == ImmutableSparseMatrix(2, 2, data)
Пример #11
0
def test_sparse_solve():
    from sympy.matrices import SparseMatrix
    A = SparseMatrix(((25, 15, -5), (15, 18, 0), (-5, 0, 11)))
    assert A.cholesky() == Matrix([[5, 0, 0], [3, 3, 0], [-1, 1, 3]])
    assert A.cholesky() * A.cholesky().T == Matrix([[25, 15, -5], [15, 18, 0],
                                                    [-5, 0, 11]])

    A = SparseMatrix(((25, 15, -5), (15, 18, 0), (-5, 0, 11)))
    L, D = A.LDLdecomposition()
    assert 15 * L == Matrix([[15, 0, 0], [9, 15, 0], [-3, 5, 15]])
    assert D == Matrix([[25, 0, 0], [0, 9, 0], [0, 0, 9]])
    assert L * D * L.T == A

    A = SparseMatrix(((3, 0, 2), (0, 0, 1), (1, 2, 0)))
    assert A.inv() * A == SparseMatrix(eye(3))

    A = SparseMatrix([[2, -1, 0], [-1, 2, -1], [0, 0, 2]])
    ans = SparseMatrix([[S(2) / 3, S(1) / 3, S(1) / 6],
                        [S(1) / 3, S(2) / 3, S(1) / 3], [0, 0, S(1) / 2]])
    assert A.inv(method='CH') == ans
    assert A.inv(method='LDL') == ans
    assert A * ans == SparseMatrix(eye(3))

    s = A.solve(A[:, 0], 'LDL')
    assert A * s == A[:, 0]
    s = A.solve(A[:, 0], 'CH')
    assert A * s == A[:, 0]
    A = A.col_join(A)
    s = A.solve_least_squares(A[:, 0], 'CH')
    assert A * s == A[:, 0]
    s = A.solve_least_squares(A[:, 0], 'LDL')
    assert A * s == A[:, 0]
Пример #12
0
def test_sparse():
    M = SparseMatrix(5, 6, {})
    M[2, 2] = 10
    M[1, 2] = 20
    M[1, 3] = 22
    M[0, 3] = 30
    M[3, 0] = x * y
    assert mcode(M) == (
        "sparse([4 2 3 1 2], [1 3 3 4 4], [x.*y 20 10 30 22], 5, 6)")
Пример #13
0
def test_LDLdecomposition():
    raises(NonSquareMatrixError, lambda: Matrix((1, 2)).LDLdecomposition())
    raises(ValueError, lambda: Matrix(((1, 2), (3, 4))).LDLdecomposition())
    raises(ValueError, lambda: Matrix(((5 + I, 0), (0, 1))).LDLdecomposition())
    raises(ValueError, lambda: Matrix(((1, 5), (5, 1))).LDLdecomposition())
    raises(ValueError, lambda: Matrix(
        ((1, 2), (3, 4))).LDLdecomposition(hermitian=False))
    A = Matrix(((1, 5), (5, 1)))
    L, D = A.LDLdecomposition(hermitian=False)
    assert L * D * L.T == A
    A = Matrix(((25, 15, -5), (15, 18, 0), (-5, 0, 11)))
    L, D = A.LDLdecomposition()
    assert L * D * L.T == A
    assert L.is_lower
    assert L == Matrix([[1, 0, 0], [Rational(3, 5), 1, 0],
                        [Rational(-1, 5), Rational(1, 3), 1]])
    assert D.is_diagonal()
    assert D == Matrix([[25, 0, 0], [0, 9, 0], [0, 0, 9]])
    A = Matrix(
        ((4, -2 * I, 2 + 2 * I), (2 * I, 2, -1 + I), (2 - 2 * I, -1 - I, 11)))
    L, D = A.LDLdecomposition()
    assert expand_mul(L * D * L.H) == A
    assert L.expand() == Matrix([[1, 0, 0], [I / 2, 1, 0],
                                 [S.Half - I / 2, 0, 1]])
    assert D.expand() == Matrix(((4, 0, 0), (0, 1, 0), (0, 0, 9)))

    raises(NonSquareMatrixError, lambda: SparseMatrix(
        (1, 2)).LDLdecomposition())
    raises(ValueError, lambda: SparseMatrix(((1, 2),
                                             (3, 4))).LDLdecomposition())
    raises(ValueError, lambda: SparseMatrix(((5 + I, 0),
                                             (0, 1))).LDLdecomposition())
    raises(ValueError, lambda: SparseMatrix(((1, 5),
                                             (5, 1))).LDLdecomposition())
    raises(
        ValueError, lambda: SparseMatrix(
            ((1, 2), (3, 4))).LDLdecomposition(hermitian=False))
    A = SparseMatrix(((1, 5), (5, 1)))
    L, D = A.LDLdecomposition(hermitian=False)
    assert L * D * L.T == A
    A = SparseMatrix(((25, 15, -5), (15, 18, 0), (-5, 0, 11)))
    L, D = A.LDLdecomposition()
    assert L * D * L.T == A
    assert L.is_lower
    assert L == Matrix([[1, 0, 0], [Rational(3, 5), 1, 0],
                        [Rational(-1, 5), Rational(1, 3), 1]])
    assert D.is_diagonal()
    assert D == Matrix([[25, 0, 0], [0, 9, 0], [0, 0, 9]])
    A = SparseMatrix(
        ((4, -2 * I, 2 + 2 * I), (2 * I, 2, -1 + I), (2 - 2 * I, -1 - I, 11)))
    L, D = A.LDLdecomposition()
    assert expand_mul(L * D * L.H) == A
    assert L == Matrix(((1, 0, 0), (I / 2, 1, 0), (S.Half - I / 2, 0, 1)))
    assert D == Matrix(((4, 0, 0), (0, 1, 0), (0, 0, 9)))
def test_SciPyPrinter():
    p = SciPyPrinter()
    expr = acos(x)
    assert 'numpy' not in p.module_imports
    assert p.doprint(expr) == 'numpy.arccos(x)'
    assert 'numpy' in p.module_imports
    assert not any(m.startswith('scipy') for m in p.module_imports)
    smat = SparseMatrix(2, 5, {(0, 1): 3})
    assert p.doprint(smat) == 'scipy.sparse.coo_matrix([3], ([0], [1]), shape=(2, 5))'
    assert 'scipy.sparse' in p.module_imports
def test_sparse():
    M = SparseMatrix(5, 6, {})
    M[2, 2] = 10
    M[1, 2] = 20
    M[1, 3] = 22
    M[0, 3] = 30
    M[3, 0] = x * y
    assert julia_code(M) == (
        "sparse([4, 2, 3, 1, 2], [1, 3, 3, 4, 4], [x.*y, 20, 10, 30, 22], 5, 6)"
    )
Пример #16
0
def test_lower_triangular_solve():
    a, b, c, d = symbols('a:d')
    u, v, w, x = symbols('u:x')

    A = SparseMatrix([[a, 0], [c, d]])
    B = MutableSparseMatrix([[u, v], [w, x]])
    C = ImmutableSparseMatrix([[u, v], [w, x]])

    sol = Matrix([[u / a, v / a], [(w - c * u / a) / d, (x - c * v / a) / d]])
    assert A.lower_triangular_solve(B) == sol
    assert A.lower_triangular_solve(C) == sol
Пример #17
0
def test_upper_triangular_solve():
    a, b, c, d = symbols('a:d')
    u, v, w, x = symbols('u:x')

    A = SparseMatrix([[a, b], [0, d]])
    B = MutableSparseMatrix([[u, v], [w, x]])
    C = ImmutableSparseMatrix([[u, v], [w, x]])

    sol = Matrix([[(u - b * w / d) / a, (v - b * x / d) / a], [w / d, x / d]])
    assert A.upper_triangular_solve(B) == sol
    assert A.upper_triangular_solve(C) == sol
Пример #18
0
def test_diagonal_solve():
    a, d = symbols('a d')
    u, v, w, x = symbols('u:x')

    A = SparseMatrix([[a, 0], [0, d]])
    B = MutableSparseMatrix([[u, v], [w, x]])
    C = ImmutableSparseMatrix([[u, v], [w, x]])

    sol = Matrix([[u / a, v / a], [w / d, x / d]])
    assert A.diagonal_solve(B) == sol
    assert A.diagonal_solve(C) == sol
Пример #19
0
def test_matrix_sum():
    A = Matrix([[0, 1], [n, 0]])

    result = Sum(A, (n, 0, 3)).doit()
    assert result == Matrix([[0, 4], [6, 0]])
    assert result.__class__ == ImmutableDenseMatrix

    A = SparseMatrix([[0, 1], [n, 0]])

    result = Sum(A, (n, 0, 3)).doit()
    assert result.__class__ == ImmutableSparseMatrix
Пример #20
0
def test_copyin():
    s = SparseMatrix(3, 3, {})
    s[1, 0] = 1
    assert s[:, 0] == SparseMatrix(Matrix([0, 1, 0]))
    assert s[3] == 1
    assert s[3:4] == [1]
    s[1, 1] = 42
    assert s[1, 1] == 42
    assert s[1, 1:] == SparseMatrix([[42, 0]])
    s[1, 1:] = Matrix([[5, 6]])
    assert s[1, :] == SparseMatrix([[1, 5, 6]])
    s[1, 1:] = [[42, 43]]
    assert s[1, :] == SparseMatrix([[1, 42, 43]])
    s[0, 0] = 17
    assert s[:, :1] == SparseMatrix([17, 1, 0])
    s[0, 0] = [1, 1, 1]
    assert s[:, 0] == SparseMatrix([1, 1, 1])
    s[0, 0] = Matrix([1, 1, 1])
    assert s[:, 0] == SparseMatrix([1, 1, 1])
    s[0, 0] = SparseMatrix([1, 1, 1])
    assert s[:, 0] == SparseMatrix([1, 1, 1])
Пример #21
0
def test_sparse_creation():
    a = SparseMatrix(2, 2, {(0, 0): [[1, 2], [3, 4]]})
    assert a == SparseMatrix([[1, 2], [3, 4]])
    a = SparseMatrix(2, 2, {(0, 0): [[1, 2]]})
    assert a == SparseMatrix([[1, 2], [0, 0]])
    a = SparseMatrix(2, 2, {(0, 0): [1, 2]})
    assert a == SparseMatrix([[1, 0], [2, 0]])
Пример #22
0
    def sparse_sympy_matrix(self, triqs_operator_expression):

        Hsp = self.sparse_matrix(triqs_operator_expression)

        from sympy.matrices import SparseMatrix
        from sympy.simplify.simplify import nsimplify

        d = dict([((i, j), nsimplify(val))
                  for (i, j), val in Hsp.todok().items()])

        H = SparseMatrix(Hsp.shape[0], Hsp.shape[1], d)

        return H
Пример #23
0
def test_csrtodok():
    h = [[5, 7, 5], [2, 1, 3], [0, 1, 1, 3], [3, 4]]
    g = [[12, 5, 4], [2, 4, 2], [0, 1, 2, 3], [3, 7]]
    i = [[1, 3, 12], [0, 2, 4], [0, 2, 3], [2, 5]]
    j = [[11, 15, 12, 15], [2, 4, 1, 2], [0, 1, 1, 2, 3, 4], [5, 8]]
    k = [[1, 3], [2, 1], [0, 1, 1, 2], [3, 3]]
    m = _csrtodok(h)
    assert isinstance(m, SparseMatrix)
    assert m == SparseMatrix(3, 4, {(0, 2): 5, (2, 1): 7, (2, 3): 5})
    assert _csrtodok(g) == SparseMatrix(3, 7, {
        (0, 2): 12,
        (1, 4): 5,
        (2, 2): 4
    })
    assert _csrtodok(i) == SparseMatrix([[1, 0, 3, 0, 0], [0, 0, 0, 0, 12]])
    assert _csrtodok(j) == SparseMatrix(5, 8, {
        (0, 2): 11,
        (2, 4): 15,
        (3, 1): 12,
        (4, 2): 15
    })
    assert _csrtodok(k) == SparseMatrix(3, 3, {(0, 2): 1, (2, 1): 3})
Пример #24
0
def test_sparse():
    M = SparseMatrix(5, 6, {})
    M[2, 2] = 10
    M[1, 2] = 20
    M[1, 3] = 22
    M[0, 3] = 30
    M[3, 0] = x * y
    assert (maple_code(M) == "Matrix([[0, 0, 0, 30, 0, 0],"
            " [0, 0, 20, 22, 0, 0],"
            " [0, 0, 10, 0, 0, 0],"
            " [x*y, 0, 0, 0, 0, 0],"
            " [0, 0, 0, 0, 0, 0]], "
            "storage = sparse)")
Пример #25
0
def test_lower_triangular_solve():
    raises(
        NonSquareMatrixError, lambda: SparseMatrix([[1, 2]]).
        lower_triangular_solve(Matrix([[1, 2]])))
    raises(
        ShapeError,
        lambda: SparseMatrix([[1, 2], [0, 4]]).lower_triangular_solve(
            Matrix([1])))
    raises(
        ValueError,
        lambda: SparseMatrix([[1, 2], [3, 4]]).lower_triangular_solve(
            Matrix([[1, 2], [3, 4]])))

    a, b, c, d = symbols('a:d')
    u, v, w, x = symbols('u:x')

    A = SparseMatrix([[a, 0], [c, d]])
    B = MutableSparseMatrix([[u, v], [w, x]])
    C = ImmutableSparseMatrix([[u, v], [w, x]])

    sol = Matrix([[u / a, v / a], [(w - c * u / a) / d, (x - c * v / a) / d]])
    assert A.lower_triangular_solve(B) == sol
    assert A.lower_triangular_solve(C) == sol
Пример #26
0
def test_upper_triangular_solve():
    raises(
        NonSquareMatrixError, lambda: SparseMatrix([[1, 2]]).
        upper_triangular_solve(Matrix([[1, 2]])))
    raises(
        ShapeError,
        lambda: SparseMatrix([[1, 2], [0, 4]]).upper_triangular_solve(
            Matrix([1])))
    raises(
        TypeError,
        lambda: SparseMatrix([[1, 2], [3, 4]]).upper_triangular_solve(
            Matrix([[1, 2], [3, 4]])))

    a, b, c, d = symbols('a:d')
    u, v, w, x = symbols('u:x')

    A = SparseMatrix([[a, b], [0, d]])
    B = MutableSparseMatrix([[u, v], [w, x]])
    C = ImmutableSparseMatrix([[u, v], [w, x]])

    sol = Matrix([[(u - b * w / d) / a, (v - b * x / d) / a], [w / d, x / d]])
    assert A.upper_triangular_solve(B) == sol
    assert A.upper_triangular_solve(C) == sol
Пример #27
0
def test_subs():
    A = MatrixSymbol('A', n, m)
    B = MatrixSymbol('B', m, l)
    C = MatrixSymbol('C', m, l)

    assert A.subs(n, m).shape == (m, m)
    assert (A * B).subs(B, C) == A * C
    assert (A * B).subs(l, n).is_square

    A = SparseMatrix([[1, 2], [3, 4]])
    B = Matrix([[1, 2], [3, 4]])
    C, D = MatrixSymbol('C', 2, 2), MatrixSymbol('D', 2, 2)

    assert (C * D).subs({C: A, D: B}) == MatMul(A, B)
Пример #28
0
def test_SciPyPrinter():
    p = SciPyPrinter()
    expr = acos(x)
    assert 'numpy' not in p.module_imports
    assert p.doprint(expr) == 'numpy.arccos(x)'
    assert 'numpy' in p.module_imports
    assert not any(m.startswith('scipy') for m in p.module_imports)
    smat = SparseMatrix(2, 5, {(0, 1): 3})
    assert p.doprint(smat) == \
        'scipy.sparse.coo_matrix(([3], ([0], [1])), shape=(2, 5))'
    assert 'scipy.sparse' in p.module_imports

    assert p.doprint(S.GoldenRatio) == 'scipy.constants.golden_ratio'
    assert p.doprint(S.Pi) == 'scipy.constants.pi'
    assert p.doprint(S.Exp1) == 'numpy.e'
Пример #29
0
def test_SciPyPrinter():
    p = SciPyPrinter()
    expr = acos(x)
    assert "numpy" not in p.module_imports
    assert p.doprint(expr) == "numpy.arccos(x)"
    assert "numpy" in p.module_imports
    assert not any(m.startswith("scipy") for m in p.module_imports)
    smat = SparseMatrix(2, 5, {(0, 1): 3})
    assert p.doprint(
        smat) == "scipy.sparse.coo_matrix([3], ([0], [1]), shape=(2, 5))"
    assert "scipy.sparse" in p.module_imports

    assert p.doprint(S.GoldenRatio) == "scipy.constants.golden_ratio"
    assert p.doprint(S.Pi) == "scipy.constants.pi"
    assert p.doprint(S.Exp1) == "numpy.e"
Пример #30
0
def test_diagonal():
    m = Matrix(3, 3, range(9))
    d = m.diagonal()
    assert d == m.diagonal(0)
    assert tuple(d) == (0, 4, 8)
    assert tuple(m.diagonal(1)) == (1, 5)
    assert tuple(m.diagonal(-1)) == (3, 7)
    assert tuple(m.diagonal(2)) == (2, )
    assert type(m.diagonal()) == type(m)
    s = SparseMatrix(3, 3, {(1, 1): 1})
    assert type(s.diagonal()) == type(s)
    assert type(m) != type(s)
    raises(ValueError, lambda: m.diagonal(3))
    raises(ValueError, lambda: m.diagonal(-3))
    raises(ValueError, lambda: m.diagonal(pi))
    M = ones(2, 3)
    assert banded({i: list(M.diagonal(i))
                   for i in range(1 - M.rows, M.cols)}) == M