×

NumPy Trigonometric and Hyperbolic functions

Trigonometric Functions:

The trigonometric functions are functions based on the angle of a right-angled triangle to ratios of two side lengths. NumPy package provides several trigonometric functions.

  • sin()
  • cos()
  • tan()
  • arcsin()
  • arccos()
  • arctan()
  • hypot()
  • degrees()
  • radians()

For all functions that we are going to discuss in this article, we will be using the below arrays.

a = np.arange(10, 60, 10)
b = np.array([-1, 0, 1])

sin():

The NumPy sin() function calculates the trigonometric sine value of the input array, element-wise. It returns an array having sine values of elements of the input array.

Syntax:

sin(x)

The x parameter is the input array and its elements are treated as angles(in radians).

import numpy as np
a = np.arange(10, 60, 10)
print(np.sin(a))

Output:
[-0.54402111  0.91294525 -0.98803162  0.74511316 -0.26237485]

cos():

The cos() function is used to calculate the trigonometric cosine value of the input array, element-wise.

Syntax:

cos(x)
print(np.cos(a))

#Output:
[-0.83907153  0.40808206  0.15425145 -0.66693806  0.96496603]

tan():

The tan() function of NumPy calculates the trigonometric tangent value of the input array, element-wise. It is equivalent to values of sin() function divided by values of cos() function.

Syntax:

tan(x)
print(np.tan(a))

#Output:
[ 0.64836083  2.23716094 -6.4053312  -1.11721493 -0.27190061]

arcsin():

The arcsin() function calculates the inverse trigonometric sine value of the input array, element-wise.  

Syntax:

arcsin(x)

The x parameter is the input array where array elements are treated as y-coordinates on the unit circle.

b = np.array([-1, 0, 1])
print(np.arcsin(b))

#Output:
[-1.57079633  0.          1.57079633]

arccos():

The arccos() function calculates the inverse trigonometric cosine value of the input array, element-wise.

Syntax:

arccos(x)

The x parameter is the input array where array elements are treated as x-coordinates on the unit circle(for real numbers, the domain is [-1, 1]).

print(np.arccos(b))

#Output:
 [3.14159265 1.57079633 0.        ]

arctan():

The arctan() function calculates the inverse trigonometric tangent value of the input array, element-wise.

Syntax:

arctan(x)

The x parameter is the input array.

print(np.arctan(b))

#Output:
[-0.78539816  0.          0.78539816] 

hypot():

The NumPy hypot() function is used to calculate the hypotenuse of any right-angle triangle for given sides of the triangle, element-wise.

Syntax:

hypot(x1, x2) 

The x1 and x2 parameters are input arrays or scalar values and the shape of x1 and x2 should be the same(if not the same, they should be broadcastable to a common shape).

x = np.array([3, 5, 6, 8])
y = np.array([4, 12, 8, 15])
print(np.hypot(x, y))

#Output:
[ 5. 13. 10. 17.]

degrees():

The NumPy degrees() function calculates the degrees of a given input array, element-wise. The degrees() function can be used to convert radian values into degrees.

Syntax:

degrees(x)

The x parameter is the input array where array elements are angles (in radians).

arr = np.arange(5)
print(np.degrees(arr))

#Output:
[  0.          57.29577951 114.59155903 171.88733854 229.18311805]

radians():

The radians() function calculates the radians of a given input array, element-wise. The radians() function can be used to convert degree values into radians.

Syntax:

radians(x)

The x parameter is the input array where array elements are angles (in degrees).

arr = np.array([0, 57.295, 114.592, 171.887, 229.183])
print(np.radians(arr))

#Output:
[0.         0.99998639 2.0000077  2.99999409 3.99999794]

Hyperbolic Functions:

The hyperbolic functions are a group of functions of an angle expressed as a relationship between the distances of a point on a hyperbola(instead of the circle as in trigonometric functions) to the origin and to the coordinate axes. The Numpy package provides the following hyperbolic functions.

  • sinh()
  • cosh()
  • tanh()
  • arcsinh()
  • arccosh()
  • arctanh()

sinh():

The NumPy sinh() function calculates the hyperbolic sine value of the input array, element-wise. It is equivalent to (1/2 *(ex – e-x)).

Syntax:

sinh(x)

The x parameter is the input array.

a = np.arange(5)
print(np.sinh(a))

#Output:
[ 0.          1.17520119  3.62686041 10.01787493 27.2899172 ]

cosh():

The cosh() function calculates the hyperbolic cosine value of the input array, element-wise. It is equivalent to (1/2 *(ex + e-x)).

Syntax:

cosh(x)

The x parameter is the input array.

a = np.arange(5)
print(np.cosh(a))

#Output:
[ 1.          1.54308063  3.76219569 10.067662   27.30823284]

tanh():

The tanh() function calculates the hyperbolic tangent value of the input array, element-wise. It is equivalent to the values of sinh() function divided by the values of cosh() function.

Syntax:

tanh(x)

The x parameter is the input array.

a = np.arange(5)
print(np.tanh(a))

#Output:
[0.         0.76159416 0.96402758 0.99505475 0.9993293 ]

arcsinh():

The arcsinh() function calculates the inverse hyperbolic sine value of the input array, element-wise.

Syntax:

arcsinh(x) 

The x parameter is the input array.

b = np.array([-1, -0.5, 0 , 0.5, 1])
print(np.arcsinh(b))

#Output:
[-0.88137359 -0.48121183  0.          0.48121183  0.88137359]

arccosh():

The arccosh() function calculates the inverse hyperbolic cosine value of the input array, element-wise.  

Syntax:

arccosh(x)

The x parameter is the input array.

b = np.arange(1,6)
print(np.arccosh(b))

#Output:
[0.         1.3169579  1.76274717 2.06343707 2.29243167]

arctanh():

The arctanh() function calculates the inverse hyperbolic tangent value of the input array, element-wise.  

Syntax:

arctanh(x)

The x parameter is the input array.

b = np.array([-0.5, 0 , 0.5])
print(np.arctanh(b))

#Output:
[-0.54930614  0.          0.54930614]