Exemple #1
0
def test_diagonal_symmetrical():
    m = Matrix(2,2,[0, 1, 1, 0])
    assert not m.is_diagonal()
    assert m.is_symmetric()
    assert m.is_symmetric(simplify=False)

    m = Matrix(2,2,[1, 0, 0, 1])
    assert m.is_diagonal()

    m = diag(1, 2, 3)
    assert m.is_diagonal()
    assert m.is_symmetric()

    m = Matrix(3,3,[1, 0, 0, 0, 2, 0, 0, 0, 3])
    assert m == diag(1, 2, 3)

    m = Matrix(2, 3, [0, 0, 0, 0, 0, 0])
    assert not m.is_symmetric()
    assert m.is_diagonal()

    m = Matrix(((5, 0), (0, 6), (0, 0)))
    assert m.is_diagonal()

    m = Matrix(((5, 0, 0), (0, 6, 0)))
    assert m.is_diagonal()

    x, y = symbols('x y')
    m = Matrix(3,3,[1, x**2 + 2*x + 1, y, (x + 1)**2 , 2, 0, y, 0, 3])
    assert m.is_symmetric()
    assert not m.is_symmetric(simplify=False)
    assert m.expand().is_symmetric(simplify=False)
Exemple #2
0
def test_diagonal_symmetrical():
    m = Matrix(2,2,[0, 1, 1, 0])
    assert not m.is_diagonal()
    assert m.is_symmetric()
    assert m.is_symmetric(simplify=False)

    m = Matrix(2,2,[1, 0, 0, 1])
    assert m.is_diagonal()

    m = diag(1, 2, 3)
    assert m.is_diagonal()
    assert m.is_symmetric()

    m = Matrix(3,3,[1, 0, 0, 0, 2, 0, 0, 0, 3])
    assert m == diag(1, 2, 3)

    m = Matrix(2, 3, [0, 0, 0, 0, 0, 0])
    assert not m.is_symmetric()
    assert m.is_diagonal()

    m = Matrix(((5, 0), (0, 6), (0, 0)))
    assert m.is_diagonal()

    m = Matrix(((5, 0, 0), (0, 6, 0)))
    assert m.is_diagonal()

    x, y = symbols('x y')
    m = Matrix(3,3,[1, x**2 + 2*x + 1, y, (x + 1)**2 , 2, 0, y, 0, 3])
    assert m.is_symmetric()
    assert not m.is_symmetric(simplify=False)
    assert m.expand().is_symmetric(simplify=False)
Exemple #3
0
def test_get_diag_blocks2():
    x, y, z = symbols("x y z")
    a = Matrix([[1, 2], [2, 3]])
    b = Matrix([[3, x], [y, 3]])
    c = Matrix([[3, x, 3], [y, 3, z], [x, y, z]])
    assert diag(a, b, b).get_diag_blocks() == [a, b, b]
    assert diag(a, b, c).get_diag_blocks() == [a, b, c]
    assert diag(a, c, b).get_diag_blocks() == [a, c, b]
    assert diag(c, c, b).get_diag_blocks() == [c, c, b]
Exemple #4
0
def test_get_diag_blocks2():
    x, y, z = symbols("x y z")
    a = Matrix([[1, 2], [2, 3]])
    b = Matrix([[3, x], [y, 3]])
    c = Matrix([[3, x, 3], [y, 3, z], [x, y, z]])
    assert diag(a, b, b).get_diag_blocks() == [a, b, b]
    assert diag(a, b, c).get_diag_blocks() == [a, b, c]
    assert diag(a, c, b).get_diag_blocks() == [a, c, b]
    assert diag(c, c, b).get_diag_blocks() == [c, c, b]
