NumPy Arctan – A Complete Guide

NUMPY Arctan

Hello Readers! In this tutorial, we will understand about NumPy arctan function with a lot of examples and we will also plot the graph of the arctan function using Matplotlib Library.

So, let’s get started.

What is the Arctan?

  • arctan is the representation of the inverse of the tangent(tan) function.
  • The arctan function takes all real numbers as input and produces the output in the range (-pi/2, pi/2).
  • An interesting fact to note is that we can extend the arctan function to complex numbers. In such a case, the domain(input) of arctan will be all complex numbers.

What is NumPy Arctan?

NumPy Arctan is one of the Trigonometric Functions provided by the NumPy Library. NumPy Arctan can take Real Numbers and Complex Numbers as input.

We can access the NumPy Arctan function as numpy.arctan.

Syntax of NumPy arctan

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

Let’s write some code.

NumPy arctan of Single Number

import numpy as np

import math

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

print("Tan inverse of 0 is :",np.arctan(0))

print("Tan inverse of 0.5 is :",np.arctan(0.5))

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

print("Tan inverse of 1 is :",np.arctan(1))

print("Tan inverse of -1 is :",np.arctan(-1))

# Tan inverse of a very large number
print("Tan inverse of 10000000 is :",np.arctan(10000000))

print("\n")

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

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

print("Tan inverse of -1 is :",np.degrees(np.arctan(-1)))

print("Tan inverse of 10000000 is :",np.degrees(np.arctan(10000000)))

Output

Printing the Tan inverse values in radians

Tan inverse of 0 is : 0.0
Tan inverse of 0.5 is : 0.4636476090008061
Tan inverse of 1/sqrt(2) is : 0.6154797086703873
Tan inverse of 1 is : 0.7853981633974483
Tan inverse of -1 is : -0.7853981633974483
Tan inverse of 10000000 is : 1.5707962267948967


Tan inverse values in degrees

Tan inverse of 1/sqrt(2) is : 35.264389682754654
Tan inverse of -1 is : -45.0
Tan inverse of 10000000 is : 89.99999427042206

In the last example, we have calculated the arctan of a very large number i.e. 10,000,000 and the output is pi/2 radians or 90 degrees. This is because the input of the arctan is a very large quantity for which the output tends to be pi/2 radians or 90 degrees.

NumPy arctan of Complex Number

import numpy as np

print("Tan inverse of 1+5j is: ",np.arctan(1+5j))

print("Tan inverse of 2+3j is: ",np.arctan(2+3j))

print("Tan inverse of 0.5+0.5j is: ",np.arctan(0.5+0.5j))

Output

Tan inverse of 1+5j is:  (1.530881333938778+0.1944261421470021j)
Tan inverse of 2+3j is:  (1.4099210495965755+0.22907268296853878j)
Tan inverse of 0.5+0.5j is:  (0.5535743588970452+0.40235947810852507j)

NumPy Arctan on Multiple Numbers

Now, let’s see how can we compute the arctan of an array of Numbers.

Combining NumPy Array with Arctan

import numpy as np

import math

a = np.array((-1 , 0 , 1/math.sqrt(3) , math.sqrt(3) , 1))

print("Tan Inverse Values in radians :\n",np.arctan(a))

print("Tan Inverse Values in degrees :\n",np.degrees(np.arctan(a)))

Output

Tan Inverse Values in radians :
 [-0.78539816  0.          0.52359878  1.04719755  0.78539816]
Tan Inverse Values in degrees :
 [-45.   0.  30.  60.  45.]

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(-2 , 2 , 20)

print("Tan Inverse Values in radians: ",np.arctan(a))

print("Tan Inverse Values in degrees: ",np.degrees(np.arctan(a)))

Output

Tan Inverse Values in radians:  [-1.10714872 -1.06120406 -1.00622693 -0.93971694 -0.85843873 -0.75837771
 -0.63502674 -0.48447793 -0.30587887 -0.10487694  0.10487694  0.30587887
  0.48447793  0.63502674  0.75837771  0.85843873  0.93971694  1.00622693
  1.06120406  1.10714872]
Tan Inverse Values in degrees:  [-63.43494882 -60.80251395 -57.6525565  -53.84181456 -49.18491613
 -43.4518423  -36.38435182 -27.7585406  -17.52556837  -6.00900596
   6.00900596  17.52556837  27.7585406   36.38435182  43.4518423
  49.18491613  53.84181456  57.6525565   60.80251395  63.43494882]

Visualizing the Arctan 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(-10,10,30)

# Storing the computed arctan values in a NumPy Array
b = np.arctan(a)
plt.plot(a, b, color = "green", marker = "o")
plt.title("numpy.arctan()")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

Output

Arctan Plot
Arctan Plot

Note: If you carefully observe the curve you will notice that the maximum value of the arctan function is less than pi/2 and the minimum value is greater than -pi/2.

plt.plot() the function is used to plot the arctan Function which takes three arguments.

  • The first argument is the NumPy Array of numbers (created in Line No 3) which is also the input to the arctan function plotted on the X-axis(Horizontal Axis).
  • The second argument is the output of the arctan function in radians plotted on the Y-axis(Vertical Axis).
  • The third argument is the color of the plot.
  • The fourth argument is marker value which emphasizes the points plotted on the curve.

You have successfully plotted and understood the nature of the arctan function.

Summary

This completes our NumPy Trigonometric Functions tutorial series. In this tutorial, we learned about the arctan function with a lot of example code snippets, practice these codes along with going through the tutorial. By now, you must have become familiar with the NumPy Trigonometric Functions, these are really easy to use 🙂

In the next tutorial, I will be covering one special trigonometric function arctan2 in detail and with a lot of different examples. Till then keep coding.

References

NumPy Documentation – NumPy Arctan

Matplotlib – Get Started