def experiment(): for i in range(10): random_test(300) print 'done!' print hilbert(-88) print hilbert(-123) n = 17600773329804421013044164003782933115981289392321878318274335494333497909423750776833564592938824735089487193 for n in prime_200: start = timeit.default_timer() r1 = atkin_morain(n) print timeit.default_timer() - start, '\n', r1
def categorize_block_array(d, base_address, blocksize, pixelsize, addr_space = 'arm'): print 'Categorizing blocks in memory, following a 2D Hilbert curve' a = [] timestamp = time.time() first_time = timestamp for y in xrange(pixelsize): row = [] for x in xrange(pixelsize): addr = base_address + blocksize * hilbert(x, y, pixelsize) row.extend(categorize_block(d, addr, blocksize, addr_space=addr_space)) # Keep the crowd informed! now = time.time() if now > timestamp + 0.5: # Estimate time completion = (x + y*pixelsize) / float(pixelsize*pixelsize) elapsed = now - first_time remaining = 0 if completion > 0.00001: total = elapsed / completion if total > elapsed: remaining = total - elapsed print 'block %08x - (%4d,%4d) of %d, %6.2f%% -- %2d:%02d:%02d elapsed, %2d:%02d:%02d est. remaining' % ( addr, x, y, pixelsize, 100 * completion, elapsed / (60 * 60), (elapsed / 60) % 60, elapsed % 60, remaining / (60 * 60), (remaining / 60) % 60, remaining % 60) timestamp = now a.append(row) print 'Calculating global scaling for blue channel' # The blue channel still has raw time deltas. Calculate pecentiles so we can scale them. times = [] for row in a: for x in xrange(pixelsize): times.append(row[2 + 3*x]) times.sort() percentile_low = log(times[int( len(times) * 0.05 )]) percentile_high = log(times[int( len(times) * 0.95 )]) percentile_s = 256.0 / (percentile_high - percentile_low) for row in a: for x in xrange(pixelsize): row[2+3*x] = min(255, max(0, int(0.5 + (log(row[2+3*x]) - percentile_low) * percentile_s))) print 'Done scaling' return a
def to_hilbert(im): im_dat = np.asarray(im) w, h = im.size if w & (w - 1) != 0 or h & (h - 1) != 0: print "Warning, dimensions", w, h, "not powers of two!" # let's create a new image for the transformed data imout = Image.new(im.mode, (w, h)) imout_dat = imout.load() imout_dat_pos = 0 for px in hilbert.hilbert(im_dat): imout_dat[imout_dat_pos % w, imout_dat_pos // w] = tuple(px) imout_dat_pos += 1 return imout
def generate_curve(p, d): ''' Essentially Algorithm 7.5.9 Args: p: Returns: parameters a, b for the curve ''' # calculate quadratic nonresidue g = gen_QNR(p, d) # find discriminant new_d = gen_discriminant(0) uv = cornacchia_smith(p, new_d) while jacobi(new_d, p) != 1 or uv is None: new_d = gen_discriminant(new_d) uv = cornacchia_smith(p, new_d) u, v = uv # storing the result of cornacchia. u^2 + v^2 * |D| = 4*p # check for -d = 3 or 4 # Choose one possible output # Look at param_gen for comparison. answer = [] if new_d == -3: x = -1 for i in range(0, 6): answer.append((0, x)) x = (x * g) % p return answer if new_d == -4: x = -1 for i in range(0, 4): answer.append((x, 0)) x = (x * g) % p return answer # otherwise compute the hilbert polynomial _, t, _ = hilbert(new_d) s = [i % p for i in t] j = equation.root_Fp(s, p) # Find a root for s in Fp. Algorithm 2.3.10 c = j * inverse(j - 1728, p) % p r = -3 * c % p s = 2 * c % p return [(r, s), (r * g * g % p, s * (g**3) % p)]
def ac_hilbert(file, envelope=True): """ Calculating Instantaneous Frequency :param file: :param envelope: If True, then output envelope of AC and normalized AC by envenlope :return: """ tr = read(file)[0] delta = tr.stats.delta data = tr.data fs = 1 / delta xa = hilbert.hilbert(tr.data) freq = hilbert.instantaneous_frequency(xa, fs) tr.data = freq # for i in range(0, len(tr.data)): # d = tr.data[i] # if d > 0.0: # tr.data[i] = 0.0 fn, fext = os.path.splitext(file) if fext == ".norm": myext = ".ifn" else: myext = ".if" fullname = fn + myext print fullname tr.write(filename=fullname, format="SAC") if envelope: fullname = fn + ".env" tr.data = hilbert.envelope(xa) tr.write(filename=fullname, format="SAC") fullname = fn + ".norm" tr.data = data / tr.data tr.data = np.nan_to_num(tr.data) tr.write(filename=fullname, format="SAC") pass
def curve_parameters(d, p): ''' Modified Algorithm 7.5.9 for the use of ecpp Args: d: discriminant p: number for prime proving Returns: a list of (a, b) parameters for ''' g = gen_QNR(p, d) #g = nzmath.ecpp.quasi_primitive(p, d==-3) u, v = cornacchia_smith(p, d) # go without the check for result of cornacchia because it's done by previous methods. if jacobi(d, p) != 1: raise ValueError('jacobi(d, p) not equal to 1.') # check for -d = 3 or 4 # Choose one possible output # Look at param_gen for comparison. answer = [] if d == -3: x = -1 % p for i in range(0, 6): answer.append((0, x)) x = (x * g) % p return answer if d == -4: x = -1 % p for i in range(0, 4): answer.append((x, 0)) x = (x * g) % p return answer # otherwise compute the hilbert polynomial _, t, _ = hilbert(d) s = [int(i % p) for i in t] j = equation.root_Fp(s, p) # Find a root for s in Fp. Algorithm 2.3.10 c = j * inverse(j - 1728, p) % p r = -3 * c % p s = 2 * c % p return [(r, s), (r * g * g % p, s * (g**3) % p)]
def from_hilbert(im): w, h = im.size if w & (w - 1) != 0 or h & (h - 1) != 0: print "Warning, dimensions", w, h, "not powers of two!" # let's create a new image for the transformed data imout = Image.new(im.mode, (w, h)) imout_dat = imout.load() imout_dat_pos = 0 im_dat = im.load() # we create a matrix of x and y positions which we pass in xy = np.dstack(np.mgrid[0:h, 0:w]) for (px, py) in hilbert.hilbert(xy): imout_dat[int(py), int(px)] = tuple( im_dat[imout_dat_pos % w, imout_dat_pos // w]) imout_dat_pos += 1 return imout
def hilbert_determinant_test ( ): #*****************************************************************************80 # ## HILBERT_DETERMINANT_TEST tests HILBERT_DETERMINANT. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 13 February 2015 # # Author: # # John Burkardt # from hilbert import hilbert from r8mat_print import r8mat_print print '' print 'HILBERT_DETERMINANT_TEST' print ' HILBERT_DETERMINANT computes the HILBERT determinant.' m = 5 n = m a = hilbert ( m, n ) r8mat_print ( m, n, a, ' HILBERT matrix:' ) value = hilbert_determinant ( n ) print ' Value = %g' % ( value ) print '' print 'HILBERT_DETERMINANT_TEST' print ' Normal end of execution.' return
def hilbert_test ( ): #*****************************************************************************80 # ## HILBERT_TEST tests HILBERT. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 13 February 2015 # # Author: # # John Burkardt # from r8mat_print import r8mat_print print '' print 'HILBERT_TEST' print ' HILBERT computes the HILBERT matrix.' m = 5 n = m a = hilbert ( m, n ) r8mat_print ( m, n, a, ' HILBERT matrix:' ) print '' print 'HILBERT_TEST' print ' Normal end of execution.' return
def msl(self, line, cell=''): """Memsquare lookup""" args = parse_argstring(self.msl, line) return int(args.base + args.scale * hilbert(args.x, args.y, args.width))
temp.lab_l = Color1.lab_l * (1.0 - t) + Color2.lab_l * t temp.lab_a = Color1.lab_a * (1.0 - t) + Color2.lab_a * t temp.lab_b = Color1.lab_b * (1.0 - t) + Color2.lab_b * t return temp def lerpLch(Color1, Color2, t): # t = [0,1]. temp = colors.LCHabColor(0, 0, 0) temp.lch_l = Color1.lch_l * (1.0 - t) + Color2.lch_l * t temp.lch_c = Color1.lch_c * (1.0 - t) + Color2.lch_c * t temp.lch_h = Color1.lch_h * (1.0 - t) + Color2.lch_h * t return temp # Get required Hilbert coordinates coords = [] print "Calculating Hilbert points..." hilbert(0, 0, size, 0, 0, size, powers, coords) exceptions = 0 for d in xrange(len(ratios)): if d % 10000 == 0: print d,"/",len(ratios) if mode == "lab": col = lerpLab(ATColor, GCColor, ratios[d][0]) else: col = lerpLch(ATColor, GCColor, ratios[d][0]) if len(ratios[d]) > 1: if mode == "lab": col = lerpLab(col, black, ratios[d][1]) else:
def categorize_block_array(d, base_address, blocksize, pixelsize, addr_space='arm'): print 'Categorizing blocks in memory, following a 2D Hilbert curve' a = [] timestamp = time.time() first_time = timestamp for y in xrange(pixelsize): row = [] for x in xrange(pixelsize): addr = base_address + blocksize * hilbert(x, y, pixelsize) row.extend( categorize_block(d, addr, blocksize, addr_space=addr_space)) # Keep the crowd informed! now = time.time() if now > timestamp + 0.5: # Estimate time completion = (x + y * pixelsize) / float(pixelsize * pixelsize) elapsed = now - first_time remaining = 0 if completion > 0.00001: total = elapsed / completion if total > elapsed: remaining = total - elapsed print 'block %08x - (%4d,%4d) of %d, %6.2f%% -- %2d:%02d:%02d elapsed, %2d:%02d:%02d est. remaining' % ( addr, x, y, pixelsize, 100 * completion, elapsed / (60 * 60), (elapsed / 60) % 60, elapsed % 60, remaining / (60 * 60), (remaining / 60) % 60, remaining % 60) timestamp = now a.append(row) print 'Calculating global scaling for blue channel' # The blue channel still has raw time deltas. Calculate pecentiles so we can scale them. times = [] for row in a: for x in xrange(pixelsize): times.append(row[2 + 3 * x]) times.sort() percentile_low = log(times[int(len(times) * 0.05)]) percentile_high = log(times[int(len(times) * 0.95)]) percentile_s = 256.0 / (percentile_high - percentile_low) for row in a: for x in xrange(pixelsize): row[2 + 3 * x] = min( 255, max( 0, int(0.5 + (log(row[2 + 3 * x]) - percentile_low) * percentile_s))) print 'Done scaling' return a
def sample(fh, order): plotter.plotStart(fh) hilbert(plotter, order, n, offset) plotter.plotEnd()
def analyze(filename, mode="lab"): if mode: if not mode in ["lab", "lch"]: print("Specify color mode -- Lab or Lch?") quit() else: mode = "lab" aligned = False outname = "" pixelsize = 50 # How many base pairs per pixel? count = 0 # current number of base pairs group = "" # current group of base pairs ratios = [] # List of GC ratios and optionally N ratio. unknowns = [] # In case data other than ATGCN is encountered. counter = 0 archive = zipfile.ZipFile(filename) textfilename = archive.namelist()[0] file = archive.open(textfilename) for line in file: line = line.decode('ascii') counter += 1 if counter % 100000 == 0: print(counter) if not aligned: if line[0] == ">": txts = line.split(" ") print("Checked: " + txts[0]) outname = txts[0].strip()[1:] print(outname) aligned = True else: group += line.strip() if len(group) >= pixelsize: GC = AT = N = 0 working = group[:pixelsize] # Copy the base pairs to work on. group = group[pixelsize:] # Remove the worked-on pairs. for dna in working: if dna == 'A' or dna == 'T': AT += 1 elif dna == 'G' or dna == 'C': GC += 1 elif dna == 'N': N += 1 else: unknowns.append(dna) info = [] if AT+GC == 0: info.append(0.0) else: info.append(GC/float(AT+GC)) if N != 0: info.append(N/float(pixelsize)) ratios.append(info) print(len(ratios)) # Now that we have all ratio data needed, time to build image. # side length = 2^(ceil(ln(sqrt(total))/ln2)). # Basically find the smallest image with a side length of 2^n that has enough pixels for this. powers = int(math.ceil(math.log(math.sqrt(len(ratios)))/math.log(2))) size = 2**powers img = Image.new('RGBA', (size, size), (0,0,0,0)) pixels = img.load() # Define the two colors to interpolate between in Lab/Lch. if mode == "lab": ATColor = colors.LabColor(100, 127, 0) # pink GCColor = colors.LabColor(100, -128, 0) # blue black = colors.LabColor(0, 0, 0) else: ATColor = colors.LCHabColor(78, 100, 157) # green GCColor = colors.LCHabColor(71, 100, 360) # pink black = colors.LCHabColor(0, 0, 0) def lerpLab(Color1, Color2, t): # t = [0,1]. temp = colors.LabColor(0, 0, 0) temp.lab_l = Color1.lab_l * (1.0 - t) + Color2.lab_l * t temp.lab_a = Color1.lab_a * (1.0 - t) + Color2.lab_a * t temp.lab_b = Color1.lab_b * (1.0 - t) + Color2.lab_b * t return temp def lerpLch(Color1, Color2, t): # t = [0,1]. temp = colors.LCHabColor(0, 0, 0) temp.lch_l = Color1.lch_l * (1.0 - t) + Color2.lch_l * t temp.lch_c = Color1.lch_c * (1.0 - t) + Color2.lch_c * t temp.lch_h = Color1.lch_h * (1.0 - t) + Color2.lch_h * t return temp # Get required Hilbert coordinates coords = [] print("Calculating Hilbert points...") hilbert(0, 0, size, 0, 0, size, powers, coords) exceptions = 0 for d in range(len(ratios)): if d % 10000 == 0: print(d,"/",len(ratios)) if mode == "lab": col = lerpLab(ATColor, GCColor, ratios[d][0]) else: col = lerpLch(ATColor, GCColor, ratios[d][0]) if len(ratios[d]) > 1: if mode == "lab": col = lerpLab(col, black, ratios[d][1]) else: col = lerpLch(col, black, ratios[d][1]) # translate Lab/Lch to RGB colors r,g,b = conversions.convert_color(col, colors.sRGBColor).get_value_tuple() # Some Lab/Lch colors can't be represented using RGB. if r < 0.0: r = 0.0 if g < 0.0: g = 0.0 if b < 0.0: b = 0.0 if r > 1.0: r = 1.0 if g > 1.0: g = 1.0 if b > 1.0: b = 1.0 x, y = coords[d] #x = d % size #y = d / size try: pixels[x,y] = (int(r*255),int(g*255),int(b*255), 255) except IndexError: print(x,y) exceptions+=1 print("Image size: (%d,%d)" % (size, size)) print(str(exceptions) + " exceptions") img.save("output/" + outname + ".png")