Hoe moet ik log op de basis twee in Python berekenen. Bijv. Ik heb deze vergelijking waarbij ik logbasis 2
gebruik
import math
e = -(t/T)* math.log((t/T)[, 2])
Antwoord 1, Autoriteit 100%
Het is goed om te weten dat
, maar weet dat ook
math.log
Neemt een optioneel tweede argument waarmee u de basis kunt opgeven:
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
Antwoord 2, Autoriteit 34%
float → float math.log2(x)
import math
log2 = math.log(x, 2.0)
log2 = math.log2(x) # python 3.3 or later
- Bedankt @Akashchandrakar en @unutbu .
Float → int math.frexp(x)
Als alles wat u nodig heeft, is het gehele getal van logbasis 2 van een drijvend puntnummer, het extraheren van de exponent is vrij efficiënt:
log2int_slow = int(math.floor(math.log(x, 2.0))) # these give the
log2int_fast = math.frexp(x)[1] - 1 # same result
-
Python frexp() roept de C-functie frexp()aan die grijpt en past gewoon de exponent aan.
-
Python frexp() retourneert een tuple (mantisse, exponent). Dus
[1]
krijgt het exponentgedeelte. -
Voor integrale machten van 2 is de exponent één meer dan je zou verwachten. 32 wordt bijvoorbeeld opgeslagen als 0,5×2⁶. Dit verklaart de
- 1
hierboven. Werkt ook voor 1/32 die wordt opgeslagen als 0,5×2⁻⁴. -
Verdiepingen richting negatief oneindig, dus log₂31 zo berekend is 4 niet 5. log₂(1/17) is -5 en niet -4.
int → int x.bit_length()
Als zowel invoer als uitvoer gehele getallen zijn, kan deze native integer-methode zeer efficiënt zijn:
log2int_faster = x.bit_length() - 1
-
- 1
omdat 2ⁿ n+1 bits vereist. Werkt voor zeer grote gehele getallen, b.v.2**10000
. -
Verdiepingen richting negatief oneindig, dus log₂31 op deze manier berekend is 4 niet 5.
Antwoord 3, autoriteit 7%
Als u python 3.3 of hoger gebruikt, heeft het al een ingebouwde functie voor het berekenen van log2(x)
import math
'finds log base2 of x'
answer = math.log2(x)
Als u een oudere versie van python gebruikt, kunt u dit als volgt doen
import math
'finds log base2 of x'
answer = math.log(x)/math.log(2)
Antwoord 4, autoriteit 4%
Numpy gebruiken:
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
Antwoord 5, autoriteit 3%
http://en.wikipedia.org/wiki/Binary_logaritme
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
Antwoord 6, autoriteit 2%
>>> 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
>>>
Antwoord 7
Probeer dit,
import math
print(math.log(8,2)) # math.log(number,base)
Antwoord 8
In python 3 of hoger heeft de wiskundeklasse de volgende functies
import math
math.log2(x)
math.log10(x)
math.log1p(x)
of je kunt in het algemeen math.log(x, base)
gebruiken voor elke basis die je wilt.
Antwoord 9
Vergeet niet dat log[base A] x = log[base B] x / log[base B] A.
Dus als je alleen log
(voor natuurlijk logboek) en log10
(voor base-10 log) hebt, kun je
myLog2Answer = log10(myInput) / log10(2)