ในการรับด้านตรงข้ามมุมฉาก ให้ใช้เมธอด numpy.hypot() ใน Python Numpy วิธีการส่งกลับด้านตรงข้ามมุมฉากของสามเหลี่ยม นี่คือสเกลาร์ถ้าทั้ง x1 และ x2 เป็นสเกลาร์ วิธีนี้เทียบเท่ากับ sqrt(x1**2 + x2**2) ซึ่งเป็นองค์ประกอบที่ชาญฉลาด ถ้า x1 หรือ x2 เป็น scalar_like จะมีการแพร่ภาพเพื่อใช้กับแต่ละองค์ประกอบของอาร์กิวเมนต์อื่น พารามิเตอร์คือขาของสามเหลี่ยม หาก x1.shape !=x2.shape จะต้องออกอากาศเป็นรูปร่างทั่วไปได้
ขั้นตอน
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import numpy as np
การสร้างอาร์เรย์ที่มีองค์ประกอบจำนวนเต็ม -
arr = np.ones((3, 3), dtype=int)
กำลังแสดงอาร์เรย์ของเรา -
print("Array...\n",arr) รับประเภทข้อมูล -
print("\nArray datatype...\n",arr.dtype)
รับขนาดของอาร์เรย์ -
print("\nArray Dimensions...\n",arr.ndim) รับจำนวนขององค์ประกอบของอาร์เรย์ -
print("\nNumber of elements in the Array...\n",arr.size) รับด้านตรงข้ามมุมฉาก -
print("\nHypotenuse..\n",np.hypot((3*arr), (4*arr))) ตัวอย่าง
import numpy as np
# To get the hypotenuse, use the numpy.hypot() method in Python Numpy.
# The method returns the hypotenuse of the triangle(s). This is a scalar if both x1 and x2 are scalars.
# This method is equivalent to sqrt(x1**2 + x2**2), element-wise. If x1 or x2 is scalar_like, it is broadcast for use with each element of the other argument.
# The parameters are the leg of the triangle(s). If x1.shape != x2.shape, they must be broadcastable to a common shape.
# Creating an array with integer elements
arr = np.ones((3, 3), dtype=int)
# Display the array
print("Array...\n", arr)
# Get the type of the array
print("\nOur Array type...\n", arr.dtype)
# Get the dimensions of the Array
print("\nOur Array Dimensions...\n",arr.ndim)
# Get the number of elements in the Array
print("\nNumber of elements...\n", arr.size)
# Get the hypotenuse
print("\nHypotenuse..\n",np.hypot((3*arr), (4*arr))) ผลลัพธ์
Array... [[1 1 1] [1 1 1] [1 1 1]] Our Array type... int64 Our Array Dimensions... 2 Number of elements... 9 Hypotenuse.. [[5. 5. 5.] [5. 5. 5.] [5. 5. 5.]]