A Tour of NTL: Examples: Vectors and Matrices

#

A Tour of NTL: Examples: Vectors and Matrices

The following routine sums up the numbers in a vector of `ZZ`'s.

    #include <NTL/ZZ.h>
    #include <NTL/vector.h>

    using namespace std;
    using namespace NTL;

    ZZ sum(const Vec<ZZ>& v)
    {
       ZZ acc;

       acc = 0;

       for (long i = 0; i < v.length(); i++)
          acc += v[i];

       return acc;
    }

The class `Vec<ZZ>` is a dynamic-length array of `ZZ`s; more generally, NTL provides a template class `Vec<T>` for dynamic-length vectors over any type `T`. Some history is in order here. NTL predates the STL and the `vector` template found in modern `C++`. Older versions of NTL (prior to v6) did not use templates, but instead defined generic vectors using macros. By convention, NTL named these `vec_T`. For backward compatibility, NTL now provides typedefs all these "legacy" vector types.

Vectors in NTL are indexed from 0, but in many situations it is convenient or more natural to index from 1. The generic vector class allows for this; the above example could be written as follows.

    #include <NTL/ZZ.h>
    #include <NTL/vector.h>

    using namespace std;
    using namespace NTL;

    ZZ sum(ZZ& s, const Vec<ZZ>& v)
    {
       ZZ acc;

       acc = 0;

       for (long i = 1; i <= v.length(); i++)
          acc += v(i);

       return acc;
    }

Note that by default, NTL does not perform range checks on vector indices. However, there is a compile-time flag that activates range checking. Therefore, it is good practice to always assume that range checking may be activated, and to not access elements that are out of range.

The following example illustrates vector I/O, as well as changing the length of a vector. This program reads a `Vec<ZZ>`, and then creates and prints a "palindrome".

    #include <NTL/ZZ.h>
    #include <NTL/vector.h>

    using namespace std;
    using namespace NTL;

    int main()
    {
       Vec<ZZ> v;
       cin >> v;

       long n = v.length();
       v.SetLength(2*n);

       long i;
       for (i = 0 ; i < n; i++)
          v[n+i] = v[n-1-i];

       cout << v << "\n";
    }

Notice that changing the length of a vector does not change its contents.

When we compile and run this program, if we type in

       [1 -2 3]

as input, the output is

       [1 -2 3 3 -2 1]

See `vector.txt` for complete details of NTL's generic vector mechanism. Also note that for several fundamental vector types, such as `Vec<ZZ>.txt`, there is a corresponding header file `<NTL/vec_ZZ.h>` that defines a number of basic arithmetic operations, as well as provides the typedef typedef `vec_ZZ` for backward compatibilty. See `vec_ZZ.txt` for complete details on the arithmetic operations for `Vec<ZZ>`'s provided by NTL.

There is also basic support for matrices in NTL. In general, the class `Mat<T>` is a special kind of `Vec< Vec< T > >`, where each row is a vector of the same length. Row `i` of matrix `M` can be accessed as `M[i]` (indexing from 0) or as `M(i)` (indexing from 1). Column `j` of row `i` can be accessed as `M[i][j]` or `M(i)(j)`; for notational convenience, the latter is equivalent to `M(i,j)`.

Here is a matrix multiplication routine, which in fact is already provided by NTL.

    #include <NTL/ZZ.h>
    #include <NTL/matrix.h>

    using namespace std;
    using namespace NTL;

    void mul(Mat<ZZ>& X, const Mat<ZZ>& A, const Mat<ZZ>& B)
    {
       long n = A.NumRows();
       long l = A.NumCols();
       long m = B.NumCols();

       if (l != B.NumRows())
          Error("matrix mul: dimension mismatch");

       X.SetDims(n, m); // make X have n rows and m columns

       long i, j, k;
       ZZ acc, tmp;

       for (i = 1; i <= n; i++) {
          for (j = 1; j <= m; j++) {
             acc = 0;
             for(k = 1; k <= l; k++) {
                mul(tmp, A(i,k), B(k,j));
                add(acc, acc, tmp);
             }
             X(i,j) = acc;
          }
       }
    }

In case of a dimension mismatch, the routine calls the `Error` function, which is a part of NTL and which simply prints the message and aborts. That is generally how NTL deals with errors.

This routine will not work properly if `X` aliases `A` or `B`. The actual matrix multiplication routine in NTL takes care of this. In fact, all of NTL's routines allow outputs to alias inputs.

To call NTL's built-in multiplication routine (declared in `<NTL/mat_ZZ.h>`), one can write

       mul(X, A, B);

or one can also use the operator notation

       X = A * B;

NTL provides several matrix types. See `matrix.txt` for complete details on NTL's generic matrix mechanism. Also see `mat_ZZ.txt` for complete details on the arithmetic operations for `Mat<ZZ>`'s provideed by NTL (including basic linear algebra). Also see `LLL.txt` for details on routines for lattice basis reduction (as well as routines for finding the kernel and image of a matrix).

One thing you may have noticed by now is that NTL code generally avoids the type `int`, preferring instead to use `long`. This seems to go against what most "style" books preach, but nevertheless seems to make the most sense in today's world. Although `int` was originally meant to represent the "natural" word size, this seems to no longer be the case. On 32-bit machines, `int` and `long` are the same, but on 64-bit machines, they are often different, with `int`'s having 32 bits and `long`'s having 64 bits. Indeed, there is a standard, called "LP64", which is being adopted by all Unix-like systems, and which specifies that on 64-bit machines, `int`'s have 32 bits, and `long`'s and pointers have 64 bits. Moreover, on such 64-bit machines, the "natural" word size is usually 64-bits; indeed, it is often more expensive to manipulate 32-bit integers. Thus, for simplicity, efficiency, and safety, NTL uses `long` for all integer values. If you are used to writing `int` all the time, it takes a little while to get used to this.