Numpy cos – Python NumPy cos() Function

Numpy cos: NumPy has trigonometric functions such as sin, cos, tan, and others. The NumPy trigonometric functions aid in the efficient solution of mathematical trigonometric calculations.

NumPy cos() Function:

Numpy cosine degrees: To calculate the trigonometric cosine of a given angle in radians, use the cos() function of the NumPy module.

Syntax:

numpy.cos(x, out=None)

Parameters

x: This is required. It is an array (array-like) having elements as angles in radians of which the trigonometric cosine is calculated.

out: This is optional. It is the location where the result will be saved. It must have a shape that the inputs broadcast to if it is provided. If None or not given, a newly allocated array is returned.

Return Value: 

The cosine value of each element of x is returned.

NumPy cos() Function in Python

Example1(In Radians)

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Convert the above-given array angles into radians using the pi/180 of numpy module.
  • Store it in the same variable.
  • Pass the above radian angles array to the cos() function of the numpy module to get the cosine values of the given array angles.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([30, 45, 120, 180])
# Convert the above given array angles into radians using the pi/180 of 
# numpy module.
# Store it in the same variable.
gvn_arry = gvn_arry*np.pi/180
# Pass the above radian angles array to the cos() function of the 
# numpy module to get the cosine values of the given array angles.
print("The given array's cosine values = \n", np.cos(gvn_arry))

Output:

The given array's cosine values = 
[ 0.8660254 0.70710678 -0.5 -1. ]

Example2(In degrees)

Approach:

  • Import numpy module using the import keyword.
  • Pass some random angle value to the cos() function of the numpy module to get the cosine value of the given angle in degrees.
  • Store it in a variable.
  • Print the cosine value of the given angle in degrees.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random angle value to the cos() function of the
# numpy module to get the cosine value of the given angle in degrees
# Store it in a variable.
rslt = np.cos(90) 
# Print the cosine value of the given angle in degrees
print("The given angle's cosine values(degrees) = ", rslt)

Output:

The given angle's cosine values(degrees) = -0.4480736161291701