w3resource

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


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: