NumPy教程-Numpy的hypot()方法
这个函数用于计算直角三角形的斜边长度(即斜边的长度)。直角三角形的斜边长度的计算公式如下:
h = sqrt(b**2 + p**2)
其中,b和p分别是直角三角形的底边和垂直边。
每个h的值都被复制到输出数组中。
语法
numpy.hypot(array1, array2[, out])
参数
array1: 给定直角三角形底边的输入数组。
array2: 给定直角三角形垂直边的输入数组。
out: 输出数组的形状。
返回
返回一个包含给定直角三角形斜边长度的数组。
👉点击领取:最全Python资料合集
示例 1
import numpy as npbase = [10,2,5,50]per= [3,10,23,6]print("Input base array:",base)print("Input perpendicular array:",per)hyp = np.hypot(base,per)print("hypotenuse ",hyp)
输出:
Input base array: [10, 2, 5, 50]Input perpendicular array: [3, 10, 23, 6]hypotenuse [10.44030651 10.19803903 23.53720459 50.35871325]
示例 2
import numpy as npbase = np.random.rand(3,4)per= np.random.rand(3, 4)print("Input base array:",base)print("Input perpendicular array:",per)hyp = np.hypot(base,per)print("hypotenuse ",hyp)
输出:
Input base array: [[0.38040712 0.8629511 0.5018026 0.08201071][0.77464952 0.68392036 0.10326945 0.69031164][0.35380139 0.58803283 0.19791751 0.48756617]]Input perpendicular array: [[0.50466085 0.38053395 0.96808322 0.69790711][0.77802303 0.5486305 0.34418331 0.69201998][0.28038631 0.35850426 0.90459831 0.13383838]]hypotenuse [[0.63197481 0.94312814 1.09040862 0.70270911][1.09790788 0.87677961 0.35934208 0.97745681][0.45143318 0.68870017 0.92599646 0.5056021 ]]。
热门推荐