// Copyright (C) 2026 Kiyotsugu Arai
// SPDX-License-Identifier: LGPL-3.0-or-later

// example_polynomial.cpp
// Polynomial<T> API samples: arithmetic, differentiation/integration, GCD, orthogonal polynomials
//
// Build: CMake target example_polynomial

#include <math/core/Polynomial.hpp>
#include <iostream>

int main() {
    using namespace sangi;

    // Construct polynomials: 1 - 3x + 2x^2
    Polynomial<double> p = {1.0, -3.0, 2.0};
    Polynomial<double> q = {1.0, 1.0};  // 1 + x

    std::cout << "p = " << p << "\n";
    std::cout << "q = " << q << "\n";
    std::cout << "p * q = " << p * q << "\n";

    // Polynomial division
    auto [quot, rem] = p.divmod(q);
    std::cout << "p / q: quot = " << quot << ", rem = " << rem << "\n";

    // Horner evaluation
    std::cout << "p(2) = " << p(2.0) << "\n";  // 1 - 6 + 8 = 3

    // Differentiation / integration
    std::cout << "p' = " << p.derivative() << "\n";  // -3 + 4x
    std::cout << "integral(p) = " << p.integral() << "\n";

    // Polynomial GCD
    Polynomial<double> a = {-1.0, 0.0, 1.0};  // x^2 - 1
    Polynomial<double> b = {-1.0, 1.0};       // x - 1
    std::cout << "gcd(x^2-1, x-1) = " << gcd(a, b) << "\n";

    // Orthogonal polynomials: Legendre P_3(x)
    auto L3 = legendre<double>(3);
    std::cout << "P_3(x) = " << L3 << "\n";
}
