我关心的是两条哈希线之间的部分。以下代码运行时间太长,我无法等待其输出。当我用另一段代码替换有问题的部分时,程序会在几秒钟内运行(见本文末尾)。我的目标是生成 90 个均匀分布在单位正方形 ( var1
, var2
) 中的数据点,然后生成 12 个随机放置在半径为 1/8 的圆中的点,该圆完全位于单位正方形 ( cir1
, cir2
) 内,最后加入这两组 ( sph1
, sph2
)。
自从昨天广告以来,我一直在研究这段代码,我真的很确定它是正确的(显然不是)。我可能错过了一些非常明显的东西......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
import math
import numpy as np
import sys
from heapq import merge
import multiprocessing as mp
from multiprocessing import Pool
boot = 2
RRpoints = 90
rrpoints = 12
step = np.arange(-8.0 , 0.5 , 0.5)
def distance_between_points(ite, jte):
xi, yi = ite
xj, yj = jte
x2 = (xi - xj) ** 2
y2 = (yi - yj) ** 2
d = math.sqrt(x2 + y2)
return d
def heaviside_step(n):
return int(n >= 0)
def myscript(iteration_number):
RRfile_name = "MC_out_cluster_1/output%d.txt" % iteration_number
with open(RRfile_name, "w") as RRf:
#############################################################################
np.random.seed()
var1 = np.random.uniform(0, 1 , RRpoints)
var2 = np.random.uniform(0, 1 , RRpoints)
cir1 = []
cir2 = []
x0 = np.random.uniform(0.125 , 0.875)
y0 = np.random.uniform(0.125 , 0.875)
while ( len(cir1) < rrpoints and len(cir2) < rrpoints ):
np.random.seed()
col1 = np.random.uniform(x0 - 0.125 , x0 + 0.125)
col2 = np.random.uniform(y0 - 0.125 , y0 + 0.125)
if (x0 - col1) ** 2 + (y0 - col2) ** 2 <= 1/64:
cir1.append(col1)
cir2.append(col2)
sph1 = list(merge(var1 , cir1))
sph2 = list(merge(var2 , cir2))
############################################################################
corr = []
for k in xrange(0, len(step)):
h = 0
for i in xrange(0, RRpoints):
for j in xrange(1 + i, RRpoints):
ite = sph1[i] , sph2[i]
jte = sph1[j] , sph2[j]
dbp = distance_between_points(ite, jte)
h += heaviside_step(math.exp( step[k] ) - dbp)
corr.append([math.exp(step[k]) , h])
for item in corr:
RRf.write("{0}\t{1}\n".format(item[0], item[1]))
x = xrange(boot)
p = mp.Pool()
y = p.imap(myscript, x)
list(y)
这是我修改以创建上述代码的另一个(工作)块:
#############################################################################
var1 = []
var2 = []
while (len(var1) < RRpoints):
np.random.seed()
col1 = np.random.uniform(0 , 1)
col2 = np.random.uniform(0 , 1)
if ( col1 ** 2 + (col2 - 0.5) ** 2 > 1/16 and (col1 - 1) ** 2 + (col2 - 0.5) ** 2 > 1/16 ):
var1.append(col1)
var2.append(col2)
cir1 = []
cir2 = []
while (len(cir1) < rrpoints and len(cir2) < rrpoints):
np.random.seed()
new1 = np.random.uniform(0.125 , 0.875)
new2 = np.random.uniform(0.125 , 0.875)
if ( new1 ** 2 + (new2 - 0.5) ** 2 >= 0.140625 and (new1 - 1) ** 2 + (new2 - 0.5) ** 2 >= 0.140625 ):
cir1.append(new1)
cir2.append(new2)
sph1 = list(merge(var1 , cir1))
sph2 = list(merge(var2 , cir2))
#############################################################################