0
0
NumPydata~15 mins

Trigonometric functions (sin, cos, tan) in NumPy - Deep Dive

Choose your learning style9 modes available
Overview - Trigonometric functions (sin, cos, tan)
What is it?
Trigonometric functions like sine (sin), cosine (cos), and tangent (tan) relate angles of a right triangle to ratios of its sides. They help us understand patterns that repeat in cycles, like waves or circles. These functions take an angle as input and give a number that describes a position on a circle or a ratio of sides. They are fundamental in many fields including physics, engineering, and data science.
Why it matters
Without trigonometric functions, we couldn't model or analyze anything that involves cycles or rotations, such as sound waves, light waves, or seasonal trends. They let us convert angles into numbers we can calculate with, making complex real-world patterns understandable and predictable. This is crucial for tasks like signal processing, computer graphics, and even machine learning.
Where it fits
Before learning trigonometric functions, you should understand basic geometry and angles measured in degrees or radians. After mastering these functions, you can explore Fourier transforms, wave analysis, and advanced geometry in data science and engineering.
Mental Model
Core Idea
Trigonometric functions convert angles into ratios that describe positions on a circle or relationships between triangle sides.
Think of it like...
Imagine walking around a circular track: sine tells you how far north or south you are, cosine tells you how far east or west, and tangent tells you how steeply you are climbing or descending.
       (0,1)
         │
         │  sin(angle)
         │
(-1,0)──┼──(1,0)  cos(angle)
         │
         │
         │

Angle starts at (1,0) and moves counterclockwise around the circle.

sin(angle) = vertical height
cos(angle) = horizontal distance

tan(angle) = sin(angle)/cos(angle)
Build-Up - 6 Steps
1
FoundationUnderstanding Angles and Radians
🤔
Concept: Angles can be measured in degrees or radians, which are units to describe rotation.
An angle measures how much you turn from a starting line. Degrees split a full turn into 360 parts. Radians measure angles based on the radius of a circle: one full turn is 2π radians. In numpy, trigonometric functions use radians, so converting degrees to radians is important.
Result
You can convert degrees to radians using numpy's np.radians function, enabling correct input for trig functions.
Knowing radians is essential because numpy's trig functions expect angles in radians, not degrees.
2
FoundationBasic Definitions of sin, cos, tan
🤔
Concept: Sine, cosine, and tangent relate an angle to ratios of sides in a right triangle or positions on a unit circle.
In a right triangle, sine is opposite side over hypotenuse, cosine is adjacent side over hypotenuse, and tangent is opposite over adjacent. On the unit circle (circle with radius 1), sine is the y-coordinate, cosine is the x-coordinate, and tangent is sine divided by cosine.
Result
You understand how these functions map angles to numbers between -1 and 1 (for sin and cos) or any real number (for tan).
Connecting triangle side ratios to circle coordinates helps unify geometric and circular views of trig functions.
3
IntermediateUsing numpy to Calculate Trig Values
🤔Before reading on: do you think numpy's trig functions accept degrees or radians? Commit to your answer.
Concept: Numpy provides functions np.sin, np.cos, and np.tan that calculate trig values for angles in radians.
You can pass a single number or an array of angles (in radians) to np.sin, np.cos, and np.tan. For example, np.sin(np.pi/2) returns 1. Arrays allow batch calculations for many angles at once.
Result
You can compute sine, cosine, and tangent values efficiently for single or multiple angles.
Understanding numpy's vectorized trig functions enables fast and scalable computations for data science tasks.
4
IntermediateInverse Trigonometric Functions
🤔Before reading on: do you think inverse trig functions return angles in degrees or radians? Commit to your answer.
Concept: Inverse trig functions (arcsin, arccos, arctan) find the angle given a trig value, returning results in radians.
Numpy provides np.arcsin, np.arccos, and np.arctan to reverse trig calculations. For example, np.arcsin(1) returns π/2 radians. These are useful to find angles from known ratios.
Result
You can convert trig values back to angles, enabling two-way conversions.
Knowing inverse functions completes the toolkit for angle and ratio conversions, essential for solving geometric problems.
5
AdvancedHandling Angle Units and Vector Inputs
🤔Before reading on: do you think np.sin(np.array([0, 90, 180])) works correctly without conversion? Commit to your answer.
Concept: Numpy trig functions require radians, so degree inputs must be converted; also, vector inputs allow batch processing.
If you input degrees directly, results will be incorrect. Use np.radians to convert arrays of degrees to radians before applying trig functions. This lets you compute trig values for many angles at once.
Result
Correct trig values for arrays of angles, enabling efficient data processing.
Recognizing the need for unit conversion prevents common bugs and leverages numpy's power for vectorized math.
6
ExpertNumerical Stability and Domain Limits
🤔Before reading on: do you think np.tan can return infinite values? Commit to your answer.
Concept: Trig functions have domain and range limits; tangent can approach infinity near certain angles, causing numerical issues.
Tangent is undefined at angles where cosine is zero (like π/2). Numpy returns very large numbers or warnings near these points. Understanding these limits helps avoid errors in calculations and data analysis.
Result
You can anticipate and handle numerical instability in trig computations.
Knowing domain limits and numerical behavior is crucial for robust scientific computing and avoiding misleading results.
Under the Hood
Numpy's trig functions use efficient algorithms based on Taylor series expansions and hardware-accelerated math libraries to compute sine, cosine, and tangent values from input radians. Internally, these functions convert inputs to floating-point numbers and apply approximations that balance speed and precision. For arrays, numpy applies these computations element-wise using vectorized operations for performance.
Why designed this way?
Trigonometric functions are fundamental in math and science, so numpy implements them to be fast and accurate for both single values and large datasets. Using radians as the standard input aligns with mathematical conventions and simplifies internal calculations. Vectorization leverages modern CPU capabilities, making numpy suitable for data science workloads.
Input angle (radians) ──▶ [Vectorized computation engine] ──▶ [Taylor series approximation] ──▶ [Floating-point result]

