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 the Numba-accelerated CPU solver by default.
roots = f1.get_real_roots()
print(f"Approximate Roots: {roots}")
# Expected output may be something like: Approximate Roots: [-1.0001, 2.5003]
# Find all roots (real and complex if find_complex=True in options)
exact_roots = f1.get_roots(options={"find_complex": True})
print(f"Exact Roots: {exact_roots}")
# Expected output: Exact Roots: [-1, 2.5]