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

คำนวณรากที่สองของอินพุตด้วย emath ใน Python


ในการคำนวณสแควร์รูทของอินพุต ให้ใช้เมธอด scimath.sqrt() ใน Python Numpy เมธอดส่งคืนรากที่สองของ x ถ้า x เป็นสเกลาร์ ก็จะออกมา ไม่เช่นนั้นอาร์เรย์จะถูกส่งคืน พารามิเตอร์ x คือค่าอินพุต สำหรับองค์ประกอบอินพุตเชิงลบ ค่าที่ซับซ้อนจะถูกส่งคืน

ขั้นตอน

ขั้นแรก นำเข้าไลบรารีที่จำเป็น -

import numpy as np

การสร้างอาร์เรย์ numpy โดยใช้เมธอด array() -

arr = np.array([1, 4, 9, 16, 25, 36])

แสดงอาร์เรย์ -

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)

ในการคำนวณสแควร์รูทของอินพุต ให้ใช้เมธอด scimath.sqrt() ใน Python Numpy เมธอดส่งคืนรากที่สองของ x ถ้า x เป็นสเกลาร์ ก็จะออกมา ไม่เช่นนั้นอาร์เรย์จะถูกส่งคืน -

print("\nResult...\n",np.emath.sqrt(arr))

ตัวอย่าง

import numpy as np

# Creating a numpy array using the array() method
arr = np.array([1, 4, 9, 16, 25, 36])

# Display the array
print("Our Array...\n",arr)

# Check the Dimensions
print("\nDimensions of our Array...\n",arr.ndim)

# Get the Datatype
print("\nDatatype of our Array object...\n",arr.dtype)

# Get the Shape
print("\nShape of our Array object...\n",arr.shape)

# To compute the square root of input, use the scimath.sqrt() method in Python Numpy
# The method returns the square root of x. If x was a scalar, so is out, otherwise an array is returned.
print("\nResult...\n",np.emath.sqrt(arr))

ผลลัพธ์

Our Array...
[ 1 4 9 16 25 36]

Dimensions of our Array...
1

Datatype of our Array object...
int64

Shape of our Array object...
(6,)

Result...
[1. 2. 3. 4. 5. 6.]