NumPy Trigonometric Functions

🌐 NumPy Trigonometric Functions

NumPy provides vectorized trigonometric functions that operate element-wise on arrays. These functions are faster than Python’s built-in math module and support radians by default.


βœ… 1. Basic Trigonometric Functions

Function Description Example
np.sin(x) Sine np.sin(np.pi/2) = 1
np.cos(x) Cosine np.cos(np.pi) = -1
np.tan(x) Tangent np.tan(np.pi/4) = 1
np.arcsin(x) Inverse sine (radians) np.arcsin(1) = Ο€/2
np.arccos(x) Inverse cosine (radians) np.arccos(0) = Ο€/2
np.arctan(x) Inverse tangent (radians) np.arctan(1) = Ο€/4

βœ… 2. Example: Sine, Cosine, Tangent

import numpy as np

angles = np.array([0, np.pi/6, np.pi/4, np.pi/2])

print("Sine:", np.sin(angles))
# [0. 0.5 0.70710678 1. ]

print("Cosine:", np.cos(angles))
# [1. 0.8660254 0.70710678 0. ]

print("Tangent:", np.tan(angles))
# [0. 0.57735027 1. inf]

  • Note: Tangent is undefined at Ο€/2 β†’ returns inf


βœ… 3. Inverse Trigonometric Functions

x = np.array([0, 0.5, 1])

print("Arcsin:", np.arcsin(x)) # [0. Ο€/6 Ο€/2]
print("Arccos:", np.arccos(x)) # [Ο€/2 Ο€/3 0]
print("Arctan:", np.arctan(x)) # [0 0.4636476 0.7853982]

  • Returns radians by default


βœ… 4. Degree-Radian Conversion

# Degrees to radians
degrees = np.array([0, 30, 45, 90])
radians = np.deg2rad(degrees)
print(radians) # [0. 0.52359878 0.78539816 1.57079633]

# Radians to degrees
deg = np.rad2deg(radians)
print(deg) # [ 0. 30. 45. 90.]

  • np.deg2rad() β†’ convert degrees to radians

  • np.rad2deg() β†’ convert radians to degrees


βœ… 5. Hyperbolic Trigonometric Functions

Function Description
np.sinh(x) Hyperbolic sine
np.cosh(x) Hyperbolic cosine
np.tanh(x) Hyperbolic tangent
np.arcsinh(x) Inverse hyperbolic sine
np.arccosh(x) Inverse hyperbolic cosine
np.arctanh(x) Inverse hyperbolic tangent
x = np.array([0, 1, 2])
print("sinh:", np.sinh(x)) # [0. 1.17520119 3.62686041]
print("cosh:", np.cosh(x)) # [1. 1.54308063 3.76219569]
print("tanh:", np.tanh(x)) # [0. 0.76159416 0.96402758]

βœ… 6. Notes & Tips

  • Trigonometric functions use radians, not degrees

  • Use vectorized operations on arrays β†’ faster than Python loops

  • Be careful with domain limits for inverse trig functions


🎯 Practice Exercises

  1. Create an array [0, 30, 45, 60, 90] in degrees and compute sin, cos, tan.

  2. Compute inverse sine and inverse cosine of [0, 0.5, 1].

  3. Convert [0, Ο€/6, Ο€/4, Ο€/2] from radians to degrees.

  4. Compute hyperbolic sine, cosine, tangent for [0,1,2].

CodeCapsule

Sanjit Sinha β€” Web Developer | PHP β€’ Laravel β€’ CodeIgniter β€’ MySQL β€’ Bootstrap Founder, CodeCapsule β€” Student projects & practical coding guides. Email: info@codecapsule.in β€’ Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *