pymodab 1.0.0__py3-none-any.whl

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.
pymodab/ModAB.dll ADDED
Binary file
pymodab/ModAB.lib ADDED
Binary file
pymodab/__init__.py ADDED
@@ -0,0 +1,15 @@
1
+ """
2
+ PyModAB - Python wrapper for the Modified Anderson-Bjork (modAB) algorithm .
3
+ ModAB is a fast and robust root-finding method that combines bisection with the
4
+ Anderson-Bjork method for that provide fast convergence for well-behaved functions
5
+ while preserving wost-case optimality.
6
+
7
+ Ganchovski, N.; Smith, O.; Rackauckas, C.; Tomov, L.; Traykov, A.
8
+ Improvements of the Modified Anderson-Björck (modAB) Root-Finding Algorithm.
9
+ Preprints 2026, 2026032190. https://doi.org/10.20944/preprints202603.2190.v1
10
+ """
11
+
12
+ from .modab import find_root, find_default, get_evaluation_count
13
+
14
+ __version__ = "1.0.0"
15
+ __all__ = ["find_root", "find_default", "get_evaluation_count"]
pymodab/libModAB.so ADDED
Binary file
Binary file
Binary file
pymodab/modab.py ADDED
@@ -0,0 +1,168 @@
1
+ """
2
+ Python wrapper for the ModAB C library.
3
+ """
4
+
5
+ import ctypes
6
+ import os
7
+ import platform
8
+ import sys
9
+ from ctypes import c_double, c_int, CFUNCTYPE
10
+ from typing import Callable
11
+
12
+ # Define the callback function type for the C library
13
+ FUNC_TYPE = CFUNCTYPE(c_double, c_double)
14
+
15
+ def _get_library_path() -> str:
16
+ """Get the path to the shared library based on the platform."""
17
+ package_dir = os.path.dirname(os.path.abspath(__file__))
18
+
19
+ system = platform.system()
20
+ if system == "Windows":
21
+ lib_name = "ModAB.dll"
22
+ elif system == "Darwin":
23
+ machine = platform.machine()
24
+ if machine == "arm64":
25
+ lib_name = "libModAB_arm64.dylib"
26
+ else:
27
+ lib_name = "libModAB_x64.dylib"
28
+ else: # Linux and others
29
+ lib_name = "libModAB.so"
30
+
31
+ lib_path = os.path.join(package_dir, lib_name)
32
+
33
+ if not os.path.exists(lib_path):
34
+ raise FileNotFoundError(
35
+ f"Could not find {lib_name} in {package_dir}. "
36
+ f"Please ensure the library is built for your platform."
37
+ )
38
+
39
+ return lib_path
40
+
41
+ def _load_library():
42
+ """Load the ModAB shared library."""
43
+ lib_path = _get_library_path()
44
+ lib = ctypes.CDLL(lib_path)
45
+
46
+ # Configure modAB_find_root
47
+ lib.modAB_find_root.argtypes = [FUNC_TYPE, c_double, c_double, c_double, c_double, c_int]
48
+ lib.modAB_find_root.restype = c_double
49
+
50
+ # Configure modAB_default
51
+ lib.modAB_default.argtypes = [FUNC_TYPE, c_double, c_double]
52
+ lib.modAB_default.restype = c_double
53
+
54
+ # Configure get_evaluation_count
55
+ lib.get_evaluation_count.argtypes = []
56
+ lib.get_evaluation_count.restype = c_int
57
+
58
+ return lib
59
+
60
+ # Load the library once at module import
61
+ _lib = _load_library()
62
+
63
+
64
+ def find_root(
65
+ f: Callable[[float], float],
66
+ x1: float,
67
+ x2: float,
68
+ atol: float = 1e-14,
69
+ rtol: float = 1e-14,
70
+ max_iter: int = 200
71
+ ) -> float:
72
+ """
73
+ Find the root of f(x) = 0 within the interval [x1, x2].
74
+
75
+ Uses an improved version of the Modified Anderson-Bjork method
76
+ (Ganchovski, Traykov) which combines bisection with the secant method
77
+ for robust and fast convergence.
78
+
79
+ Parameters
80
+ ----------
81
+ f : callable
82
+ A continuous function of one variable.
83
+ x1 : float
84
+ Left endpoint of the bracket interval.
85
+ x2 : float
86
+ Right endpoint of the bracket interval.
87
+ atol : float, optional
88
+ Absolute tolerance for convergence (default: 1e-14).
89
+ rtol : float, optional
90
+ Relative tolerance for convergence (default: 1e-14).
91
+ max_iter : int, optional
92
+ Maximum number of iterations (default: 200).
93
+
94
+ Returns
95
+ -------
96
+ float
97
+ The root of f(x) = 0 within [x1, x2].
98
+ Returns NaN if no root is found or if f(x1) and f(x2) have the same sign.
99
+
100
+ Notes
101
+ -----
102
+ The function f must be continuous on [x1, x2] and f(x1) * f(x2) < 0
103
+ (i.e., the function must have opposite signs at the endpoints).
104
+
105
+ Examples
106
+ --------
107
+ >>> import math
108
+ >>> from pymodab import find_root
109
+ >>> # Find the root of cos(x) - x in [0, 1]
110
+ >>> root = find_root(lambda x: math.cos(x) - x, 0, 1)
111
+ >>> print(f"{root:.10f}")
112
+ 0.7390851332
113
+ """
114
+ c_func = FUNC_TYPE(f)
115
+ return _lib.modAB_find_root(c_func, x1, x2, atol, rtol, max_iter)
116
+
117
+
118
+ def find_default(f: Callable[[float], float], x1: float, x2: float) -> float:
119
+ """
120
+ Find the root of f(x) = 0 within the interval [x1, x2] using default tolerances.
121
+
122
+ This is a convenience function that calls find_root with default parameters:
123
+ atol=1e-14, rtol=1e-14, max_iter=200.
124
+
125
+ Parameters
126
+ ----------
127
+ f : callable
128
+ A continuous function of one variable.
129
+ x1 : float
130
+ Left endpoint of the bracket interval.
131
+ x2 : float
132
+ Right endpoint of the bracket interval.
133
+
134
+ Returns
135
+ -------
136
+ float
137
+ The root of f(x) = 0 within [x1, x2].
138
+ Returns NaN if no root is found or if f(x1) and f(x2) have the same sign.
139
+
140
+ Examples
141
+ --------
142
+ >>> import math
143
+ >>> from pymodab import find_default
144
+ >>> root = find_default(lambda x: x**2 - 2, 1, 2)
145
+ >>> print(f"{root:.10f}")
146
+ 1.4142135624
147
+ """
148
+ c_func = FUNC_TYPE(f)
149
+ return _lib.modAB_default(c_func, x1, x2)
150
+
151
+
152
+ def get_evaluation_count() -> int:
153
+ """
154
+ Get the number of function evaluations from the last root-finding call.
155
+
156
+ Returns
157
+ -------
158
+ int
159
+ The number of times the function was evaluated during the last
160
+ call to find_root or find_default.
161
+
162
+ Examples
163
+ --------
164
+ >>> from pymodab import find_default, get_evaluation_count
165
+ >>> root = find_default(lambda x: x**2 - 2, 1, 2)
166
+ >>> print(f"Evaluations: {get_evaluation_count()}")
167
+ """
168
+ return _lib.get_evaluation_count()
pymodab/py.typed ADDED
File without changes
@@ -0,0 +1,94 @@
1
+ Metadata-Version: 2.4
2
+ Name: pymodab
3
+ Version: 1.0.0
4
+ Summary: Fast and robust root-finding using the Modified Anderson-Bjork method
5
+ Author: Ganchovski, Traykov
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/ModAB-Root-Finding/ModAB-Python
8
+ Project-URL: Repository, https://github.com/ModAB-Root-Finding/ModAB-Python
9
+ Keywords: root-finding,numerical,mathematics,optimization,bisection,secant
10
+ Classifier: Development Status :: 5 - Production/Stable
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Operating System :: Microsoft :: Windows
14
+ Classifier: Operating System :: POSIX :: Linux
15
+ Classifier: Operating System :: MacOS
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.8
18
+ Classifier: Programming Language :: Python :: 3.9
19
+ Classifier: Programming Language :: Python :: 3.10
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Programming Language :: Python :: 3.13
23
+ Classifier: Programming Language :: C
24
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
25
+ Requires-Python: >=3.8
26
+ Description-Content-Type: text/markdown
27
+ License-File: LICENSE
28
+ Dynamic: license-file
29
+
30
+ A fast and robust root-finding library using the Modified Anderson-Bjork method (Ganchovski, Traykov), written in C for Python.
31
+
32
+
33
+ ## Installation
34
+
35
+ ```bash
36
+ pip install pymodab
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ ```python
42
+ import math
43
+ from pymodab import find_root, find_default, get_evaluation_count
44
+
45
+ # Find the root of cos(x) - x = 0 in [0, 1]
46
+ root = find_root(lambda x: math.cos(x) - x, 0, 1, 100)
47
+ print(f"Root: {root}") # 0.7390851332151607
48
+
49
+ # Using default tolerances
50
+ root = find_default(lambda x: x**2 - 2, 1, 2)
51
+ print(f"sqrt(2) = {root}") # 1.4142135623730951
52
+
53
+ # Get the number of function evaluations
54
+ print(f"Evaluations: {get_evaluation_count()}")
55
+ ```
56
+
57
+ ## API
58
+
59
+ ### `find_root(f, x1, x2, atol=1e-14, rtol=1e-14, max_iter=200)`
60
+
61
+ Find the root of `f(x) = 0` within the interval `[x1, x2]`.
62
+
63
+ **Parameters:**
64
+ - `f`: A continuous function of one variable
65
+ - `x1`, `x2`: Bracket interval endpoints (must satisfy `f(x1) * f(x2) < 0`)
66
+ - `atol`: Absolute tolerance (default: 1e-14)
67
+ - `rtol`: Relative tolerance (default: 1e-14)
68
+ - `max_iter`: Maximum iterations (default: 200)
69
+
70
+ **Returns:** The root, or `NaN` if not found.
71
+
72
+ ### `find_default(f, x1, x2)`
73
+
74
+ Convenience wrapper for `find_root` with default tolerances.
75
+
76
+ ### `get_evaluation_count()`
77
+
78
+ Returns the number of function evaluations from the last root-finding call.
79
+
80
+ ## Algorithm
81
+
82
+ Modified Anderson-Björck's method is a new robust and efficient bracketing root-finding algorithm. It combines bisection with Anderson-Björk's method to achieve both fast performance and worst-case optimality.
83
+
84
+ References:
85
+
86
+ Ganchovski N.; Traykov A. Modified Anderson-Björck's method for solving non-linear equations in structural mechanics. IOP Conference Series: Materials Science and Engineering 2023, 1276 (1) 012010, IOP Publishing.
87
+ https://iopscience.iop.org/article/10.1088/1757-899X/1276/1/012010/pdf
88
+
89
+ Ganchovski, N.; Smith, O.; Rackauckas, C.; Tomov, L.; Traykov, A. Improvements of the Modified Anderson-Björck (modAB) Root-Finding Algorithm. Preprints 2026, 2026032190.
90
+ https://www.preprints.org/manuscript/202603.2190
91
+
92
+ ## License
93
+
94
+ MIT License
@@ -0,0 +1,13 @@
1
+ pymodab/ModAB.dll,sha256=e6hEOycSdhDfi9Aao8gzketqFYZ3FKwDZEhnPYjpoiw,166400
2
+ pymodab/ModAB.lib,sha256=8V5V5tnndkdGVwlOg5naw8P988mDU8U66ikMQBLePBg,1762
3
+ pymodab/__init__.py,sha256=fX1v3VuLp2JF4v4K9SzRn6tifl4YYRrBt-HJR8LbQfA,665
4
+ pymodab/libModAB.so,sha256=WhQ3PUHjrQHiIZNqwBFnlmDfZn7ITx5rcuimwgG5hTg,8832
5
+ pymodab/libModAB_arm64.dylib,sha256=KHLfSC_AgkxZnk3BWeG0X2D9zDYgfvo0dPgPvHPMxy0,50264
6
+ pymodab/libModAB_x64.dylib,sha256=c_Mo97amaMVP-VGF7LjIGFbhA61YjCriS9EjYEbeHy0,13119
7
+ pymodab/modab.py,sha256=ezfgBWhF_HAh2FJT7YYrQcH-Kjwpg2-chLBFtH-fByA,4736
8
+ pymodab/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
+ pymodab-1.0.0.dist-info/licenses/LICENSE,sha256=cRYFwc9i-Ih9uB6i8Wz0wqpheWoZKhZSLcIrqxBcMcI,1092
10
+ pymodab-1.0.0.dist-info/METADATA,sha256=sQFdzEuByIU6vV58_cUJpX12_VVPAOm_EScfVnueuaU,3421
11
+ pymodab-1.0.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
12
+ pymodab-1.0.0.dist-info/top_level.txt,sha256=wAcy2W5m5vIuB2YX9WNF3K6hFOeqgMT0Xb-5O-cOHg8,8
13
+ pymodab-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ned Ganchovski
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.
@@ -0,0 +1 @@
1
+ pymodab