Table of Contents
In Python, one can perform many kinds of tasks including mathematical operations like add, subtract, divide, dealing with matrices, etc. Besides all these operations, one can also perform complex mathematical operations like trigonometric operations using many libraries and modules provided by Python.
This tutorial will demonstrate how to calculate inverse tangent in Python.
Use the arctan()
Function from the NumPy
Library to calculate the inverse tangent in Python.
The NumPy
library is a widely used library in Python. This library helps in dealing with arrays, matrices, linear algebra, and Fourier transform. NumPy
stands for Numerical Python
.
The NumPy
has a function known as the arctan()
function that is a mathematical function used to calculate the inverse tangent of elements in an array.
Some Parameters of the numpy.arctan()
Function
x (array)
– This parameter defines the input array of which the inverse sine values are to be found.out (array, None, or tuple)
– This parameter defines the location in which the result is stored. If this parameter is not defined then the result is returned in a new array.
There are many other parameters like where (array)
, casting
, order
, etc. Also, note that only the x (array)
parameter is necessary to define. All the other parameters are optional.
Example:
1 2 3 4 5 6 7 |
import numpy as np array = [-1, 0, 0.5, 1] print ("Array: ", array) arctan_Values = np.arctan(array) print ("Inverse Tangent values of elements in the given Array : ",arctan_Values) |
Output:
This function calculates the inverse tangent values of every element present in the input array.
Graphical Representation of the numpy.arctan()
function.
In this program, the Matplotlib
library is used to plot the graph. This library is a very good library when it comes to data visualisation in python.
In the program below, the linspace()
function of the NumPy
is used. The linspace()
function helps in creating a random numerical sequence that are evenly spaced.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import numpy as np import matplotlib.pyplot as plt input_array = np.linspace(-np.pi, np.pi, 15) output_array = np.arctan(input_array) print("input_array :", input_array) print("output_array with arctan :", output_array) plt.plot(input_array, output_array,color = 'red', marker = "o") plt.title("numpy.arctan()") plt.xlabel("X") plt.ylabel("Y") plt.show() |
Output:
-0.44879895 0. 0.44879895 0.8975979 1.34639685 1.7951958
2.24399475 2.6927937 3.14159265] output_array with arctan : [-1.26262726 -1.21521935 -1.15157923 -1.06256244 -0.93196875 -0.73148639
-0.42185468 0. 0.42185468 0.73148639 0.93196875 1.06256244
1.15157923 1.21521935 1.26262726]
Use the atan()
Function from the SymPy
Library to calculate the inverse tangent in Python.
Python’s SymPy
library is used in symbolic mathematics. It is a Computer Algebra System (CAS) that provides short and precise codes that can be used easily by the user.
The atan()
function of the SymPy
library is used for calculating the inverse tangent value of a given input value in Python.
1 2 3 4 5 6 7 |
from sympy import * inv_tan1 = atan(0) inv_tan2 = atan(0.5) print(inv_tan1) print(inv_tan2) |
Output:
0.463647609000806
Use the atan()
Function from the Math
Library to calculate the inverse tangent in Python.
Python’s Math
library helps in performing complex mathematical problems by providing different mathematical constants and functions in Python.
The atan()
function of the Math
library is also used for calculating the inverse tangent value (between -PI/2 and PI/2) of a given input value in Python.
Example:
1 2 3 4 5 |
import math print (math.atan(0)) print (math.atan(0.5)) |
Output:
0.46364760900080615
That’s all about how to get inverse tangent in Python.