def calculate_edge_weight(subscriber_cnt):
""" Keep weights relatively small despite large subscriber disparities """
if subscriber_cnt == 0:
log_cnt = 0
else:
log_cnt = math.log2(subscriber_cnt)
return str(log_cnt)
python类log2()的实例源码
def check_pow(config, num, exp, mod=None):
if num == 0 and exp < 0:
# 0 ** -1 raises a ZeroDivisionError
return False
if num < 0 and exp < 1.0 and exp != 0.0:
# pow(-25, 0.5) raises a ValueError
return False
if mod is not None:
# pow(a, b, m) only works if a and b are integers
if not isinstance(num, int):
return False
if not isinstance(exp, int):
return False
if mod == 0:
# pow(2, 1024, 0) raises a ValueError:
# 'pow() 3rd argument cannot be 0'
return False
if (isinstance(num, int)
and isinstance(exp, int)
# don't call log2(0) (error)
and num != 0
# if exp < 0, the result is a float which has a fixed size
and exp > 0):
# bits(num ** exp) = log2(num) * exp
if math.log2(abs(num)) * exp >= config.max_int_bits:
# pow() result will be larger than max_constant_size.
return False
return True
def __init__(self, conn=None, capacity=1000000000, error_rate=0.00000001, key='BloomFilter'):
self.m = math.ceil(capacity*math.log2(math.e)*math.log2(1/error_rate)) #????bit??
self.k = math.ceil(math.log1p(2)*self.m/capacity) #?????hash??
self.mem = math.ceil(self.m/8/1024/1024) #?????M??
self.blocknum = math.ceil(self.mem/512) #?????512M????,value?????????ascii???????256????
self.seeds = self.SEEDS[0:self.k]
self.key = key
self.N = 2**31-1
self.redis = conn
def _handle_blk_SET_SPEED(self, params):
if not self._jtag_on:
self._blk_read_buffer.append(b'\x01\x04')
else:
req_speed = struct.unpack("<I", params)[0]
self._speed = 62500*(2**min(6,max(0,math.floor(
math.log2(req_speed/62500)
))))
self._blk_read_buffer.append(b'\x05\x00'+\
struct.pack("<I", self._speed))
def __init__(self, max_depth=-1, min_leaf_examples=6, max_split_features="auto"):
self.root_node = None
self.max_depth = max_depth
self.min_leaf_examples = min_leaf_examples
if max_split_features in ["auto","sqrt","log2"] or \
isinstance(max_split_features, int) or max_split_features is None:
self.max_split_features = max_split_features
self.considered_features = None
else:
raise ValueError("Argument max_split_features must be 'auto', \
'sqrt', 'log2', an int or None")
def entropy(self, targets):
"""
Returns the entropy in the given rows.
:param targets: 1D array-like targets
:return: Float value of entropy
"""
results = self.unique_counts(targets)
ent = 0.0
for val in results.values():
p = float(val) / len(targets)
ent -= p * log2(p)
return ent
def get_coordinates(self):
return (int(log2(self.solver.GetValue(self.x).as_int())), int(log2(self.solver.GetValue(self.y).as_int())))
def get_coordinates(self):
return (int(log2(self.solver.GetValue(self.x).as_int())), int(log2(self.solver.GetValue(self.y).as_int())))
def testLog2Exact(self):
# Check that we get exact equality for log2 of powers of 2.
actual = [math.log2(math.ldexp(1.0, n)) for n in range(-1074, 1024)]
expected = [float(n) for n in range(-1074, 1024)]
self.assertEqual(actual, expected)
def greatest_pow2(n):
return 2 ** floor(log2(n))
def __init__(self, array):
self.array = array
self.block_size = ceil(log2(len(array)) / 4)
self.block_cnt = ceil(len(self.array) / self.block_size)
self.block_mins = self._calculate_block_mins()
self.processed_block_mins = self._process_block_mins()
self.rmq_map = dict()
self.signatures = self._compute_signatures()
def _process_block_mins(self):
max_size = floor(log2(len(self.block_mins)))
res = [[i for i in self.block_mins]]
def global_argmin(*sub_blocks):
return sub_blocks[argmin(self.array[i] for i in sub_blocks)]
for si in range(max_size):
t = [
global_argmin(res[si][i], res[si][i + 2**si]) for i in range(len(self.block_mins) - 2**si)
] + [
res[si][i] for i in range(len(self.block_mins) - 2**si, len(self.block_mins))
]
res.append(t)
return res
def _query_whole_blocks(self, bi, bj):
cnt = floor(log2(bj - bi))
sub_blocks = [
self.processed_block_mins[cnt][bi],
self.processed_block_mins[cnt][bj - 2 ** cnt],
]
return sub_blocks[argmin(self.array[i] for i in sub_blocks)]
def __init__(self, capacity=100, error_rate=0.01, conn=None, key='BloomFilter'):
self.m = math.ceil(capacity * math.log2(math.e) * math.log2(1 / error_rate)) # ????bit??
self.k = math.ceil(math.log1p(2) * self.m / capacity) # ?????hash??
self.mem = math.ceil(self.m / 8 / 1024 / 1024) # ?????M??
self.blocknum = math.ceil(self.mem / 512) # ?????512M????,value?????????ascii???????256????
self.seeds = self.SEEDS[0:self.k]
self.key = key
self.N = 2 ** 31 - 1
self.redis = conn
def calc_bit_width(self):
return int(math.log2(self.max_val)) + 1
def rectriangle(x, y, s):
th = s*S3/4
cr = S3*s/12
stepest = int(math.log2(s))+2
triangle(x, y, s, colors[stepest%len(colors)])
circle(x+s/2-cr, y+th-cr*2, cr*2, colors[(stepest+1)%len(colors)])
if s >= SIZE_LIMIT:
s /= 2
root.after(DELAY, rectriangle, x, y, s)
root.after(DELAY+100, rectriangle, x+s, y, s)
root.after(DELAY+200, rectriangle, x+s/2, y+th, s)
root.after(DELAY+300, rectriangle, x+3/4*s, y+S3/4*s, s/2)
def print_zipf(data):
print(len(data))
plt.bar(range(len(data)),[math.log2(t[1]) for t in data])
# In[6]:
def testLog2Exact(self):
# Check that we get exact equality for log2 of powers of 2.
actual = [math.log2(math.ldexp(1.0, n)) for n in range(-1074, 1024)]
expected = [float(n) for n in range(-1074, 1024)]
self.assertEqual(actual, expected)
def draw_heap(cls, draw, a, num, i=-1, j=-1, ck=-1, cw=-1):
for n, v in enumerate(a):
if n > num:
break
# ??????????
heap_ny = int(math.log2(n + 1))
# ??????????
heap_nx_a = 2**heap_ny
# ?????????
nx = n + 1 - heap_nx_a
# ?????x???????
bx = (2**(cls.heap_hn - heap_ny) - 1) * cls.heap_xs / 2
# ?????x?????
xs = 2**(cls.heap_hn - heap_ny) * cls.heap_xs
# ???x??
x = cls.heap_bx + bx + xs * nx
# ???y??
y = cls.heap_by + cls.heap_ys * heap_ny
cls.heap_xy[n] = [x, y]
# print(x, y)
draw.text((x, y), v, (0, 0, 0))
if n > 0:
k = (n - 1) // 2
kx, ky = cls.heap_xy[k]
draw.line((x, y, kx, ky + 10), (0, 0, 0))
if i != -1 and j != -1:
x1, y1 = cls.heap_xy[i]
x2, y2 = cls.heap_xy[j]
draw.line((x1, y1 + 10, x2, y2), (255, 0, 0), width=2)
if ck != -1 and cw != -1:
x1, y1 = cls.heap_xy[ck]
x2, y2 = cls.heap_xy[cw]
draw.line((x1, y1 + 10, x2, y2), (0, 255, 255))