Python에서 base 2에 로그인
파이썬에서 기본 2에 대한 로그를 어떻게 계산해야합니까? 예 : 나는 log base 2를 사용하는 방정식이 있습니다.
import math
e = -(t/T)* math.log((t/T)[, 2])
알아서 반가워요
또한 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
플로트 → 플로트 math.log2(x)
import math
log2 = math.log(x, 2.0)
log2 = math.log2(x) # python 3.4 or later
- @akashchandrakar 와 @unutbu 에게 감사드립니다 .
float → int math.frexp(x)
필요한 모든 것이 부동 소수점 수의 밑수 2 로그의 정수 부분이면 지수를 추출하는 것이 매우 효율적입니다.
log2int_slow = int(math.floor(math.log(x, 2.0)))
log2int_fast = math.frexp(x)[1] - 1
파이썬 frexp () 는 지수를 잡고 조정하는 C 함수 frexp () 를 호출합니다 .
파이썬 frexp ()는 튜플 (가수, 지수)을 반환합니다. 그래서
[1]
지수 부분을 얻습니다.2의 적분 거듭 제곱의 경우 지수는 예상보다 1이 더 많습니다. 예를 들어 32는 0.5x2⁶으로 저장됩니다. 이것은
- 1
위의 설명 입니다. 0.5x2⁻⁴로 저장된 1/32에서도 작동합니다.음의 무한대를 향하는 플로어이므로 log₂31은 5가 아니라 4입니다. log₂ (1/17)은 -4가 아니라 -5입니다.
int → int x.bit_length()
입력과 출력이 모두 정수인 경우이 기본 정수 메서드는 매우 효율적일 수 있습니다.
log2int_faster = x.bit_length() - 1
- 1
2ⁿ에는 n + 1 비트가 필요하기 때문입니다. 매우 큰 정수 (예 :2**10000
.음의 무한대를 향하는 플로어이므로 log₂31은 5가 아니라 4입니다. log₂ (1/17)은 -4가 아니라 -5입니다.
파이썬 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)
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
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
>>> 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
>>>
logbase2 (x) = log (x) / log (2)
이 시도 ,
import math
print(math.log(8,2)) # math.log(number,base)
파이썬 3 이상에서 수학 클래스에는 다음과 같은 기능이 있습니다.
import math
math.log2(x)
math.log10(x)
math.log1p(x)
또는 일반적으로 math.log(x, base)
원하는베이스에 사용할 수 있습니다 .
log_base_2 (x) = log (x) / log (2)
log [base A] x = log [base B] x / log [base B] A 라는 것을 잊지 마십시오 .
따라서 log
(자연 로그의 경우) 및 log10
(베이스 10 로그의 경우) 만 있으면 다음을 사용할 수 있습니다.
myLog2Answer = log10(myInput) / log10(2)
참고 URL : https://stackoverflow.com/questions/3719631/log-to-the-base-2-in-python
'program tip' 카테고리의 다른 글
## 및 __LINE __ (위치 매크로와 토큰 연결)로 C 매크로 만들기 (0) | 2020.08.18 |
---|---|
PyPlot을 사용하여 부드러운 선 플로팅 (0) | 2020.08.18 |
HTML 링크를 통해 SMS 본문 텍스트를 미리 채우는 방법 (0) | 2020.08.17 |
코드 실행 시간 측정 (0) | 2020.08.17 |
업데이트 후 Android 스튜디오의 리소스 오류 : 리소스를 찾을 수 없음 (0) | 2020.08.17 |