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

คำนวณลอการิทึมฐาน 10 ด้วย scimath ใน Python

ฟังก์ชัน numpy.emath.log10() คำนวณ log10 รองรับค่าลบโดยให้ผลลัพธ์เป็นเชิงซ้อน

ขั้นตอน

import numpy as np

arr = np.array([10**1, -10**1, -10**2, 10**2, -10**3, 10**3])
print("Our Array...\n", arr)
print("\nDimensions of our Array...\n", arr.ndim)
print("\nDatatype of our Array object...\n", arr.dtype)
print("\nShape of our Array object...\n", arr.shape)
print("\nResult (log10)...\n", np.emath.log10(arr))

ตัวอย่างโค้ดสมบูรณ์

import numpy as np

arr = np.array([10**1, -10**1, -10**2, 10**2, -10**3, 10**3])
print("Our Array...\n", arr)
print("\nDimensions of our Array...\n", arr.ndim)
print("\nDatatype of our Array object...\n", arr.dtype)
print("\nShape of our Array object...\n", arr.shape)
print("\nResult (log10)...\n", np.emath.log10(arr))

ผลลัพธ์

Our Array... [ 10 -10 -100 100 -1000 1000]
Dimensions of our Array... 1
Datatype of our Array object... int64
Shape of our Array object... (6,)
Result (log10)... [1.+0.j 1.+1.36437635j 2.+1.36437635j 2.+0.j 3.+1.36437635j 3.+0.j ]