我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用math.log2()。
def __init__(self, n, h, in_size, in_channels, embed_size, block_size): super().__init__( l0=L.Convolution2D(in_channels, n, 3, stride=1, pad=1), ln=L.Linear(None, h)) self.n_blocks = int(log2(in_size / embed_size)) + 1 self.block_size = block_size for i in range(self.n_blocks): n_in = (i + 1) * n n_out = (i + 2) * n if i < self.n_blocks - 1 else n_in for j in range(block_size - 1): self.add_link('c{}'.format(i * block_size + j), L.Convolution2D(n_in, n_in, 3, stride=1, pad=1)) self.add_link('c{}'.format(i * block_size + block_size - 1), L.Convolution2D(n_in, n_out, 3, stride=1, pad=1))
def get_entropy(question, animals): """ Finds the entropy of the answers of a question for a set of animals """ response_set = {answer:0.000001 for answer in ANSWERS} all_responses = query_all_responses(question=question, animals=animals) for animal in animals: responses = all_responses.get(animal.name, NO_RESPONSE) if responses is not None: responses = sorted( responses.items(), key=lambda resp: -resp[1] ) (first, _) = responses[0] (last, _) = responses[-1] response_set[first] += animal.prob response_set[last] += (1-animal.prob) else: print("COULDN'T FIND ANIMAL \"{}\"".format(animal.name)) total = sum(response_set.values()) probs = [count/total for count in response_set.values()] entropy = sum([-(p)*log2(p) for p in probs]) return entropy
def create_for_user(cls, pkcs12, user, cn=None, days=None): if not cn: cn = get_full_name(user) ec = EthicsCommission.objects.get(uuid=settings.ETHICS_COMMISSION_UUID) subject = '/CN={}/O={}/emailAddress={}'.format(cn, ec.name[:64], user.email) passphrase_len = math.ceil( PASSPHRASE_ENTROPY / math.log2(len(PASSPHRASE_CHARS))) passphrase = ''.join( SystemRandom().choice(PASSPHRASE_CHARS) for i in range(passphrase_len) ) from ecs.pki import openssl data = openssl.make_cert(subject, pkcs12, passphrase=passphrase, days=days) cert = cls.objects.create(user=user, cn=cn, **data) return (cert, passphrase)
def PKCS1(message : int, size_block : int) -> int: """ PKCS1 padding function the format of this padding is : 0x02 | 0x00 | [0xFF...0xFF] | 0x00 | [message] """ # compute the length in bytes of the message length = math.ceil(math.ceil(math.log2(message-1)) / 8) template = "0200" # generate a template 0xFFFFF.....FF of size_block bytes for i in range(size_block-2): template = template + 'FF' template = int(template,16) # Add the 00 of the end of the padding to the template for i in range(length+1) : template = template ^ (0xFF << i*8) # add the padding to the original message message = message | template return message
def testLog2(self): self.assertRaises(TypeError, math.log2) # Check some integer values self.assertEqual(math.log2(1), 0.0) self.assertEqual(math.log2(2), 1.0) self.assertEqual(math.log2(4), 2.0) # Large integer values #fixme brython (javascript chokes on these large values) #self.assertEqual(math.log2(2**1023), 1023.0) #self.assertEqual(math.log2(2**1024), 1024.0) #self.assertEqual(math.log2(2**2000), 2000.0) self.assertRaises(ValueError, math.log2, -1.5) self.assertRaises(ValueError, math.log2, NINF) self.assertTrue(math.isnan(math.log2(NAN))) #@unittest.skip("brython, skip for now")
def set_number_features_evaluated_split(self, row): """ Sets the number of considered features at each split depending on the max_split_features parameter. :param row: A single row of the features of shape (nb_features) """ if isinstance(self.max_split_features, int): self.considered_features = self.max_split_features if \ self.max_split_features <= len(row) else len(row) elif isinstance(self.max_split_features, str): if self.max_split_features in ['auto','sqrt']: self.considered_features = int(sqrt(len(row))) elif self.max_split_features == 'log2': self.considered_features = int(log2(len(row))) else: self.considered_features = len(row)
def testLog2(self): self.assertRaises(TypeError, math.log2) # Check some integer values self.assertEqual(math.log2(1), 0.0) self.assertEqual(math.log2(2), 1.0) self.assertEqual(math.log2(4), 2.0) # Large integer values self.assertEqual(math.log2(2**1023), 1023.0) self.assertEqual(math.log2(2**1024), 1024.0) self.assertEqual(math.log2(2**2000), 2000.0) self.assertRaises(ValueError, math.log2, -1.5) self.assertRaises(ValueError, math.log2, NINF) self.assertTrue(math.isnan(math.log2(NAN)))
def dynamic_timeout_formular(min_votes, votes_fraction): """ Formula used for dynamic timeout :param min_votes: :param votes_fraction: :return: """ if votes_fraction >= 1: return 1 / votes_fraction # Reduce timeout if more people than necessary voted result = 1 result += (1 - votes_fraction) * math.log2(min_votes) # Linear part makes timeout rise result += min_votes * ( min_votes ** ((1 - votes_fraction) ** 3) - 1) # Exponential part to punish really low vote counts result += (40 - 40 ** (votes_fraction)) / min_votes ** 2 # Punish missing votes harder if min_votes is low return result
def sampled_entropy(sample_size, success_size): ''' Estimate the change in entropy given a sample of passwords from a set. Rationalle: Assume that we can produce passwords with a known entropy. However, not all of these passwords are at least 24 characters long. Rather than try to calculate the exact change in entropy, we estimate it by generating {sample_size} passwords, and seeing that {success_size} meet our constraints. We then assume that the ratio of these numbers is proportional to the overall ratio of possible_passwords to allowed_passwords. This allows us to do the following caluclation: original_entropy = log2(permutation_space) new_entropy = log2(permutation_space * ratio) = log2(permutation_space) + log2(ratio) = log2(permutation_space) + log2(success_size / sample_size) = log2(permutation_space) + log2(success_size) - log2(sample_size) = original_entropy + log2(success_size) - log2(sample_size) ''' return log2(success_size) - log2(sample_size)
def Waifu2xResize(src, width, height, targetWidth, targetHeight): resize_factor = 2 ** math.ceil(max(math.log2(targetHeight / height), math.log2(targetWidth / targetWidth))) logging.debug("w: {} h: {} tw: {} th: {} rf: {}".format(width, height, targetWidth, targetHeight, resize_factor)) tmp = tempfile.mkstemp(suffix=".png", prefix="pmsync_cover_") try: subprocess.run(["waifu2x-converter-cpp", "--scale_ratio", str(resize_factor), "-m", "scale", "-i", src, "-o", tmp[1]], check=True, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE) except subprocess.CalledProcessError as e: logging.debug("=== CalledProcessError ===") logging.debug("cmd: {}".format(e.cmd)) logging.debug("output: {}".format(e.stdout.decode())) logging.debug("stderr: {}".format(e.stderr.decode())) logging.debug("=== CalledProcessError ===") os.remove(tmp[1]) return PILResize(tmp[1], width*resize_factor, height*resize_factor, targetWidth, targetHeight)
def getClass(self, string): maxProb = float('-inf') maxClass = '' for i in self.classCount: logProb = log2(self.classCount[i])-log2(sum(self.classCount.values())) # print(i) for word in string.split(): if word in self.wordsFreq: wordLogProb = log2(self.wordsFreq[word][i]+1)-log2(len(self.classFreq[i])+len(self.wordsFreq)+1) else: wordLogProb = -log2(len(self.classFreq[i])+len(self.wordsFreq)+1) logProb += wordLogProb # print('\t',word, wordProb) # print(prob,'\n****************') if logProb > maxProb: maxProb = logProb maxClass = i # print(maxProb) return maxClass
def calculateEntropy(self, corpus=None): corpus_size = len(corpus) if corpus is not None else len(self.corpus) return corpus_size, sum([1 / corpus_size * log(1 / corpus_size) for n in range(corpus_size)]) * -1
def WriteFloat(self, v): if v < 0: neg = (1 << 31) v *= -1 else: neg = 0 if v == 0: e = 0 m = 0 else: e = int(floor(log2(v/0x1000000))+1 + 150) if e < 0 or e > 0xff: print("float off range, assuming zero", file=sys.stderr) e = 0 m = 0 elif e == 0: e = e << 23 m = int(v / 2**(-150)) >> 1 else: e = e << 23 m = int(v/2**(floor(log2(v/0x1000000))+1)) if m < 0x800000 or m >= 0x1000000: raise ValueError('float dump: bad m value') m &= 0x7fffff self.writeDword(neg | e | m)
def log2(x: Real) -> Real: """ Return the logarithm of x in base 2. """ return _math.log2(x)
def get_metric_range(self, pre_dist): num_classes = len(pre_dist) if num_classes < 2: num_classes = 2 return math.log2(num_classes)
def _impl(self): self.DATA_WIDTH = int(self.DATA_WIDTH) vldAll = mask(self.DATA_WIDTH // 8) dout = self.dataOut DATA_LEN = len(self.DATA) wordIndex_w = int(math.log2(DATA_LEN) + 1) wordIndex = self._reg("wordIndex", Bits(wordIndex_w), defVal=0) Switch(wordIndex)\ .addCases([(i, dout.data(d)) for i, d in enumerate(self.DATA)])\ .Default(dout.data(None)) dout.last(wordIndex._eq(DATA_LEN - 1)) If(wordIndex < DATA_LEN, dout.strb(vldAll), dout.valid(1) ).Else( dout.strb(None), dout.valid(0) ) If(self.dataRd(), self.nextWordIndexLogic(wordIndex) )
def __init__(self, ranges, eps=0.001): ''' Class for individual in population. Random solution will be initialized by default. NOTE: The decrete precisions for different components in varants may be adjusted automatically (possible precision loss) if eps and ranges are not appropriate. :param ranges: value ranges for all entries in solution. :type ranges: list of range tuples. e.g. [(0, 1), (-1, 1)] :param eps: decrete precisions for binary encoding, default is 0.001. :type eps: float or float list with the same length with ranges. ''' super(self.__class__, self).__init__(ranges, eps) # Lengths for all binary sequence in chromsome and adjusted decrete precisions. self.lengths = [] for i, ((a, b), eps) in enumerate(zip(self.ranges, self.eps)): length = int(log2((b - a)/eps)) precision = (b - a)/(2**length) self.lengths.append(length) self.precisions[i] = precision # The start and end indices for each gene segment for entries in solution. self.gene_indices = self._get_gene_indices() # Initialize individual randomly. self.init()
def calculateIDF(self): # ??????????????? if len(self.wordset) == 0: self.buildWordSet() if len(self.words_location_record) == 0: self.buildWordLocationRecord() # ?? idf for word in self.wordset: self.words_idf[word] = math.log2((self.D + .5)/(self.words_location_record[word] + .5))
def _entropy(temp, prob): if prob == 0 or prob == 0.5 or temp == 0: return prob if prob < 0.5: return 1.0 - _original(temp, 1.0 - prob) coldness = 100.0 - temp a = math.sqrt(coldness) c = (10 - a) / 100 f = (c + 1) * prob return -f * math.log2(f)
def size(value, suffixes=None): """ >>> size(1024) '1 KB' :param size: :param suffixes: :return: """ suffixes = suffixes or [ 'bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] order = int(log2(value) / 10) if value else 0 return '{:.4g} {}'.format(value / (1 << (order * 10)), suffixes[order])
def __init__(self, n, out_size, out_channels, embed_size, block_size): super().__init__( l0=L.Linear(None, n * embed_size * embed_size), ln=L.Convolution2D(n, out_channels, 3, stride=1, pad=1)) self.embed_shape = (n, embed_size, embed_size) self.n_blocks = int(log2(out_size / embed_size)) + 1 self.block_size = block_size for i in range(self.n_blocks * block_size): self.add_link('c{}'.format(i), L.Convolution2D(n, n, 3, stride=1, pad=1))
def entropy_term(x): """Helper function for entropy_single: calculates one term in the sum.""" if x==0: return 0.0 else: return -x*math.log2(x)
def kl_term(x,y): """Helper function for kl: calculates one term in the sum.""" if x>0 and y>0: return x*math.log2(x/y) elif x==0: return 0.0 else: return math.inf
def _calc_entropy(self, data_set): """ compute the entropy of the data_set :param data_set: the data_set you want to calculator entropy :return: x, x is a numerical value represent the entropy """ class_list = [example[-1] for example in data_set] unique_class = set(class_list) record_num = len(data_set) entropy = 0.0 for item in unique_class: item_data = self._find_record_with_value(-1, data_set, item) probability = len(item_data) / record_num entropy -= probability * math.log2(probability) return entropy
def decode(message : int, pad_option : str) -> str : size_block = math.ceil(math.ceil(math.log2(message))/8)*8 message = unpadding(message, size_block, pad_option) message = binascii.unhexlify(hex(message)[2:]) return str(message)[2:-1]
def unpad_PKCS1(message : int, size_block : int) -> int: """ PKCS1 unpadding function """ pts = 0xff << (size_block - 24) while (pts & message != 0): pts = pts >> 8 a = ~pts & ((1 << size_block) - 1) message = message & (~pts & ((1 << math.ceil(math.log2(pts))) - 1) ) return message
def obj_IA_sum_rate(H, p, var_noise, K): y = 0.0 for i in range(K): s = var_noise for j in range(K): if j!=i: s = s+H[i,j]**2*p[j] y = y+math.log2(1+H[i,i]**2*p[i]/s) return y # Functions for WMMSE algorithm
def WMMSE_sum_rate(p_int, H, Pmax, var_noise): K = np.size(p_int) vnew = 0 b = np.sqrt(p_int) f = np.zeros(K) w = np.zeros(K) for i in range(K): f[i] = H[i, i] * b[i] / (np.square(H[i, :]) @ np.square(b) + var_noise) w[i] = 1 / (1 - f[i] * b[i] * H[i, i]) vnew = vnew + math.log2(w[i]) VV = np.zeros(100) for iter in range(100): vold = vnew for i in range(K): btmp = w[i] * f[i] * H[i, i] / sum(w * np.square(f) * np.square(H[:, i])) b[i] = min(btmp, np.sqrt(Pmax)) + max(btmp, 0) - btmp vnew = 0 for i in range(K): f[i] = H[i, i] * b[i] / ((np.square(H[i, :])) @ (np.square(b)) + var_noise) w[i] = 1 / (1 - f[i] * b[i] * H[i, i]) vnew = vnew + math.log2(w[i]) VV[iter] = vnew if vnew - vold <= 1e-3: break p_opt = np.square(b) return p_opt # Functions for performance evaluation
def testLog2Exact(self): #fixme brython. # 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 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)
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 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 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))