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

การคำนวณรากที่สาม (Cube Root) และเลขยกกำลัง 10 (exp10) ด้วย SciPy ใน Python

SciPy มีฟังก์ชันทางคณิตศาสตร์พิเศษในโมดูล scipy.special รวมถึงการคำนวณรากที่สามและเลขยกกำลัง 10

1. Cube Root - รากที่สาม (cbrt)

คำนวณ ∛x สำหรับทุกค่า Real (รวมลบได้) ต่างจาก x**(1/3) ที่ให้ Complex สำหรับค่าลบ

ไวยากรณ์

scipy.special.cbrt(x)

ตัวอย่าง


from scipy.special import cbrt

my_cb = cbrt([27, 89, -8, -27])
print("The cube roots are :")
print(my_cb)

ผลลัพธ์


The cube roots are : [ 3.          4.4647451  -2.         -3.        ]

2. Exponential Base 10 (exp10)

คำนวณ 10^x สำหรับแต่ละ Element

ไวยากรณ์

scipy.special.exp10(x)

ตัวอย่าง


from scipy.special import exp10

my_exp = exp10([12, 17, -3, 0])
print("The exponential values are :")
print(my_exp)

ผลลัพธ์


The exponential values are : [1.e+12 1.e+17 1.e-03 1.e+00]

คำอธิบาย

  • นำเข้า cbrt และ exp10 จาก scipy.special

  • ส่ง List/Array เข้าไป คืนค่า Array ผลลัพธ์

  • cbrt จัดการค่าลบได้ถูกต้อง (คืนค่าลบ)

  • exp10 เทียบเท่า 10**x แต่เร็วกว่าสำหรับ Array ใหญ่

ฟังก์ชันเกี่ยวข้องใน scipy.special

ฟังก์ชันคำอธิบาย
cbrt(x)Cube Root (∛x)
exp10(x)10^x
exp(x)e^x
exp2(x)2^x
log10(x)log₁₀(x)
log2(x)log₂(x)
sqrt(x)Square Root (√x)

ประโยชน์: ใช้ใน Signal Processing, Scientific Computing, Log-scale Calculations และการจัดการค่าที่มี Dynamic Range กว้าง