Array input ──▶ [Element-wise application] ──▶ [Batch output]

Special cases (e.g., tan near π/2) ──▶ [Domain checks] ──▶ [Warnings or large values]
Myth Busters - 4 Common Misconceptions
Quick: Do numpy trig functions accept degrees directly? Commit to yes or no.
Common Belief:Numpy trig functions accept angles in degrees by default.
Tap to reveal reality
Reality:Numpy trig functions require angles in radians; degrees must be converted first.
Why it matters:Using degrees directly leads to incorrect results, causing errors in calculations and analyses.
Quick: Is tangent defined for all angles? Commit to yes or no.
Common Belief:Tangent is defined and finite for every angle input.
Tap to reveal reality
Reality:Tangent is undefined where cosine is zero, leading to infinite or very large values.
Why it matters:Ignoring this causes numerical errors or misleading outputs in computations involving tangent.
Quick: Does sine always return positive values? Commit to yes or no.
Common Belief:Sine values are always positive because they represent lengths.
Tap to reveal reality
Reality:Sine can be negative depending on the angle's position on the unit circle.
Why it matters:Assuming sine is always positive can lead to wrong interpretations in wave or signal analysis.
Quick: Are inverse trig functions outputs in degrees? Commit to yes or no.
Common Belief:Inverse trig functions return angles in degrees.
Tap to reveal reality
Reality:Inverse trig functions return angles in radians; conversion is needed for degrees.
Why it matters:Misunderstanding output units causes errors when interpreting or using inverse trig results.
Expert Zone
1
Numpy's trig functions use hardware acceleration and vectorization, but floating-point precision limits can cause tiny errors in results, especially for very large or very small inputs.
2
Inverse tangent (arctan) has a two-argument variant (arctan2) that correctly handles signs of inputs to determine the angle's quadrant, which is crucial for accurate angle calculations in 2D space.
3
When chaining trig functions, rounding errors can accumulate, so careful numerical methods or symbolic math may be needed for high-precision applications.
When NOT to use
Avoid using basic trig functions for angles in degrees without conversion; also, for angles near tangent's undefined points, consider using alternative formulations or limit handling. For symbolic or exact math, use libraries like SymPy instead of numpy.
Production Patterns
In production, trig functions are used in signal processing pipelines, computer graphics transformations, and feature engineering for cyclic data (like time of day). Efficient batch processing with numpy arrays and careful unit management are standard practices.
Connections
Fourier Transform
Builds-on
Understanding sine and cosine waves is essential to grasp how Fourier transforms decompose signals into frequency components.
Circular Motion in Physics
Same pattern
Trigonometric functions describe positions and velocities in circular motion, linking math to real-world physical phenomena.
Periodic Functions in Biology
Builds-on
Trigonometric functions model biological rhythms like heartbeats and circadian cycles, showing math's role in life sciences.
Common Pitfalls
#1Using degrees directly in numpy trig functions.
Wrong approach:np.sin(90)
Correct approach:np.sin(np.radians(90))
Root cause:Misunderstanding that numpy expects radians, not degrees.
#2Ignoring tangent's undefined points causing infinite values.
Wrong approach:np.tan(np.pi/2)
Correct approach:Avoid np.tan near π/2 or handle with np.isclose checks and limits.
Root cause:Not recognizing domain restrictions of tangent function.
#3Assuming inverse trig outputs are in degrees.
Wrong approach:angle = np.arcsin(0.5) * 1 # treat as degrees
Correct approach:angle = np.degrees(np.arcsin(0.5)) # convert radians to degrees
Root cause:Confusing output units of inverse trig functions.
Key Takeaways
Trigonometric functions convert angles into numeric ratios that describe positions on a circle or triangle side relationships.
Numpy's trig functions require angles in radians, so always convert degrees before use to avoid errors.
Sine and cosine values range between -1 and 1, while tangent can be any real number but is undefined at certain angles.
Inverse trig functions return angles in radians, requiring conversion if degrees are needed.
Understanding domain limits and numerical behavior of trig functions is essential for accurate and stable computations.