Beispiel #1
0
def lu_fact(matrix):
    if(isinstance(matrix, basestring)):
        matrix = np.loadtxt(matrix, unpack=False, delimiter=" ")
    originalMatrix = matrix
    lMatrixQueue = Queue.Queue()
    rowIndex = 0
    colIndex = 0
    while (rowIndex < matrix.shape[0]):
        if matrix[rowIndex, colIndex] != 0:
            for currentRow in range(rowIndex + 1, matrix.shape[0]):
                currentLMatrix = np.identity(matrix.shape[0])
                # THIS MAY BE BETTER --> if (matrixA[currentRow, colIndex] > 0 and matrixA[rowIndex, colIndex] > 0) or  (matrixA[currentRow, colIndex] < 0 and matrixA[rowIndex, colIndex] < 0):
                if (matrix[currentRow, colIndex] > 0 and matrix[rowIndex, colIndex] > 0) or (matrix[currentRow, colIndex] < 0 and matrix[rowIndex, colIndex] < 0):
                    currentLMatrix[currentRow, :] = currentLMatrix[currentRow, :] - currentLMatrix[rowIndex, :]*(float(matrix[currentRow, colIndex])/float(matrix[rowIndex, colIndex]))
                else:
                    currentLMatrix[currentRow, :] = currentLMatrix[currentRow, :] + currentLMatrix[rowIndex, :]*(float(matrix[currentRow, colIndex])/float(matrix[rowIndex, colIndex]))
                matrix = util.multiplyMatrices(currentLMatrix, matrix)
                currentLMatrix[currentRow, colIndex] = -currentLMatrix[currentRow, colIndex] #Changing the sign of the item so that we don't need to take the inverse later.
                lMatrixQueue.put(currentLMatrix)
        rowIndex = rowIndex + 1
        colIndex = colIndex + 1

    #Form the L matrix
    lMatrix = lMatrixQueue.get()
    while not lMatrixQueue.empty():
        lMatrix = util.multiplyMatrices(lMatrix, lMatrixQueue.get())

    #Get error
    error = util.matrix_max_norm(util.multiplyMatrices(lMatrix, matrix) - originalMatrix)

    #Return
    returnList = [lMatrix, matrix, error]
    return returnList
Beispiel #2
0
def solve_househ_b(matrixInput):
    if isinstance(matrixInput, basestring):
        matrixInput = np.loadtxt(matrixInput, unpack=False, delimiter=" ")
    matrixA = matrixInput[:, 0 : matrixInput.shape[1] - 1]
    matrixB = np.matrix(matrixInput[:, [matrixInput.shape[1] - 1]])
    matrixBCopy = np.copy(matrixB)
    matrixACopy = np.copy(matrixA)
    qr = qr_fact_househ(matrixACopy)
    matrixQCopy = np.copy(qr[0])
    matrixRCopy = np.copy(qr[1])
    xSolution = solve_b(matrixRCopy, solve_b(matrixQCopy, matrixBCopy))
    return [
        xSolution,
        util.matrix_max_norm(util.multiplyMatrices(matrixA, xSolution) - matrixB),
        util.matrix_max_norm(util.multiplyMatrices(qr[0], qr[1]) - matrixA),
    ]
Beispiel #3
0
def solve_lu_b(matrixInput):
    if isinstance(matrixInput, basestring):
        matrixInput = np.loadtxt(matrixInput, unpack=False, delimiter=" ")
    matrixA = matrixInput[:, 0 : matrixInput.shape[1] - 1]
    matrixB = np.matrix(matrixInput[:, [matrixInput.shape[1] - 1]])
    matrixBCopy = np.copy(matrixB)
    matrixACopy = np.copy(matrixA)
    lu = lu_fact(matrixACopy)
    matrixLCopy = np.copy(lu[0])
    matrixUCopy = np.copy(lu[1])
    xSolution = solve_b(matrixUCopy, solve_b(matrixLCopy, matrixBCopy))
    return [
        xSolution,
        util.matrix_max_norm(util.multiplyMatrices(matrixA, xSolution) - matrixB),
        util.matrix_max_norm(util.multiplyMatrices(lu[0], lu[1]) - matrixA),
    ]
