NumPy cos()

The cos() function computes the cosine of the elements in an array.

The cosine is the trigonometric function that calculates the ratio of the length of the side adjacent to an angle to the length of the hypotenuse in a right-angled triangle.

Example

import numpy as np

# array of angles in radians
angles = np.array([0, 1, 5])

# compute the cosine of the angles cosineValues = np.cos(angles)
print(cosineValues) # Output : [1. 0.54030231 0.28366219]

cos() Syntax

The syntax of cos() is:

numpy.cos(array, out = None, dtype = None)

cos() Arguments

The cos() function takes following arguments:

  • array - the input arrays
  • out (optional) - the output array where the result will be stored
  • dtype (optional) - data type of the output array

cos() Return Value

The cos() function returns an array containing the element-wise cosine values of the input array.


Example 1: Compute cosine of the Angles

import numpy as np

# array of angles in radians
angles = np.array([0, np.pi/4, np.pi/2, np.pi])
print("Angles:", angles)

# compute the cosine of the angles cosineValues = np.cos(angles)
print("cosine values:", cosineValues)

Output

Angles: [0.         0.78539816 1.57079633 3.14159265]
cosine values: [ 1.00000000e+00  7.07106781e-01  6.12323400e-17 -1.00000000e+00]

In this example, we have the array angles containing four angles in radians: 0, π/4, π/2, and π.

The np.cos() function is used to calculate the cosine values for each element in the angles array.


Example 2: Use out to Store the Result in Desired Location

import numpy as np

# create an array of angles in radians
angles = np.array([0, np.pi/6, np.pi/4, np.pi/3, np.pi/2])

# create an empty array to store the result
result = np.empty_like(angles)

# compute the cosine of angles and store the result in the 'result' array np.cos(angles, out=result)
print("Result:", result)

Output

Result: [1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
 6.12323400e-17]

Here, we have used cos() with the out parameter to compute the cosine of the angles array and store the result directly in the result array.

The resulting result array contains the computed cosine values.


Example 3: Use of dtype Argument in cos()

import numpy as np

# create an array of angles in radians
angles = np.array([0, np.pi/4, np.pi/2, np.pi])

# calculate the cosine of each angle with a specific dtype floatCosines = np.cos(angles, dtype=float) complexCosines = np.cos(angles, dtype=complex)
print("Cosines with 'float' dtype:") print(floatCosines) print("\nCosines with 'complex' dtype:") print(complexCosines)

Output

Cosines with 'float' dtype:
[ 1.00000000e+00  7.07106781e-01  6.12323400e-17 -1.00000000e+00]

Cosines with 'complex' dtype:
[ 1.00000000e+00-0.j  7.07106781e-01-0.j  6.12323400e-17-0.j
 -1.00000000e+00-0.j]

Here, by specifying the desired dtype, we can control the data type of the output array according to our specific requirements.

Note: To learn more about the dtype argument, please visit NumPy Data Types.