Advanced Calculations
Complex Mathematical Operations
Trigonometric and Logarithmic Functions
import math
## Trigonometric calculations
angle = math.pi / 4
print("Sine of 45 degrees:", math.sin(angle))
print("Cosine of 45 degrees:", math.cos(angle))
## Logarithmic functions
print("Natural logarithm:", math.log(10))
print("Base 10 logarithm:", math.log10(100))
Statistical Calculations
import statistics
## Statistical operations
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print("Mean:", statistics.mean(numbers))
print("Median:", statistics.median(numbers))
print("Standard Deviation:", statistics.stdev(numbers))
Numerical Computation Techniques
Precision and Rounding
Operation |
Method |
Example |
Rounding |
round() |
round(3.14159, 2) = 3.14 |
Ceiling |
math.ceil() |
math.ceil(3.1) = 4 |
Floor |
math.floor() |
math.floor(3.9) = 3 |
Complex Number Operations
## Complex number calculations
z1 = 3 + 4j
z2 = 2 - 1j
## Complex arithmetic
print("Addition:", z1 + z2)
print("Multiplication:", z1 * z2)
print("Absolute value:", abs(z1))
Advanced Numeric Algorithms
graph TD
A[Numeric Computation] --> B[Precision Handling]
A --> C[Complex Calculations]
A --> D[Statistical Analysis]
B --> E[Rounding Techniques]
C --> F[Complex Number Operations]
D --> G[Descriptive Statistics]
NumPy for Advanced Calculations
import numpy as np
## NumPy advanced calculations
array = np.array([1, 2, 3, 4, 5])
print("Sum:", np.sum(array))
print("Cumulative Sum:", np.cumsum(array))
print("Product:", np.prod(array))
print("Mean:", np.mean(array))
Scientific Computing Techniques
## Solving mathematical problems
def newton_method(f, df, x0, epsilon=1e-6, max_iter=100):
x = x0
for _ in range(max_iter):
fx = f(x)
if abs(fx) < epsilon:
return x
x = x - fx / df(x)
return x
## Example: Finding square root
def f(x): return x**2 - 16
def df(x): return 2*x
result = newton_method(f, df, 2)
print("Approximate square root of 16:", result)
Key Considerations for LabEx Learners
- Use appropriate libraries for complex calculations
- Be aware of computational precision
- Choose the right method for specific numerical problems
- Understand the limitations of floating-point arithmetic
By mastering these advanced calculation techniques, you'll enhance your Python programming skills with LabEx.