Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ฟังก์ชัน Logarithm ใน Python: math.log, log2, log10, log1p

โมดูล math ใน Python มาพร้อมกับฟังก์ชัน Logarithm หลายรูปแบบสำหรับฐานต่างๆ ซึ่งมีประโยชน์ในการคำนวณทางคณิตศาสตร์ วิศวกรรม และ Data Science

ฟังก์ชัน Logarithm หลัก

ฟังก์ชันฐาน (Base)คำอธิบาย
math.log(x)e (Natural Log)Natural Logarithm (ln)
math.log(x, base)กำหนดเองLogarithm ฐานใดก็ได้
math.log2(x)2Binary Logarithm (เร็วกว่า log(x, 2))
math.log10(x)10Common Logarithm (เร็วกว่า log(x, 10))
math.log1p(x)elog(1+x) - แม่นยำสำหรับ x ใกล้ 0

ตัวอย่างการใช้งาน

import math

# Natural Log (ฐาน e)
print(f"ln(56) = {math.log(56):.6f}")           # 4.025352

# Log ฐาน 8
print(f"log8(64) = {math.log(64, 8):.6f}")       # 2.000000

# Log ฐาน 2 (Binary Log)
print(f"log2(12) = {math.log2(12):.6f}")         # 3.584963

# Log ฐาน 10 (Common Log)
print(f"log10(64) = {math.log10(64):.6f}")       # 1.806180

# log1p(x) = log(1+x) - แม่นยำสำหรับค่าเล็ก
print(f"log1p(4) = {math.log1p(4):.6f}")         # 1.609438 (เท่ากับ ln(5))

คุณสมบัติทางคณิตศาสตร์

import math

x, y = 100, 10

# log(a*b) = log(a) + log(b)
print(math.log(x * y) == math.log(x) + math.log(y))  # True (ใกล้เคียง)

# log(a/b) = log(a) - log(b)
print(math.log(x / y) == math.log(x) - math.log(y))  # True

# log(a^b) = b * log(a)
print(math.log(x ** 3) == 3 * math.log(x))           # True

# Change of base: log_a(b) = log_c(b) / log_c(a)
print(math.log(64, 8) == math.log2(64) / math.log2(8))  # True

การจัดการข้อผิดพลาด (Error Handling)

Logarithm ของค่า ≤ 0 ไม่ได้ถูกนิยามในจำนวนจริง จะโยน ValueError

import math

try:
    print(math.log(0))      # ValueError: math domain error
except ValueError as e:
    print(f"Error: {e}")

try:
    print(math.log(-5))     # ValueError: math domain error
except ValueError as e:
    print(f"Error: {e}")

# log1p กับค่าติดลบ ≤ -1 ก็ error
try:
    print(math.log1p(-2))   # ValueError
except ValueError as e:
    print(f"Error: {e}")

NumPy Logarithm (สำหรับ Array)

import numpy as np

arr = np.array([1, 2, 4, 8, 16, 32])

print("np.log:", np.log(arr))      # Natural log
print("np.log2:", np.log2(arr))    # Base 2
print("np.log10:", np.log10(arr))  # Base 10
print("np.log1p:", np.log1p(arr))  # log(1+x)

# กำหนดฐานเอง
print("log base 3:", np.log(arr) / np.log(3))

การประยุกต์ใช้งานจริง

1. คำนวณ Compound Interest (ดอกเบี้ยทบต้น)

import math

# t = ln(A/P) / ln(1+r)
P = 10000    # เงินต้น
A = 15000    # เงินรวม
r = 0.05     # อัตราดอกเบี้ย 5%

t = math.log(A / P) / math.log(1 + r)
print(f"ใช้เวลา {t:.2f} ปี")

2. Entropy ใน Information Theory

import math

probabilities = [0.5, 0.25, 0.125, 0.125]
entropy = -sum(p * math.log2(p) for p in probabilities)
print(f"Entropy: {entropy:.4f} bits")

3. pH Calculation

import math

h_concentration = 1e-7  # Molar
pH = -math.log10(h_concentration)
print(f"pH: {pH:.2f}")

สรุป

  • ใช้ math.log(x) สำหรับ Natural Log (ฐาน e)
  • ใช้ math.log2(x) และ math.log10(x) สำหรับฐาน 2 และ 10 (เร็วและแม่นยำกว่า)
  • ใช้ math.log1p(x) สำหรับ log(1+x) เมื่อ x เล็กมาก
  • ระวัง ValueError สำหรับค่า ≤ 0
  • สำหรับ Array ให้ใช้ numpy.log แทน