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

ParameterTypeDescription
xarray_likeInput array of angles in radians. Each element will have its cosine computed.
outndarray, None, or tuple of ndarray and None, optionalOptional output array where the result is stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying which elements to compute. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing the cosine function.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines 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

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]

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]

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]

The cosine values are computed only for elements where mask=True. The other values remain unchanged.