Python技术迷

NumPy教程-Numpy的hypot()方法

整理:python架构师

这个函数用于计算直角三角形的斜边长度(即斜边的长度)。直角三角形的斜边长度的计算公式如下:

h = sqrt(b**2 + p**2)

其中,b和p分别是直角三角形的底边和垂直边。

每个h的值都被复制到输出数组中。

语法

numpy.hypot(array1, array2[, out])

参数

  1. array1: 给定直角三角形底边的输入数组。

  2. array2: 给定直角三角形垂直边的输入数组。

  3. out: 输出数组的形状。

返回

返回一个包含给定直角三角形斜边长度的数组。

专属福利 

👉点击领取:最全Python资料合集

示例 1

import numpy as np  base = [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 np  base = 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 ]]。
Image
 1

Image

热门推荐

Image