Пример #1
0
def LDL(matlist, K):
    """
    Performs the LDL decomposition of a hermitian matrix and returns L, D and
    transpose of L. Only applicable to rational entries.

    Examples
    ========

    >>> from diofant.matrices.densesolve import LDL
    >>> from diofant import QQ

    >>> a = [
    ... [QQ(4), QQ(12), QQ(-16)],
    ... [QQ(12), QQ(37), QQ(-43)],
    ... [QQ(-16), QQ(-43), QQ(98)]]
    >>> LDL(a, QQ)
    ([[1, 0, 0], [3, 1, 0], [-4, 5, 1]], [[4, 0, 0], [0, 1, 0], [0, 0, 9]], [[1, 3, -4], [0, 1, 5], [0, 0, 1]])

    """
    new_matlist = copy.deepcopy(matlist)
    nrow = len(new_matlist)
    L, D = eye(nrow, K), eye(nrow, K)
    for i in range(nrow):
        for j in range(i + 1):
            a = K.zero
            for k in range(j):
                a += L[i][k] * L[j][k] * D[k][k]
            if i == j:
                D[j][j] = new_matlist[j][j] - a
            else:
                L[i][j] = (new_matlist[i][j] - a) / D[j][j]
    return L, D, conjugate_transpose(L, K)
Пример #2
0
def LU(matlist, K, reverse=0):
    """
    It computes the LU decomposition of a matrix and returns L and U
    matrices.

    Examples
    ========

    >>> from diofant.matrices.densesolve import LU
    >>> from diofant import QQ
    >>> a = [
    ... [QQ(1), QQ(2), QQ(3)],
    ... [QQ(2), QQ(-4), QQ(6)],
    ... [QQ(3), QQ(-9), QQ(-3)]]
    >>> LU(a, QQ)
    ([[1, 0, 0], [2, 1, 0], [3, 15/8, 1]], [[1, 2, 3], [0, -8, 0], [0, 0, -12]])

    See Also
    ========

    upper_triangle
    lower_triangle
    """
    nrow = len(matlist)
    new_matlist1, new_matlist2 = eye(nrow, K), copy.deepcopy(matlist)
    for i in range(nrow):
        for j in range(i + 1, nrow):
            if (new_matlist2[j][i] != 0):
                new_matlist1[j][i] = new_matlist2[j][i] / new_matlist2[i][i]
                rowadd(new_matlist2, j, i,
                       -new_matlist2[j][i] / new_matlist2[i][i], K)
    return new_matlist1, new_matlist2
Пример #3
0
def cholesky(matlist, K):
    """
    Performs the cholesky decomposition of a Hermitian matrix and
    returns L and it's conjugate transpose.

    Examples
    ========

    >>> from diofant.matrices.densesolve import cholesky
    >>> from diofant import QQ
    >>> cholesky([[QQ(25), QQ(15), QQ(-5)], [QQ(15), QQ(18), QQ(0)], [QQ(-5), QQ(0), QQ(11)]], QQ)
    ([[5, 0, 0], [3, 3, 0], [-1, 1, 3]], [[5, 3, -1], [0, 3, 1], [0, 0, 3]])

    See Also
    ========

    cholesky_solve
    """
    new_matlist = copy.deepcopy(matlist)
    nrow = len(new_matlist)
    L = eye(nrow, K)
    for i in range(nrow):
        for j in range(i + 1):
            a = K.zero
            for k in range(j):
                a += L[i][k] * L[j][k]
            if i == j:
                L[i][j] = int(sqrt(new_matlist[i][j] - a))
            else:
                L[i][j] = (new_matlist[i][j] - a) / L[j][j]
    return L, conjugate_transpose(L, K)
Пример #4
0
def test_mulmatmat():
    a = [[ZZ(3), ZZ(4)], [ZZ(5), ZZ(6)]]
    b = [[ZZ(1), ZZ(2)], [ZZ(7), ZZ(8)]]
    c = eye(2, ZZ)
    d = [[ZZ(6)], [ZZ(7)]]

    assert mulmatmat(a, b, ZZ) == [[ZZ(31), ZZ(38)], [ZZ(47), ZZ(58)]]
    assert mulmatmat(b, d, ZZ) == [[ZZ(20)], [ZZ(98)]]
Пример #5
0
def test_transpose():
    a = [[ZZ(3), ZZ(7), ZZ(4)], [ZZ(2), ZZ(4), ZZ(5)], [ZZ(6), ZZ(2), ZZ(3)]]
    b = eye(4, ZZ)

    assert transpose(a, ZZ) == ([[ZZ(3), ZZ(2), ZZ(6)], [ZZ(7),
                                                         ZZ(4),
                                                         ZZ(2)],
                                 [ZZ(4), ZZ(5), ZZ(3)]])
    assert transpose(b, ZZ) == b
Пример #6
0
def test_mulmatscaler():
    a = eye(3, ZZ)
    b = [[ZZ(3), ZZ(7), ZZ(4)], [ZZ(2), ZZ(4), ZZ(5)], [ZZ(6), ZZ(2), ZZ(3)]]

    assert mulmatscaler(a, ZZ(4), ZZ) == [[ZZ(4), ZZ(0), ZZ(0)],
                                          [ZZ(0), ZZ(4), ZZ(0)],
                                          [ZZ(0), ZZ(0), ZZ(4)]]
    assert mulmatscaler(b, ZZ(1), ZZ) == [[ZZ(3), ZZ(7), ZZ(4)],
                                          [ZZ(2), ZZ(4), ZZ(5)],
                                          [ZZ(6), ZZ(2), ZZ(3)]]
Пример #7
0
def test_trace():
    a = [[ZZ(3), ZZ(7), ZZ(4)], [ZZ(2), ZZ(4), ZZ(5)], [ZZ(6), ZZ(2), ZZ(3)]]
    b = eye(2, ZZ)

    assert trace(a, ZZ) == ZZ(10)
    assert trace(b, ZZ) == ZZ(2)