対数の値を取得するPythonコード

対数 $\log_a x$ の値をPythonで計算してみよう。

対数の値の取得①

mathモジュールのlog(x,a)で対数 $\log_ax$ の値を取得することができる。log(x)とすると底が自然対数である $\log_ex$ の値が取得できる。

対数の値の取得②

numpyモジュールのlog(x)で底が自然対数である $\log_ex$ の値が取得できる。log2(x)log10(x)で対数 $\log_2x$ と $\log_{10}x$ の値を取得することができる。

任意の底の対数の値 $\log_ax$ を取得する場合, numpy.log(x)/numpy.log(a)で取得する。

Python. $\log_{10}{2}$ と $\log_e2$, $\log_35$ の値を取得して表示する。

import math
import numpy as np

#mathモジュール
print(math.log(2,10))
print(math.log(2))
print(math.log(5, 3))

#numpyモジュール
print(np.log10(2))
print(np.log(2))
print(np.log(5)/np.log(3))

$0.30102999566398114$
$0.6931471805599453$
$1.4649735207179269$
$0.3010299956639812$
$0.6931471805599453$
$1.4649735207179269$

と表示された。

コメントを残す