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
def getHarmFlow(matrices): y, w, grad, gradAdj, curl, curlAdj = matrices S = np.matmul(curlAdj, curl) - np.matmul(grad, gradAdj) Dpos = np.diag(w**0.5) Dneg = np.diag(w**-0.5) S2 = util.multiplyMatrices([Dpos, S, Dneg]) S2pinv = np.linalg.pinv(S2) harmFlow = y - util.multiplyMatrices([Dneg, S2pinv, S2, Dpos, y]) return harmFlow
def iter(x0, sigma, m, S, T): S_inv = inverse_lower_3x3(S) counter = 1 x_cur = x0 while (counter <= m): x_prev = x_cur x_cur = multiplyMatrices(S_inv, multiplyMatrices(T, x_cur) + B) err = x_cur - x_prev if math.sqrt(dot_product(err, err)) <= sigma: return (x0, x_cur, counter) counter += 1 return None
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
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"
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), ]
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), ]
def getGradFlow(matrices): y, w, grad, gradAdj, curl, curlAdj = matrices lapInv = np.linalg.pinv(np.matmul(gradAdj, grad)) # Inv of laplacian s = util.multiplyMatrices([lapInv, gradAdj, y]) gradFlow = np.matmul(grad, s) return gradFlow, s
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