NumPy arccos() Function:
Numpy arccos: The arc cosine i.e, inverse cosine of the array elements or a given value is calculated using this arccos() function of the NumPy module. The value returned will be in the range of 0 to 𝜋.
Syntax:
numpy.arccos(x, out=None)
Parameters
x: This is required. It is an array (array-like) having elements in the range [-1, 1] for which the arc 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 arc cosine value of each element of x is returned.
NumPy arccos() Function in Python
Example1
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.
- Store it in another variable.
- Pass the above cosine values array to the arccos() function of the numpy module to get the arc cosine(inverse cosine) values of the given array.
- Store it in another variable.
- Print the given array’s angles cosine values.
- Print the given array’s angles inverse cosine values in radians.
- Print the given array’s angles inverse cosine values in degrees by passing the above inverse cosine values to the degrees() function.
- 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 # Store it in another variable. cosine_arry = np.cos(gvn_arry) # Pass the above cosine values array to the arccos() function of the # numpy module to get the arc cosine values of the given array inversecos_arry = np.arccos(cosine_arry) # Print the given array's angles cosine values print("The given array's angles cosine values:") print(cosine_arry) print() # Print the given array's angles inverse cosine values in radians print("The given array's angles inverse cosine values in radians:") print(inversecos_arry) print() # Print the given array's angles inverse cosine values in degrees by # passing the above inverse cosine values to the degrees() function. print("The given array's angles inverse cosine values in degrees:") print(np.degrees(inversecos_arry))
Output:
The given array's angles cosine values: [ 0.8660254 0.70710678 -0.5 -1. ] The given array's angles inverse cosine values in radians: [0.52359878 0.78539816 2.0943951 3.14159265] The given array's angles inverse cosine values in degrees: [ 30. 45. 120. 180.]
Example2
Approach:
- Import numpy module using the import keyword.
- Give the array as static input and store it in a variable.
- Print the above-given array.
- Pass the above given array as an argument to the arccos() function of the numpy module to get the arc cosine(inverse cosine) values of given array elements.
- Store it in another variable.
- Print the arc cosine(inverse cosine) values of given array elements.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Give the array as static input and store it in a variable gvn_arry = [1, 0.5, -0.2] # Print the above given array. print("The above given array is:") print(gvn_arry) # Pass the above given array as an argument to the arccos() function of the # numpy module to get the arc cosine(inverse cosine) values of # given array elements # Store it in another variable. invcos_arry = np.arccos(gvn_arry) # Print the arc cosine(inverse cosine) values of given array elements print("The inverse cosine values of given array elements:") print(invcos_arry)
Output:
The above given array is: [1, 0.5, -0.2] The inverse cosine values of given array elements: [0. 1.04719755 1.77215425]