Exemplo n.º 1
0
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 solve_matrix3(a, b, matrix_type, x0, tol, Nmax, w):
    solution_dic = {}
    a, b, matrix = matrix_function.mix_matrix(a, b)
    x0 = matrix_function.fromStringToFloatVector(x0)
    try:
        tol = float(tol)
        Nmax = float(Nmax)
        w = float(w)
    except:
        dic = {}
        dic[0] = "Error Be careful with values of tolerance, iterations number or W values"
        print(json.dumps(dic))
        exit(1)
    if (matrix_function.determinant(a)):
        if (matrix_type == 'SOR'):
            d, l, u = matrix_function.extract_D_L_U(a)
            dic, dic_result, C_aux, spectral_radius = sor_method.sorMethod(
                l, d, u, b, x0, tol, Nmax, w)
            dic = matrix_function.rebuild_matrix(dic)
            dic["C"] = C_aux
            dic["spectral_radius"] = spectral_radius
        else:
            dic = {}
            dic[0] = "Error function type doesn't exist"
            print(json.dumps(dic))
            exit(1)

        print(json.dumps(dic))
        print(json.dumps(dic_result))
    else:
        dic = {}
        dic[0] = "Error function determinant is equals to 0"
        print(json.dumps(dic))
        exit(1)
def solve_matrix2(a, b, matrix_type, x0, tol, Nmax):
    solution_dic = {}
    a, b, matrix = matrix_function.mix_matrix(a, b)
    d, l, u = matrix_function.extract_D_L_U(a)
    x0 = matrix_function.fromStringToFloatVector(x0)
    try:
        tol = float(tol)
        Nmax = float(Nmax)
    except:
        dic = {}
        dic[0] = "Error be careful with values of tolerance or iterations number"
        print(json.dumps(dic))
        exit(1)
    if (matrix_function.determinant(a)):
        if (matrix_type == 'J'):
            dic, dic_result, C_aux, spectral_radius = jacobi_method.jacobiMethod(
                l, d, u, b, x0, tol, Nmax)
            dic = matrix_function.rebuild_matrix(dic)
            dic["C"] = C_aux
            dic["spectral_radius"] = spectral_radius
        elif (matrix_type == 'GS'):
            dic, dic_result, C_aux, spectral_radius = gauss_seidel.gauss_seidel(
                l, d, u, b, x0, tol, Nmax)
            dic = matrix_function.rebuild_matrix(dic)
            dic["C"] = C_aux
            dic["spectral_radius"] = spectral_radius
        else:
            dic = {}
            dic[0] = "Error function type doesn't exist"
            print(json.dumps(dic))
            exit(1)

        print(json.dumps(dic))
        print(json.dumps(dic_result))

    else:
        dic = {}
        dic[0] = "Error function determinant is equals to 0"
        print(json.dumps(dic))
        exit(1)
Exemplo n.º 5
0
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))
Exemplo n.º 8
0
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)