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 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))
예제 #3
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 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)
예제 #5
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)