-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython Math 2
More file actions
256 lines (243 loc) · 9.48 KB
/
Python Math 2
File metadata and controls
256 lines (243 loc) · 9.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
Getting Started with the Math Module
The math module comes pre-installed with Python, so no additional installation is required. To use it, simply import the module:
import math
You can also import specific functions or constants:
from math import sqrt, pi, sin, cos
Mathematical Constants
The math module provides several important mathematical constants that are frequently used in calculations:
Core Constants
math.pi: The mathematical constant π (approximately 3.14159)
math.e: Euler's number, the base of natural logarithms (approximately 2.71828)
math.tau: The mathematical constant τ = 2π (approximately 6.28318)
math.inf: Positive infinity (floating-point)
math.nan: Not a Number (NaN) floating-point value
import math
print(f"π = {math.pi}")
print(f"e = {math.e}")
print(f"τ = {math.tau}")
print(f"Infinity: {math.inf}")
Number Theory and Arithmetic Functions
Basic Arithmetic Operations
The math module extends Python's built-in arithmetic capabilities with additional functions:
math.ceil(x): Returns the ceiling (smallest integer ≥ x)
math.floor(x): Returns the floor (largest integer ≤ x)
math.trunc(x): Truncates x to an integer (removes decimal part)
math.fabs(x): Returns the absolute value of x as a float
math.gcd(a, b): Returns the greatest common divisor
math.lcm(*integers): Returns the least common multiple (Python 3.9+)
import math
# Ceiling and floor operations
print(math.ceil(4.3)) # Output: 5
print(math.floor(4.7)) # Output: 4
print(math.trunc(-4.7)) # Output: -4
# GCD and LCM
print(math.gcd(48, 18)) # Output: 6
print(math.lcm(12, 18)) # Output: 36
Factorials and Combinatorics
math.factorial(x): Returns x! (factorial of x)
math.perm(n, k): Returns permutations P(n,k) = n!/(n-k)! (Python 3.8+)
math.comb(n, k): Returns combinations C(n,k) = n!/(k!(n-k)!) (Python 3.8+)
import math
# Factorial
print(math.factorial(5)) # Output: 120
# Permutations and combinations
print(math.perm(5, 2)) # Output: 20
print(math.comb(5, 2)) # Output: 10
Power and Logarithmic Functions
Exponential Functions
math.pow(x, y): Returns x raised to the power y
math.exp(x): Returns e^x
math.exp2(x): Returns 2^x (Python 3.11+)
math.expm1(x): Returns e^x - 1 (useful for small x values)
Logarithmic Functions
math.log(x, base): Returns logarithm of x to the given base (default: natural log)
math.log10(x): Returns base-10 logarithm of x
math.log2(x): Returns base-2 logarithm of x
math.log1p(x): Returns ln(1+x) (useful for small x values)
import math
# Exponential functions
print(math.exp(2)) # e^2
print(math.pow(2, 3)) # 2^3 = 8
# Logarithmic functions
print(math.log(100, 10)) # log₁₀(100) = 2
print(math.log2(8)) # log₂(8) = 3
print(math.ln(math.e)) # ln(e) = 1
Square Root and Related Functions
math.sqrt(x): Returns the square root of x
math.isqrt(x): Returns integer square root (Python 3.8+)
math.cbrt(x): Returns the cube root of x (Python 3.11+)
Trigonometric Functions
The math module provides comprehensive trigonometric functionality:
Basic Trigonometric Functions
math.sin(x): Sine of x (x in radians)
math.cos(x): Cosine of x (x in radians)
math.tan(x): Tangent of x (x in radians)
Inverse Trigonometric Functions
math.asin(x): Arc sine of x (returns radians)
math.acos(x): Arc cosine of x (returns radians)
math.atan(x): Arc tangent of x (returns radians)
math.atan2(y, x): Arc tangent of y/x (handles quadrants correctly)
Hyperbolic Functions
math.sinh(x): Hyperbolic sine of x
math.cosh(x): Hyperbolic cosine of x
math.tanh(x): Hyperbolic tangent of x
math.asinh(x): Inverse hyperbolic sine
math.acosh(x): Inverse hyperbolic cosine
math.atanh(x): Inverse hyperbolic tangent
import math
# Convert degrees to radians
angle_degrees = 45
angle_radians = math.radians(angle_degrees)
# Trigonometric calculations
print(f"sin(45°) = {math.sin(angle_radians):.4f}")
print(f"cos(45°) = {math.cos(angle_radians):.4f}")
print(f"tan(45°) = {math.tan(angle_radians):.4f}")
# Convert back to degrees
result = math.degrees(math.asin(0.5))
print(f"arcsin(0.5) = {result}°")
Angular Conversion Functions
math.degrees(x): Converts radians to degrees
math.radians(x): Converts degrees to radians
Special Functions
Error and Gamma Functions
math.erf(x): Error function
math.erfc(x): Complementary error function
math.gamma(x): Gamma function
math.lgamma(x): Natural logarithm of absolute value of Gamma function
Statistical Functions
math.fsum(iterable): Accurate floating-point sum
math.prod(iterable, start=1): Product of elements (Python 3.8+)
import math
# Accurate sum for floating-point numbers
numbers = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
print(sum(numbers)) # May have floating-point errors
print(math.fsum(numbers)) # More accurate result
# Product of numbers
values = [2, 3, 4, 5]
print(math.prod(values)) # Output: 120
Classification and Testing Functions
The math module provides several functions to test and classify floating-point values:
math.isfinite(x): Returns True if x is finite
math.isinf(x): Returns True if x is infinity
math.isnan(x): Returns True if x is NaN
math.isclose(a, b, rel_tol=1e-09, abs_tol=0.0): Tests if values are close
import math
# Testing special values
print(math.isfinite(5.0)) # True
print(math.isinf(math.inf)) # True
print(math.isnan(math.nan)) # True
# Comparing floating-point numbers
a = 0.1 + 0.1 + 0.1
b = 0.3
print(a == b) # False (floating-point precision)
print(math.isclose(a, b)) # True (within tolerance)
Distance and Norm Functions
math.hypot(*coordinates): Euclidean distance/norm
math.dist(p, q): Euclidean distance between two points (Python 3.8+)
import math
# 2D distance
print(math.hypot(3, 4)) # √(3² + 4²) = 5
# 3D distance
print(math.hypot(1, 2, 3)) # √(1² + 2² + 3²)
# Distance between points
point1 = (1, 2)
point2 = (4, 6)
print(math.dist(point1, point2)) # Distance between points
Practical Applications and Examples
Calculating Compound Interest
import math
def compound_interest(principal, rate, time, n=1):
"""Calculate compound interest"""
amount = principal * math.pow((1 + rate/n), n * time)
return amount
# Example: $1000 at 5% annual interest for 3 years
principal = 1000
rate = 0.05
time = 3
result = compound_interest(principal, rate, time)
print(f"Amount after {time} years: ${result:.2f}")
Working with Angles and Rotations
import math
def rotate_point(x, y, angle_degrees):
"""Rotate a point around the origin"""
angle_rad = math.radians(angle_degrees)
cos_a = math.cos(angle_rad)
sin_a = math.sin(angle_rad)
new_x = x * cos_a - y * sin_a
new_y = x * sin_a + y * cos_a
return new_x, new_y
# Rotate point (1, 0) by 90 degrees
new_x, new_y = rotate_point(1, 0, 90)
print(f"Rotated point: ({new_x:.6f}, {new_y:.6f})")
Statistical Calculations
import math
def standard_deviation(data):
"""Calculate standard deviation using math functions"""
n = len(data)
mean = math.fsum(data) / n
variance = math.fsum((x - mean) ** 2 for x in data) / n
return math.sqrt(variance)
# Example dataset
data = [2, 4, 4, 4, 5, 5, 7, 9]
std_dev = standard_deviation(data)
print(f"Standard deviation: {std_dev:.4f}")
Performance Considerations
The math module is implemented in C and is highly optimized for performance. However, keep these points in mind:
math functions work only with individual numbers, not sequences
For array operations, consider using NumPy instead
math.fsum() is more accurate but slower than sum() for floating-point addition
Use math.isclose() for floating-point comparisons instead of ==
Common Pitfalls and Best Practices
Handling Edge Cases
import math
# Always check for domain errors
def safe_sqrt(x):
if x < 0:
raise ValueError("Cannot compute square root of negative number")
return math.sqrt(x)
# Handle special values
def safe_log(x):
if x <= 0:
raise ValueError("Logarithm undefined for non-positive numbers")
return math.log(x)
Working with Degrees vs Radians
import math
# Always be explicit about angle units
def sin_degrees(angle_deg):
return math.sin(math.radians(angle_deg))
def cos_degrees(angle_deg):
return math.cos(math.radians(angle_deg))
# Use consistently throughout your code
angle = 30 # degrees
result = sin_degrees(angle)
Integration with Other Libraries
The math module integrates well with other Python libraries:
With NumPy
import math
import numpy as np
# math works with individual numbers
angle = math.pi / 4
sin_val = math.sin(angle)
# NumPy works with arrays
angles = np.array([0, math.pi/4, math.pi/2])
sin_vals = np.sin(angles)
With Decimal Module
import math
from decimal import Decimal, getcontext
# Set precision for decimal calculations
getcontext().prec = 50
# Convert math constants to high-precision decimals
pi_decimal = Decimal(str(math.pi))
Conclusion
The Python math module is a powerful and comprehensive library that provides essential mathematical functions for scientific computing, engineering applications, and general programming tasks. Its extensive collection of functions covers arithmetic operations, trigonometry, logarithms, special functions, and more.
Get Ebo Jackson’s stories in your inbox
Join Medium for free to get updates from this writer.
Enter your email
Subscribe
Key takeaways for using the math module effectively:
Import only the functions you need for better code readability
Be mindful of angle units (radians vs degrees) in trigonometric functions
Use appropriate functions for floating-point comparisons and summation
Handle edge cases and domain errors appropriately
Consider NumPy for array-based mathematical operations
Leverage the module’s accuracy and performance optimizations