Polynomial Demo — The World of Polynomials
Polynomial<T> is a polynomial template class that works with any coefficient type.
It provides a unified interface for algebraic operations ranging from arithmetic
to differentiation, integration, and orthogonal polynomial generation.
This page presents 4 demos covering fundamentals through advanced topics of polynomial operations. All outputs below were actually computed by calx. The source code is provided at the bottom of the page.
Demo 1: Arithmetic and GCD
We compute polynomial addition, subtraction, multiplication, division, and the greatest common divisor (GCD).
divmod returns both quotient and remainder simultaneously,
and we can verify that $p = q \cdot \text{quot} + \text{rem}$.
Demo 2: Differentiation, Integration, and Definite Integrals
Polynomial differentiation via derivative() and indefinite integration via integral()
are performed as exact symbolic computations.
definiteIntegral(p, a, b) computes the indefinite integral and then substitutes the endpoints.
T = Rational, even fractional coefficients yield completely exact results.
Demo 3: Generating Orthogonal Polynomials
calx supports generating classical orthogonal polynomials via recurrence relations. Chebyshev, Legendre, Hermite, Laguerre, and others can be generated to any desired degree.
T = Rational, the coefficients can be obtained as exact rational numbers.
Demo 4: Composition and Evaluation
Polynomial composition $p(q(x))$ is naturally expressed as p(q).
Evaluation via Horner's method runs in $O(n)$, and passing
Complex<double> as the argument enables complex evaluation.
operator() is templated, so the argument type is automatically deduced.
Scalar yields numeric evaluation, Polynomial yields composition,
and Complex yields complex evaluation — all seamlessly.
Source Code and How to Run
example_polynomial_demo.cpp (full source code)
// example_polynomial_demo.cpp — Polynomial<T> demo
#include <math/core/Polynomial.hpp>
#include <math/core/Complex.hpp>
#include <iostream>
using namespace calx;
int main() {
std::cout << "=== calx: Polynomial Demo ===\n";
// --- Demo 1: Arithmetic & GCD ---
std::cout << "\n--- Arithmetic & GCD ---\n";
Polynomial<double> p = {1.0, -3.0, 2.0}; // 1 - 3x + 2x^2
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";
std::cout << "p * q = " << p * q << "\n";
auto [quot, rem] = p.divmod(q);
std::cout << "p / q = " << quot << " (quotient)\n";
std::cout << "p % q = " << rem << " (remainder)\n";
std::cout << "check: q * quot + rem = " << q * quot + rem << "\n";
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";
// --- Demo 2: Calculus ---
std::cout << "\n--- Calculus ---\n";
Polynomial<double> f = {1.0, 2.0, 3.0, 4.0}; // 1+2x+3x^2+4x^3
std::cout << "f = " << f << "\n";
std::cout << "f' = " << f.derivative() << "\n";
std::cout << "f'' = " << f.derivative(2) << "\n";
std::cout << "int f = " << f.integral() << "\n";
std::cout << "int_0^1 f = " << definiteIntegral(f, 0.0, 1.0) << "\n";
// --- Demo 3: Orthogonal Polynomials ---
std::cout << "\n--- Chebyshev T_n(x) ---\n";
for (int n : {0, 1, 2, 3, 5})
std::cout << "T_" << n << " = " << chebyshevT<double>(n) << "\n";
std::cout << "\n--- Legendre P_n(x) ---\n";
for (int n : {0, 1, 2, 3})
std::cout << "P_" << n << " = " << legendre<double>(n) << "\n";
// --- Demo 4: Composition & Evaluation ---
std::cout << "\n--- Composition ---\n";
Polynomial<double> s = {1.0, 1.0, 1.0}; // 1+x+x^2
Polynomial<double> t = {1.0, 2.0}; // 1+2x
std::cout << "s = " << s << "\n";
std::cout << "t = " << t << "\n";
std::cout << "s(t) = " << s(t) << "\n";
std::cout << "s(3.0) = " << s(3.0) << "\n";
Complex<double> z(1.0, 1.0);
std::cout << "s(1+i) = " << s(z) << "\n";
return 0;
}
For API details, see the Polynomial<T> API Reference.
Build and Run
cd calx
mkdir build && cd build
cmake .. -G "Visual Studio 17 2022" -A x64
cmake --build . --config Release --target example-polynomial-demo
examples\Release\example-polynomial-demo.exe