Quick Start

3. Quick Start

Here’s a simple example of creating a quadratic function and finding its roots:

import polysolve

# Create an object representing a degree-2 polynomial (a quadratic)
# f(x) = 2x² - 3x - 5
f1 = polysolve.Function(2)

# Set the coefficients, from the largest exponent down to the constant
f1.set_coeffs([2, -3, -5])

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

# Find the approximate real roots (where f(x) = 0)
# This uses a genetic algorithm by default.
roots = f1.get_real_roots()

print(f"Approximate Roots: {roots}")
# Expected output may be something like: Approximate Roots: [-1.0001, 2.5003]