numpy.cos#
- numpy.cos(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'cos'>#
余弦,逐元素。
- 参数:
- xarray_like
以弧度为单位的输入数组。
- outndarray, None, or tuple of ndarray and None, optional
结果存储的位置。如果提供了,它必须具有输入广播到的形状。如果未提供或为 None,则返回一个新分配的数组。元组(仅可能作为关键字参数)的长度必须等于输出的数量。
- wherearray_like, optional
此条件将广播到输入。在条件为 True 的位置,out 数组将设置为 ufunc 结果。在其他地方,out 数组将保留其原始值。请注意,如果通过默认的
out=None创建了一个未初始化的 out 数组,那么其中条件为 False 的位置将保持未初始化状态。- **kwargs
有关其他关键字参数,请参阅 ufunc 文档。
- 返回:
- yndarray
相应的余弦值。如果 x 是标量,则这是标量。
备注
如果提供了 out,则函数将其写入其中,并返回 out 的引用。(参见 示例)
参考
M. Abramowitz and I. A. Stegun, Handbook of Mathematical Functions. New York, NY: Dover, 1972。
示例
>>> import numpy as np >>> np.cos(np.array([0, np.pi/2, np.pi])) array([ 1.00000000e+00, 6.12303177e-17, -1.00000000e+00]) >>> >>> # Example of providing the optional output parameter >>> out1 = np.array([0], dtype='d') >>> out2 = np.cos([0.1], out1) >>> out2 is out1 True >>> >>> # Example of ValueError due to provision of shape mis-matched `out` >>> np.cos(np.zeros((3,3)),np.zeros((2,2))) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: operands could not be broadcast together with shapes (3,3) (2,2)