NumPy cos()
The numpy.cos()
function computes the trigonometric cosine of each element in an input array.
The input values should be in radians.
Syntax
</>
Copy
numpy.cos(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x | array_like | Input array of angles in radians. Each element will have its cosine computed. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to compute. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when computing the cosine function. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns an array with the cosine values of the input array elements. If the input is a scalar, a scalar is returned.
Examples
1. Computing Cosine of a Single Value
Here, we compute the cosine of a single angle in radians.
</>
Copy
import numpy as np
# Define an angle in radians
angle = np.pi / 3 # 60 degrees in radians
# Compute the cosine of the angle
result = np.cos(angle)
# Print the result
print("Cosine of 60 degrees (π/3 radians):", result)
Output:
Cosine of 60 degrees (π/3 radians): 0.5000000000000001
![](https://www.tutorialkart.com/wp-content/uploads/2025/02/numpy-2025-02-02-at-8.48.56 AM.png)
2. Computing Cosine for an Array of Angles
We compute the cosine values for multiple angles provided in an array.
</>
Copy
import numpy as np
# Define an array of angles in radians
angles = np.array([0, np.pi/6, np.pi/4, np.pi/3, np.pi/2]) # [0°, 30°, 45°, 60°, 90°]
# Compute the cosine of each angle
cosine_values = np.cos(angles)
# Print the results
print("Angles (in radians):", angles)
print("Cosine values:", cosine_values)
Output:
Angles (in radians): [0. 0.52359878 0.78539816 1.04719755 1.57079633]
Cosine values: [1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
6.12323400e-17]
![](https://www.tutorialkart.com/wp-content/uploads/2025/02/numpy-2025-02-02-at-8.49.13 AM.png)
3. Using the out
Parameter
Using an output array to store results instead of creating a new array.
</>
Copy
import numpy as np
# Define an array of angles
angles = np.array([0, np.pi/4, np.pi/2, np.pi])
# Create an output array with the same shape
output_array = np.empty_like(angles)
# Compute cosine and store the result in output_array
np.cos(angles, out=output_array)
# Print the results
print("Computed cosine values:", output_array)
Output:
Computed cosine values: [ 1.00000000e+00 7.07106781e-01 6.12323400e-17 -1.00000000e+00]
![](https://www.tutorialkart.com/wp-content/uploads/2025/02/numpy-2025-02-02-at-8.49.25 AM.png)
4. Using the where
Parameter
Using a condition to compute cosine only for selected elements.
</>
Copy
import numpy as np
# Define an array of angles
angles = np.array([0, np.pi/4, np.pi/2, np.pi])
# Define a mask (compute cosine only where mask is True)
mask = np.array([True, False, True, False])
# Compute cosine values where mask is True
result = np.cos(angles, where=mask)
# Print the results
print("Computed cosine values with mask:", result)
Output:
Computed cosine values with mask: [1.000000e+00 0.000000e+00 6.123234e-17 0.000000e+00]
![](https://www.tutorialkart.com/wp-content/uploads/2025/02/numpy-2025-02-02-at-8.49.38 AM.png)
The cosine values are computed only for elements where mask=True
. The other values remain unchanged.