问题:登录到以2为底的python

我应该如何计算以python为底数的两个日志。例如。我在使用对数基数2的地方有这个方程式

import math
e = -(t/T)* math.log((t/T)[, 2])

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 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

It’s good to know that

alt text

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

回答 1

浮动→浮动 math.log2(x)

import math

log2 = math.log(x, 2.0)
log2 = math.log2(x)   # python 3.4 or later

浮点数→整数 math.frexp(x)

如果您只需要浮点数的对数2的整数部分,则提取指数非常有效:

log2int_slow = int(math.floor(math.log(x, 2.0)))
log2int_fast = math.frexp(x)[1] - 1
  • Python frexp()调用C函数frexp(),该函数仅捕获和调整指数。

  • Python frexp()返回一个元组(尾数,指数)。因此[1]得到指数部分。

  • 对于2的整数次方,指数比您期望的多一。例如,将32存储为0.5×2⁶。- 1上面解释了这一点。也适用于1/32(存储为0.5×2⁻⁴)。

  • 朝向负无穷大,因此log 2 31是4而不是5。log 2(1/17)是-5不是-4。


整数→整数 x.bit_length()

如果输入和输出均为整数,则此本机整数方法可能非常有效:

log2int_faster = x.bit_length() - 1
  • - 1因为2ⁿ需要n + 1位。适用于非常大的整数,例如2**10000

  • 朝向负无穷大,因此log 2 31是4而不是5。log 2(1/17)是-5不是-4。

float → float math.log2(x)

import math

log2 = math.log(x, 2.0)
log2 = math.log2(x)   # python 3.3 or later

float → int math.frexp(x)

If all you need is the integer part of log base 2 of a floating point number, extracting the exponent is pretty efficient:

log2int_slow = int(math.floor(math.log(x, 2.0)))
log2int_fast = math.frexp(x)[1] - 1
  • 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.


int → int x.bit_length()

If both input and output are integers, this native integer method could be very efficient:

log2int_faster = x.bit_length() - 1
  • - 1 because 2ⁿ requires n+1 bits. Works for very large integers, e.g. 2**10000.

  • Floors toward negative infinity, so log₂31 computed this way is 4 not 5.


回答 2

如果您使用的是python 3.4或更高版本,则它已经具有用于计算log2(x)的内置函数

import math
'finds log base2 of x'
answer = math.log2(x)

如果您使用的是旧版本的python,则可以这样做

import math
'finds log base2 of x'
answer = math.log(x)/math.log(2)

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 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

Using numpy:

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

回答 4

http://en.wikipedia.org/wiki/Binary_logarithm

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

http://en.wikipedia.org/wiki/Binary_logarithm

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
>>> 
>>> 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
>>> 

回答 6

试试这个 ,

import math
print(math.log(8,2))  # math.log(number,base) 

Try this ,

import math
print(math.log(8,2))  # math.log(number,base) 

回答 7

logbase2(x)= log(x)/ log(2)

logbase2(x) = log(x)/log(2)


回答 8

在python 3或更高版本中,math类具有休闲功能

import math

math.log2(x)
math.log10(x)
math.log1p(x)

或者您通常可以将其math.log(x, base)用于任何所需的基础。

In python 3 or above, math class has the fallowing functions

import math

math.log2(x)
math.log10(x)
math.log1p(x)

or you can generally use math.log(x, base) for any base you want.


回答 9

log_base_2(x)= log(x)/ log(2)

log_base_2(x) = log(x) / log(2)


回答 10

不要忘记,日志[基A] X =日志[底座B]×/日志[基B]甲

因此,如果您仅拥有log(用于自然对数)和log10(用于以10为底的对数),则可以使用

myLog2Answer = log10(myInput) / log10(2)

Don’t forget that log[base A] x = log[base B] x / log[base B] A.

So if you only have log (for natural log) and log10 (for base-10 log), you can use

myLog2Answer = log10(myInput) / log10(2)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。