fix(ga): Suppress divide-by-zero warning in NumPy solver
All checks were successful
Publish Python Package to PyPI / deploy (push) Successful in 18s
Run Python Tests / test (3.12) (pull_request) Successful in 12s
Run Python Tests / test (3.10) (pull_request) Successful in 17s
Run Python Tests / test (3.8) (pull_request) Successful in 10s

The `_solve_x_numpy` method was correctly using `np.where(error == 0, ...)` to handle perfect roots. However, NumPy eagerly calculates `1.0 / error` for the entire array before applying the `where` condition, which was causing a `RuntimeWarning: divide by zero` when a perfect root was found.

This warning was harmless but created unnecessary console noise during testing and use.

This commit wraps the `ranks = ...` assignments in a `with np.errstate(divide='ignore'):` block to silence this specific, expected warning. The CUDA kernel is unaffected as its ternary operator already prevents this calculation.
This commit was merged in pull request #17.
This commit is contained in:
2025-10-27 12:25:54 -04:00
parent c3b3513e79
commit 7c75000637
2 changed files with 5 additions and 3 deletions

View File

@@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
[project]
# --- Core Metadata ---
name = "polysolve"
version = "0.4.0"
version = "0.4.1"
authors = [
{ name="Jonathan Rampersad", email="jonathan@jono-rams.work" },
]

View File

@@ -295,6 +295,7 @@ class Function:
y_calculated = np.polyval(self.coefficients, solutions)
error = y_calculated - y_val
with np.errstate(divide='ignore'):
ranks = np.where(error == 0, np.finfo(float).max, np.abs(1.0 / error))
# Sort solutions by fitness (descending)
@@ -345,6 +346,7 @@ class Function:
# After all generations, do one last ranking to find the best solutions
y_calculated = np.polyval(self.coefficients, solutions)
error = y_calculated - y_val
with np.errstate(divide='ignore'):
ranks = np.where(error == 0, np.finfo(float).max, np.abs(1.0 / error))
sorted_indices = np.argsort(-ranks)