in this (short) series, i want to explain how to implement support for multivariate polynomials in c++ using templates. for a toy project i worked on mostly last weekend, i needed to work with polynomials in different ways:

- create a polynomial in three variables with double coefficients;
- computing the partial derivatives of this polynomial;
- evaluate the above polynomials in many points (with double coordinates);
- plug in different univariate polynomials (of form ) into the first polynomial to obtain a univariate polynomial whose zeros correspond to the intersection of the surface defined by the multivariate polynomial together with a parameterized line;
- test whether a univariate polynomial has zeros in using sturm chains.

for a given, simple enough polynomial, everything can be done “by hand”, but sooner or later, this is annoying and too restricting. and it gets really annoying for the second part, where i wanted to obtain the univariate polynomial. therefore, i thought about how to create a multivariate polynomial template which provides means to implement all of the above in an easy manner. in the end, i had a multivariate polynomial template with a nice interface, for which – as a small example – the following test program compiles:

1 #include <iostream> 2 #include "polynomial.hpp" 3 4 int main() 5 { 6 poly<2, int> f = Monomial<int>(1, 2) + 3 * Monomial<int>(4, 5); 7 std::cout << f << "\n"; 8 int e = f(4)(2); 9 std::cout << e << "\n"; 10 std::cout << derivative<1>(f) << "\n"; 11 poly<1, int> g = f(2), h = f(3); 12 std::cout << g << " and " << h << "\n"; 13 std::cout << GCD<double>(g, h) << "\n"; 14 // cast polynomials to poly<1, double> first; otherwise, 15 // result will be incorrect since int is not a field 16 std::cout << GCD(g, h) << "\n"; 17 // to prove our point, check for yourself that the 18 // result is X_0^5 instead of X_0^2 19 poly<1, double> l = f(Monomial<double>(1) - 1)(Monomial<double>(1) - 2); 20 std::cout << l << "\n"; 21 std::cout << derivative<0>(l) << "\n"; 22 std::cout << GCD(l, derivative<0>(l)) << "\n"; 23 }

compiling it via `g++ -O3 -Wall -o poly-test poly-test.cpp`

, it yielded the following output when run:

1 (1 X_1^2 X_0 + 3 X_1^5 X_0^4) 2 24592 3 (2 X_1 X_0 + 15 X_1^4 X_0^4) 4 (2 X_0^2 + 48 X_0^5) and (3 X_0^2 + 243 X_0^5) 5 1 X_0^2 6 1 X_0^5 7 (-100 + 632 X_0 + -1781 X_0^2 + 2905 X_0^3 + -3006 X_0^4 + 2043 X_0^5 + -912 X_0^6 + 258 X_0^7 + -42 X_0^8 + 3 X_0^9) 8 (632 + -3562 X_0 + 8715 X_0^2 + -12024 X_0^3 + 10215 X_0^4 + -5472 X_0^5 + 1806 X_0^6 + -336 X_0^7 + 27 X_0^8) 9 1

let me explain this example. first, a polynomial in two indeterminates with `int`

coefficients is created as the linear combination of two monomials ( and ), which is the first line of the output. then the polynomial is evaluated in , and the result is printed as line 2. after that, the partial derivative of the polynomial with respect to is computed and written as line 3. after that, the polynomial is evaluated in in 2 and 3, yielding two new polynomials and which are printed in line 4. in line 5, the monic greatest common divisor of and is computed, for which the polynomials are treated as polynomials with `double`

coefficients. that this is necessary is demonstrated by the sixth line, in which the output is printed when `int`

s are used to compute the greatest common divisor: clearly, the result is wrong. then, in line 7 of the output, we evaluate at , yielding a univariate polynomial with double coefficients. line 8 of the output shows its derivative , and line 9 the monic greatest common divisor of and .

note that the program is easy to read for people used to the notion of multivariate polynomials and c++.

in the second part of the series, i want to describe how to represent the polynomials. in the third part, i want to explain how partial derivatives are computed. the fourth part will describe how to do efficient evaluation, and the fifth part will explain the long division and euclidean algorithm implementations.

finally, the source code of the library `polynomial.hpp`

, the test program `poly-test.cpp`

and a simple `makefile`

(using g++) can be downloaded as a `zip`

file here (10 kb).

## comments.