Skip to main content

polysolve Logo

PyPI version PyPI pyversions

A Python library for representing, manipulating, and solving polynomial equations. Features a high-performance, Numba-accelerated genetic algorithm for CPU, with an optional CUDA/GPU backend for massive-scale parallel solving.


Key Features

  • Numerically Stable Solver: Makes complex calculations practical. Leverage your GPU to power the robust genetic algorithm, solving high-degree polynomials accurately in a reasonable timeframe.
  • Complex Number Support: Fully supports complex coefficients and finding roots in the complex plane (e.g., $x^2 + 1 = 0 \to \pm i$).
  • Numba Accelerated CPU Solver: The default genetic algorithm is JIT-compiled with Numba for high-speed CPU performance, right out of the box.
  • CUDA Accelerated: Leverage NVIDIA GPUs for a massive performance boost when finding roots in large solution spaces.
  • Create and Manipulate Polynomials: Easily define polynomials of any degree using integer or float coefficients, and perform arithmetic operations like addition, subtraction, multiplication, and scaling.
  • Analytical Solvers: Includes standard, exact solvers for simple cases (e.g., quadratic_solve).
  • Simple API: Designed to be intuitive and easy to integrate into any project.

Installation

Install the base package from PyPI:

pip install polysolve

CUDA Acceleration

To enable GPU acceleration, install the extra that matches your installed NVIDIA CUDA Toolkit version. This provides a significant speedup for the genetic algorithm.

For CUDA 12.x users:

pip install polysolve[cuda12]

Quick Start

Here is a simple example of how to define a quadratic function, find its properties, and solve for its roots.

from polysolve import Function, GA_Options

# 1. Define the function f(x) = 2x^2 - 3x - 5
#    Coefficients can be integers or floats.
f1 = Function(largest_exponent=2)
f1.set_coeffs([2, -3, -5])
f2 = Function(largest_exponent=2)
f2.set_coeffs([3, -4, -5])

print(f"Function f1: {f1}")
# > Function f1: 2x^2 - 3x - 5

# 2. Solve for y at a given x
y_val = f1.solve_y(5)
print(f"Value of f1 at x=5 is: {y_val}")
# > Value of f1 at x=5 is: 30.0

# 3. Get the derivative: 4x - 3
df1 = f1.derivative()
print(f"Derivative of f1: {df1}")
# > Derivative of f1: 4x - 3

# 4. Get the 2nd derivative: 4
ddf1 = f1.nth_derivative(2)
print(f"2nd Derivative of f1: {ddf1}")
# > Derivative of f1: 4

# 5. Find the integral: x^3 - 2x^2 - 5x + 0
igl = f2.integral()
print(f"Integral of f2: {igl}")
# > integral of f2: x^3 - 2x^2 - 5x + 0

# 6. Find the integral with custom constant of integration: x^3 - 2x^2 - 5x + 2
igl = f2.integral(constant_of_integration=2)
print(f"Integral of f2: {igl}")
# > integral of f2: x^3 - 2x^2 - 5x + 2

# 7. Find roots analytically using the quadratic formula
#    This is exact and fast for degree-2 polynomials.
roots_analytic = f1.quadratic_solve()
print(f"Analytic roots: {sorted(roots_analytic)}")
# > Analytic roots: [-1.0, 2.5]

# 8. Find REAL roots with the genetic algorithm (Numba CPU)
#    This is the default, JIT-compiled CPU solver.
ga_opts = GA_Options(num_of_generations=20)
roots_ga = f1.get_real_roots(ga_opts, use_cuda=False)
print(f"Approximate real roots: {roots_ga[:2]}")
# > Approximate real roots: [-1.000..., 2.500...]

# 9. Find ALL roots (Real + Complex)
#    Use get_roots() to search the complex plane.
f_complex = Function(2, [1, 0, 1]) # x^2 + 1
roots_all = f_complex.get_roots(ga_opts)
print(f"Approximate complex roots: {roots_all}")
# > Approximate complex roots: [-1.00...j, 1.00...j]

# If you installed a CUDA extra, you can run it on the GPU:
# roots_ga_gpu = f1.get_real_roots(ga_opts, use_cuda=True)
# print(f"Approximate roots from GA (GPU): {roots_ga_gpu[:2]}")

Tuning the Genetic Algorithm

The GA_Options class gives you fine-grained control over the genetic algorithm's performance, letting you trade speed for accuracy.

The default options are balanced, but for very complex polynomials, you may want a more exhaustive search.

from polysolve import GA_Options

# Create a config for a deep search, optimized for finding
# *all* real roots (even if they are far apart).
ga_robust_search = GA_Options(
    num_of_generations=50,  # Run for more generations
    data_size=500000,       # Use a larger population

    # --- Key Tuning Parameters for Multi-Root Finding ---

    # Widen the parent pool to 75% to keep more "niches"
    # (solution-clouds around different roots) alive.
    selection_percentile=0.75, 

    # Increase the crossover blend factor to 0.75.
    # This allows new solutions to be created further
    # away from their parents, increasing exploration.
    blend_alpha=0.75,

    # Enable complex root finding (default is True)
    find_complex=True
)

# Pass the custom options to the solver
roots = f1.get_real_roots(ga_accurate)

For a full breakdown of all parameters, including crossover_ratio, mutation_strength, and more, please see the full GA_Options API Documentation.


Development & Testing Environment

This project is automatically tested against a specific set of dependencies to ensure stability. Our Continuous Integration (CI) pipeline runs on an environment using CUDA 12.5 on Ubuntu 24.04.

While the code may work on other configurations, all contributions must pass the automated tests in our reference environment. For detailed information on how to replicate the testing environment, please see our Contributing Guide.

Contributing

PRs Welcome GitHub issues GitHub pull requests

Contributions are welcome! Whether it's a bug report, a feature request, or a pull request, please feel free to get involved.

Please read our CONTRIBUTING.md file for details on our code of conduct and the process for submitting pull requests.

Contributors

Jonathan Rampersad
Jonathan Rampersad

🚧 💻 📖 🚇
Add your contributions

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for polysolve 0.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for polysolve 0.8.0
File Size Uploaded
polysolve-0.8.0.tar.gz 22.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for polysolve 0.8.0
File Interpreter ABI Platform
polysolve-0.8.0-py3-none-any.whl Python 3 none any Details

Total release size: 39.1 kB

Release files / polysolve-0.8.0.tar.gz

Download URL polysolve-0.8.0.tar.gz
Size 22.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c2149829a0f6518ee5f2b2bf2db20f380ec13ac39fa97bf8f2bce75f16505258
BLAKE2b-256 checksum
How to use checksums
d9e4908af865347c54b30bcef81423f9960b5e6c502eebba98f4732f4aebca59
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / polysolve-0.8.0-py3-none-any.whl

Download URL polysolve-0.8.0-py3-none-any.whl
Size 17.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e8a1ad422935cff507ddc069dfb85500716e40999adb8a0d479d9e493026ccad
BLAKE2b-256 checksum
How to use checksums
df41db1f69834f92120795facfc56a4cf35e059420308c38260cc6389ee9bd40
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release history Release notifications | RSS feed

0.8.1

2 release files

This release

0.8.0 This release

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.3

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page