w3resource

NumPy: Calculate inverse sine, cosine, and tangent for all elements in a given array

NumPy Mathematics: Exercise-22 with Solution

Write a NumPy program to calculate inverse sine, inverse cosine, and inverse tangent for all elements in a given array.

Sample Solution:

Python Code:

# Importing the NumPy library
import numpy as np

# Creating an array of values (-1, 0, 1)
x = np.array([-1., 0, 1.])

# Computing the inverse sine for each element in the array
print("Inverse sine:", np.arcsin(x))

# Computing the inverse cosine for each element in the array
print("Inverse cosine:", np.arccos(x))

# Computing the inverse tangent for each element in the array
print("Inverse tangent:", np.arctan(x)) 

Sample Output:

Inverse sine: [-1.57079633  0.          1.57079633]
Inverse cosine: [3.14159265 1.57079633 0.        ]
Inverse tangent: [-0.78539816  0.          0.78539816]

Explanation:

In the above code –

x = np.array([-1., 0, 1.]) – This code defines a one-dimensional numpy array x with values [-1., 0, 1.].

np.arcsin(x) returns the arcsine (in radians) of each element in the input array x. The arcsine is the inverse function of sine. The output array contains [-1.57079633 0. 1.57079633]

np.arccos(x) returns the arccosine (in radians) of each element in the input array x. The arccosine is the inverse function of cosine. The output array contains [3.14159265 1.57079633 0.] ]

np.arctan(x) returns the arctangent (in radians) of each element in the input array x. The arctangent is the inverse function of tangent. The output array contains [-0.78539816 0. 0.78539816].

Python-Numpy Code Editor:

Previous: Write a NumPy program to create a random array with 1000 elements and compute the average, variance, standard deviation of the array elements.

Next: Write a NumPy program to convert angles from radians to degrees for all elements in a given array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/numpy/python-numpy-math-exercise-22.php