Mathematical Functions

Loading concept...

🔢 NumPy Mathematical Functions: Your Magic Calculator Toolbox

Imagine you have a super-powered calculator that can do math on thousands of numbers at once. That’s NumPy! Today, we’ll explore its magical math tools.


🌟 The Big Picture

Think of NumPy as a kitchen full of cooking tools. Just like you have:

  • A measuring cup (constants like π)
  • A timer (exponential growth/decay)
  • A rounding spoon (rounding numbers)
  • A spinning wheel (trigonometry for circles)
  • A mirror box (complex numbers with real and imaginary parts)

NumPy gives you all these math tools, but for millions of numbers at once!


📐 NumPy Constants: Numbers That Never Change

Some numbers are so special, they have names. NumPy keeps them ready for you!

Meet the Famous Constants

import numpy as np

# Pi - the circle number
print(np.pi)        # 3.14159...

# e - the growth number
print(np.e)         # 2.71828...

# Infinity - bigger than anything
print(np.inf)       # inf

# Not a Number - for impossible math
print(np.nan)       # nan

🎯 Why Do We Need Them?

Constant What It Is Real Example
np.pi Circle’s magic number Finding wheel circumference
np.e Growth rate base Bank interest, population
np.inf Infinity Limits, unbounded values
np.nan “Not a Number” Missing data, 0/0 results

Simple Example:

# Circle with radius 5
radius = 5
area = np.pi * radius ** 2
print(f"Area: {area}")
# Output: Area: 78.539...

📈 Exponential and Log Functions: Growth and Shrink Magic

The Story of Exponential Growth

Imagine you have 1 bunny. Each day, every bunny has 2 babies.

  • Day 0: 1 bunny
  • Day 1: 2 bunnies
  • Day 2: 4 bunnies
  • Day 3: 8 bunnies

This is exponential growth – things multiply super fast!

Exponential Functions

import numpy as np

# e raised to power (natural exponential)
print(np.exp(1))      # 2.718... (e^1)
print(np.exp(2))      # 7.389... (e^2)

# 2 raised to power
print(np.exp2(3))     # 8 (2^3 = 2×2×2)

# Any base raised to power
print(np.power(2, 4)) # 16 (2^4)

Logarithm: The Reverse Button

If exponential is multiplication on steroids, logarithm is the undo button.

# Natural log (base e)
print(np.log(np.e))     # 1.0

# Log base 2
print(np.log2(8))       # 3.0 (2^3 = 8)

# Log base 10
print(np.log10(1000))   # 3.0 (10^3 = 1000)

🎯 Quick Reference

graph TD A[Number] --> B{Operation?} B --> C[exp: Grow fast] B --> D[log: Find the power] C --> E[Small → Big] D --> F[Big → Power used]
Function What It Does Example
np.exp(x) e^x np.exp(2) → 7.39
np.exp2(x) 2^x np.exp2(3) → 8
np.log(x) ln(x) np.log(np.e) → 1
np.log2(x) log₂(x) np.log2(8) → 3
np.log10(x) log₁₀(x) np.log10(100) → 2

🔄 Rounding and Absolute Value: Making Numbers Neat

Rounding: Tidying Up Messy Numbers

Think of rounding like cleaning your room – you put things in the nearest right place.

import numpy as np

messy = np.array([1.2, 2.5, 3.7, -1.4])

# Round to nearest integer
print(np.round(messy))
# [1. 2. 4. -1.]

# Always round DOWN (floor)
print(np.floor(messy))
# [1. 2. 3. -2.]

# Always round UP (ceil)
print(np.ceil(messy))
# [2. 3. 4. -1.]

# Chop off decimals (truncate)
print(np.trunc(messy))
# [1. 2. 3. -1.]

🎯 Rounding Cheat Sheet

Function Rule 2.7 becomes -2.3 becomes
round Nearest 3 -2
floor Down ↓ 2 -3
ceil Up ↑ 3 -2
trunc Toward 0 2 -2

Absolute Value: Distance from Zero

Absolute value is like asking “How far from zero?” – direction doesn’t matter!

numbers = np.array([-5, -2, 0, 3, 7])

# Get absolute values
print(np.abs(numbers))
# [5 2 0 3 7]

# Also works with fabs for floats
print(np.fabs([-3.5, 4.2]))
# [3.5 4.2]

Real Life: Temperature difference doesn’t care about direction. Whether it’s 5° warmer or 5° colder, the change is 5°!


🎡 Trigonometric Functions: The Circle Math

The Ferris Wheel Story

