numeth 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- numeth-0.1.0/LICENSE +21 -0
- numeth-0.1.0/PKG-INFO +113 -0
- numeth-0.1.0/README.md +73 -0
- numeth-0.1.0/pyproject.toml +24 -0
- numeth-0.1.0/setup.cfg +4 -0
- numeth-0.1.0/src/numeth/__init__.py +57 -0
- numeth-0.1.0/src/numeth/differentiation.py +123 -0
- numeth-0.1.0/src/numeth/integration.py +211 -0
- numeth-0.1.0/src/numeth/interpolation.py +115 -0
- numeth-0.1.0/src/numeth/linear_algebra.py +200 -0
- numeth-0.1.0/src/numeth/optimization.py +79 -0
- numeth-0.1.0/src/numeth/root_finding.py +176 -0
- numeth-0.1.0/src/numeth.egg-info/PKG-INFO +113 -0
- numeth-0.1.0/src/numeth.egg-info/SOURCES.txt +21 -0
- numeth-0.1.0/src/numeth.egg-info/dependency_links.txt +1 -0
- numeth-0.1.0/src/numeth.egg-info/requires.txt +1 -0
- numeth-0.1.0/src/numeth.egg-info/top_level.txt +1 -0
- numeth-0.1.0/tests/test_differentiation.py +46 -0
- numeth-0.1.0/tests/test_integration.py +60 -0
- numeth-0.1.0/tests/test_interpolation.py +29 -0
- numeth-0.1.0/tests/test_linear_algebra.py +45 -0
- numeth-0.1.0/tests/test_optimization.py +29 -0
- numeth-0.1.0/tests/test_root_finding.py +46 -0
numeth-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Numeth
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
numeth-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: numeth
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A comprehensive set of core numerical methods used in engineering and applied mathematics
|
|
5
|
+
Author-email: Abhisumat Kundu <kunduabhisumat@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2025 Numeth
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/example/numeth
|
|
29
|
+
Project-URL: Repository, https://github.com/example/numeth.git
|
|
30
|
+
Project-URL: Documentation, https://numeth.readthedocs.io
|
|
31
|
+
Keywords: numerical methods,scientific computing,engineering,applied mathematics
|
|
32
|
+
Classifier: Programming Language :: Python :: 3
|
|
33
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
34
|
+
Classifier: Operating System :: OS Independent
|
|
35
|
+
Requires-Python: >=3.8
|
|
36
|
+
Description-Content-Type: text/markdown
|
|
37
|
+
License-File: LICENSE
|
|
38
|
+
Requires-Dist: numpy
|
|
39
|
+
Dynamic: license-file
|
|
40
|
+
|
|
41
|
+
# numeth
|
|
42
|
+
|
|
43
|
+
A fully functional Python package implementing core numerical methods for engineering and applied mathematics. Designed for usability and educational clarity.
|
|
44
|
+
|
|
45
|
+
## Installation
|
|
46
|
+
|
|
47
|
+
Install via pip:
|
|
48
|
+
|
|
49
|
+
```
|
|
50
|
+
pip install numeth
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Quick Start
|
|
54
|
+
|
|
55
|
+
Here's a simple example using the Newton-Raphson method to find the square root of 2:
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from numeth import newton_raphson
|
|
59
|
+
|
|
60
|
+
def f(x):
|
|
61
|
+
return x**2 - 2
|
|
62
|
+
|
|
63
|
+
def df(x):
|
|
64
|
+
return 2 * x
|
|
65
|
+
|
|
66
|
+
root, iterations, converged = newton_raphson(f, df, x0=1.0, tol=1e-6, max_iter=100)
|
|
67
|
+
print(f"Root: {root}, Iterations: {iterations}, Converged: {converged}")
|
|
68
|
+
# Output: Root: 1.414213562373095, Iterations: 4, Converged: True
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Supported Methods
|
|
72
|
+
|
|
73
|
+
### Integration
|
|
74
|
+
- Trapezoidal Rule (single and composite)
|
|
75
|
+
- Simpson’s 1/3 Rule (single and composite)
|
|
76
|
+
- Simpson’s 3/8 Rule
|
|
77
|
+
- Gaussian Quadrature (2-point and 3-point)
|
|
78
|
+
|
|
79
|
+
### Differentiation
|
|
80
|
+
- Forward difference (first derivative)
|
|
81
|
+
- Backward difference (first derivative)
|
|
82
|
+
- Central difference (first derivative)
|
|
83
|
+
- Central difference (second derivative)
|
|
84
|
+
- Richardson extrapolation (first derivative)
|
|
85
|
+
|
|
86
|
+
### Root Finding
|
|
87
|
+
- Bisection Method
|
|
88
|
+
- Newton-Raphson Method
|
|
89
|
+
- Secant Method
|
|
90
|
+
- False Position Method
|
|
91
|
+
|
|
92
|
+
### Interpolation
|
|
93
|
+
- Linear Interpolation
|
|
94
|
+
- Lagrange Interpolation
|
|
95
|
+
- Newton’s Divided Difference Interpolation
|
|
96
|
+
|
|
97
|
+
### Linear Algebra
|
|
98
|
+
- Gauss Elimination with partial pivoting
|
|
99
|
+
- LU Decomposition (Doolittle’s method)
|
|
100
|
+
- Jacobi Iterative Method
|
|
101
|
+
- Gauss-Seidel Iterative Method
|
|
102
|
+
|
|
103
|
+
### Optimization
|
|
104
|
+
- Golden Section Search (minimization)
|
|
105
|
+
- Newton’s Method for Optimization (1D)
|
|
106
|
+
|
|
107
|
+
## How to Contribute or Report Issues
|
|
108
|
+
|
|
109
|
+
Contributions are welcome! Please submit pull requests or open issues on the [GitHub repository](https://github.com/example/numeth).
|
|
110
|
+
|
|
111
|
+
## License
|
|
112
|
+
|
|
113
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
numeth-0.1.0/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# numeth
|
|
2
|
+
|
|
3
|
+
A fully functional Python package implementing core numerical methods for engineering and applied mathematics. Designed for usability and educational clarity.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install via pip:
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
pip install numeth
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
Here's a simple example using the Newton-Raphson method to find the square root of 2:
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
from numeth import newton_raphson
|
|
19
|
+
|
|
20
|
+
def f(x):
|
|
21
|
+
return x**2 - 2
|
|
22
|
+
|
|
23
|
+
def df(x):
|
|
24
|
+
return 2 * x
|
|
25
|
+
|
|
26
|
+
root, iterations, converged = newton_raphson(f, df, x0=1.0, tol=1e-6, max_iter=100)
|
|
27
|
+
print(f"Root: {root}, Iterations: {iterations}, Converged: {converged}")
|
|
28
|
+
# Output: Root: 1.414213562373095, Iterations: 4, Converged: True
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Supported Methods
|
|
32
|
+
|
|
33
|
+
### Integration
|
|
34
|
+
- Trapezoidal Rule (single and composite)
|
|
35
|
+
- Simpson’s 1/3 Rule (single and composite)
|
|
36
|
+
- Simpson’s 3/8 Rule
|
|
37
|
+
- Gaussian Quadrature (2-point and 3-point)
|
|
38
|
+
|
|
39
|
+
### Differentiation
|
|
40
|
+
- Forward difference (first derivative)
|
|
41
|
+
- Backward difference (first derivative)
|
|
42
|
+
- Central difference (first derivative)
|
|
43
|
+
- Central difference (second derivative)
|
|
44
|
+
- Richardson extrapolation (first derivative)
|
|
45
|
+
|
|
46
|
+
### Root Finding
|
|
47
|
+
- Bisection Method
|
|
48
|
+
- Newton-Raphson Method
|
|
49
|
+
- Secant Method
|
|
50
|
+
- False Position Method
|
|
51
|
+
|
|
52
|
+
### Interpolation
|
|
53
|
+
- Linear Interpolation
|
|
54
|
+
- Lagrange Interpolation
|
|
55
|
+
- Newton’s Divided Difference Interpolation
|
|
56
|
+
|
|
57
|
+
### Linear Algebra
|
|
58
|
+
- Gauss Elimination with partial pivoting
|
|
59
|
+
- LU Decomposition (Doolittle’s method)
|
|
60
|
+
- Jacobi Iterative Method
|
|
61
|
+
- Gauss-Seidel Iterative Method
|
|
62
|
+
|
|
63
|
+
### Optimization
|
|
64
|
+
- Golden Section Search (minimization)
|
|
65
|
+
- Newton’s Method for Optimization (1D)
|
|
66
|
+
|
|
67
|
+
## How to Contribute or Report Issues
|
|
68
|
+
|
|
69
|
+
Contributions are welcome! Please submit pull requests or open issues on the [GitHub repository](https://github.com/example/numeth).
|
|
70
|
+
|
|
71
|
+
## License
|
|
72
|
+
|
|
73
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=45", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "numeth"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A comprehensive set of core numerical methods used in engineering and applied mathematics"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
authors = [{ name = "Abhisumat Kundu", email = "kunduabhisumat@gmail.com" }]
|
|
11
|
+
license = { file = "LICENSE" }
|
|
12
|
+
keywords = ["numerical methods", "scientific computing", "engineering", "applied mathematics"]
|
|
13
|
+
dependencies = ["numpy"]
|
|
14
|
+
requires-python = ">=3.8"
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
"Homepage" = "https://github.com/example/numeth"
|
|
23
|
+
"Repository" = "https://github.com/example/numeth.git"
|
|
24
|
+
"Documentation" = "https://numeth.readthedocs.io"
|
numeth-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
from .differentiation import (
|
|
2
|
+
backward_difference,
|
|
3
|
+
central_difference,
|
|
4
|
+
central_second_difference,
|
|
5
|
+
forward_difference,
|
|
6
|
+
richardson_extrapolation,
|
|
7
|
+
)
|
|
8
|
+
from .integration import (
|
|
9
|
+
composite_simpsons_13,
|
|
10
|
+
composite_trapezoidal,
|
|
11
|
+
gaussian_quadrature_2,
|
|
12
|
+
gaussian_quadrature_3,
|
|
13
|
+
simpsons_13,
|
|
14
|
+
simpsons_38,
|
|
15
|
+
trapezoidal,
|
|
16
|
+
)
|
|
17
|
+
from .interpolation import (
|
|
18
|
+
lagrange_interpolation,
|
|
19
|
+
linear_interpolation,
|
|
20
|
+
newton_divided_difference,
|
|
21
|
+
)
|
|
22
|
+
from .linear_algebra import (
|
|
23
|
+
gauss_elimination,
|
|
24
|
+
gauss_seidel,
|
|
25
|
+
jacobi,
|
|
26
|
+
lu_decomposition,
|
|
27
|
+
)
|
|
28
|
+
from .optimization import golden_section_search, newton_optimization
|
|
29
|
+
from .root_finding import bisection, false_position, newton_raphson, secant
|
|
30
|
+
|
|
31
|
+
__all__ = [
|
|
32
|
+
"trapezoidal",
|
|
33
|
+
"composite_trapezoidal",
|
|
34
|
+
"simpsons_13",
|
|
35
|
+
"composite_simpsons_13",
|
|
36
|
+
"simpsons_38",
|
|
37
|
+
"gaussian_quadrature_2",
|
|
38
|
+
"gaussian_quadrature_3",
|
|
39
|
+
"forward_difference",
|
|
40
|
+
"backward_difference",
|
|
41
|
+
"central_difference",
|
|
42
|
+
"central_second_difference",
|
|
43
|
+
"richardson_extrapolation",
|
|
44
|
+
"bisection",
|
|
45
|
+
"newton_raphson",
|
|
46
|
+
"secant",
|
|
47
|
+
"false_position",
|
|
48
|
+
"linear_interpolation",
|
|
49
|
+
"lagrange_interpolation",
|
|
50
|
+
"newton_divided_difference",
|
|
51
|
+
"gauss_elimination",
|
|
52
|
+
"lu_decomposition",
|
|
53
|
+
"jacobi",
|
|
54
|
+
"gauss_seidel",
|
|
55
|
+
"golden_section_search",
|
|
56
|
+
"newton_optimization",
|
|
57
|
+
]
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
from typing import Callable
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def forward_difference(f: Callable[[float], float], x: float, h: float) -> float:
|
|
5
|
+
"""
|
|
6
|
+
Forward difference approximation of the first derivative.
|
|
7
|
+
|
|
8
|
+
Args:
|
|
9
|
+
f: Function to differentiate.
|
|
10
|
+
x: Point at which to evaluate.
|
|
11
|
+
h: Step size.
|
|
12
|
+
|
|
13
|
+
Returns:
|
|
14
|
+
Approximation of f'(x).
|
|
15
|
+
|
|
16
|
+
Raises:
|
|
17
|
+
ValueError: If h <= 0.
|
|
18
|
+
|
|
19
|
+
Example:
|
|
20
|
+
>>> forward_difference(lambda x: x**2, 1, 0.1)
|
|
21
|
+
2.100000000000002
|
|
22
|
+
"""
|
|
23
|
+
if h <= 0:
|
|
24
|
+
raise ValueError("Step size h must be positive")
|
|
25
|
+
return (f(x + h) - f(x)) / h
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def backward_difference(f: Callable[[float], float], x: float, h: float) -> float:
|
|
29
|
+
"""
|
|
30
|
+
Backward difference approximation of the first derivative.
|
|
31
|
+
|
|
32
|
+
Args:
|
|
33
|
+
f: Function to differentiate.
|
|
34
|
+
x: Point at which to evaluate.
|
|
35
|
+
h: Step size.
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
Approximation of f'(x).
|
|
39
|
+
|
|
40
|
+
Raises:
|
|
41
|
+
ValueError: If h <= 0.
|
|
42
|
+
|
|
43
|
+
Example:
|
|
44
|
+
>>> backward_difference(lambda x: x**2, 1, 0.1)
|
|
45
|
+
1.9000000000000013
|
|
46
|
+
"""
|
|
47
|
+
if h <= 0:
|
|
48
|
+
raise ValueError("Step size h must be positive")
|
|
49
|
+
return (f(x) - f(x - h)) / h
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def central_difference(f: Callable[[float], float], x: float, h: float) -> float:
|
|
53
|
+
"""
|
|
54
|
+
Central difference approximation of the first derivative.
|
|
55
|
+
|
|
56
|
+
Args:
|
|
57
|
+
f: Function to differentiate.
|
|
58
|
+
x: Point at which to evaluate.
|
|
59
|
+
h: Step size.
|
|
60
|
+
|
|
61
|
+
Returns:
|
|
62
|
+
Approximation of f'(x).
|
|
63
|
+
|
|
64
|
+
Raises:
|
|
65
|
+
ValueError: If h <= 0.
|
|
66
|
+
|
|
67
|
+
Example:
|
|
68
|
+
>>> central_difference(lambda x: x**2, 1, 0.1)
|
|
69
|
+
2.0000000000000018
|
|
70
|
+
"""
|
|
71
|
+
if h <= 0:
|
|
72
|
+
raise ValueError("Step size h must be positive")
|
|
73
|
+
return (f(x + h) - f(x - h)) / (2 * h)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def central_second_difference(f: Callable[[float], float], x: float, h: float) -> float:
|
|
77
|
+
"""
|
|
78
|
+
Central difference approximation of the second derivative.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
f: Function to differentiate.
|
|
82
|
+
x: Point at which to evaluate.
|
|
83
|
+
h: Step size.
|
|
84
|
+
|
|
85
|
+
Returns:
|
|
86
|
+
Approximation of f''(x).
|
|
87
|
+
|
|
88
|
+
Raises:
|
|
89
|
+
ValueError: If h <= 0.
|
|
90
|
+
|
|
91
|
+
Example:
|
|
92
|
+
>>> central_second_difference(lambda x: x**2, 1, 0.1)
|
|
93
|
+
1.9999999999999991
|
|
94
|
+
"""
|
|
95
|
+
if h <= 0:
|
|
96
|
+
raise ValueError("Step size h must be positive")
|
|
97
|
+
return (f(x + h) - 2 * f(x) + f(x - h)) / (h**2)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def richardson_extrapolation(f: Callable[[float], float], x: float, h: float) -> float:
|
|
101
|
+
"""
|
|
102
|
+
Richardson extrapolation for first derivative using central differences.
|
|
103
|
+
|
|
104
|
+
Args:
|
|
105
|
+
f: Function to differentiate.
|
|
106
|
+
x: Point at which to evaluate.
|
|
107
|
+
h: Initial step size.
|
|
108
|
+
|
|
109
|
+
Returns:
|
|
110
|
+
Improved approximation of f'(x).
|
|
111
|
+
|
|
112
|
+
Raises:
|
|
113
|
+
ValueError: If h <= 0.
|
|
114
|
+
|
|
115
|
+
Example:
|
|
116
|
+
>>> richardson_extrapolation(lambda x: x**2, 1, 0.1)
|
|
117
|
+
2.0000000000000004
|
|
118
|
+
"""
|
|
119
|
+
if h <= 0:
|
|
120
|
+
raise ValueError("Step size h must be positive")
|
|
121
|
+
d1 = central_difference(f, x, h)
|
|
122
|
+
d2 = central_difference(f, x, h / 2)
|
|
123
|
+
return (4 * d2 - d1) / 3
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import math
|
|
2
|
+
from typing import Callable, Optional
|
|
3
|
+
|
|
4
|
+
try:
|
|
5
|
+
import numpy as np
|
|
6
|
+
except ImportError:
|
|
7
|
+
np = None
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _has_numpy() -> bool:
|
|
11
|
+
return np is not None
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def trapezoidal(f: Callable[[float], float], a: float, b: float) -> float:
|
|
15
|
+
"""
|
|
16
|
+
Single trapezoidal rule for numerical integration.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
f: Function to integrate.
|
|
20
|
+
a: Lower bound.
|
|
21
|
+
b: Upper bound.
|
|
22
|
+
|
|
23
|
+
Returns:
|
|
24
|
+
Approximation of the integral.
|
|
25
|
+
|
|
26
|
+
Raises:
|
|
27
|
+
ValueError: If a >= b.
|
|
28
|
+
|
|
29
|
+
Example:
|
|
30
|
+
>>> trapezoidal(lambda x: x**2, 0, 1)
|
|
31
|
+
0.5
|
|
32
|
+
"""
|
|
33
|
+
if a >= b:
|
|
34
|
+
raise ValueError("Lower bound a must be less than upper bound b")
|
|
35
|
+
h = b - a
|
|
36
|
+
return (h / 2) * (f(a) + f(b))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def composite_trapezoidal(
|
|
40
|
+
f: Callable[[float], float], a: float, b: float, n: int
|
|
41
|
+
) -> float:
|
|
42
|
+
"""
|
|
43
|
+
Composite trapezoidal rule for numerical integration.
|
|
44
|
+
|
|
45
|
+
Args:
|
|
46
|
+
f: Function to integrate.
|
|
47
|
+
a: Lower bound.
|
|
48
|
+
b: Upper bound.
|
|
49
|
+
n: Number of intervals.
|
|
50
|
+
|
|
51
|
+
Returns:
|
|
52
|
+
Approximation of the integral.
|
|
53
|
+
|
|
54
|
+
Raises:
|
|
55
|
+
ValueError: If a >= b or n < 1.
|
|
56
|
+
|
|
57
|
+
Example:
|
|
58
|
+
>>> composite_trapezoidal(lambda x: x**2, 0, 1, 2)
|
|
59
|
+
0.375
|
|
60
|
+
"""
|
|
61
|
+
if a >= b:
|
|
62
|
+
raise ValueError("Lower bound a must be less than upper bound b")
|
|
63
|
+
if n < 1:
|
|
64
|
+
raise ValueError("Number of intervals n must be at least 1")
|
|
65
|
+
h = (b - a) / n
|
|
66
|
+
integral = f(a) + f(b)
|
|
67
|
+
for i in range(1, n):
|
|
68
|
+
integral += 2 * f(a + i * h)
|
|
69
|
+
return (h / 2) * integral
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def simpsons_13(f: Callable[[float], float], a: float, b: float) -> float:
|
|
73
|
+
"""
|
|
74
|
+
Single Simpson's 1/3 rule for numerical integration.
|
|
75
|
+
|
|
76
|
+
Args:
|
|
77
|
+
f: Function to integrate.
|
|
78
|
+
a: Lower bound.
|
|
79
|
+
b: Upper bound.
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
Approximation of the integral.
|
|
83
|
+
|
|
84
|
+
Raises:
|
|
85
|
+
ValueError: If a >= b.
|
|
86
|
+
|
|
87
|
+
Example:
|
|
88
|
+
>>> simpsons_13(lambda x: x**2, 0, 1)
|
|
89
|
+
0.3333333333333333
|
|
90
|
+
"""
|
|
91
|
+
if a >= b:
|
|
92
|
+
raise ValueError("Lower bound a must be less than upper bound b")
|
|
93
|
+
h = (b - a) / 2
|
|
94
|
+
return (h / 3) * (f(a) + 4 * f(a + h) + f(b))
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def composite_simpsons_13(
|
|
98
|
+
f: Callable[[float], float], a: float, b: float, n: int
|
|
99
|
+
) -> float:
|
|
100
|
+
"""
|
|
101
|
+
Composite Simpson's 1/3 rule for numerical integration. Requires even n.
|
|
102
|
+
|
|
103
|
+
Args:
|
|
104
|
+
f: Function to integrate.
|
|
105
|
+
a: Lower bound.
|
|
106
|
+
b: Upper bound.
|
|
107
|
+
n: Number of intervals (must be even).
|
|
108
|
+
|
|
109
|
+
Returns:
|
|
110
|
+
Approximation of the integral.
|
|
111
|
+
|
|
112
|
+
Raises:
|
|
113
|
+
ValueError: If a >= b, n < 2, or n is odd.
|
|
114
|
+
|
|
115
|
+
Example:
|
|
116
|
+
>>> composite_simpsons_13(lambda x: x**2, 0, 1, 2)
|
|
117
|
+
0.3333333333333333
|
|
118
|
+
"""
|
|
119
|
+
if a >= b:
|
|
120
|
+
raise ValueError("Lower bound a must be less than upper bound b")
|
|
121
|
+
if n < 2 or n % 2 != 0:
|
|
122
|
+
raise ValueError("Number of intervals n must be even and at least 2")
|
|
123
|
+
h = (b - a) / n
|
|
124
|
+
integral = f(a) + f(b)
|
|
125
|
+
for i in range(1, n, 2):
|
|
126
|
+
integral += 4 * f(a + i * h)
|
|
127
|
+
for i in range(2, n, 2):
|
|
128
|
+
integral += 2 * f(a + i * h)
|
|
129
|
+
return (h / 3) * integral
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def simpsons_38(f: Callable[[float], float], a: float, b: float) -> float:
|
|
133
|
+
"""
|
|
134
|
+
Simpson's 3/8 rule for numerical integration.
|
|
135
|
+
|
|
136
|
+
Args:
|
|
137
|
+
f: Function to integrate.
|
|
138
|
+
a: Lower bound.
|
|
139
|
+
b: Upper bound.
|
|
140
|
+
|
|
141
|
+
Returns:
|
|
142
|
+
Approximation of the integral.
|
|
143
|
+
|
|
144
|
+
Raises:
|
|
145
|
+
ValueError: If a >= b.
|
|
146
|
+
|
|
147
|
+
Example:
|
|
148
|
+
>>> simpsons_38(lambda x: x**2, 0, 1)
|
|
149
|
+
0.3333333333333333
|
|
150
|
+
"""
|
|
151
|
+
if a >= b:
|
|
152
|
+
raise ValueError("Lower bound a must be less than upper bound b")
|
|
153
|
+
h = (b - a) / 3
|
|
154
|
+
return (3 * h / 8) * (f(a) + 3 * f(a + h) + 3 * f(a + 2 * h) + f(b))
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def gaussian_quadrature_2(f: Callable[[float], float], a: float, b: float) -> float:
|
|
158
|
+
"""
|
|
159
|
+
2-point Gaussian quadrature for numerical integration.
|
|
160
|
+
|
|
161
|
+
Args:
|
|
162
|
+
f: Function to integrate.
|
|
163
|
+
a: Lower bound.
|
|
164
|
+
b: Upper bound.
|
|
165
|
+
|
|
166
|
+
Returns:
|
|
167
|
+
Approximation of the integral.
|
|
168
|
+
|
|
169
|
+
Raises:
|
|
170
|
+
ValueError: If a >= b.
|
|
171
|
+
|
|
172
|
+
Example:
|
|
173
|
+
>>> gaussian_quadrature_2(lambda x: x**2, 0, 1)
|
|
174
|
+
0.33333333333333337
|
|
175
|
+
"""
|
|
176
|
+
if a >= b:
|
|
177
|
+
raise ValueError("Lower bound a must be less than upper bound b")
|
|
178
|
+
# Transform to [-1,1]
|
|
179
|
+
t = lambda x: ((b - a) * x + (b + a)) / 2
|
|
180
|
+
w = (b - a) / 2
|
|
181
|
+
x1, x2 = -math.sqrt(1 / 3), math.sqrt(1 / 3)
|
|
182
|
+
return w * (f(t(x1)) + f(t(x2)))
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def gaussian_quadrature_3(f: Callable[[float], float], a: float, b: float) -> float:
|
|
186
|
+
"""
|
|
187
|
+
3-point Gaussian quadrature for numerical integration.
|
|
188
|
+
|
|
189
|
+
Args:
|
|
190
|
+
f: Function to integrate.
|
|
191
|
+
a: Lower bound.
|
|
192
|
+
b: Upper bound.
|
|
193
|
+
|
|
194
|
+
Returns:
|
|
195
|
+
Approximation of the integral.
|
|
196
|
+
|
|
197
|
+
Raises:
|
|
198
|
+
ValueError: If a >= b.
|
|
199
|
+
|
|
200
|
+
Example:
|
|
201
|
+
>>> gaussian_quadrature_3(lambda x: x**2, 0, 1)
|
|
202
|
+
0.3333333333333333
|
|
203
|
+
"""
|
|
204
|
+
if a >= b:
|
|
205
|
+
raise ValueError("Lower bound a must be less than upper bound b")
|
|
206
|
+
# Transform to [-1,1]
|
|
207
|
+
t = lambda x: ((b - a) * x + (b + a)) / 2
|
|
208
|
+
w = (b - a) / 2
|
|
209
|
+
x1, x2, x3 = -math.sqrt(3 / 5), 0, math.sqrt(3 / 5)
|
|
210
|
+
w1, w2, w3 = 5 / 9, 8 / 9, 5 / 9
|
|
211
|
+
return w * (w1 * f(t(x1)) + w2 * f(t(x2)) + w3 * f(t(x3)))
|