Пример #1
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
Пример #2
0
def power_method(matrix, ev, error, n, inverse):
    if(isinstance(matrix, basestring)):
        matrix = np.genfromtxt(matrix, unpack=False, delimiter=" ")
    if(isinstance(ev, basestring)):
        ev = np.genfromtxt(ev, unpack=False, delimiter=" ")
        ev =np.reshape(ev, (ev.shape[0], 1))
    if matrix.shape[0] != matrix.shape[0]:
        print "matrix must be square"
        return
    else:
        matrixU = ev
        matrixW = np.zeros(shape=(ev.shape[0],1))
        matrixW[0,0]=1
        eValue = 0

        for i in range(1, n):
            eValueOld = eValue
            oldMatrixU = matrixU
            matrixU = util.multiplyMatrices(matrix, matrixU)
            eValue = np.dot(matrixW.transpose(), matrixU) / np.dot(matrixW.transpose(),oldMatrixU)
            if (abs(eValueOld - eValue) < error):
                eVector = matrixU / util.vector_length(matrixU)
                if (inverse == True):
                    eValue = 1 / eValue
                return [eValue, eVector, i]
        return "failure"