Beispiel #1
0
def LU(matlist, K, reverse=0):
    """
    It computes the LU decomposition of a matrix and returns L and U
    matrices.

    Examples
    ========

    >>> from sympy.matrices.densesolve import LU
    >>> from sympy 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
Beispiel #2
0
def rref(matlist, K):
    """
    Returns the reduced row echelon form of a Matrix.

    Examples
    ========

    >>> from sympy.matrices.densesolve import rref
    >>> from sympy import QQ
    >>> a = [
    ... [QQ(1), QQ(2), QQ(1)],
    ... [QQ(-2), QQ(-3), QQ(1)],
    ... [QQ(3), QQ(5), QQ(0)]]
    >>> rref(a, QQ)
    [[1, 0, -5], [0, 1, 3], [0, 0, 0]]

    See Also
    ========

    row_echelon
    """
    result_matlist = copy.deepcopy(matlist)
    result_matlist = row_echelon(result_matlist, K)
    nrow = len(result_matlist)
    for i in range(nrow):
        if result_matlist[i][i] == 1:
            for j in range(i):
                rowadd(result_matlist, j, i, -result_matlist[j][i], K)
    return result_matlist
Beispiel #3
0
def row_echelon(matlist, K):
    """
    Returns the row echelon form of a matrix with diagonal elements
    reduced to 1.

    Examples
    ========

    >>> from sympy.matrices.densesolve import row_echelon
    >>> from sympy import QQ
    >>> a = [
    ... [QQ(3), QQ(7), QQ(4)],
    ... [QQ(2), QQ(4), QQ(5)],
    ... [QQ(6), QQ(2), QQ(3)]]
    >>> row_echelon(a, QQ)
    [[1, 7/3, 4/3], [0, 1, -7/2], [0, 0, 1]]

    See Also
    ========

    rref
    """
    result_matlist = copy.deepcopy(matlist)
    nrow = len(result_matlist)
    for i in range(nrow):
        if result_matlist[i][i] != 1 and result_matlist[i][i] != 0:
            rowmul(result_matlist, i, 1 / result_matlist[i][i], K)
        for j in range(i + 1, nrow):
            if result_matlist[j][i] != 0:
                rowadd(result_matlist, j, i, -result_matlist[j][i], K)
    return result_matlist
Beispiel #4
0
def LU(matlist, K, reverse = 0):
    """
    It computes the LU decomposition of a matrix and returns L and U
    matrices.

    Examples
    ========

    >>> from sympy.matrices.densesolve import LU
    >>> from sympy 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
Beispiel #5
0
def rref(matlist, K):
    """
    Returns the reduced row echelon form of a Matrix.

    Examples
    ========

    >>> from sympy.matrices.densesolve import rref
    >>> from sympy import QQ
    >>> a = [
    ... [QQ(1), QQ(2), QQ(1)],
    ... [QQ(-2), QQ(-3), QQ(1)],
    ... [QQ(3), QQ(5), QQ(0)]]
    >>> rref(a, QQ)
    [[1, 0, -5], [0, 1, 3], [0, 0, 0]]

    See Also
    ========

    row_echelon
    """
    result_matlist = copy.deepcopy(matlist)
    result_matlist = row_echelon(result_matlist, K)
    nrow = len(result_matlist)
    for i in range(nrow):
        if result_matlist[i][i] == 1:
            for j in range(i):
                rowadd(result_matlist, j, i, -result_matlist[j][i], K)
    return result_matlist
Beispiel #6
0
def row_echelon(matlist, K):
    """
    Returns the row echelon form of a matrix with diagonal elements
    reduced to 1.

    Examples
    ========

    >>> from sympy.matrices.densesolve import row_echelon
    >>> from sympy import QQ
    >>> a = [
    ... [QQ(3), QQ(7), QQ(4)],
    ... [QQ(2), QQ(4), QQ(5)],
    ... [QQ(6), QQ(2), QQ(3)]]
    >>> row_echelon(a, QQ)
    [[1, 7/3, 4/3], [0, 1, -7/2], [0, 0, 1]]

    See Also
    ========

    rref
    """
    result_matlist = copy.deepcopy(matlist)
    nrow = len(result_matlist)
    for i in range(nrow):
        if (result_matlist[i][i] != 1 and result_matlist[i][i] != 0):
            rowmul(result_matlist, i, 1/result_matlist[i][i], K)
        for j in range(i + 1, nrow):
            if (result_matlist[j][i] != 0):
                rowadd(result_matlist, j, i, -result_matlist[j][i], K)
    return result_matlist