Imagine sitting on a Ferris wheel. As it spins:

  • Your height goes up and down smoothly → that’s sine!
  • Your distance from the center goes back and forth → that’s cosine!
import numpy as np

# Angles in radians
angle = np.pi / 4  # 45 degrees

# Sine - height on circle
print(np.sin(angle))  # 0.707...

# Cosine - horizontal distance
print(np.cos(angle))  # 0.707...

# Tangent - slope of line
print(np.tan(angle))  # 1.0

Converting Degrees ↔ Radians

# Degrees to radians
deg = 90
rad = np.radians(deg)
# or np.deg2rad(deg)
print(rad)  # 1.5707... (π/2)

# Radians to degrees
rad = np.pi
deg = np.degrees(rad)
# or np.rad2deg(rad)
print(deg)  # 180.0

Inverse Trig: Finding the Angle

# If sin(angle) = 0.5, what's angle?
print(np.arcsin(0.5))    # 0.523... rad
print(np.degrees(
    np.arcsin(0.5)))     # 30.0 degrees!

# Same for others
print(np.arccos(0.5))    # 1.047... rad
print(np.arctan(1.0))    # 0.785... rad

🎯 Trig Function Summary

graph LR A[Angle θ] --> B[sin θ: Height] A --> C[cos θ: Width] A --> D[tan θ: Slope] E[Value] --> F[arcsin: Find angle] E --> G[arccos: Find angle] E --> H[arctan: Find angle]
Function Input Output Example
sin Angle Height (-1 to 1) sin(π/2) → 1
cos Angle Width (-1 to 1) cos(0) → 1
tan Angle Slope tan(π/4) → 1
arcsin Value Angle arcsin(1) → π/2

🔮 Complex Number Operations: Numbers with Imagination

What Are Complex Numbers?

Some equations have no “real” answer. Like: What number times itself equals -1?

We invented i (imaginary unit) where: i² = -1

A complex number has two parts:

  • Real part: Normal number
  • Imaginary part: Number × i
import numpy as np

# Create complex number: 3 + 4i
z = 3 + 4j  # Python uses 'j' not 'i'
# or
z = np.complex64(3 + 4j)

print(z)  # (3+4j)

Working with Complex Numbers

z = 3 + 4j

# Get the real part
print(np.real(z))      # 3.0

# Get the imaginary part
print(np.imag(z))      # 4.0

# Absolute value (magnitude)
print(np.abs(z))       # 5.0 (like 3-4-5 triangle!)

# Angle (phase) in radians
print(np.angle(z))     # 0.927... rad

# Complex conjugate (flip sign of i)
print(np.conj(z))      # (3-4j)

🎯 Complex Number Visual

Think of complex numbers as arrows on a treasure map:

graph LR A[Origin 0,0] --> B[3 + 4j] subgraph Parts C[Real: 3 steps right] D[Imaginary: 4 steps up] E[Magnitude: 5 total distance] end
Function What It Gives Example with 3+4j
np.real(z) Real part 3.0
np.imag(z) Imaginary part 4.0
np.abs(z) Distance from 0 5.0
np.angle(z) Direction (radians) 0.927
np.conj(z) Flip imaginary 3-4j

🎁 Putting It All Together

Here’s a mini program using everything we learned:

import numpy as np

# Constants
print(f"Circle area (r=2): {np.pi * 2**2}")

# Exponential & Log
print(f"e^3 = {np.exp(3):.2f}")
print(f"log2(16) = {np.log2(16)}")

# Rounding
print(f"Round 3.7 = {np.round(3.7)}")
print(f"Abs of -5 = {np.abs(-5)}")

# Trigonometry
angle = np.radians(45)
print(f"sin(45°) = {np.sin(angle):.3f}")

# Complex
z = 3 + 4j
print(f"|3+4j| = {np.abs(z)}")

Output:

Circle area (r=2): 12.566...
e^3 = 20.09
log2(16) = 4.0
Round 3.7 = 4.0
Abs of -5 = 5
sin(45°) = 0.707
|3+4j| = 5.0

🌈 Remember This!

Category Key Functions Think Of
Constants pi, e, inf, nan Famous numbers
Exp/Log exp, log, log2, log10 Growth & undo
Rounding round, floor, ceil, abs Tidying up
Trig sin, cos, tan, arcsin Circles & waves
Complex real, imag, abs, angle 2D numbers

You did it! 🎉 You now have a full toolbox of NumPy math functions. These aren’t just for homework – they power video games, weather prediction, music apps, and rockets to space!

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.