Exemple #5
0
def test_diag():
    x, y, z = symbols("x y z")
    a = Matrix([[1, 2], [2, 3]])
    b = Matrix([[3, x], [y, 3]])
    c = Matrix([[3, x, 3], [y, 3, z], [x, y, z]])
    assert diag(a, b, b) == Matrix([
            [1, 2, 0, 0, 0, 0],
            [2, 3, 0, 0, 0, 0],
            [0, 0, 3, x, 0, 0],
            [0, 0, y, 3, 0, 0],
            [0, 0, 0, 0, 3, x],
            [0, 0, 0, 0, y, 3],
            ])
    assert diag(a, b, c) == Matrix([
            [1, 2, 0, 0, 0, 0, 0],
            [2, 3, 0, 0, 0, 0, 0],
            [0, 0, 3, x, 0, 0, 0],
            [0, 0, y, 3, 0, 0, 0],
            [0, 0, 0, 0, 3, x, 3],
            [0, 0, 0, 0, y, 3, z],
            [0, 0, 0, 0, x, y, z],
            ])
    assert diag(a, c, b) == Matrix([
            [1, 2, 0, 0, 0, 0, 0],
            [2, 3, 0, 0, 0, 0, 0],
            [0, 0, 3, x, 3, 0, 0],
            [0, 0, y, 3, z, 0, 0],
            [0, 0, x, y, z, 0, 0],
            [0, 0, 0, 0, 0, 3, x],
            [0, 0, 0, 0, 0, y, 3],
            ])
    a = Matrix([x, y, z])
    b = Matrix([[1, 2], [3, 4]])
    c = Matrix([[5, 6]])
    assert diag(a, 7, b, c) == Matrix([
            [x, 0, 0, 0, 0, 0],
            [y, 0, 0, 0, 0, 0],
            [z, 0, 0, 0, 0, 0],
            [0, 7, 0, 0, 0, 0],
            [0, 0, 1, 2, 0, 0],
            [0, 0, 3, 4, 0, 0],
            [0, 0, 0, 0, 5, 6],
            ])
Exemple #6
0
def test_diag():
    x, y, z = symbols("x y z")
    a = Matrix([[1, 2], [2, 3]])
    b = Matrix([[3, x], [y, 3]])
    c = Matrix([[3, x, 3], [y, 3, z], [x, y, z]])
    assert diag(a, b, b) == Matrix([
            [1, 2, 0, 0, 0, 0],
            [2, 3, 0, 0, 0, 0],
            [0, 0, 3, x, 0, 0],
            [0, 0, y, 3, 0, 0],
            [0, 0, 0, 0, 3, x],
            [0, 0, 0, 0, y, 3],
            ])
    assert diag(a, b, c) == Matrix([
            [1, 2, 0, 0, 0, 0, 0],
            [2, 3, 0, 0, 0, 0, 0],
            [0, 0, 3, x, 0, 0, 0],
            [0, 0, y, 3, 0, 0, 0],
            [0, 0, 0, 0, 3, x, 3],
            [0, 0, 0, 0, y, 3, z],
            [0, 0, 0, 0, x, y, z],
            ])
    assert diag(a, c, b) == Matrix([
            [1, 2, 0, 0, 0, 0, 0],
            [2, 3, 0, 0, 0, 0, 0],
            [0, 0, 3, x, 3, 0, 0],
            [0, 0, y, 3, z, 0, 0],
            [0, 0, x, y, z, 0, 0],
            [0, 0, 0, 0, 0, 3, x],
            [0, 0, 0, 0, 0, y, 3],
            ])
    a = Matrix([x, y, z])
    b = Matrix([[1, 2], [3, 4]])
    c = Matrix([[5, 6]])
    assert diag(a, 7, b, c) == Matrix([
            [x, 0, 0, 0, 0, 0],
            [y, 0, 0, 0, 0, 0],
            [z, 0, 0, 0, 0, 0],
            [0, 7, 0, 0, 0, 0],
            [0, 0, 1, 2, 0, 0],
            [0, 0, 3, 4, 0, 0],
            [0, 0, 0, 0, 5, 6],
            ])
Exemple #7
0
def test_inv_block():
    x, y, z = symbols("x y z")
    a = Matrix([[1, 2], [2, 3]])
    b = Matrix([[3, x], [y, 3]])
    c = Matrix([[3, x, 3], [y, 3, z], [x, y, z]])
    A = diag(a, b, b)
    assert A.inv(try_block_diag=True) == diag(a.inv(), b.inv(), b.inv())
    A = diag(a, b, c)
    assert A.inv(try_block_diag=True) == diag(a.inv(), b.inv(), c.inv())
    A = diag(a, c, b)
    assert A.inv(try_block_diag=True) == diag(a.inv(), c.inv(), b.inv())
    A = diag(a, a, b, a, c, a)
    assert A.inv(try_block_diag=True) == diag(
        a.inv(), a.inv(), b.inv(), a.inv(), c.inv(), a.inv())
    assert A.inv(try_block_diag=True, method="ADJ") == diag(
        a.inv(method="ADJ"), a.inv(method="ADJ"), b.inv(method="ADJ"),
        a.inv(method="ADJ"), c.inv(method="ADJ"), a.inv(method="ADJ"))
