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

คำนวณกำลังของค่าลบด้วย scimath ใน Python

ฟังก์ชัน numpy.emath.power() คำนวณ x**p หาก x มีค่าลบ ผลลัพธ์จะเป็นชนิดเชิงซ้อน

ขั้นตอน

import numpy as np

arr = np.array([2, -4, -8, 16, -32])
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...\n", np.emath.power(arr, 2))

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

import numpy as np

arr = np.array([2, -4, -8, 16, -32])
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...\n", np.emath.power(arr, 2))

ผลลัพธ์

Our Array... [ 2 -4 -8 16 -32]
Dimensions of our Array... 1
Datatype of our Array object... int64
Shape of our Array object... (5,)
Result... [ 4.+0.j 16.-0.j 64.-0.j 256.+0.j 1024.-0.j]