This project implements a class template for handling rings of multivariate polynomials. The core component is the following class template:
template <typename T, std::size_t N = 0> class poly;
An object of this class represents a polynomial in a single variable:
To extend the implementation to multivariate polynomials, the type T itself can be another poly. In general, a polynomial of m variables is represented recursively using the poly class up to depth m, with the innermost coefficient type no longer being a poly.
Compile with -std=c++20
.
poly<poly<poly<int, 3>, 2>, 4>
represents a polynomial in three variables over the integer type. Conceptually, a polynomial in m variables is structured as follows:
- The outermost definition represents a polynomial in
$x_1$ , with coefficients being polynomials in variables$x_2$ through$x_m$ . - The coefficient type of this polynomial is itself a polynomial in
$x_2$ , with coefficients being polynomials in variables$x_3$ through$x_m$ , and so on.
This recursive approach enables the representation and manipulation of polynomials with multiple variables in a structured and efficient manner.
- A default constructor initializes a polynomial identically equal to zero.
- Copy and move constructors allow initialization from another
poly<U, M>
where M ≤ N and U is convertible to T. - A conversion constructor creates a polynomial of size 1 from a type convertible to T.
- A variadic constructor initializes the polynomial using up to N coefficients with perfect forwarding.
- The const_poly function constructs a polynomial of size 1 where the sole coefficient is another polynomial.
- Deduction guides enable template argument inference for poly objects.
- Copy and move assignment operators are implemented to support
poly<U, M>
where M ≤ N and U is convertible to T.
- Supports +=, -=, and *= with both polynomials and scalars.
- Implements +, -, * in binary form and unary -.
- Type deduction ensures the result has the minimal required size and a coefficient type of
std::common_type_t<U, V>
.
-
operator[](std::size_t i)
: Returns a reference to the coefficient$a_i$ . -
at(...)
: Evaluates the polynomial as a function of multiple variables, supporting different argument counts andstd::array<U, K>
inputs.
size()
: Returns the polynomial's size.
cross(p, q)
: multiplying two polynomials p and q of different variables, producing a new polynomial in the combined variable set.