无涯教程-NumPy - hypot()函数

169 阅读1分钟

此函数用于计算直角三角形的斜边。直角的斜边的计算公式为:

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

其中b和p是直角三角形的底和垂直方向。

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

语法

numpy.hypot(array1, array2 out)

参数

  1. array1:它是给定直角三角形的底边的输入数组。
  2. array2:它是给定直角三角形的垂直线的输入数组。
  3. out:它是输出数组的形状。

返回

它返回一个数组,其中包含给定直角三角形的斜边。

例子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 ]]

参考链接

www.learnfk.com/numpy/numpy…