#!/usr/bin/python ## the shebang above works for Linux... ## ...in FreeBSD use: ##!/usr/local/bin/python # -*- coding: cp1252 -*- """ Calculates the transition times for the XOR with two delays with arbitrary precision """ #imports from decimal import * import os #constants Prec = 12 # number of decimal places getcontext().prec=Prec iterations = 1000 # counts the number of transitions Verbose_level = 1 # 1 = normal messages, 2 = debug mode, 0 = silent mode # tau1 = 1 tau1 = Decimal(1) # tau2 = 1/phi , the golden ratio tau2 = Decimal(2)/(Decimal(1)+Decimal(5).sqrt()) # Minimum allowed pulse width w_min = Decimal("0.000") # choices of initial conditions: #ic = Decimal(0) ic = -tau2 #tn = [ic-max(tau1,tau2),ic-min(tau1,tau2),ic] tn = [ic] # list of transitions # The programs starts here: # open outputfile f = open("xor_2delays_"+str(Prec)+"digits.dat",'wt') if Verbose_level>0: print "output file: ", "xor_2delays_"+str(Prec)+"digits.dat" f.write("#tau1 = %s, tau2 = %s\n" % (str(float(tau1)), str(float(tau2))) ) f.write("#Prec = %d, iterations = %d\n" % (Prec, iterations) ) # main loop: for i in range(iterations): collided1 = False collided2 = False # calculates the two candidates of future transitions from the first tn # using tn[0], instead of min(tn), could be faster next_t_candidate_1 = min(tn)+min(tau1,tau2) next_t_candidate_2 = min(tn)+max(tau1,tau2) if Verbose_level>1: # optionally prints debug information # print "Processing ", min(tn) # print "list = %f " % tn[0], # for t in tn[1:]: # print ", %f" % float(t), # print "\nt1 = %f, t2 = %f" % (next_t_candidate_1, next_t_candidate_2) # if len(tn)>0: # if we have transitions in the list ... for t in tn: # ... checks, for all of them, if they collide ... if (abs(next_t_candidate_1-t) <= w_min): # ... with candidate 1 ... collided1 = True # remembers a collision occurred if Verbose_level>1: print "removing ", float(t) tn.remove(t) # removes colliding transition else: if Verbose_level>0: print "empty list, stopping now" f.close() exit() if len(tn)>0: for t in tn: if (abs(next_t_candidate_2-t) <= w_min): # ... and/or candidate 2 collided2 = True # remembers a collision occurred if Verbose_level>1: print "removing ", float(t) tn.remove(t) # removes colliding transition else: if Verbose_level>0: print "empty list, stopping now" f.close() exit() if not(collided1): # if the first candidate did not collide if Verbose_level>1: print "saving transition at", float(tn[0]) #f.write("%s\n" % str(float(tn[0]))) f.write("%s\n" % str(tn[0])) # saves generating transition as good one tn.append(next_t_candidate_1) # appends the good candidate do the list if not(collided2): # if candidate 2 did not collide tn.append(next_t_candidate_2) # appends the good candidate do the list if collided1: # if candidate 1 collided, we haven't saved the good transition yet if Verbose_level>1: print "saving transition at", float(tn[0]) #f.write("%s\n" % str(float(tn[0]))) f.write("%s\n" % str(tn[0])) # saves the transition now tn = tn[1:] # removes the processed transition tn.sort() # sorts the list, to make sure of the right ordering # we have almost finished after the main loop f.close() if Verbose_level>0: print "finished!" # pauses on Windows, to give the user a chance of looking at the error messages, just in case # we can pause (or wait a few seconds) on posix (Linux), too if os.name=="posix": os.system("sleep 1") else: os.system("pause")