Beispiel #4
0
def qr_fact_househ(matrix):
    if isinstance(matrix, basestring):
        matrix = np.loadtxt(matrix, unpack=False, delimiter=" ")
    originalMatrix = matrix
    diagonalIndex = 0
    hMatrixQueue = Queue.Queue()
    while diagonalIndex < matrix.shape[1] - 1:
        # Check to see if there are all zeros below the pivot
        allZeros = True
        for currentIndex in range(diagonalIndex + 1, matrix.shape[0]):
            if matrix[currentIndex, diagonalIndex] != 0:
                allZeros = False

        # If not all zeros
        if allZeros == False:
            # Creating the U matrix
            matrixU = np.zeros(shape=(matrix.shape[0] - diagonalIndex, 1))
            for currentIndex in range(0, matrix.shape[0] - diagonalIndex):
                matrixU[currentIndex, 0] = matrix[diagonalIndex + currentIndex, diagonalIndex]
            matrixU[0, 0] += util.vector_length(matrixU)

            # Creating the H matrix
            matrixH = np.identity(matrix.shape[0] - diagonalIndex)
            matrixH = matrixH - (2.0 / (util.vector_length(matrixU) ** 2)) * util.multiplyMatrices(
                matrixU, matrixU.transpose()
            )
            matrixHFinal = matrixH

            # Putting H matrix in correct size
            if matrixH.shape[0] < matrix.shape[0]:
                matrixHFinal = np.identity(matrix.shape[0])
                for rowIndex in range(diagonalIndex, matrix.shape[0]):
                    for colIndex in range(diagonalIndex, matrix.shape[1]):
                        matrixHFinal[rowIndex, colIndex] = matrixH[rowIndex - diagonalIndex, colIndex - diagonalIndex]

            # Put H in a queue
            hMatrixQueue.put(matrixHFinal)
            # Use matrix to form R
            matrix = util.multiplyMatrices(matrixHFinal, matrix)
        diagonalIndex += 1
    # Form Q
    matrixQ = hMatrixQueue.get()
    while not hMatrixQueue.empty():
        matrixQ = util.multiplyMatrices(matrixQ, hMatrixQueue.get())

    # Get error
    error = util.matrix_max_norm(util.multiplyMatrices(matrixQ, matrix) - originalMatrix)

    # Return Q and R
    returnList = [matrixQ, matrix, error]
    return returnList
Beispiel #5
0
def qr_fact_givens(matrix):
    if(isinstance(matrix, basestring)):
        matrix = np.loadtxt(matrix, unpack=False, delimiter=" ")
    originalMatrix = matrix
    diagonalIndex = 0
    gMatrixQueue = Queue.Queue()
    #while we still have diagonal elements
    while (diagonalIndex < matrix.shape[1] - 1):
        # X is the pivot
        x = matrix[diagonalIndex, diagonalIndex]
        for currentIndex in range(diagonalIndex + 1, matrix.shape[0]):
            #y is at index below the pivot
            y = matrix[currentIndex, diagonalIndex]
            if y != 0:
                # set cos and sin
                cos = x/math.sqrt(x**2 + y**2)
                sin = -y/math.sqrt(x**2 + y**2)
                matrixG = np.identity(matrix.shape[0])
                matrixG[diagonalIndex, diagonalIndex] = cos
                matrixG[currentIndex, diagonalIndex] = sin
                matrixG[diagonalIndex, currentIndex] = -sin
                matrixG[currentIndex, currentIndex] = cos

                #add g matrix and update matrix and pivot
                gMatrixQueue.put(matrixG.transpose())
                matrix = util.multiplyMatrices(matrixG, matrix)
                x = matrix[diagonalIndex,diagonalIndex]
        diagonalIndex += 1

    #Form Q
    matrixQ = gMatrixQueue.get()
    while not gMatrixQueue.empty():
        matrixQ = util.multiplyMatrices(matrixQ, gMatrixQueue.get())

    #Get error
    error = util.matrix_max_norm(util.multiplyMatrices(matrixQ, matrix) - originalMatrix)

    returnList = [matrixQ, matrix, error]
    return returnList