def cholesky(A,b): answer = {} answer["error"] = False dict_A = {} dict_L = {} dict_U = {} dict_X = {} try: A,b = parser_input_helper.parse_input(A,b) dict_A[0] = parser_input_helper.rebuild_matrix(copy.deepcopy(A)) if np.linalg.det(A) == 0: raise Exception("Derterminant can not be 0") n = len(A) L = [[0.0 for x in range(n)] for y in range(n)] U = [[0.0 for x in range(n)] for y in range(n)] L = np.array(L) U = np.array(U) step = 1 for k in range(n): sum1 = 0.0 for i in range(n): sum1 += L[k][i] * U[i][k] L[k][k] = math.sqrt(A[k][k] - sum1) U[k][k] = L[k][k] for i in range(k + 1, n): sum2 = 0.0 for p in range(k): sum2 += L[i][p] * U[p][k] L[i][k] = (A[i][k] - sum2) / U[k][k] for j in range(k + 1, n): sum3 = 0.0 for p in range(k): sum3 += L[k][p] * U [p][j] U[k][j] = (A[k][j] - sum3) / L[k][k] # print("Step 0") # npA = np.array(A) # dict_A[0] = rebuild_matrix(copy.deepcopy(npA)) # print(dict_A[0]) dict_L[step] = parser_input_helper.rebuild_matrix(copy.deepcopy(L)) dict_U[step] = parser_input_helper.rebuild_matrix(copy.deepcopy(U)) step += 1 z = np.array(matrix_function.soltion(L,b),float) x = matrix_function.soltion(U,z) except ValueError as valueError: answer["error"] = "A complex operation was encounter while running the method" except BaseException as e: answer["error"] = str(e) print(json.dumps(answer)) print(json.dumps(dict_A)) print(json.dumps(dict_L)) print(json.dumps(dict_U)) try: dict_X["solution"] = list(x) print(json.dumps(dict_X)) except BaseException: pass
def crout(A, b): try: A, b, matrix = mf.mix_matrix(A, b) A = np.array(A) b = np.array(b) n = len(A) L = np.eye(n) U = np.eye(n) dic_a = {} dic_l = {} dic_u = {} dic_a[0] = np.array(A) dic_l[0] = np.array(L) dic_u[0] = np.array(U) for i in range(n): for k in range(i, n): suma = 0 for j in range(i): suma += (L[k][j] * U[j][i]) L[k][i] = A[k][i] - suma for k in range(i, n): if (i == k): U[i][i] = 1 else: suma = 0 for j in range(i): suma += (L[i][j] * U[j][k]) U[i][k] = ((A[i][k] - suma) / L[i][i]) dic_a[i + 1] = np.array(A) dic_l[i + 1] = np.array(L) dic_u[i + 1] = np.array(U) z = np.array(mf.soltion(L, b), float) x = mf.soltion(U, z) sol = [] for i in range(0, len(x)): sol.append(float(x[i])) solution = {} solution[0] = sol except BaseException as e: print("Error" + str(e)) exit(1) dic_a = mf.rebuild_matrix(dic_a) dic_l = mf.rebuild_matrix(dic_l) dic_u = mf.rebuild_matrix(dic_u) print(json.dumps(solution)) print(json.dumps(dic_a)) print(json.dumps(dic_l)) print(json.dumps(dic_u))
def doolittle(A, b, size): size = int(size) A, b, matrix = matrix_function.mix_matrix(A, b) A = np.array(A) if matrix_function.determinant(A): try: b = np.array(b) L = np.eye(size) U = np.eye(size) dictL = {} dictU = {} count = 0 dictL[count] = copy.deepcopy(L) dictU[count] = copy.deepcopy(U) for i in range(size): for k in range(i, size): suma = 0 for j in range(i): suma += (L[i][j] * U[j][k]) U[i][k] = A[i][k] - suma for k in range(i, size): if (i == k): L[i][i] = 1 else: suma = 0 for j in range(i): suma += (L[k][j] * U[j][i]) L[k][i] = ((A[k][i] - suma) / U[i][i]) count = count + 1 dictL[count] = copy.deepcopy(L) dictU[count] = copy.deepcopy(U) z = np.array(matrix_function.soltion(L, b), float) x = matrix_function.soltion(U, z) sol = [] for i in range(0, len(x)): sol.append(float(x[i])) solution = {} solution[0] = sol dictL = matrix_function.rebuild_matrix(dictL) dictU = matrix_function.rebuild_matrix(dictU) print(json.dumps(solution)) print(json.dumps(dictL)) print(json.dumps(dictU)) except: results = {} results[0] = "Error Divide by 0" print(json.dumps(results)) else: results = {} results[0] = "Error The matrix determinant is 0" print(json.dumps(results))
def initialData(A,b): results = {} try: A,b,matrix2 = matrix_function.mix_matrix(A,b) if (matrix_function.determinant(A) == True): matrix2 = np.array(matrix2) A,B,dic = SteppedPartialPivot(matrix2) dic = matrix_function.rebuild_matrix(dic) print(json.dumps(dic)) x = matrix_function.soltion(A,B) x = x.tolist() xSolution = {} xSolution[0] = x print(json.dumps(xSolution)) else: results[0] = "Error the matrix determinant is 0" print(json.dumps(results)) except BaseException as e: results[0] = "Error" + str(e) print(json.dumps(results))
def vandermonde(X, Y): dic = {} X = X.replace('[', '').replace(']', '').split(',') Y = Y.replace('[', '').replace(']', '').split(',') X = np.array(X, dtype=float) Y = np.array(Y, dtype=float) validate = False unique_elements, counts_elements = np.unique(X, return_counts=True) for i in counts_elements: if(i>1): validate=True break if (validate): dic[0]="Error: there are equal points in the vector X" else: n = len(X) A = np.zeros((n,n)) for i in range(n): for j in range(n): A[j][i] = X[j]**(n-(i+1)) coef = np.array(mf.soltion(A, Y.T)) dic["v_matrix"] = np.array(A) dic = mf.rebuild_matrix(dic) dic["coef"] = str(coef) polynomial = "" for i in range(len(coef)): if coef[i][0] != '-' and i != 0: polynomial += "+" polynomial += coef[i] if(i != len(coef)-1): polynomial += "x^"+str((len(coef)-(i+1))) dic["polynomial"] = polynomial print(json.dumps(dic))
def gaussianTridiagonalMatrixMethod(a, b, c, d): dic = {} a = a.replace('[', '').replace(']', '').split(',') b = b.replace('[', '').replace(']', '').split(',') c = c.replace('[', '').replace(']', '').split(',') d = d.replace('[', '').replace(']', '').split(',') a = np.array(a, dtype=float) b = np.array(b, dtype=float) c = np.array(c, dtype=float) d = np.array(d, dtype=float) validate = False for i in b: if (i==0): validate = True break if (validate): dic[0]="Error: There are points equals to zero in vector b" else: n = len(d) # número de filas matrix = np.zeros((n,n)) matrix[0][0] = b[0] for i in range(n-1): m = a[i]/b[i] matrix[i+1][i+1] = b[i+1] = b[i+1] - (m*c[i]) matrix[i][i+1] = c[i] d[i+1] = d[i+1] - (m*d[i]) dic[i]=np.array(matrix) d = np.array(d) dic[n-1] = np.array(matrix) dic = matrix_function.rebuild_matrix(dic) x = matrix_function.soltion(matrix, d) dic["x"]=x.tolist() print(json.dumps(dic))
def solve_matrix(a, b, matrix_type): try: solution_dic = {} a, b, matrix = matrix_function.mix_matrix(a, b) if (matrix_function.determinant(a)): if (matrix_type == 'S'): a, b, dic = simple_gaussian_method.simpleGaussianMethod(matrix) elif (matrix_type == 'P'): a, b, dic = partial_gaussian_method.partialGaussianMethod( matrix) elif (matrix_type == 'T'): a, b, dic, movement = total_gaussian_method.totalGaussianMethod( matrix) elif (matrix_type == 'LUS'): #LU_simple l, u, dic, dic_l, dic_u = lu_simple_method.luSimpleMethod(a) elif (matrix_type == 'LUP'): #partial_lu l, u, pb, dic, dic_l, dic_u, dic_p = partial_lu.partial_lu( a, b) else: print("function type doesn't exist") if (matrix_type == 'LUP'): z = np.array(matrix_function.soltion(l, pb), float) x = matrix_function.soltion(u, z) dic_l = matrix_function.rebuild_matrix(dic_l) dic_u = matrix_function.rebuild_matrix(dic_u) dic_p = matrix_function.rebuild_matrix(dic_p) elif (matrix_type == 'LUS'): z = np.array(matrix_function.soltion(l, b), float) x = matrix_function.soltion(u, z) dic_l = matrix_function.rebuild_matrix(dic_l) dic_u = matrix_function.rebuild_matrix(dic_u) #dic["dic_l"] = dic_l #dic["dic_u"] = dic_u else: x = matrix_function.soltion(a, b) dic = matrix_function.rebuild_matrix(dic) if (matrix_type == 'T'): x = matrix_function.sort(x, movement) dic["x"] = x.tolist() solution_dic["dic"] = dic solution_dic["x"] = x print(json.dumps(dic)) if (matrix_type == 'LUS'): print(json.dumps(dic_l)) print(json.dumps(dic_u)) elif (matrix_type == 'LUP'): print(json.dumps(dic_l)) print(json.dumps(dic_u)) print(json.dumps(dic_p)) else: dic = {} raise Exception("Error function determinant is equals to 0") except BaseException as e: dic = {} dic[0] = "Error" + str(e) print(json.dumps(dic)) exit(1)
def linealSpline(data, n): results = {} data = data.replace('[', '').replace(']', '').split(',') x = [] y = [] validate = False validate2 = False try: for i in range(0, len(data)): if i < int(n): x.append(float(data[i])) else: y.append(float(data[i])) for i in range(len(x)): if (x.count(x[i]) > 1): validate = True break validate2 = True except: results[0] = "Error in initial data" if validate == True: results[0] = "Error X coordinates contain a duplicate" elif validate2 == True: xValor = sm.symbols('x') sizeX = len(x) sizeY = len(y) if sizeX != sizeY: results[ 0] = "Error the size of the x vector is different to y vector" else: try: m = 2 * (sizeX - 1) A = np.eye(m) for i in range(0, m): A[i][i] = 0 counter = 0 counterRows = 0 b = [] for i in range(0, sizeX): vec_x = x[i] A[i][counter] = vec_x A[i][counter + 1] = 1 counter = counter + 2 if i == 0: counter = 0 b.append(y[i]) counterRows = counterRows + 1 counter = 0 for i in range(1, len(x) - 1): A[counterRows][counter] = x[i] A[counterRows][counter + 1] = 1 A[counterRows][counter + 2] = -(x[i]) A[counterRows][counter + 3] = -1 counter = counter + 2 counterRows = counterRows + 1 b.append(0) array = [] for i in range(0, m): array2 = [] for j in range(0, m): array2.append(A[i][j]) array.append(array2) A = str(array).replace(" ", "") b = str(b) a, b, matrix = matrix_function.mix_matrix(A, b) a, b, dic, movement = total_gaussian_method.totalGaussianMethod( matrix) x = matrix_function.soltion(a, b) x = matrix_function.sort(x, movement) aux = [] for i in range(0, len(x)): aux.append(x[i]) coefficient = [] counter = 0 counterAux = 0 while counter < len(aux) - 1: i = [] i.append(aux[counter]) i.append(aux[counter + 1]) counter = counter + 2 results[counterAux] = i counterAux = counterAux + 1 coefficient.append(i) plotter = [] for i in range(len(coefficient)): plotter.append( str((float(coefficient[i][0]) * xValor) + float(coefficient[i][1]))) results["plotter"] = plotter except: results[0] = "Error to found a solution to x array" data = json.dumps(results) print(data)