Math Libraries Exploration
Standard Math Library
Importing and Basic Usage
>>> import math
## Trigonometric functions
>>> math.sin(math.pi/2)
1.0
## Logarithmic functions
>>> math.log(10)
2.302585092994046
## Rounding functions
>>> math.ceil(4.3)
5
>>> math.floor(4.7)
4
NumPy: Advanced Numerical Computing
Installation and Import
## Install NumPy
$ pip3 install numpy
>>> import numpy as np
## Array operations
>>> np.array([1, 2, 3]) * 2
array([2, 4, 6])
## Statistical functions
>>> data = [1, 2, 3, 4, 5]
>>> np.mean(data)
3.0
>>> np.median(data)
3.0
SciPy: Scientific Computing
Installation and Advanced Calculations
## Install SciPy
$ pip3 install scipy
>>> from scipy import stats
## Statistical distributions
>>> stats.norm.pdf(0, loc=0, scale=1)
0.3989422804014327
## Integration
>>> from scipy import integrate
>>> integrate.quad(lambda x: x**2, 0, 1)
(0.33333333333333337, 3.700743415417189e-15)
Sympy: Symbolic Mathematics
Symbolic Calculations
>>> from sympy import symbols, diff
## Define symbolic variables
>>> x = symbols('x')
## Symbolic differentiation
>>> diff(x**2, x)
2*x
## Equation solving
>>> from sympy import solve
>>> solve(x**2 - 4, x)
[-2, 2]
Math Libraries Comparison
Library |
Specialization |
Key Features |
math |
Basic mathematics |
Trigonometry, logarithms |
NumPy |
Numerical computing |
Array operations, statistics |
SciPy |
Scientific computing |
Advanced mathematics, integration |
SymPy |
Symbolic mathematics |
Equation solving, symbolic manipulation |
Library Selection Flowchart
graph TD
A[Start] --> B{Mathematical Task}
B --> |Basic Calculations| C[math Library]
B --> |Numerical Arrays| D[NumPy]
B --> |Scientific Computing| E[SciPy]
B --> |Symbolic Manipulation| F[SymPy]
C --> G[End]
D --> G
E --> G
F --> G
Installation Best Practices
## Recommended installation method
$ pip3 install numpy scipy sympy
LabEx Learning Tip
LabEx recommends exploring these libraries progressively, starting with basic math and advancing to more complex libraries.