NumPy arctan()

numpy.arctan() 方法计算数组的反正切(逆正切)。

示例

import numpy as np

# create an array 
array1 = np.array([0, 1, -1])

# calculates the element-wise arctangent (inverse tangent) of array1 result = np.arctan(array1)
print(result) # Output: [ 0. 0.78539816 -0.78539816]

arctan() 语法

numpy.arctan() 方法的语法是:

numpy.arctan(x, out = None, where = True, dtype = None)

arctan() 参数

numpy.arctan() 方法接受以下参数:

  • x - 输入数组
  • out (可选) - 用于存储结果的输出数组
  • where (可选) - 一个布尔数组或条件,指示在何处计算反正切。
  • dtype (可选) - 输出数组的数据类型

arctan() 返回值

numpy.arctan() 方法返回一个包含相应反正切值的数组。


示例 1:在 arctan() 中使用 out 和 where

import numpy as np

array1 = np.array([0, -1, 1, 10, 100, -2])

# create an array of zeros with the same shape as array1
result = np.zeros_like(array1, dtype=float)  

# compute inverse tangent of elements in array1 # only where the element is greater than or equal to 0 np.arctan(array1, out = result, where = (array1 >= 0))
print(result)

输出

[0.         0.         0.78539816 1.47112767 1.56079666 0.        ]

这里,

  • out = result 指定 numpy.arctan() 方法的输出应存储在 result 数组中,
  • where = (array1 >= 0) 指定逆正切运算仅应用于 array1 中大于或等于 0 的元素。

示例 2:在 arctan() 中使用 dtype 参数

import numpy as np

# create an array
values = np.array([0, 1, -1])

# calculate the inverse tangent of # each value with float data type arctans_float = np.arctan(values, dtype = float)
print("Inverse Tangent with 'float' dtype:") print(arctans_float)
# calculate the inverse tangent of # each value with complex data type arctans_complex = np.arctan(values, dtype = complex)
print("\nInverse Tangent with 'complex' dtype:") print(arctans_complex)

输出

Inverse Tangent with 'float' dtype:
[ 0.          0.78539816 -0.78539816]

Inverse Tangent with 'complex' dtype:
[ 0.        +0.j  0.78539816+0.j -0.78539816+0.j]

在这里,通过指定所需的 dtype,我们可以根据特定需求控制输出数组的数据类型。

注意:要了解有关 dtype 参数的更多信息,请访问 NumPy 数据类型

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战