Skip to main content

A Python library for representing, manipulating, and solving exponential functions using analytical methods and genetic algorithms, with optional CUDA acceleration.

Project description

polysolve Logo

PyPI version PyPI pyversions

A Python library for representing, manipulating, and solving polynomial equations using a high-performance genetic algorithm, with optional CUDA/GPU acceleration.


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.
  • 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])

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 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]

# 6. Find roots with the genetic algorithm (CPU)
#    This can solve polynomials of any degree.
ga_opts = GA_Options(num_of_generations=20)
roots_ga = f1.get_real_roots(ga_opts, use_cuda=False)
print(f"Approximate roots from GA: {roots_ga[:2]}")
# > Approximate roots from GA: [-1.000..., 2.500...]

# 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 much deeper, more accurate search
# (slower, but better for high-degree, complex functions)
ga_accurate = GA_Options(
    num_of_generations=50,  # Run for more generations
    data_size=500000,       # Use a larger population
    elite_ratio=0.1,        # Keep the top 10%
    mutation_ratio=0.5      # Mutate 50%
)

# 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

polysolve-0.5.1.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

polysolve-0.5.1-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file polysolve-0.5.1.tar.gz.

File metadata

  • Download URL: polysolve-0.5.1.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for polysolve-0.5.1.tar.gz
Algorithm Hash digest
SHA256 2a8ab762737826c3ab281f9c57aeae2bb7cac856907f75b7b04d3e3fc22f3829
MD5 751f06147f475e8b272c7b345a69cea9
BLAKE2b-256 0024f94ee480821a339136475856a7fc4b68002c0b3fc9799a68172f7bb218d0

See more details on using hashes here.

File details

Details for the file polysolve-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: polysolve-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for polysolve-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c9243764c7f6a81d848aa0db09bb022d8830d874b82a89568d32952424b8dd9d
MD5 f142e92a5068db408f78aed6d5cdf67f
BLAKE2b-256 9bec68c7441b4c69cf5017cacc54476f4d461266b73bb387e20f8f8c5e48035e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page