本文整理汇总了Python中math.log1p方法的典型用法代码示例。如果您正苦于以下问题:Python math.log1p方法的具体用法?Python math.log1p怎么用?Python math.log1p使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math
的用法示例。
在下文中一共展示了math.log1p方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_math_subclass
# 需要导入模块: import math [as 别名]
# 或者: from math import log1p [as 别名]
def test_math_subclass(self):
"""verify subtypes of float/long work w/ math functions"""
import math
class myfloat(float): pass
class mylong(long): pass
mf = myfloat(1)
ml = mylong(1)
for x in math.log, math.log10, math.log1p, math.asinh, math.acosh, math.atanh, math.factorial, math.trunc, math.isinf:
try:
resf = x(mf)
except ValueError:
resf = None
try:
resl = x(ml)
except ValueError:
resl = None
self.assertEqual(resf, resl)
示例2: include_revision
# 需要导入模块: import math [as 别名]
# 或者: from math import log1p [as 别名]
def include_revision(revision_num, skip_factor=1.1):
"""Decide whether to include a revision.
If the number of revisions is large, we exclude some revisions to avoid
a quadratic blowup in runtime, since the article is likely also large.
We make the ratio between consecutive included revision numbers
appproximately equal to "factor".
Args:
revision_num: an integer
skip_factor: a floating point number >= 1.0
Returns:
a boolean
"""
if skip_factor <= 1.0:
return True
return (int(math.log1p(revision_num) / math.log(skip_factor)) != int(
math.log(revision_num + 2.0) / math.log(skip_factor)))
示例3: _isapressalt_m
# 需要导入模块: import math [as 别名]
# 或者: from math import log1p [as 别名]
def _isapressalt_m(self, pressure_pa):
"""Returns the geopotential alt (m) at which ISA has the given pressure"""
press_it = np.nditer([pressure_pa, None])
for press_pa, alt_m in press_it:
if press_pa > self._pLevel1:
# Troposphere
alt_m[...] = (press_pa ** (1 / self._E1) - self._C1) / self._D1
elif press_pa > self._pLevel2:
# Lower stratopshere
alt_m[...] = (1 / self._G2) * math.log1p(press_pa / self._F2 - 1)
elif press_pa > self._pLevel3:
# Upper stratopshere
alt_m[...] = (press_pa ** (1 / self._E3) - self._C3) / self._D3
elif press_pa > self._pLevel4:
# Between 32 and 47 km
alt_m[...] = (press_pa ** (1 / self._E4) - self._C4) / self._D4
else:
# Between 47km and 51km
alt_m[...] = (1 / self._G5) * math.log1p(press_pa / self._F5 - 1)
return press_it.operands[1]
示例4: _isadensalt_m
# 需要导入模块: import math [as 别名]
# 或者: from math import log1p [as 别名]
def _isadensalt_m(self, dens_kgpm3):
"""Returns the geopotential alt (m) at which ISA has given density (kg/m^3)"""
dense_it = np.nditer([dens_kgpm3, None])
for d_kgpm3, alt_m in dense_it:
if d_kgpm3 > self._dLevel1:
# Troposphere
alt_m[...] = (d_kgpm3 ** (1 / self._L1) - self._I1) / self._J1
elif d_kgpm3 > self._dLevel2:
# Lower stratopshere
alt_m[...] = (1 / self._N2) * math.log1p(dens_kgpm3 / self._M2 - 1)
elif d_kgpm3 > self._dLevel3:
# Upper stratopshere
alt_m[...] = (d_kgpm3 ** (1 / self._L3) - self._I3) / self._J3
elif dens_kgpm3 > self._dLevel4:
# Between 32 and 47 km
alt_m[...] = (d_kgpm3 ** (1 / self._L4) - self._I4) / self._J4
else:
# Between 47km and 51km
alt_m[...] = (1 / self._N5) * math.log1p(d_kgpm3 / self._M5 - 1)
# Adjust for the temperature offset
return dense_it.operands[1]
示例5: __add__
# 需要导入模块: import math [as 别名]
# 或者: from math import log1p [as 别名]
def __add__(self, other):
if other < 0:
return self - -other
log_other = self._log(other)
if self.log_value > log_other:
log_l, log_s = self.log_value, log_other
elif self.log_value < log_other:
log_l, log_s = log_other, self.log_value
else: # Must be equal, so just double value
return self * 2
if log_s == float("-inf"): # Just return largest value
return Probability(log_l, log_value=True)
return Probability(log_l + log1p(exp(log_s - log_l)),
log_value=True)
示例6: __sub__
# 需要导入模块: import math [as 别名]
# 或者: from math import log1p [as 别名]
def __sub__(self, other):
if other < 0:
return self + -other
log_other = self._log(other)
if self.log_value > log_other:
log_l, log_s = self.log_value, log_other
elif self.log_value < log_other: # Result will be negative
return float(self) - other
else: # Must be equal, so return 0
return Probability(float("-inf"), log_value=True)
if log_s == float("-inf"): # Just return largest value
return Probability(log_l, log_value=True)
exp_diff = exp(log_s - log_l)
if exp_diff == 1: # Diff too small, so result is effectively zero
return Probability(float("-inf"), log_value=True)
return Probability(log_l + log1p(-exp_diff),
log_value=True)
示例7: __rsub__
# 需要导入模块: import math [as 别名]
# 或者: from math import log1p [as 别名]
def __rsub__(self, other):
if other < 0: # Result will be negative
return other + -float(self)
log_other = self._log(other)
if log_other > self.log_value:
log_l, log_s = log_other, self.log_value
elif log_other < self.log_value: # Result will be negative
return other + -float(self)
else: # Must be equal, so return 0
return Probability(float("-inf"), log_value=True)
if log_s == float("-inf"): # Just return largest value
return Probability(log_l, log_value=True)
exp_diff = exp(log_s - log_l)
if exp_diff == 1: # Diff too small, so result is effectively zero
return Probability(float("-inf"), log_value=True)
return Probability(log_l + log1p(-exp_diff),
log_value=True)
示例8: PDF
# 需要导入模块: import math [as 别名]
# 或者: from math import log1p [as 别名]
def PDF(self,x):
if (x <= 0.0) or (x >= 1.0):
return 0.0
else:
logX = math.log(x)
if (x < 0.0) and (x > 0.0):
return 0.0
log1mX = math.log1p(-x)
ret = math.exp((self.alpha - 1.0) * logX + (self.beta - 1.0) \
* log1mX - self.Z)
return ret
示例9: _get_most_used_nodes
# 需要导入模块: import math [as 别名]
# 或者: from math import log1p [as 别名]
def _get_most_used_nodes(preferences: Preferences) -> Set[int]:
d = {}
for node in preferences:
d[node] = round(math.log1p(preferences[node] * 1000))
nodes = sorted(d.items(), reverse=True, key=lambda x: x[1])
z = nodes[0][1]
best_nodes = {x[0] for x in nodes if x[1] == z}
return best_nodes
示例10: testLog1p
# 需要导入模块: import math [as 别名]
# 或者: from math import log1p [as 别名]
def testLog1p(self):
self.assertRaises(TypeError, math.log1p)
self.ftest('log1p(1/e -1)', math.log1p(1/math.e-1), -1)
self.ftest('log1p(0)', math.log1p(0), 0)
self.ftest('log1p(e-1)', math.log1p(math.e-1), 1)
self.ftest('log1p(1)', math.log1p(1), math.log(2))
self.assertEqual(math.log1p(INF), INF)
self.assertRaises(ValueError, math.log1p, NINF)
self.assertTrue(math.isnan(math.log1p(NAN)))
n= 2**90
self.assertAlmostEqual(math.log1p(n), 62.383246250395075)
self.assertAlmostEqual(math.log1p(n), math.log1p(float(n)))
示例11: log_sum_exp
# 需要导入模块: import math [as 别名]
# 或者: from math import log1p [as 别名]
def log_sum_exp(a, b):
"""
Stable log sum exp.
"""
return max(a, b) + math.log1p(math.exp(-abs(a-b)))
示例12: test_log1p_expr
# 需要导入模块: import math [as 别名]
# 或者: from math import log1p [as 别名]
def test_log1p_expr():
expr = ast.Log1pExpr(ast.NumVal(2.0))
interpreter = interpreters.PythonInterpreter()
expected_code = """
import math
def score(input):
return math.log1p(2.0)
"""
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
示例13: test_pure_python_math_module
# 需要导入模块: import math [as 别名]
# 或者: from math import log1p [as 别名]
def test_pure_python_math_module(self):
vals = [1, -.5, 1.5, 0, 0.0, -2, -2.2, .2]
# not being tested: math.asinh, math.atanh, math.lgamma, math.erfc, math.acos
def f():
functions = [
math.sqrt, math.cos, math.sin, math.tan, math.asin, math.atan,
math.acosh, math.cosh, math.sinh, math.tanh, math.ceil,
math.erf, math.exp, math.expm1, math.factorial, math.floor,
math.log, math.log10, math.log1p
]
tr = []
for idx1 in range(len(vals)):
v1 = vals[idx1]
for funIdx in range(len(functions)):
function = functions[funIdx]
try:
tr = tr + [function(v1)]
except ValueError as ex:
pass
return tr
r1 = self.evaluateWithExecutor(f)
r2 = f()
self.assertGreater(len(r1), 100)
self.assertTrue(numpy.allclose(r1, r2, 1e-6))
示例14: idf
# 需要导入模块: import math [as 别名]
# 或者: from math import log1p [as 别名]
def idf(self, term: str) -> float:
r"""Calculate the Inverse Document Frequency of a term in the corpus.
Parameters
----------
term : str
The term to calculate the IDF of
Returns
-------
float
The IDF
Examples
--------
>>> tqbf = 'the quick brown fox jumped over the lazy dog\n\n'
>>> tqbf += 'and then it slept\n\n and the dog ran off'
>>> corp = UnigramCorpus(tqbf)
>>> round(corp.idf('dog'), 10)
0.6931471806
>>> round(corp.idf('the'), 10)
0.6931471806
.. versionadded:: 0.4.0
"""
if term in self.corpus:
count, term_doc_count = self.corpus[term]
return log1p(self.doc_count / term_doc_count)
else:
return float('inf')
示例15: sim
# 需要导入模块: import math [as 别名]
# 或者: from math import log1p [as 别名]
def sim(self, src: str, tar: str) -> float:
"""Return the Consonni & Todeschini III similarity of two strings.
Parameters
----------
src : str
Source string (or QGrams/Counter objects) for comparison
tar : str
Target string (or QGrams/Counter objects) for comparison
Returns
-------
float
Consonni & Todeschini III similarity
Examples
--------
>>> cmp = ConsonniTodeschiniIII()
>>> cmp.sim('cat', 'hat')
0.1648161441769704
>>> cmp.sim('Niall', 'Neil')
0.1648161441769704
>>> cmp.sim('aluminum', 'Catalan')
0.10396755253417303
>>> cmp.sim('ATCG', 'TAGC')
0.0
.. versionadded:: 0.4.0
"""
self._tokenize(src, tar)
a = self._intersection_card()
n = self._population_unique_card()
if src == tar and n <= a:
return 1.0
return log1p(a) / log1p(n)