From 7c75000637a615b831f3206230a69bca322347d8 Mon Sep 17 00:00:00 2001 From: Jonathan Rampersad Date: Mon, 27 Oct 2025 12:25:54 -0400 Subject: [PATCH] fix(ga): Suppress divide-by-zero warning in NumPy solver 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. --- pyproject.toml | 2 +- src/polysolve/__init__.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 9c667cf..03fdd4f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" }, ] diff --git a/src/polysolve/__init__.py b/src/polysolve/__init__.py index b37b691..4bb0cd4 100644 --- a/src/polysolve/__init__.py +++ b/src/polysolve/__init__.py @@ -295,7 +295,8 @@ class Function: y_calculated = np.polyval(self.coefficients, solutions) error = y_calculated - y_val - ranks = np.where(error == 0, np.finfo(float).max, np.abs(1.0 / error)) + with np.errstate(divide='ignore'): + ranks = np.where(error == 0, np.finfo(float).max, np.abs(1.0 / error)) # Sort solutions by fitness (descending) sorted_indices = np.argsort(-ranks) @@ -345,7 +346,8 @@ 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 - ranks = np.where(error == 0, np.finfo(float).max, np.abs(1.0 / error)) + with np.errstate(divide='ignore'): + ranks = np.where(error == 0, np.finfo(float).max, np.abs(1.0 / error)) sorted_indices = np.argsort(-ranks) # Get the top 'sample_size' solutions the user asked for -- 2.49.1