NumPy Arcsin- A Complete Guide

NumPy Arcsin

Hello Readers! Welcome to the Fourth Tutorial of the series NumPy Trigonometric Functions. From this tutorial, we will start with Inverse Trigonometric Functions provided by the NumPy Library.

In this tutorial, we will understand the NumPy arcsin function and practice a lot of examples. We will also plot the arcsin() function using Matplotlib Library. Let’s get started.

Also read: NumPy Cos – A Complete Guide

Arcsin Function – Quick Overview

  • There is an interesting fact to know about these Inverse Trigonometric Functions the output of these functions is an angle and the input is a number.
  • arcsin is the representation of the inverse of the sine function.
  • The arcsin function takes input in the range [-1,1] and produces the output between [-pi/2, pi/2].

What is NumPy arcsin?

NumPy arcsin is one of the trigonometric functions provided by the NumPy Library. Real numbers and Complex Numbers can be passed as input to the NumPy arcsin function.

NumPy arcsin can be accessed as numpy.arcsin(input).

Syntax of NumPy arcsin

Syntax: numpy.arcsin(input) where input can be a single number or a NumPy array of numbers.

NumPy arcsin of Single Number

Let’s try some examples of the NumPy arcsin function to help us understand it better.

import numpy as np

import math

print("Printing the Sine inverse values in radians\n")

print("Sin inverse of 0 is :",np.arcsin(0))

print("Sin inverse of 0.5 is :",np.arcsin(0.5))

print("Sin inverse of 1/sqrt(2) is :",np.arcsin(1/math.sqrt(2)))

print("Sin inverse of 1 is :",np.arcsin(1))

print("Sin inverse of -1 is :",np.arcsin(-1))

print("\n")

print("Sine inverse values in degrees\n")

print("Sin inverse of 1/sqrt(2) is :",np.degrees(np.arcsin(1/math.sqrt(2))))

print("Sin inverse of -1 is :",np.degrees(np.arcsin(-1)))


Output

Printing the Sine inverse values in radians

Sin inverse of 0 is : 0.0
Sin inverse of 0.5 is : 0.5235987755982989
Sin inverse of 1/sqrt(2) is : 0.7853981633974482
Sin inverse of 1 is : 1.5707963267948966
Sin inverse of -1 is : -1.5707963267948966


Sine inverse values in degrees

Sin inverse of 1/sqrt(2) is : 44.99999999999999
Sin inverse of -1 is : -90.0

Let’s take an example of the sine inverse of 1/sqrt(2) which is equal to 45 degrees. Since the sine of 45 degrees is 1/sqrt(2) so the sine inverse of 1/sqrt(2) is 45 degrees. This is an interesting way to understand how the inverse trigonometric functions compute the result.

The above code snippet is pretty much clear where a number in the range [-1,1] is passed as an argument to the arcsin function and the output is an angle in radians.

Note: I have used np.degrees() a function to convert the output angle to degrees.

The output of the arcsin function in all the above cases lies in the range [-pi/2,pi/2].

NumPy arcsin of Complex Number

NumPy arcsin function takes a complex number as an argument.

import numpy as np

print("Sin inverse of 1+5j is: ",np.arcsin(1+5j))

print("Sin inverse of 2+3j is: ",np.arcsin(2+3j))

print("Sin inverse of 0.5+0.5j is: ",np.arcsin(0.5+0.5j))

Output

Sin inverse of 1+5j is:  (0.1937931365549322+2.3309746530493123j)
Sin inverse of 2+3j is:  (0.5706527843210994+1.9833870299165355j)
Sin inverse of 0.5+0.5j is:  (0.45227844715119064+0.5306375309525179j)

The output is also a complex number.

Note: We cannot use the np.degrees() function with numpy.arcsin() when the input to the arcsin function is a complex number.

NumPy arcsin of Invalid Number

If a number that is not in the domain of the arcsin function is passed as an argument to the function, then the output is nan.

import numpy as np

print("Sin inverse of 5 is :",np.arcsin(5))

Output

Sin inverse of 5 is : nan

Note: Every number out of the range [-1,1] is considered an invalid input to the arcsin function.

NumPy arcsin on Multiple Numbers

The NumPy arcsin function can also take a NumPy Array of numbers as an argument.

Combining NumPy Array with Arcsin

import numpy as np

a = np.array((-1 , 0 , 0.5 , 0.3 , 1))

print("Sine Inverse Values in radians :\n",np.arcsin(a))

print("Sine Inverse Values in degrees :\n",np.degrees(np.arcsin(a)))

Output

Sine Inverse Values in radians :
 [-1.57079633  0.          0.52359878  0.30469265  1.57079633]
Sine Inverse Values in degrees :
 [-90.           0.          30.          17.45760312  90.        ]

NumPy Arcsin on an Evenly-Spaced NumPy Array

In this example, we will create a NumPy Array of 20 evenly spaced values using numpy.linspace.

import numpy as np

a = np.linspace(-1 , 1 , 20)

print("Sin Inverse Values in radians: ",np.arcsin(a))

print("Sin Inverse Values in degrees: ",np.degrees(np.arcsin(a)))

Output

Sin Inverse Values in radians:  [-1.57079633 -1.1078416  -0.90995103 -0.75352062 -0.6174371  -0.49346939
 -0.37731003 -0.26629402 -0.15855828 -0.05265591  0.05265591  0.15855828
  0.26629402  0.37731003  0.49346939  0.6174371   0.75352062  0.90995103
  1.1078416   1.57079633]
Sin Inverse Values in degrees:  [-90.         -63.47464798 -52.13635364 -43.17355111 -35.37654015
 -28.27371363 -21.61827242 -15.25752329  -9.08472029  -3.01696131
   3.01696131   9.08472029  15.25752329  21.61827242  28.27371363
  35.37654015  43.17355111  52.13635364  63.47464798  90.        ]

Visualizing the Arcsin Function

import numpy as np

# Importing the Matplotlib Library
import matplotlib.pyplot as plt

# Creating a NumPy Array of 30 evenly-spaced elements
a = np.linspace(-1,1,30)

# Storing the computed arcsin values in a NumPy Array
b = np.arcsin(a)

plt.plot(a, b, color = "blue", marker = "o" , ms = 5,mfc = "r")
plt.title("numpy.arcsin()")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

Output

Arcsin Plot
Arcsin Plot

In the plot, the values on Y-axis(Vertical Axis) are the output of arcsin the function in radians.

plt.plot() the function is used to plot the arcsin Function which takes six arguments.

  • The first argument is the NumPy Array of numbers (created in Line No 7), plotted on the X-axis(Horizontal Axis).
  • The second argument is the output of the arcsin function, plotted on the Y-axis(Vertical Axis).
  • The third argument is the color of the plot.
  • The fourth argument is the marker value which emphasizes each point with a specified marker. There are different types of markers that can be used to denote the points on the curve.
  • The fifth argument is the size of the points on the curve. (ms is marker size)
  • The sixth argument is the color of the marker. (mfc is marker FACE color)

Summary

In this tutorial, we learned about a variety of inputs that numpy.arcsin() function takes. Do practice these codes along with going through the articles. In the next tutorial, we will be covering the NumPy Arccos function in detail.

References

NumPy Documentation – NumPy Arcsin

Matplotlib – Get Started

Matplotlib Documentation