Exemple #8
0
def test_inv_block():
    x, y, z = symbols("x y z")
    a = Matrix([[1, 2], [2, 3]])
    b = Matrix([[3, x], [y, 3]])
    c = Matrix([[3, x, 3], [y, 3, z], [x, y, z]])
    A = diag(a, b, b)
    assert A.inv(try_block_diag=True) == diag(a.inv(), b.inv(), b.inv())
    A = diag(a, b, c)
    assert A.inv(try_block_diag=True) == diag(a.inv(), b.inv(), c.inv())
    A = diag(a, c, b)
    assert A.inv(try_block_diag=True) == diag(a.inv(), c.inv(), b.inv())
    A = diag(a, a, b, a, c, a)
    assert A.inv(try_block_diag=True) == diag(
        a.inv(), a.inv(), b.inv(), a.inv(), c.inv(), a.inv())
    assert A.inv(try_block_diag=True, method="ADJ") == diag(
        a.inv(method="ADJ"), a.inv(method="ADJ"), b.inv(method="ADJ"),
        a.inv(method="ADJ"), c.inv(method="ADJ"), a.inv(method="ADJ"))
Exemple #9
0
def test_diagonal_symmetrical():
    m = Matrix(2,2,[0, 1, 1, 0])
    assert not m.is_diagonal()
    assert m.is_symmetric()

    m = Matrix(2,2,[1, 0, 0, 1])
    assert m.is_diagonal()

    m = diag(1, 2, 3)
    assert m.is_diagonal()
    assert m.is_symmetric()

    m = Matrix(3,3,[1, 0, 0, 0, 2, 0, 0, 0, 3])
    assert m == diag(1, 2, 3)

    m = Matrix(2,3,[0, 0, 0, 0, 0, 0])
    assert not m.is_symmetric()

    x, y = symbols('x','y')
    m = Matrix(3,3,[1, x**2 + 2*x + 1, y, (x + 1)**2 , 2, 0, y, 0, 3])
    assert m.is_symmetric()
Exemple #10
0
def test_diagonal_symmetrical():
    m = Matrix(2, 2, [0, 1, 1, 0])
    assert not m.is_diagonal()
    assert m.is_symmetric()

    m = Matrix(2, 2, [1, 0, 0, 1])
    assert m.is_diagonal()

    m = diag(1, 2, 3)
    assert m.is_diagonal()
    assert m.is_symmetric()

    m = Matrix(3, 3, [1, 0, 0, 0, 2, 0, 0, 0, 3])
    assert m == diag(1, 2, 3)

    m = Matrix(2, 3, [0, 0, 0, 0, 0, 0])
    assert not m.is_symmetric()

    x, y = symbols('x', 'y')
    m = Matrix(3, 3, [1, x**2 + 2 * x + 1, y, (x + 1)**2, 2, 0, y, 0, 3])
    assert m.is_symmetric()
Exemple #11
0
def test_diagonalization():
    x, y, z = symbols('x y z')
    m = Matrix(3,2,[-3, 1, -3, 20, 3, 10])
    assert not m.is_diagonalizable()
    assert not m.is_symmetric()
    raises(NonSquareMatrixError, 'm.diagonalize()')

    # diagonalizable
    m = diag(1, 2, 3)
    (P, D) = m.diagonalize()
    assert P == eye(3)
    assert D == m

    m = Matrix(2,2,[0, 1, 1, 0])
    assert m.is_symmetric()
    assert m.is_diagonalizable()
    (P, D) = m.diagonalize()
    assert P.inv() * m * P == D

    m = Matrix(2,2,[1, 0, 0, 3])
    assert m.is_symmetric()
    assert m.is_diagonalizable()
    (P, D) = m.diagonalize()
    assert P.inv() * m * P == D
    assert P == eye(2)
    assert D == m

    m = Matrix(2,2,[1, 1, 0, 0])
    assert m.is_diagonalizable()
    (P, D) = m.diagonalize()
    assert P.inv() * m * P == D

    m = Matrix(3,3,[1, 2, 0, 0, 3, 0, 2, -4, 2])
    assert m.is_diagonalizable()
    (P, D) = m.diagonalize()
    assert P.inv() * m * P == D

    m = Matrix(2,2,[1, 0, 0, 0])
    assert m.is_diagonal()
    assert m.is_diagonalizable()
    (P, D) = m.diagonalize()
    assert P.inv() * m * P == D
    assert P == eye(2)

    # diagonalizable, complex only
    m = Matrix(2,2,[0, 1, -1, 0])
    assert not m.is_diagonalizable(True)
    raises(MatrixError, '(D, P) = m.diagonalize(True)')
    assert m.is_diagonalizable()
    (P, D) = m.diagonalize()
    assert P.inv() * m * P == D

    m = Matrix(2,2,[1, 0, 0, I])
    raises(NotImplementedError, 'm.is_diagonalizable(True)')
    # !!! bug because of eigenvects() or roots(x**2 + (-1 - I)*x + I, x)
    # see issue 2193
    # assert not m.is_diagonalizable(True)
    # raises(MatrixError, '(P, D) = m.diagonalize(True)')
    # (P, D) = m.diagonalize(True)

    # not diagonalizable
    m = Matrix(2,2,[0, 1, 0, 0])
    assert not m.is_diagonalizable()
    raises(MatrixError, '(D, P) = m.diagonalize()')

    m = Matrix(3,3,[-3, 1, -3, 20, 3, 10, 2, -2, 4])
    assert not m.is_diagonalizable()
    raises(MatrixError, '(D, P) = m.diagonalize()')

    # symbolic
    a, b, c, d = symbols('a b c d')
    m = Matrix(2,2,[a, c, c, b])
    assert m.is_symmetric()
    assert m.is_diagonalizable()
Exemple #12
0
def test_diagonalization():
    x, y, z = symbols('x y z')
    m = Matrix(3,2,[-3, 1, -3, 20, 3, 10])
    assert not m.is_diagonalizable()
    assert not m.is_symmetric()
    raises(NonSquareMatrixError, 'm.diagonalize()')

    # diagonalizable
    m = diag(1, 2, 3)
    (P, D) = m.diagonalize()
    assert P == eye(3)
    assert D == m

    m = Matrix(2,2,[0, 1, 1, 0])
    assert m.is_symmetric()
    assert m.is_diagonalizable()
    (P, D) = m.diagonalize()
    assert P.inv() * m * P == D

    m = Matrix(2,2,[1, 0, 0, 3])
    assert m.is_symmetric()
    assert m.is_diagonalizable()
    (P, D) = m.diagonalize()
    assert P.inv() * m * P == D
    assert P == eye(2)
    assert D == m

    m = Matrix(2,2,[1, 1, 0, 0])
    assert m.is_diagonalizable()
    (P, D) = m.diagonalize()
    assert P.inv() * m * P == D

    m = Matrix(3,3,[1, 2, 0, 0, 3, 0, 2, -4, 2])
    assert m.is_diagonalizable()
    (P, D) = m.diagonalize()
    assert P.inv() * m * P == D

    m = Matrix(2,2,[1, 0, 0, 0])
    assert m.is_diagonal()
    assert m.is_diagonalizable()
    (P, D) = m.diagonalize()
    assert P.inv() * m * P == D
    assert P == eye(2)

    # diagonalizable, complex only
    m = Matrix(2,2,[0, 1, -1, 0])
    assert not m.is_diagonalizable(True)
    raises(MatrixError, '(D, P) = m.diagonalize(True)')
    assert m.is_diagonalizable()
    (P, D) = m.diagonalize()
    assert P.inv() * m * P == D

    m = Matrix(2,2,[1, 0, 0, I])
    raises(NotImplementedError, 'm.is_diagonalizable(True)')
    # !!! bug because of eigenvects() or roots(x**2 + (-1 - I)*x + I, x)
    # see issue 2193
    # assert not m.is_diagonalizable(True)
    # raises(MatrixError, '(P, D) = m.diagonalize(True)')
    # (P, D) = m.diagonalize(True)

    # not diagonalizable
    m = Matrix(2,2,[0, 1, 0, 0])
    assert not m.is_diagonalizable()
    raises(MatrixError, '(D, P) = m.diagonalize()')

    m = Matrix(3,3,[-3, 1, -3, 20, 3, 10, 2, -2, 4])
    assert not m.is_diagonalizable()
    raises(MatrixError, '(D, P) = m.diagonalize()')

    # symbolic
    a, b, c, d = symbols('a b c d')
    m = Matrix(2,2,[a, c, c, b])
    assert m.is_symmetric()
    assert m.is_diagonalizable()