w3resource

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


22. Inverse Trigonometric Functions

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].


For more Practice: Solve these Related Problems:

  • Implement a function that calculates arcsin, arccos, and arctan for an array of valid inputs using np.arcsin, np.arccos, and np.arctan.
  • Test the function on a range of values and verify that the output angles are within the expected domains.
  • Create a solution that returns a tuple of arrays for the three inverse trigonometric functions.
  • Compare the computed inverse trigonometric values with manually computed values for a sample subset.

Go to:


PREV : Trigonometric Functions in Degrees
NEXT : Convert Radians to Degrees

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.