def manual_mod_homog_1(coefficients, initial_terms, degree, filename): try: # First try this step automated sequence = "s(n)=" i = 0 for x in coefficients: if parse_expr(coefficients[i]) >= 0 and i == 0: sequence = sequence + "(" + str( coefficients[i]) + ")*s(n-" + str(i + 1) + ")" # add a + for the positive coefficients elif parse_expr(coefficients[i]) >= 0: sequence = sequence + "+(" + str( coefficients[i]) + ")*s(n-" + str(i + 1) + ")" elif parse_expr(coefficients[i]) < 0: sequence = sequence + "(" + str( coefficients[i]) + ")*s(n-" + str(i + 1) + ")" else: print("wrong sequence step 1") i = i + 1 print( color.GREEN + "Step 1: The rewritten sequence is: \n" + str(sequence) + "\n", color.RESET) # Ask if altering is needed continue_or_not = input("Is this correct? (yes or no)\n") if continue_or_not == "yes": # print("Great!") manual_mode_homog_2(coefficients, initial_terms, degree, sequence, filename) elif continue_or_not == "no": sequence = input("Give the relation manually:\n") # Confirm manual input or quit confirm_or_exit = input("Is this correct? (yes or no)\n") if confirm_or_exit == "yes": manual_mode_homog_2(coefficients, initial_terms, degree, sequence, filename) else: print("Ending manual mode") else: print("Neither yes nor no given") except Exception as error: print("1 doesnt work, ERROR: {}\n".format(error)) print(color.RED + "Error occurs in file: {}\n".format(filename), color.RESET) try: file_writer.error_in_file(filename=filename, homogeneous=True, step="Step 1", automatic=True, error=error) except Exception as error: print( color.RED + "Error during writing error file.\nHere is the data:\nFile: {}\nHomogeneous: {}\nStep: {}\nAutomatic: {}\nOrginal error: {}\n" .format(filename, True, "Step 1", True, error))
def manual_mode_homog_2(coefficients, initial_terms, degree, sequence, filename): try: # First try this step automated characteristic_equation = char_equation_2(coefficients) print("\nStep 2: The characteristic equation is: \n" + str(characteristic_equation) + "=0" + "\n") # Ask if altering is needed continue_or_not = input("Is this correct? (yes or no)\n") if continue_or_not == "yes": manual_mode_homog_3(coefficients, initial_terms, degree, characteristic_equation, filename) elif continue_or_not == "no": characteristic_equation = input( "Manually input the characteristic equation:\n") # Confirm manual input or quit confirm_or_exit = input("Is this correct? (yes or no)\n") if confirm_or_exit == "yes": manual_mode_homog_3(coefficients, initial_terms, degree, characteristic_equation, filename) else: print("Ending manual mode") else: print("Neither yes nor no given") except: print("2 doesnt work, shocker dude") print(color.RED + "Error occurs in file: {}".format(filename), color.RESET) try: file_writer.error_in_file(filename=filename, homogeneous=True, step="Step 2", automatic=True, error=error) except Exception as error: print( color.RED + "Error during writing error file.\nHere is the data:\nFile: {}\nHomogeneous: {}\nStep: {}\nAutomatic: {}\nOrginal error: {}\n" .format(filename, True, "Step 2", True, error))
def solve_nonhom_relations(filename, fn_parts, fn_part_sn, degree, initial_terms, homogeneous_coeffs, ordered_relation): # Step 1: rewrite the relation to its default form: a_n = homog + F(n) homogeneous_part = "" # fn_part = "3**2" # Needs to be in the right order, so biggest power to lowest power # fn_parts = [0, 3] # every elemts in this list is the next b in fn, so bn**0, bn**1, bn**2 # all powers from max up to and including 0 # fn_parts = {1:1, 0:0} # power:coeff, so n^2 = 2:1, cuz power=2 ^ coeff=1. SORTED FROM HIGH TO LOW 2:1, 1:3, 0:2 # fn_part_sn = 3 # the s in s**n, if no s**n part in fn, then put 1 here highest_power_fn_part = next( iter(fn_parts) ) # represents the "t" in the particular solution of non-hom part (step 5) # degree = 2 # initial_terms = [1, 4] # homogeneous_coeffs = [8, 2] # coeffs of the associated homogeneous relation # try: fn_parts = literal_eval(fn_parts) print("Converted fn parts: {}".format(type(fn_parts))) print("The dict is now: {}".format(fn_parts)) except Exception as error: print("cannot read f_parts as dict.\nError: {}".format(error)) fn_parts = fn_parts print("We have the fn parts set to string: {}".format(type(fn_parts))) fn_part_sn = fn_part_sn degree = int(degree[0]) initial_terms = initial_terms homogeneous_coeffs = homogeneous_coeffs ordered_relation = ordered_relation highest_power_fn_part = next( iter(fn_parts) ) # represents the "t" in the particular solution of non-hom part (step 5) # Step 2: Obtaining the characteristic equation of the associated homog part try: characteristic_equation = char_equation_2(homogeneous_coeffs) print("Step 2: The characteristic equation is: \n" + str(characteristic_equation) + "=0" + "\n") except Exception as error: print( color.RED + "2 doesnt work, shocker dude: ERROR: {}".format(error), color.RESET) print("2 doesnt work, shocker dude: ERROR: {}".format(error)) try: file_writer.error_in_file(filename=filename, homogeneous=False, step="Step 2", automatic=True, error=error) except Exception as error: print( color.RED + "Error during writing error file.\nHere is the data:\nFile: {}\nHomogeneous: {}\nStep: {}\nAutomatic: {}\nOrginal error: {}\n" .format(filename, False, "Step 2", True, error), color.RESET) return # Step 3: Obtain the roots of the associated homog part try: r = symbols('r') r_and_m_found = roots(characteristic_equation, r) # returns root:multiplicity print("Step 3: The roots of this equation are:") print(r_and_m_found) # root:multiplicity print() except Exception as error: print( color.RED + "3 doesnt work, shocker dude: ERROR: {}".format(error), color.RED) print("3 doesnt work, shocker dude: ERROR: {}".format(error)) try: file_writer.error_in_file(filename=filename, homogeneous=False, step="Step 3", automatic=True, error=error) except Exception as error: print( color.RED + "Error during writing error file.\nHere is the data:\nFile: {}\nHomogeneous: {}\nStep: {}\nAutomatic: {}\nOrginal error: {}\n" .format(filename, False, "Step 3", True, error), color.RESET) return # Step 4: Obtain general solution of the associated homog part try: general_solution = find_general_solution_2(r_and_m_found) general_solution = general_solution.replace("s(n)=+", "s(n)=") # general_solution = "Sup Nerd!" print("Step 4: The general solution of this equation is: \n" + str(general_solution) + "\n") except Exception as error: print( color.RED + "4 doesnt work, shocker dude: ERROR: {}".format(error), color.RESET) try: file_writer.error_in_file(filename=filename, homogeneous=False, step="Step 4", automatic=True, error=error) except Exception as error: print( color.RED + "Error during writing error file.\nHere is the data:\nFile: {}\nHomogeneous: {}\nStep: {}\nAutomatic: {}\nOrginal error: {}\n" .format(filename, False, "Step 4", True, error), color.RESET) return # Step 5: Obtain the particular solution of the non-homog part try: if type(fn_parts) == dict: particular_solution = find_part_sol_non_homog( fn_parts, fn_part_sn, highest_power_fn_part, r_and_m_found, degree, ordered_relation) else: """If the theorem is not needed, the solution is already calculated in the file reader.""" particular_solution = fn_parts print("Step 5: The particular solution of the non-homog part is: \n" + "s(n)=" + str(particular_solution) + "\n") except Exception as error: print( color.RED + "5 doesnt work, shocker dude: ERROR: {}".format(error), color.RESET) # particular_solution = find_part_sol_non_homog(fn_parts, fn_part_sn, highest_power_fn_part, r_and_m_found) try: file_writer.error_in_file(filename=filename, homogeneous=False, step="Step 5", automatic=True, error=error) except Exception as error: print( color.RED + "Error during writing error file.\nHere is the data:\nFile: {}\nHomogeneous: {}\nStep: {}\nAutomatic: {}\nOrginal error: {}\n" .format(filename, False, "Step 5", True, error), color.RESET) return # print("The particular solution is: {}".format(particular_solution)) # Step 6: Obtain the solution a_n = a_n(p) + a_n(h) try: general_solution = general_solution.replace("s(n)=", "") setup_spec_sol = "s(n)=" + particular_solution + "+" + general_solution # for step 7.2 print("Step 6: a_n = a_n(p) + a_n(h):\n" + "s(n)=" + particular_solution + "+" + general_solution + "\n") except Exception as error: print( color.RED + "6 doesnt work, shocker dude: ERROR: {}".format(error), color.RESET) try: file_writer.error_in_file(filename=filename, homogeneous=False, step="Step 6", automatic=True, error=error) except Exception as error: print( color.RED + "Error during writing error file.\nHere is the data:\nFile: {}\nHomogeneous: {}\nStep: {}\nAutomatic: {}\nOrginal error: {}\n" .format(filename, False, "Step 6", True, error), color.RESET) return # Step 7.1: Obtain the alpha values with a_n(p) + a_n(h) try: outcome = find_alpha_values(initial_terms, r_and_m_found, particular_solution) print("Step 7.1: The value of the Alphas:") print(outcome) except Exception as error: print( color.RED + "7.1 doesnt work, shocker dude: ERROR: {}".format(error), color.RESET) try: file_writer.error_in_file(filename=filename, homogeneous=False, step="Step 7.1", automatic=True, error=error) except Exception as error: print( color.RED + "Error during writing error file.\nHere is the data:\nFile: {}\nHomogeneous: {}\nStep: {}\nAutomatic: {}\nOrginal error: {}\n" .format(filename, False, "Step 7.1", True, error), color.RESET) return # Step 7.2: Obtain specific solution try: # specific_solution = setup_spec_sol specific_solution = get_specific_solution(setup_spec_sol, outcome) file_writer.write_solution(filename=filename, solution=specific_solution) print( color.BLUE + "\nStep 7.2: The specific solution for this equation is: \n" + str(specific_solution) + "\n", color.RESET) return specific_solution except Exception as error: print( color.RED + "7.2 doesnt work, shocker dude: ERROR: {}".format(error), color.RESET) try: file_writer.error_in_file(filename=filename, homogeneous=False, step="Step 7.2", automatic=True, error=error) except Exception as error: print( color.RED + "Error during writing error file.\nHere is the data:\nFile: {}\nHomogeneous: {}\nStep: {}\nAutomatic: {}\nOrginal error: {}\n" .format(filename, False, "Step 7.2", True, error), color.RESET) return
def menu(): #This function is the main menu for the program. # banner() try: print("""Menu, choose a number:\n 1) Solve the equations automatically.\n 2) Solve manually equations.\n 3) Remove some files.\n 4) Check outcomes of solutions and relations.\n 5) Exit the program.\n""") choice = int(input("Enter your choice: ")) # choice = 1 if choice == 1: read_files() #For loop to find all commass files, then find the degree of all these files. # find_degree.find_degree(pathstring=) """Find all the commass files in the homogeneous folder""" hom_comass_path = str( os.path.dirname(os.path.realpath(__file__)) + str(Path("/output_files/homogeneous/comass[0-9][0-9].txt"))) print( color.CYAN + "\n\nAUTOMATC READING HOMOGENEOUS EQUATION.\n\n", color.RESET) time.sleep(1) #Find every comass file in the /homogeneous/ folder. Then process the 5 steps for every file. for hom_comass_file in glob.glob(hom_comass_path): print( color.GREEN + "\nHomogeneous file {} found!\n".format(hom_comass_file), color.RESET) """The automatic mode uses the hom_calling_test.py file. THIS IS THE HOMOGENEOUS PART!""" """Move the files from the root folder, to the automatic folder.""" file_writer.move_to_step(filename=hom_comass_file, homogeneous=True, step="automatic") degree = file_reader.read_lists_from_files( file_type="degree", filename=hom_comass_file, homogeneous=True, automatic=True, step=None) coefficients = file_reader.read_lists_from_files( file_type="coefficients", filename=hom_comass_file, homogeneous=True, automatic=True, step=None) initial_terms = file_reader.read_lists_from_files( file_type="init", filename=hom_comass_file, homogeneous=True, automatic=True, step=None) parts = file_reader.read_lists_from_files( file_type="parts", filename=hom_comass_file, homogeneous=True, automatic=True, step=None) print( color.MAGENTA + "Degree is: {}\nInitial terms are: {}\nCoefficients are: {}\nParts are: {}\n" .format(degree, initial_terms, coefficients, parts), color.RESET) try: hom_solution = hom_calling_test.solve_homog_relation( degree=degree, initial=initial_terms, parts=parts, coefficients=coefficients, filename=hom_comass_file) try: pass # answer_checker.automatic_check_full_automatic(filename=hom_comass_file, degree=degree, initial_terms=initial_terms,all_coefficient=coefficients, fn_part=None,homogeneous=True,specific_solution=hom_solution) except IOError: pass # if there exist no comass file then continue because there are only few dir files. except Exception as error: print(color.RED + "Cannot check if answer is correct!\nERROR: {}\n" .format(error)) except Exception as error: print( color.RED + "Error in hom_calling_test, try manually!\nERROR: {}\n" .format(error), color.RESET) try: file_writer.error_in_file(filename=hom_comass_file, homogeneous=True, step=None, error=error, automatic=True) print(color.LIGHTGREEN_EX + "Error file created!\n", color.RESET) except Exception as error: print(color.RED + "Can not create error file for: {}\nERROR: {}\n". format(hom_comass_file, error)) """Find all the commass files in the nonhomogeneous folder""" nonhom_comass_path = str( os.path.dirname(os.path.realpath(__file__)) + str( Path( "/output_files/nonhomogeneous/comass[0-9][0-9].txt" ))) print( color.CYAN + "\n\nAUTOMATC READING NON-HOMOGENEOUS EQUATION.\n\n", color.RESET) time.sleep(1) # Find every comass file in the /homogeneous/ folder. Then process the 5 steps for every file. for nonhom_comass_file in glob.glob(nonhom_comass_path): print( color.GREEN + "\nNon-Homogeneous file {} found!\n".format( nonhom_comass_file), color.RESET) """The automatic mode uses the hom_calling_test.py file. THIS IS THE NON-HOMOGENEOUS PART!""" """Move the files from the root folder, to the automatic folder.""" file_writer.move_to_step(filename=nonhom_comass_file, homogeneous=False, step="automatic") degree = file_reader.read_lists_from_files( file_type="degree", filename=nonhom_comass_file, homogeneous=False, automatic=True, step=None) coefficients = file_reader.read_lists_from_files( file_type="coefficients", filename=nonhom_comass_file, homogeneous=False, automatic=True, step=None) initial_terms = file_reader.read_lists_from_files( file_type="init", filename=nonhom_comass_file, homogeneous=False, automatic=True, step=None) parts = file_reader.read_lists_from_files( file_type="parts", filename=nonhom_comass_file, homogeneous=False, automatic=True, step=None) fn_parts = file_reader.read_lists_from_files( file_type="fn_parts", filename=nonhom_comass_file, homogeneous=False, automatic=True, step=None) fn_parts_sn = file_reader.read_lists_from_files( file_type="fn_part_sn", filename=nonhom_comass_file, homogeneous=False, automatic=True, step=None) ordered_relation = file_reader.read_lists_from_files( file_type="ordered_relation", filename=nonhom_comass_file, homogeneous=False, automatic=True, step=None) print( color.MAGENTA + "Degree is: {}\nInitial terms are: {}\nCoefficients are: {}\nParts are: {}\nfn_parts are: {}\nfn_parts_sn is: {}\nOrdered Realtion: {}\n" .format(degree, initial_terms, coefficients, parts, fn_parts, fn_parts_sn, ordered_relation), color.RESET) try: nonhom_solution = nonhom_calling_test.solve_nonhom_relations( filename=nonhom_comass_file, fn_parts=fn_parts, fn_part_sn=fn_parts_sn, degree=degree, initial_terms=initial_terms, homogeneous_coeffs=coefficients, ordered_relation=ordered_relation) try: pass # answer_checker.automatic_check_full_automatic(filename=nonhom_comass_file, degree=degree, initial_terms=initial_terms, all_coefficient=coefficients, fn_part=fn_parts, homogeneous=False, specific_solution=nonhom_solution) except IOError: pass #if there exist no comass file then continue because there are only few dir files. except Exception as error: print( color.RED + "Cannot check if answer is correct!\nERROR: {}\n" .format(error)) except Exception as error: print( color.RED + "Error in nonhom_calling_test, try manually!\nERROR: {}\n" .format(error), color.RESET) try: file_writer.error_in_file( filename=nonhom_comass_file, homogeneous=False, step=None, error=error, automatic=True) print( color.LIGHTGREEN_EX + "Error file created!\n", color.RESET) except Exception as error: print( color.RED + "Can not create error file for: {}\nERROR: {}\n" .format(nonhom_comass_file, error)) elif choice == 2: manual_mode( filename="empty") # Is filename needed here? since it's manual elif choice == 3: print("Which files do you want to remove?.\n") print("""1) All files, including solutions.\n 2) Error files.\n 3) All files in a particular step.\n 4) Remove all solutions.\n""") delete_choice = int(input("Enter your choice: ")) if delete_choice == 1: file_remover.remove_all() elif delete_choice == 2: file_remover.remove_error() elif delete_choice == 3: homogeneous = bool( input("Is de folder homogeneous (True or False)?: ")) folder = input("Type folder name: ") file_remover.remove_files_in_folder(homogeneous=homogeneous, folder=folder) elif delete_choice == 4: file_remover.remove_all_solutions() elif choice == 4: answer_check_manual_or_auto() elif choice == 5: print("Closing program, BYE!\n") exit() else: print("This number is unknown.\n") exit() except IOError as error: print(color.RED + "Cannot find file: {}\n".format(error), color.RESET) time.sleep(2) menu() except ValueError: print(color.RED + "Only integers allowed!\n", color.RESET) time.sleep(2) menu()
def solve_homog_relation(degree, initial, coefficients, parts, filename): # Step 0: Read .txt and obtain initial terms (list?), degree and each C_1*A_n-1 """ IMPORTANT: if a_1 and a_3 are the used terms, then list needs 3 coeffs, one for a_2 as well, which is 0!!! if there's no coeff in front of a_x then 1 needs to be written as a coeff in the list """ # degree = find_degree(pathstring=pathstring, degree=0) # initial_terms = [1, 1] # List of all initial terms # coefficients = [1, 1] # if n+1 in s():=, then make every n -1 (so one add a -1 to the n's) # parts = ["*s(n-1)", "*s(n-2)", "*s(n-3)"] # If terms come from read.txt function, then comment this line # # fill in all parts and coeffs, if n-2 and n-4 only, then still fill in 0*n-3, etc. """Where do you use this degree??""" degree = degree initial_terms = initial coefficients = coefficients parts = parts # print(color.GREEN + "Degree is: {}\nInitial terms are: {}\nCoefficients are: {}\nParts are: {}\n".format(degree, initial_terms, coefficients,parts), color.RESET) # Step 1: Rewriting the sequence # Maybe replace this part with now Rico's read parts added together from the dictionary sort result? try: sequence = "s(n)=" i = 0 for x in coefficients: if int(coefficients[i]) >= 0 and i == 0: sequence = sequence + str(coefficients[i]) + str(parts[i]) # add a + for the positive coefficients elif int(coefficients[i]) >= 0: sequence = sequence + "+" + str(coefficients[i]) + str(parts[i]) elif int(coefficients[i]) < 0: sequence = sequence + str(coefficients[i]) + str(parts[i]) else: print("wrong sequence step 1") i = i + 1 print("Step 1: The rewritten sequence is: \n" + str(sequence) + "\n") except Exception as error: print("1 doesnt work, ERROR: {}\n".format(error)) print(color.RED + "Error occurs in file: {}\n".format(filename), color.RESET) try: file_writer.error_in_file(filename=filename, homogeneous=True, step="Step 1", automatic=True, error=error) except Exception as error: print(color.RED + "Error during writing error file.\nHere is the data:\nFile: {}\nHomogeneous: {}\nStep: {}\nAutomatic: {}\nOrginal error: {}\n".format(filename, True, "Step 1", True, error), color.RESET) return # Step 2: Obtaining the characteristic equation try: characteristic_equation = char_equation_2(coefficients) print("Step 2: The characteristic equation is: \n" + str(characteristic_equation) + "=0" + "\n") except: print("2 doesnt work, shocker dude") print(color.RED + "Error occurs in file: {}".format(filename), color.RESET) try: file_writer.error_in_file(filename=filename, homogeneous=True, step="Step 2", automatic=True, error=error) except Exception as error: print( color.RED + "Error during writing error file.\nHere is the data:\nFile: {}\nHomogeneous: {}\nStep: {}\nAutomatic: {}\nOrginal error: {}\n".format( filename, True, "Step 2", True, error), color.RESET) return # Step 3: Obtain the roots try: r = symbols('r') r_and_m_found = roots(characteristic_equation, r) # returns root:multiplicity print("Step 3: The roots of this equation are:") print(r_and_m_found) # root:multiplicity except: print("3 doesnt work, shocker dude") print(color.RED + "Error occurs in file: {}".format(filename), color.RESET) try: file_writer.error_in_file(filename=filename, homogeneous=True, step="Step 3", automatic=True, error=error) except Exception as error: print( color.RED + "Error during writing error file.\nHere is the data:\nFile: {}\nHomogeneous: {}\nStep: {}\nAutomatic: {}\nOrginal error: {}\n".format( filename, True, "Step 3", True, error), color.RESET) return # Step 4: Obtain general solution try: general_solution = find_general_solution_2(r_and_m_found) general_solution = general_solution.replace("s(n)=+", "s(n)=") print("Step 4: The general solution of this equation is: \n" + str(general_solution) + "\n") except: print("4 doesnt work, shocker dude") print(color.RED + "Error occurs in file: {}".format(filename), color.RESET) try: file_writer.error_in_file(filename=filename, homogeneous=True, step="Step 4", automatic=True, error=error) except Exception as error: print( color.RED + "Error during writing error file.\nHere is the data:\nFile: {}\nHomogeneous: {}\nStep: {}\nAutomatic: {}\nOrginal error: {}\n".format( filename, True, "Step 4", True, error), color.RESET) return # Step 5.1: Obtain alpha values try: outcome = hom_find_alpha_values(my_initial_terms=initial_terms, all_r_and_m=r_and_m_found) print("Step 5.1: The value of the Alphas:") print(outcome) except: print("5.1 doesnt work, shocker dude") print(color.RED + "Error occurs in file: {}".format(filename), color.RESET) try: file_writer.error_in_file(filename=filename, homogeneous=True, step="Step 5.1", automatic=True, error=error) except Exception as error: print( color.RED + "Error during writing error file.\nHere is the data:\nFile: {}\nHomogeneous: {}\nStep: {}\nAutomatic: {}\nOrginal error: {}\n".format( filename, True, "Step 5.1", True, error), color.RESET) return # Step 5.2: Obtain specific solution try: specific_solution = gimme_specific_solution(general_solution, outcome) file_writer.write_solution(filename=filename, solution=specific_solution) print(color.BLUE +"\nStep 5.2: The specific solution for this equation is: \n" + str(specific_solution) + "\n", color.RESET) return specific_solution except Exception as error: print("5.2 doesnt work, shocker dude: ERROR: {}".format(error)) print(color.RED + "Error occurs in file: {}".format(filename), color.RESET) try: file_writer.error_in_file(filename=filename, homogeneous=True, step="Step 5.2", automatic=True, error=error) except Exception as error: print( color.RED + "Error during writing error file.\nHere is the data:\nFile: {}\nHomogeneous: {}\nStep: {}\nAutomatic: {}\nOrginal error: {}\n".format( filename, True, "Step 5.2", True, error), color.RESET) return