Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 6911af3fc82cf758634776a159d4f34f > files > 147

libntl-devel-5.5.2-2mdv2010.0.i586.rpm

<html>
<head>
<title>
A Tour of NTL: Examples: Vectors and Matrices </title>
</head>

<body bgcolor="#fff9e6">
<center>
<a href="tour-ex1.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
 <a href="tour-examples.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a> 
<a href="tour-ex3.html"> <img src="arrow3.gif" alt="[Next]" align=bottom></a>
</center>

<h1> 
<p align=center>
A Tour of NTL: Examples: Vectors and Matrices
</p>
</h1>


<p> <hr> <p>


<p>
The following routine sums up the 
numbers in a vector of <tt>ZZ</tt>'s.

<pre>
#include &lt;NTL/vec_ZZ.h&gt;

NTL_CLIENT

ZZ sum(const vec_ZZ&amp; v)
{
   ZZ acc;

   acc = 0;

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

   return acc;
}
</pre>

<p>
The class <tt>vec_ZZ</tt> is a dynamic-length array of <tt>ZZ</tt>s;
more generally, NTL provides template-like macros to create dynamic-length 
vectors over any type T.
By convention, NTL names these vec_T.
The reason that macros are used instead of true templates is simple:
at the present time, compiler support for templates is not entirely
satisfactory, and their use would make NTL much more difficult to port.
At some point in the future, a template-version of NTL may be made
available.

<p>
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.

<pre>
#include &lt;NTL/vec_ZZ.h&gt;

NTL_CLIENT

ZZ sum(ZZ&amp; s, const vec_ZZ&amp; v)
{
   ZZ acc;

   acc = 0;

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

   return acc;
}
</pre>

<p>
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.

<p> <hr> <p>

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

<pre>
#include &lt;NTL/vec_ZZ.h&gt;

NTL_CLIENT

int main()
{
   vec_ZZ v;
   cin &gt;&gt; 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 &lt;&lt; v &lt;&lt "\n";
}
</pre>

<p>

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

<p>

When we compile and run this program,
if we type in
<pre>
   [1 -2 3]
</pre>
as input, the output is
<pre>
   [1 -2 3 3 -2 1]
</pre>

<p>

NTL pre-defines a number of vector types.
In addition, you can create your own.
See <a href="vector.txt"><tt>vector.txt</tt></a> for
complete details of NTL's generic vector mechanism.
Also see <a href="vec_ZZ.txt"><tt>vec_ZZ.txt</tt></a> for
complete details on the arithmetic operations for <tt>vec_ZZ</tt>s
provided by NTL.



<p> <hr> <p>

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

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

<pre>
#include &lt;NTL/mat_ZZ.h&gt;

NTL_CLIENT

void mul(mat_ZZ&amp; X, const mat_ZZ&amp; A, const mat_ZZ&amp; 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 &lt;= n; i++) {
      for (j = 1; j &lt;= m; j++) {
         acc = 0;
         for(k = 1; k &lt;= l; k++) {
            mul(tmp, A(i,k), B(k,j));
            add(acc, acc, tmp);
         }
         X(i,j) = acc;
      }
   }
}
</pre>

<p>
In case of a dimension mismatch, the routine calls the 
<tt>Error</tt> function, which is a part of NTL and which simply
prints the message and aborts.
That is generally how NTL deals with errors.
Currently, NTL makes no use of exceptions (for the same reason
it does not use templates--see above), but a future version
may incorporate them.

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

<p>

To call the multiplication routine, one can write 
<pre>
   mul(X, A, B);
</pre>
or one can also use the operator notation 
<pre>
   X = A * B;
</pre>

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

<p>
One thing you may have noticed by now is that
NTL code generally avoids the type
<tt>int</tt>, preferring instead to use <tt>long</tt>.
This seems to go against what most "style" books preach,
but nevertheless seems to make the most sense in today's world.
Although <tt>int</tt> was originally meant to represent the
"natural" word size, this seems to no longer be the case.
On 32-bit machines, <tt>int</tt> and <tt>long</tt> 
are the same,
but on 64-bit machines, they are often different, with 
<tt>int</tt>'s having 32 bits and <tt>long</tt>'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,
<tt>int</tt>'s have 32 bits, and <tt>long</tt>'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 <tt>long</tt>
for all integer values.
If you are used to writing <tt>int</tt> all the time,
it takes a little while to get used to this.

<p>

<center>
<a href="tour-ex1.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
 <a href="tour-examples.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a> 
<a href="tour-ex3.html"> <img src="arrow3.gif" alt="[Next]" align=bottom></a>
</center>


</body>
</html>