NumPy Arccos- A Complete Guide

NumpyArccos

Hello Readers! In this tutorial, we will understand the NumPy arccos function along with a lot of examples. We will also plot the curve of the arccos function. So, let’s get started.

Also read: NumPy Arctan – A Complete Guide

Arccos Function – Quick Overview

  • arccos is the representation of the inverse of the cosine function.
  • The arccos function takes input in the range [-1,1] and produces the output in the range [0, pi].

What is NumPy Arccos?

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

We can access the NumPy Arccos function as numpy.arccos.

Syntax of NumPy arccos

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

Let’s write some code.

NumPy arccos of Single Number

import numpy as np

import math

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

print("Cos inverse of 0 is :",np.arccos(0))

print("Cos inverse of 0.5 is :",np.arccos(0.5))

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

print("Cos inverse of 1 is :",np.arccos(1))

print("Cos inverse of -1 is :",np.arccos(-1))

print("\n")

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

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

print("Cos inverse of -1 is :",np.degrees(np.arccos(-1)))

Output

Printing the Cosine inverse values in radians

Cos inverse of 0 is : 1.5707963267948966
Cos inverse of 0.5 is : 1.0471975511965979
Cos inverse of 1/sqrt(2) is : 0.7853981633974484
Cos inverse of 1 is : 0.0
Cos inverse of -1 is : 3.141592653589793


Cosine inverse values in degrees

Cos inverse of 1/sqrt(2) is : 45.00000000000001
Cos inverse of -1 is : 180.0

Let’s take an example of the cos inverse of 0 which is equal to 90 degrees. Since the cosine of 90 degrees is 0 so the cosine inverse of 0 is 90 degrees. This is an interesting way to understand how the inverse trigonometric functions compute the result.

Task: Try using the NumPy Arccos function with other inputs as well and observe the outputs.

NumPy arccos of Complex Number

import numpy as np

print("Cosine inverse of 1+5j is: ",np.arccos(1+5j))

print("Cosine inverse of 2+3j is: ",np.arccos(2+3j))

print("Cosine inverse of 0.5+0.5j is: ",np.arccos(0.5+0.5j))

Output

Cosine inverse of 1+5j is:  (1.3770031902399644-2.3309746530493123j)
Cosine inverse of 2+3j is:  (1.0001435424737972-1.9833870299165355j)
Cosine inverse of 0.5+0.5j is:  (1.118517879643706-0.5306375309525179j)

NumPy arccos of Invalid Number

If an invalid input is passed as an argument to the arccos function then the output will be nan.

import numpy as np

print("Cosine inverse of -3 is :",np.arccos(5))

Output

Cosine inverse of -3 is : nan

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

NumPy Arccos on Multiple Numbers

The arccos function also takes NumPy Array of numbers as an argument.

Combining NumPy Array with Arccos

import numpy as np

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

print("Cosine Inverse Values in radians :\n",np.arccos(a))

print("Cosine Inverse Values in degrees :\n",np.degrees(np.arccos(a)))

Output

Cosine Inverse Values in radians :
 [3.14159265 1.57079633 1.04719755 1.26610367 0.        ]
Cosine Inverse Values in degrees :
 [180.          90.          60.          72.54239688   0.        ]

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("Cosine Inverse Values in radians: ",np.arccos(a))

print("Cosine Inverse Values in degrees: ",np.degrees(np.arccos(a)))

Output

Cosine Inverse Values in radians:  [3.14159265 2.67863793 2.48074736 2.32431694 2.18823343 2.06426572
 1.94810636 1.83709034 1.72935461 1.62345224 1.51814042 1.41223805
 1.30450231 1.19348629 1.07732693 0.95335922 0.81727571 0.6608453
 0.46295473 0.        ]
Cosine Inverse Values in degrees:  [180.         153.47464798 142.13635364 133.17355111 125.37654015
 118.27371363 111.61827242 105.25752329  99.08472029  93.01696131
  86.98303869  80.91527971  74.74247671  68.38172758  61.72628637
  54.62345985  46.82644889  37.86364636  26.52535202   0.        ]

Visualizing the Arccos 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 arccos values in a NumPy Array
b = np.arccos(a)
plt.plot(a, b, color = "green", marker = "o")
plt.title("numpy.arccos()")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()


Output

Arccos Plot
Arccos Plot

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

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

  • The first argument is the NumPy Array of numbers (created in Line No 3), plotted on the X-axis(Horizontal Axis).
  • The second argument is the output of the arccos 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.

There you go, you have plotted the arccos curve using Matplotlib Library.

Summary

So, we learned about the arccos function of the NumPy Library. In the next tutorial, we will be covering the arctan function. Till then keep learning and keep coding :).

References

NumPy Documentation – NumPy Arccos

Matplotlib – Get Started