.cos()

Anonymous contributor's avatar
Anonymous contributor
Published Jun 20, 2024Updated Sep 7, 2024
Contribute to Docs

In NumPy, the .cos() function computes the cosine of each element in an array. This trigonometric function is essential for various mathematical computations, especially in physics, engineering, computer graphics, signal processing, and more.

Syntax

numpy.cos(array, out = None, dtype = None)
  • array: The Python library that provides support for large, multi-dimensional arrays and matrices, along with a large collection of mathematical functions to operate on these arrays.
  • out: Optional. This is an alternative output array in which to place the result. It must have the same shape as the expected output. If not provided, a new array is returned.
  • dtype: Optional. The desired data type for the output array. If not provided, the data type of the input array is used.

Example

The below example shows the .cos() function in use:

import numpy as np
# A single angle in radians
angle = np.pi / 4
# Calculate the cosine of the angle
cos_value = np.cos(angle)
print("Angle (radians):", angle)
print("Cosine value:", cos_value)

The above code generates the output as below:

Angle (radians): 0.7853981633974483
Cosine value: 0.7071067811865476

Codebyte Example

Run the following code to understand how the .cos() function works:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python:NumPy on Codecademy