import os #################### # Clear the screen # #################### def clear(): os.system('clear') ############################################ # Organizes a list of numbers into ranges. # ############################################ def find_ranges(num_list): # Sort the numbers new_list = num_list new_list.sort() # Exit for the empty list if len(new_list) == 0: return [], "" # Group them into ranges range_list = [] tmp_range = [] tmp_begin = new_list[0] for i in range(len(new_list)): if i == (len(new_list)-1): tmp_end = new_list[-1] range_list.append([tmp_begin, tmp_end]) elif new_list[i+1] != (new_list[i] + 1): tmp_end = new_list[i] range_list.append([tmp_begin, tmp_end]) tmp_begin = new_list[i+1] # Assemble an output string out_str = "" for pairs in range_list: out_str = out_str + str(pairs[0]) + "--"+ str(pairs[1]) if range_list.index(pairs) != (len(range_list)-1): out_str = out_str + "," return range_list, out_str ############################################################ # This routine reads in the cuspidal information for the # # Basic Quaternaries from their logfiles in "Basic_Logs/". # ############################################################ def read_cusp_constants(formtype): assert (formtype == "basic") or (formtype == "aux") # Set some important variables if formtype == "basic": print "Starting to read the basic quaternary constants." form_dir = "Basic_Logs/" filename_template = "Basic_X__cusp_info.txt" else: print "Starting to read the auxiliary quaternary constants." form_dir = "Auxiliary_Logs/" filename_template = "Auxilliary_X__cusp_info.txt" # Check that the directory exists assert os.path.isdir(form_dir) # Set the filename template filename_template_list = filename_template.split("X") assert (len(filename_template_list) == 2) name_begin = filename_template_list[0] name_end = filename_template_list[1] # print "name_begin = ", name_begin # print "name_end = ", name_end, type(name_end) # Get the current directory contents basic_dir_list = os.listdir(form_dir) # print basic_dir_list # List the associated form numbers form_number_list = [] for name in basic_dir_list: if (name.startswith(name_begin) and name.endswith(name_end)): form_number_list.append(int(name.lstrip(name_begin).rstrip(name_end))) form_number_list.sort() # print form_number_list # Report on the forms found form_ranges, form_ranges_str = find_ranges(form_number_list) print " We have found cuspidal logfiles for forms #", form_ranges_str # Loop through all datafiles to assemble cuspidal information (and constants) cusp_const_header = " This gives the overall cuspidal constant of:" cusp_constant_list = [] for num in form_number_list: name = name_begin + str(num) + name_end file = open(form_dir + name, "r") lines = file.xreadlines() found_const_flag = False ## This checks we found a constant for each form! linebreak_flag = False # This is a sloppy way to deal with unexpected s... for line in lines: if line.startswith(cusp_const_header) or (linebreak_flag == True): # Get 20 chars of the cusp const clean_line = line.rstrip().lstrip(cusp_const_header)[:20] # Deal with possible fraction notation if (clean_line.find("/") != -1): frac = clean_line.split("/") tmp_const = float(frac[0]) / float(frac[1]) cusp_constant_list.append([num, tmp_const]) found_const_flag = True elif clean_line.strip() == "": # This is an unexpected , so parse the next line # print line # print num linebreak_flag = True else: # Last time was an overflow, so parse this line tmp_const = float(clean_line) cusp_constant_list.append([num, tmp_const]) found_const_flag = True linebreak_flag = False ## Error Checking if (found_const_flag == False): print " Error in form #" + str(num) + " -- NO CONSTANT FOUND! =(" assert found_const_flag == True ## DIAGNOSTIC # print " len(form_number_list) = " + str(len(form_number_list)) # print " len(cusp_constant_list) = " + str(len(cusp_constant_list)) # Make a consolidated list small_cusp_list = [ ] for pair in cusp_constant_list: small_cusp_list.append(pair[1]) # Return the list of constants (in order) return small_cusp_list, form_number_list, form_ranges_str ############################################################ # This routine reads in the cuspidal information for the # # Basic Quaternaries from their logfiles in "Basic_Logs/". # ############################################################ def new_read_cusp_constants(formtype): assert (formtype == "basic") or (formtype == "aux") # Set some important variables if formtype == "basic": print "Starting to read the basic quaternary constants." form_dir = "Basic_Logs/" filename_template = "Basic_X__cusp_info.txt" else: print "Starting to read the auxiliary quaternary constants." form_dir = "Auxiliary_Logs/" filename_template = "Auxilliary_X__cusp_info.txt" # Check that the directory exists assert os.path.isdir(form_dir) # Set the filename template filename_template_list = filename_template.split("X") assert (len(filename_template_list) == 2) name_begin = filename_template_list[0] name_end = filename_template_list[1] # print "name_begin = ", name_begin # print "name_end = ", name_end, type(name_end) # Get the current directory contents basic_dir_list = os.listdir(form_dir) # print basic_dir_list # List the associated form numbers form_number_list = [] for name in basic_dir_list: if (name.startswith(name_begin) and name.endswith(name_end)): form_number_list.append(int(name.lstrip(name_begin).rstrip(name_end))) form_number_list.sort() # print form_number_list # Report on the forms found form_ranges, form_ranges_str = find_ranges(form_number_list) print " We have found cuspidal logfiles for forms #", form_ranges_str # Loop through all datafiles to assemble cuspidal information (and constants) # cusp_const_header = " This gives the overall cuspidal constant of:" cusp_const_header = "Adding the cusp bound " cusp_constant_list = [] matrix_list = [ ] # for num in range(1,10): for num in form_number_list: ## DIAGNOSTIC print " Starting Form #", num # Setup name = name_begin + str(num) + name_end tmp_name = "tmp_cusp_tail_file.txt" os.system("rm -f " + tmp_name) # Remove the temporary tail file (if it existed from before) os.system("head -n15 " + form_dir + name + " >> " + tmp_name) # Make a temp tail file os.system("tail -n50 " + form_dir + name + " >> " + tmp_name) # Make a temp tail file file = open(tmp_name, "r") lines = file.xreadlines() found_const_flag = False ## This checks we found a constant for each form! linebreak_flag = False # This is a sloppy way to deal with unexpected s... # Pick out the last few lines of each file with the cusp constant save_line_flag = False matrix_lines = [] cusp_const_lines = [] for line in lines: if line.startswith('[') and line.rstrip().endswith(']') and (len(matrix_lines) < 16): line = line.rstrip() matrix_lines += line.lstrip("[").rstrip("]").split() # print line # print line.lstrip("[").rstrip("]").split() if line.startswith(cusp_const_header): save_line_flag = True if save_line_flag == True: cusp_const_lines.append(line) ## Error Checking -- Make sure there's a cusp constant there! if (len(cusp_const_lines) == 0): print " Error in form #" + str(num) + " -- NO CONSTANT FOUND! =(" assert(0 == 1) # Concatenate the constant into a string cusp_const_str = '' # print len(cusp_const_lines) # print cusp_const_lines # print cusp_const_lines[0] cusp_const_lines[0] = cusp_const_lines[0].replace(cusp_const_header, "") cusp_const_lines[-1] = cusp_const_lines[-1].replace("to the big list", "") for line in cusp_const_lines: cusp_const_str += line.strip().strip('\\') # Deal with possible fraction notation if (cusp_const_str.find("/") != -1): frac = cusp_const_str.split("/") tmp_const = float(frac[0]) / float(frac[1]) cusp_constant_list.append([num, tmp_const]) found_const_flag = True else: tmp_const = float(cusp_const_str[:20]) # If its already a float, we'll only take the first 20 characters. =) cusp_constant_list.append([num, tmp_const]) # Remove the temporary tail file os.system("rm -f " + tmp_name) # Convert the matrix list of string numbers to integers, and append it to the list # print matrix_lines tmp_matrix_nums = [] for x in matrix_lines: tmp_matrix_nums.append(int(x)) matrix_list.append(tmp_matrix_nums) ## DIAGNOSTIC # print matrix_nums # print tmp_const # Make a consolidated list small_cusp_list = [ ] for pair in cusp_constant_list: small_cusp_list.append(pair[1]) # Return the list of constants (in order) return small_cusp_list, form_number_list, form_ranges_str, matrix_list ##################################################################### execfile("290-cusp-all.py") # Loads in William's tables ##################################################################### def cuspidal_report(): ## Read Datafiles: ## --------------- # Reads in William's cuspidal constants execfile("290-cusp-all.py") ## <==== THIS DOESN'T WORK LOCALLY!!! =( data.sort() # Read my basic cusp constants basic_cusp_list_tuple = new_read_cusp_constants("basic") basic_cusp_constants = basic_cusp_list_tuple[0] basic_form_numbers = basic_cusp_list_tuple[1] basic_form_matrices = basic_cusp_list_tuple[3] # Read my aux cusp constants aux_cusp_list_tuple = new_read_cusp_constants("aux") aux_cusp_constants = aux_cusp_list_tuple[0] aux_form_numbers = aux_cusp_list_tuple[1] aux_form_matrices = aux_cusp_list_tuple[3] # ----------------------------------------------------- ## Compute Basic Info: ## ------------------- # Make the differences error = [basic_cusp_constants[basic_form_numbers.index(i)] - data[i-1][3] for i in basic_form_numbers] abs_error = [abs(x) for x in error] # Find the biggest positive error max_error = max(error) max_error_index = error.index(max_error) max_error_form = basic_cusp_list_tuple[1][max_error_index] # Find the biggest total error max_abs_error = max(abs_error) max_abs_error_index = abs_error.index(max_abs_error) max_abs_error_form = basic_cusp_list_tuple[1][max_abs_error_index] # Find the biggest cusp constant max_const = max(basic_cusp_constants) max_const_index = basic_cusp_constants.index(max_const) max_const_form = basic_form_numbers[max_const_index] # Report on the results print print "Cuspidal report for basic forms:" print "--------------------------------" print "The largest positive error was", max_error, "which occurred at form #", max_error_form tmp_cusp_results = [basic_cusp_list_tuple[i][max_error_index] for i in range(2)] print " Our computation:", tmp_cusp_results print " Approx computation:", data[max_error_form - 1] print print "The largest total error was", max_abs_error, "which occurred at form #", max_abs_error_form tmp_cusp_results = [basic_cusp_list_tuple[i][max_abs_error_index] for i in range(2)] print " Our computation:", tmp_cusp_results print " Approx computation:", data[max_abs_error_form - 1] print print "The largest (exact) basic cuspidal constant is", max_const, "which occurred at form #", max_const_form ## ------------------------------------------------------- ## Compute Auxiliary Info: ## ----------------------- # Find the biggest auxiliary cusp constant aux_max_const = max(aux_cusp_constants) aux_max_const_index = aux_cusp_constants.index(aux_max_const) aux_max_const_form = aux_form_numbers[aux_max_const_index] # Report on the results print print "Cuspidal report for auxiliary forms:" print "------------------------------------" print "The largest auxiliary cuspidal constant is", aux_max_const, "which occurred at form #", aux_max_const_form ## DIAGNOSTIC # print # print "basic_cusp_constants = ", basic_cusp_constants # print "basic_form_numbers = ", basic_form_numbers # print # print "len(basic_cusp_constants) = ", len(basic_cusp_constants) # print "len(basic_form_numbers) = ", len(basic_form_numbers) # print # Print the constants to a file (to read in later in C++) ans = raw_input("Generate Basic Cusp Constant Summaries? (y/n) ") if (ans == "y"): for i in range(len(basic_cusp_constants)): outfile = open("Basic_Constants/Basic_const_" + str(basic_form_numbers[i]) + ".txt", 'w') outfile.write(str(basic_cusp_constants[i])) outfile.write("\n") outfile.write(str(basic_form_matrices[i])) outfile.close() ans = raw_input("Generate Auxiliary Cusp Constant Summaries? (y/n) ") if (ans == "y"): for i in range(len(aux_cusp_constants)): outfile = open("Auxiliary_Constants/Aux_const_" + str(aux_form_numbers[i]) + ".txt", 'w') outfile.write(str(aux_cusp_constants[i])) outfile.write("\n") outfile.write(str(aux_form_matrices[i])) outfile.close() ##################################################################### ## Print the cuspidal report #cuspidal_report();