NumPy arcsin()

arcsin() 方法计算数组中每个元素的反正弦(反 SINE)。

示例

import numpy as np

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

# calculate the inverse sine of each value inverseSine = np.arcsin(values)
print(inverseSine) # Output: [-1.57079633 -0.52359878 0. 0.52359878 1.57079633]

arcsin() 语法

arcsin() 的语法是:

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

arcsin() 参数

arcsin() 方法接受以下参数:

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

arcsin() 返回值

arcsin() 方法返回一个包含相应反正弦值的数组。


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

import numpy as np

# create an array of values between -0.5 and 0.5
values = np.array([-0.5, -0.2, 1, 0.2, 0.5])

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

# calculate inverse sine where values >= 0 and store in result. np.arcsin(values, out=result, where=(values >= 0))
print(result)

输出

[0.         0.         1.57079633 0.20135792 0.52359878]

这里,

  • out=result 指定 np.arcsin() 函数的输出将存储在 result 数组中。
  • where=(values >= 0) 指定反正弦运算仅应用于 values 中大于或等于 0 的元素。

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

import numpy as np

# create an array of values between -0.5 and 0.5
values = np.array([-0.5, -0.2, 0, 0.2, 0.5])

# calculate the inverse sine of each value with a specific dtype inverse_sines_float = np.arcsin(values, dtype=float) inverse_sines_complex = np.arcsin(values, dtype=complex)
print("Inverse sines with 'float' dtype:") print(inverse_sines_float) print("\nInverse sines with 'complex' dtype:") print(inverse_sines_complex)

输出

Inverse sines with 'float' dtype:
[-0.52359878 -0.20135792  0.          0.20135792  0.52359878]

Inverse sines with 'complex' dtype:
[-0.52359878+0.j -0.20135792+0.j  0.        +0.j  0.20135792+0.j
  0.52359878+0.j]

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

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

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

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

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