[Previous] [Up] [Next]

A Tour of NTL: Examples: Polynomials

NTL provides extensive support for very fast polynomial arithmetic. In fact, this was the main motivation for creating NTL in the first place, because existing computer algebra systems and software libraries had very slow polynomial arithmetic. The class ZZX represents univariate polynomials with integer coefficients. The following program reads a polynomial, factors it, and prints the factorization.

#include <NTL/ZZXFactoring.h>

using namespace std;
using namespace NTL;

int main()
   ZZX f;

   cin >> f;

   Vec< Pair< ZZX, long > > factors;
   ZZ c;

   factor(c, factors, f);

   cout << c << "\n";
   cout << factors << "\n";

When this program is compiled an run on input

   [2 10 14 6]
which represents the polynomial 2 + 10*X + 14*x^2 +6*X^3, the output is
   [[[1 3] 1] [[1 1] 2]]
The first line of output is the content of the polynomial, which is 2 in this case as each coefficient of the input polynomial is divisible by 2. The second line is a vector of pairs, the first member of each pair is an irreducible factor of the input, and the second is the exponent to which is appears in the factorization. Thus, all of the above simply means that
2 + 10*X + 14*x^2 +6*X^3 = 2 * (1 + 3*X) * (1 + X)^2 

Admittedly, I/O in NTL is not exactly user friendly, but then NTL has no pretensions about being an interactive computer algebra system: it is a library for programmers.

In this example, the type Vec< Pair< ZZX, long > > is an NTL vector whose base type is Pair< ZZX, long >. The type Pair< ZZX, long > is the instantiation of a template "pair" type defined by NTL. See pair.txt for more details.

Here is another example. The following program prints out the first 100 cyclotomic polynomials.

#include <NTL/ZZX.h>

using namespace std;
using namespace NTL;

int main()
   Vec<ZZX> phi(INIT_SIZE, 100);  

   for (long i = 1; i <= 100; i++) {
      ZZX t;
      t = 1;

      for (long j = 1; j <= i-1; j++)
         if (i % j == 0)
            t *= phi(j);

      phi(i) = (ZZX(INIT_MONO, i) - 1)/t;  

      cout << phi(i) << "\n";

To illustrate more of the NTL interface, let's look at alternative ways this routine could have been written.

First, instead of

   Vec<ZZX> phi(INIT_SIZE, 100);  

one can write

   Vec<ZZX> phi;

Second, instead of

            t *= phi(j);

one can write this as

            mul(t, t, phi(j));


            t = t * phi(j);

Also, one can write phi[j-1] in place of phi(j).

Third, instead of

      phi(i) = (ZZX(INIT_MONO, i) - 1)/t;  

one can write

      ZZX t1;
      SetCoeff(t1, i);
      SetCoeff(t1, 0, -1);
      div(phi(i), t1, t);

Alternatively, one could directly access the coefficient vector as follows:

      ZZX t1;
      t1.SetLength(i+1); // all vector elements are initialized to zero
      t1[i] = 1;
      t1[0] = -1;
      t1.normalize();  // not necessary here, but good practice in general
      div(phi(i), t1, t);

Generally, you can freely access the coefficient vector of a polynomial, as above. However, after fiddling with this vector, you should "normalize" the polynomial, so that the leading coefficient is non-zero: this is an invariant which all routines that work with polynomials expect to hold. Of course, if you can avoid directly accessing the coefficient vector, you should do so. You can always use the SetCoeff routine above to set or change coefficients, and you can always read the value of a coefficient using the routine coeff, e.g.,

   ... f[i] == 1 ...

is equivalent to

   ... coeff(f, i) == 1 ...

except that in the latter case, a read-only reference to zero is returned if the index i is out of range. There are also special-purpose read-only access routines LeadCoeff(f) and ConstTerm(f).

NTL provides a full compliment of arithmetic operations for polynomials over the integers, in both operator and procedural form. All of the basic operations support a "promotion logic" similar to that for ZZ, except that inputs of both types long and ZZ are promoted to ZZX. See ZZX.txt for details, and see ZZXFactoring.txt for details on the polynomial factoring routines.

[Previous] [Up] [Next]