/usr/share/doc/libntl-dev/NTL/tour-ex2.html is in libntl-dev 6.2.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | <html>
<head>
<title>
A Tour of NTL: Examples: Vectors and Matrices </title>
</head>
<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.
<!-- STARTPLAIN
#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;
}
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
<font color="#1874cd">#include </font><font color="#4a708b"><NTL/ZZ.h></font><br>
<font color="#1874cd">#include </font><font color="#4a708b"><NTL/vector.h></font><br>
<br>
using namespace std;<br>
using namespace NTL;<br>
<br>
ZZ sum(<font color="#008b00"><b>const</b></font> Vec<ZZ>& v)<br>
{<br>
ZZ acc;<br>
<br>
acc = <font color="#ff8c00">0</font>;<br>
<br>
<font color="#b03060"><b>for</b></font> (<font color="#008b00"><b>long</b></font> i = <font color="#ff8c00">0</font>; i < v.length(); i++)<br>
acc += v[i];<br>
<br>
<font color="#b03060"><b>return</b></font> acc;<br>
}<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
<p>
The class <tt>Vec<ZZ></tt> is a dynamic-length array of <tt>ZZ</tt>s;
more generally, NTL provides a template class <tt>Vec<T></tt>
for
dynamic-length
vectors over any type <tt>T</tt>.
Some history is in order here.
NTL predates the STL and the <tt>vector</tt> template
found in modern <tt>C++</tt>.
Older versions of NTL (prior to v6) did not use templates, but instead
defined generic vectors using macros.
By convention, NTL named these <tt>vec_T</tt>.
For backward compatibility, NTL now provides typedefs
all these "legacy" vector types.
<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.
<!-- STARTPLAIN
#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;
}
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
<font color="#1874cd">#include </font><font color="#4a708b"><NTL/ZZ.h></font><br>
<font color="#1874cd">#include </font><font color="#4a708b"><NTL/vector.h></font><br>
<br>
using namespace std;<br>
using namespace NTL;<br>
<br>
ZZ sum(ZZ& s, <font color="#008b00"><b>const</b></font> Vec<ZZ>& v)<br>
{<br>
ZZ acc;<br>
<br>
acc = <font color="#ff8c00">0</font>;<br>
<br>
<font color="#b03060"><b>for</b></font> (<font color="#008b00"><b>long</b></font> i = <font color="#ff8c00">1</font>; i <= v.length(); i++)<br>
acc += v(i); <br>
<br>
<font color="#b03060"><b>return</b></font> acc;<br>
}<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
<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".
<!-- STARTPLAIN
#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";
}
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
<font color="#1874cd">#include </font><font color="#4a708b"><NTL/ZZ.h></font><br>
<font color="#1874cd">#include </font><font color="#4a708b"><NTL/vector.h></font><br>
<br>
using namespace std;<br>
using namespace NTL;<br>
<br>
<font color="#008b00"><b>int</b></font> main()<br>
{<br>
Vec<ZZ> v;<br>
cin >> v;<br>
<br>
<font color="#008b00"><b>long</b></font> n = v.length();<br>
v.SetLength(<font color="#ff8c00">2</font>*n);<br>
<br>
<font color="#008b00"><b>long</b></font> i;<br>
<font color="#b03060"><b>for</b></font> (i = <font color="#ff8c00">0</font> ; i < n; i++)<br>
v[n+i] = v[n-<font color="#ff8c00">1</font>-i];<br>
<br>
cout << v <&lt <font color="#4a708b">"</font><font color="#8a2be2">\n</font><font color="#4a708b">"</font>;<br>
}<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
<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>
See <a href="vector.cpp.html"><tt>vector.txt</tt></a> for
complete details of NTL's generic vector mechanism.
Also note that for several fundamental vector types, such as
<tt>Vec<ZZ>.txt</tt>, there is a corresponding header file
<tt><NTL/vec_ZZ.h></tt> that defines
a number of basic arithmetic operations,
as well as provides the typedef
typedef <tt>vec_ZZ</tt> for backward compatibilty.
See <a href="vec_ZZ.cpp.html"><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.
<!-- STARTPLAIN
#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;
}
}
}
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
<font color="#1874cd">#include </font><font color="#4a708b"><NTL/ZZ.h></font><br>
<font color="#1874cd">#include </font><font color="#4a708b"><NTL/matrix.h></font><br>
<br>
using namespace std;<br>
using namespace NTL;<br>
<br>
<font color="#008b00"><b>void</b></font> mul(Mat<ZZ>& X, <font color="#008b00"><b>const</b></font> Mat<ZZ>& A, <font color="#008b00"><b>const</b></font> Mat<ZZ>& B)<br>
{<br>
<font color="#008b00"><b>long</b></font> n = A.NumRows();<br>
<font color="#008b00"><b>long</b></font> l = A.NumCols();<br>
<font color="#008b00"><b>long</b></font> m = B.NumCols();<br>
<br>
<font color="#b03060"><b>if</b></font> (l != B.NumRows())<br>
Error(<font color="#4a708b">"matrix mul: dimension mismatch"</font>);<br>
<br>
X.SetDims(n, m); <font color="#0000ee"><i>// make X have n rows and m columns</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> i, j, k;<br>
ZZ acc, tmp;<br>
<br>
<font color="#b03060"><b>for</b></font> (i = <font color="#ff8c00">1</font>; i <= n; i++) {<br>
<font color="#b03060"><b>for</b></font> (j = <font color="#ff8c00">1</font>; j <= m; j++) {<br>
acc = <font color="#ff8c00">0</font>;<br>
<font color="#b03060"><b>for</b></font>(k = <font color="#ff8c00">1</font>; k <= l; k++) {<br>
mul(tmp, A(i,k), B(k,j));<br>
add(acc, acc, tmp);<br>
}<br>
X(i,j) = acc;<br>
}<br>
}<br>
}<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
<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.
<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 NTL's built-in multiplication routine
(declared in <tt><NTL/mat_ZZ.h></tt>), 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.cpp.html"><tt>matrix.txt</tt></a>
for complete details on NTL's generic matrix mechanism.
Also see <a href="mat_ZZ.cpp.html"><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.cpp.html"><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>
|