How should I compute log to the base two in python. Eg. I have this equation where I am using log base 2
import math
e = -(t/T)* math.log((t/T)[, 2])
回答 0
很高兴知道
但也知道它
math.log带有一个可选的第二个参数,该参数允许您指定基数:
In[22]:import mathIn[23]: math.log?Type: builtin_function_or_methodBaseClass:<type 'builtin_function_or_method'>StringForm:<built-in function log>Namespace:InteractiveDocstring:
log(x[, base])-> the logarithm of x to the given base.If the base not specified, returns the natural logarithm (base e) of x.In[25]: math.log(8,2)Out[25]:3.0
but also know that
math.log takes an optional second argument which allows you to specify the base:
In [22]: import math
In [23]: math.log?
Type: builtin_function_or_method
Base Class: <type 'builtin_function_or_method'>
String Form: <built-in function log>
Namespace: Interactive
Docstring:
log(x[, base]) -> the logarithm of x to the given base.
If the base not specified, returns the natural logarithm (base e) of x.
In [25]: math.log(8,2)
Out[25]: 3.0
Python frexp() calls the C function frexp() which just grabs and tweaks the exponent.
Python frexp() returns a tuple (mantissa, exponent). So [1] gets the exponent part.
For integral powers of 2 the exponent is one more than you might expect. For example 32 is stored as 0.5×2⁶. This explains the - 1 above. Also works for 1/32 which is stored as 0.5×2⁻⁴.
Floors toward negative infinity, so log₂31 computed this way is 4 not 5. log₂(1/17) is -5 not -4.
If you are on python 3.3 or above then it already has a built-in function for computing log2(x)
import math
'finds log base2 of x'
answer = math.log2(x)
If you are on older version of python then you can do like this
import math
'finds log base2 of x'
answer = math.log(x)/math.log(2)
回答 3
使用numpy:
In[1]:import numpy as npIn[2]: np.log2?Type: functionBaseClass:<type 'function'>StringForm:<function log2 at 0x03049030>Namespace:InteractiveFile: c:\python26\lib\site-packages\numpy\lib\ufunclike.pyDefinition: np.log2(x, y=None)Docstring:Return the base 2 logarithm of the input array, element-wise.Parameters----------
x : array_likeInput array.
y : array_likeOptional output array with the same shape as`x`.Returns-------
y : ndarrayThe logarithm to the base 2 of `x` element-wise.NaNs are returned where `x`is negative.SeeAlso--------
log, log1p, log10Examples-------->>> np.log2([-1,2,4])
array([NaN,1.,2.])In[3]: np.log2(8)Out[3]:3.0
In [1]: import numpy as np
In [2]: np.log2?
Type: function
Base Class: <type 'function'>
String Form: <function log2 at 0x03049030>
Namespace: Interactive
File: c:\python26\lib\site-packages\numpy\lib\ufunclike.py
Definition: np.log2(x, y=None)
Docstring:
Return the base 2 logarithm of the input array, element-wise.
Parameters
----------
x : array_like
Input array.
y : array_like
Optional output array with the same shape as `x`.
Returns
-------
y : ndarray
The logarithm to the base 2 of `x` element-wise.
NaNs are returned where `x` is negative.
See Also
--------
log, log1p, log10
Examples
--------
>>> np.log2([-1, 2, 4])
array([ NaN, 1., 2.])
In [3]: np.log2(8)
Out[3]: 3.0
def lg(x, tol=1e-13):
res =0.0# Integer partwhile x<1:
res -=1
x *=2while x>=2:
res +=1
x /=2# Fractional part
fp =1.0while fp>=tol:
fp /=2
x *= xif x >=2:
x /=2
res += fpreturn res
def lg(x, tol=1e-13):
res = 0.0
# Integer part
while x<1:
res -= 1
x *= 2
while x>=2:
res += 1
x /= 2
# Fractional part
fp = 1.0
while fp>=tol:
fp /= 2
x *= x
if x >= 2:
x /= 2
res += fp
return res
回答 5
>>>def log2( x ):...return math.log( x )/ math.log(2)...>>> log2(2)1.0>>> log2(4)2.0>>> log2(8)3.0>>> log2(2.4)1.2630344058337937>>>