给定直角三角形的两条直角边,用Python返回它的斜边。
要获得斜边,请在Python NumPy中使用numpy.hypot()方法。该方法返回三角形(s)的斜边。如果x1和x2都是标量,则这是一个标量。此方法等效于sqrt(x1**2 + x2**2),逐元素计算。如果x1或x2是标量类型,则将其广播以用于另一个参数的每个元素。参数是三角形(s)的直角边。如果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.]]
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP