Codebase list ntl / d9bcf4f
Import ntl_6.2.1.orig.tar.gz Julien Puydt 9 years ago
28 changed file(s) with 2094 addition(s) and 528 deletion(s). Raw diff Collapse all Expand all
0 NTL -- a library for doing numbery theory -- version 6.2
1 Release date: 2014.08.21
0 NTL -- a library for doing numbery theory -- version 6.2.1
1 Release date: 2014.08.26
22
33 Author: Victor Shoup (victor@shoup.net)
44
33 A Tour of NTL: Acknowledgements </title>
44 </head>
55
6 <body bgcolor="#fff9e6">
76
87 <center>
98 <a href="tour-changes.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
33 A Tour of NTL: Summary of Changes </title>
44 </head>
55
6 <body bgcolor="#fff9e6">
76 <center>
87 <a href="tour-roadmap.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
98 <a href="tour.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a>
1817
1918 <p> <hr> <p>
2019 <h3>
20 2014.8.26: Changes between NTL 6.2 and 6.2.1
21 </h3>
22
23 <ul>
24 <li>
25 Fixed syntax problem in <tt>NTL/vector.h</tt>
26 </ul>
27
28 <p> <hr> <p>
29 <h3>
2130 2014.8.21: Changes between NTL 6.1 and 6.2
2231 </h3>
2332
2736 <li>
2837 I added <i>explicit</i> constructors corresponding to promotions.
2938 For example:
30 <pre>
39 <!-- STARTPLAIN
3140 ZZ w = ZZ(1); // legal
3241 ZZ w(1); // legal
3342 ZZ w{1}; // legal in C++11
3443 ZZ w = 1; // not legal
35 </pre>
44 ENDPLAIN -->
45 <!-- STARTPRETTY {{{ -->
46 <p><p><table cellPadding=10px><tr><td><font color="#000000">
47 <font face="monospace">
48 &nbsp;&nbsp; ZZ w = ZZ(<font color="#ff8c00">1</font>);&nbsp;<font color="#0000ee"><i>// legal</i></font><br>
49 &nbsp;&nbsp; ZZ w(<font color="#ff8c00">1</font>);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// legal</i></font><br>
50 &nbsp;&nbsp; ZZ w{<font color="#ff8c00">1</font>};&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// legal in C++11</i></font><br>
51 &nbsp;&nbsp; ZZ w =&nbsp;<font color="#ff8c00">1</font>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// not legal</i></font><br>
52 </font>
53 </font></td></tr></table><p><p>
54 <!-- }}} ENDPRETTY -->
55
3656 <p>
3757 Also added new names for the "monomial constructors", e.g.,
3858 <tt>ZZX(INIT_MONO, i, c)</tt> is now preferred to <tt>ZZX(i, c)</tt>,
94114 <tt>zz_pPush</tt>, <tt>zz_pEPush</tt>, <tt>GF2EPush</tt>.
95115 These allow one to conveniently backup and optionally install
96116 a new modulus in one step:
97 <pre>
117 <!-- STARTPLAIN
98118 { ZZ_pPush push(p); ... }
99 </pre>
119 ENDPLAIN -->
120 <!-- STARTPRETTY {{{ -->
121 <p><p><table cellPadding=10px><tr><td><font color="#000000">
122 <font face="monospace">
123 &nbsp;&nbsp; { ZZ_pPush push(p); ... }<br>
124 </font>
125 </font></td></tr></table><p><p>
126 <!-- }}} ENDPRETTY -->
127
100128 will save the current modulus and install <tt>p</tt> as the
101129 new modulus; when the destructor for <tt>push</tt> is invoked,
102130 the old modulus will be re-installed.
178206 <li>
179207 Added support for "user defined" FFT primes for <tt>zz_p</tt>.
180208 See the functions
181 <pre>
209 <!-- STARTPLAIN
182210 static void zz_p::UserFFTInit(long p);
183211 zz_pContext::zz_pContext(INIT_USER_FFT_TYPE, long p);
184 </pre>
212 ENDPLAIN -->
213 <!-- STARTPRETTY {{{ -->
214 <p><p><table cellPadding=10px><tr><td><font color="#000000">
215 <font face="monospace">
216 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>static</b></font>&nbsp;<font color="#008b00"><b>void</b></font>&nbsp;zz_p::UserFFTInit(<font color="#008b00"><b>long</b></font>&nbsp;p);<br>
217 &nbsp;&nbsp; zz_pContext::zz_pContext(INIT_USER_FFT_TYPE,&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;p);<br>
218 </font>
219 </font></td></tr></table><p><p>
220 <!-- }}} ENDPRETTY -->
221
185222 in the <tt>lzz_p</tt> module.
186223
187224 </ul>
275312 In the case where <tt>b</tt> is a <tt>long</tt>,
276313 this may be much faster than writing
277314 <tt>mul(t, a, b); add(x, x, t)</tt>.
278 See <a href="ZZ.txt">ZZ.txt</a> for details.
315 See <a href="ZZ.cpp.html">ZZ.txt</a> for details.
279316
280317 These new routines are used in a number of places in
281318 NTL to get faster algorithms (for example, the <tt>LLL</tt> routine).
338375
339376 <li>
340377 Added a callback mechanism to NTL's error reporting function.
341 See <tt>ErrorCallback</tt> in <a href="tools.txt">tools.txt</a>.
378 See <tt>ErrorCallback</tt> in <a href="tools.cpp.html">tools.txt</a>.
342379
343380 <li>
344381 Added support for the <tt>gf2x</tt> library for speeding up
525562 <li>
526563 Added functions <tt>IsWhiteSpace</tt>, <tt>CharToIntVal</tt>,
527564 and <tt>IntValToChar</tt> to the <tt>tools</tt> module
528 <a href="tools.txt">[more details]</a>.
565 <a href="tools.cpp.html">[more details]</a>.
529566
530567 <p>
531568 <li>
532569 Added methods <tt>allocated</tt>, <tt>position1</tt> to generic vector classes
533 <a href="vector.txt">[more details]</a>.
570 <a href="vector.cpp.html">[more details]</a>.
534571
535572 <p>
536573 <li>
537574 Added method <tt>allocated</tt> to the class <tt>vec_GF2</tt>
538 <a href="vec_GF2.txt">[more details]</a>.
575 <a href="vec_GF2.cpp.html">[more details]</a>.
539576
540577 <p>
541578 <li>
547584 Added routines <tt>AddPrec</tt>, <tt>SubPrec</tt>, etc., to the <tt>RR</tt>
548585 module, and declared the practice of directly assigning to the variable
549586 <tt>RR::prec</tt> obsolete
550 <a href="RR.txt">[more details]</a>.
587 <a href="RR.cpp.html">[more details]</a>.
551588
552589 <p>
553590 <li>
572609 used by NTL, and is the default (one can switch back to the old algorithm
573610 with a run-time switch).
574611 <p>
575 <a href="ZZXFactoring.txt">[documentation]</a>
612 <a href="ZZXFactoring.cpp.html">[documentation]</a>
576613 <p>
577614 <a href="tour-time.html">[performance measurements]</a>
578615 <p>
582619 <tt>LLL</tt> routines, except that they return the exact values of the
583620 squared lengths of the Gramm-Schmidt basis vectors.
584621 This is useful in implementing van Hoeij's algorithm.
585 <a href="LLL.txt">[more details]</a>.
622 <a href="LLL.cpp.html">[more details]</a>.
586623 <p>
587624
588625 <li>
607644 Any small bug fixes to this version will be named "5.2.1", "5.2.2", etc.
608645 Also, macros are now defined so that the numerical components
609646 of the version number are available to the programmer.
610 <a href="version.txt">[more details]</a>.
647 <a href="version.cpp.html">[more details]</a>.
611648
612649
613650 </ul>
629666 <li>
630667 Added a routine <tt>LatticeSolve()</tt> for finding integer
631668 solutions to linear systems of integer equations.
632 <a href="LLL.txt">[more details]</a>
669 <a href="LLL.cpp.html">[more details]</a>
633670
634671 <p>
635672 <li>
636673 Modified the stragey used by the <tt>LLL()</tt> and <tt>image()</tt>
637 routines in the <a href="LLL.txt">LLL package</a> to deal
674 routines in the <a href="LLL.cpp.html">LLL package</a> to deal
638675 with linear dependencies.
639676 The new strategy guarantees better worst-case bounds on the
640677 sizes of intermediate values.
808845 <li>
809846 Added function <tt>GenGermainPrime</tt>
810847 to efficiently generate random Germain primes, i.e., primes <i>p</i>
811 such that <i>2p+1</i> is also prime. <a href="ZZ.txt">[more details]</a>
848 such that <i>2p+1</i> is also prime. <a href="ZZ.cpp.html">[more details]</a>
812849 <li>
813850 Added a function <tt>random</tt> to generate random <tt>quad_floats</tt>.
814 <a href="quad_float.txt">[more details]</a>
851 <a href="quad_float.cpp.html">[more details]</a>
815852 <li>
816853 Added an <tt>ifdef</tt> in <tt>tools.h</tt> that allows
817854 one to suppress the declaration of <tt>min</tt> and <tt>max</tt>
901938 <ul>
902939 <li>
903940 Improved time and space efficiency of the HNF routine
904 (see <a href="HNF.txt"><tt>HNF.txt</tt></a>).
941 (see <a href="HNF.cpp.html"><tt>HNF.txt</tt></a>).
905942 The old version was based on the description in Henri Cohen's book,
906943 which was not really properly optimized.
907944 </ul>
948985 RoundToPrecision, MakeRR
949986 random
950987 </pre>
951 See <a href="RR.txt"><tt>RR.txt</tt></a> for details.
988 See <a href="RR.cpp.html"><tt>RR.txt</tt></a> for details.
952989
953990 <li>
954991 Improved the accuracy of <tt>quad_float</tt> input/output,
9841021 <li>
9851022 Tightened up some size checks, so that now some nice "size invariants"
9861023 are guaranteed, e.g., for a <tt>ZZ</tt> <tt>n</tt>,
987 <pre>
988 NumBits(NumBits(n)) &lt;= NTL_BITS_PER_LONG-4
989 </pre>
1024 <!-- STARTPLAIN
1025 NumBits(NumBits(n)) <= NTL_BITS_PER_LONG-4
1026 ENDPLAIN -->
1027 <!-- STARTPRETTY {{{ -->
1028 <p><p><table cellPadding=10px><tr><td><font color="#000000">
1029 <font face="monospace">
1030 &nbsp;&nbsp; NumBits(NumBits(n)) &lt;= NTL_BITS_PER_LONG-<font color="#ff8c00">4</font><br>
1031 </font>
1032 </font></td></tr></table><p><p>
1033 <!-- }}} ENDPRETTY -->
1034
9901035 Similarly for the type <tt>GF2X</tt>.
9911036 Of course, on most platforms, one will run out of memory before
9921037 these bounds are exceeded, but they are nevertheless convenient.
10151060 for conversion between byte vectors and polynomials over <tt>GF(2)</tt>,
10161061 along with routines <tt>NumBits</tt> and <tt>NumBytes</tt>
10171062 for such polynomials.
1018 See <a href="GF2X.txt"><tt>GF2X.txt</tt></a> for details.
1063 See <a href="GF2X.cpp.html"><tt>GF2X.txt</tt></a> for details.
10191064
10201065 <li>
10211066 Added a hack in the <tt>ZZX</tt> factorizer
10221067 to exploit polynomials of the form <tt>g(x^k)</tt>.
10231068 This can be disabled by setting the variable <tt>ZZXFac_PowerHack</tt>
10241069 to zero.
1025 See <a href="ZZXFactoring.txt"><tt>ZZXFactoring.txt</tt></a>
1070 See <a href="ZZXFactoring.cpp.html"><tt>ZZXFactoring.txt</tt></a>
10261071 for details.
10271072
10281073 <li>
10291074 Improved the hensel system solver <tt>solve1</tt>.
1030 See <a href="mat_ZZ.txt"><tt>mat_ZZ.txt</tt></a> for details.
1075 See <a href="mat_ZZ.cpp.html"><tt>mat_ZZ.txt</tt></a> for details.
10311076
10321077 <li>
10331078 Changed documentation for <tt>RationalReconstruction</tt>
10341079 to reflect the Wang, Guy, Davenport bounds.
1035 See <a href="ZZ.txt"><tt>ZZ.txt</tt></a> for details.
1080 See <a href="ZZ.cpp.html"><tt>ZZ.txt</tt></a> for details.
10361081
10371082 <li>
10381083 Improved the routine <tt>GenPrime</tt> a bit.
10591104 <ul>
10601105 <li>
10611106 Added a "rational reconstruction" routine.
1062 See the routine <tt>ReconstructRational</tt> in <a href="ZZ.txt">ZZ.txt</a>.
1107 See the routine <tt>ReconstructRational</tt> in <a href="ZZ.cpp.html">ZZ.txt</a>.
10631108 <li>
10641109 Added another routine for solving linear systems over <tt>ZZ</tt>
10651110 that is based on Hensel lifting, rather than Chinese Remaindering.
10661111 It can be significantly faster in some cases.
1067 See the routine <tt>solve1</tt> in <a href="mat_ZZ.txt">mat_ZZ.txt</a>).
1112 See the routine <tt>solve1</tt> in <a href="mat_ZZ.cpp.html">mat_ZZ.txt</a>).
10681113 <li>
10691114 Some performace tuning, especially CRT and polynomial interpolation code.
10701115 <li>
12001245 The names of header files themeselves pollute another (extra-linguitsic) namespace.
12011246 To alleviate this problem, the header files have been renamed.
12021247 Instead of
1203 <pre>
1248 <!-- STARTPLAIN
12041249 #include "foo.h"
1205 </pre>
1250 ENDPLAIN -->
1251 <!-- STARTPRETTY {{{ -->
1252 <p><p><table cellPadding=10px><tr><td><font color="#000000">
1253 <font face="monospace">
1254 <font color="#1874cd">&nbsp;&nbsp; #include&nbsp;</font><font color="#4a708b">&quot;foo.h&quot;</font><br>
1255 </font>
1256 </font></td></tr></table><p><p>
1257 <!-- }}} ENDPRETTY -->
1258
12061259 one now should write
1207 <pre>
1208 #include &lt;NTL/foo.h&gt;
1209 </pre>
1260 <!-- STARTPLAIN
1261 #include <NTL/foo.h>
1262 ENDPLAIN -->
1263 <!-- STARTPRETTY {{{ -->
1264 <p><p><table cellPadding=10px><tr><td><font color="#000000">
1265 <font face="monospace">
1266 <font color="#1874cd">&nbsp;&nbsp; #include&nbsp;</font><font color="#4a708b">&lt;NTL/foo.h&gt;</font><br>
1267 </font>
1268 </font></td></tr></table><p><p>
1269 <!-- }}} ENDPRETTY -->
1270
12101271 The only exceptions are the old header files "ntl_vector.h",
12111272 "ntl_matrix.h", and "ntl_pair.h", which are now called
12121273 <tt>&lt;NTL/vector.h&gt;</tt>, <tt>&lt;NTL/matrix.h&gt;</tt>, and
12821343 Added floating point LLL routines based on Givens rotations,
12831344 instead of classical Gramm-Schmidt orthogonalization.
12841345 This is a more stable, but somewhat slower, method.
1285 See <a href="LLL.txt">LLL.txt</a> for details.
1346 See <a href="LLL.cpp.html">LLL.txt</a> for details.
12861347
12871348 <li>
12881349 Added support for irreducible trinomials and pentanomials
14881549 <li>
14891550 The conversion operator "<tt>&lt;&lt;</tt>" has been retired.
14901551 Now instead of
1491 <pre>
1492 x &lt;&lt; a;
1493 </pre>
1552 <!-- STARTPLAIN
1553 x << a;
1554 ENDPLAIN -->
1555 <!-- STARTPRETTY {{{ -->
1556 <p><p><table cellPadding=10px><tr><td><font color="#000000">
1557 <font face="monospace">
1558 &nbsp;&nbsp; x &lt;&lt; a;&nbsp;<br>
1559 </font>
1560 </font></td></tr></table><p><p>
1561 <!-- }}} ENDPRETTY -->
1562
14941563 one writes
1495 <pre>
1564 <!-- STARTPLAIN
14961565 conv(x, a);
1497 </pre>
1566 ENDPLAIN -->
1567 <!-- STARTPRETTY {{{ -->
1568 <p><p><table cellPadding=10px><tr><td><font color="#000000">
1569 <font face="monospace">
1570 &nbsp;&nbsp; conv(x, a);<br>
1571 </font>
1572 </font></td></tr></table><p><p>
1573 <!-- }}} ENDPRETTY -->
1574
14981575 <p>
14991576 Operator "<tt>&lt;&lt;</tt>" is now used for shift operations.
15001577 <li>
15021579 which has the name <tt>to_T</tt>, where <tt>T</tt> is the result type.
15031580 These new names replace old names that were less consistent.
15041581 So instead of
1505 <pre>
1582 <!-- STARTPLAIN
15061583 x = Long(a);
1507 </pre>
1584 ENDPLAIN -->
1585 <!-- STARTPRETTY {{{ -->
1586 <p><p><table cellPadding=10px><tr><td><font color="#000000">
1587 <font face="monospace">
1588 &nbsp;&nbsp; x = Long(a);<br>
1589 </font>
1590 </font></td></tr></table><p><p>
1591 <!-- }}} ENDPRETTY -->
1592
15081593 one writes
1509 <pre>
1594 <!-- STARTPLAIN
15101595 x = to_long(a);
1511 </pre>
1596 ENDPLAIN -->
1597 <!-- STARTPRETTY {{{ -->
1598 <p><p><table cellPadding=10px><tr><td><font color="#000000">
1599 <font face="monospace">
1600 &nbsp;&nbsp; x = to_long(a);<br>
1601 </font>
1602 </font></td></tr></table><p><p>
1603 <!-- }}} ENDPRETTY -->
1604
15121605
15131606
15141607 <li>
16351728 A more consistent and natural interface, including arithmetic operators
16361729 and a disciplined use of automatic conversion.
16371730 So now one can write
1638 <pre>
1731 <!-- STARTPLAIN
16391732 x = a * b + c;
1640 </pre>
1733 ENDPLAIN -->
1734 <!-- STARTPRETTY {{{ -->
1735 <p><p><table cellPadding=10px><tr><td><font color="#000000">
1736 <font face="monospace">
1737 &nbsp;&nbsp; x = a * b + c;<br>
1738 </font>
1739 </font></td></tr></table><p><p>
1740 <!-- }}} ENDPRETTY -->
1741
16411742 instead of
1642 <pre>
1743 <!-- STARTPLAIN
16431744 mul(x, a, b);
16441745 add(x, x, c);
1645 </pre>
1746 ENDPLAIN -->
1747 <!-- STARTPRETTY {{{ -->
1748 <p><p><table cellPadding=10px><tr><td><font color="#000000">
1749 <font face="monospace">
1750 &nbsp;&nbsp; mul(x, a, b);<br>
1751 &nbsp;&nbsp; add(x, x, c);<br>
1752 </font>
1753 </font></td></tr></table><p><p>
1754 <!-- }}} ENDPRETTY -->
1755
16461756 as one must in older versions of NTL.
16471757 The operator notation leads to somewhat less efficient code,
16481758 and one can always use the old notation in situations
33 A Tour of NTL: Examples: Big Integers </title>
44 </head>
55
6 <body bgcolor="#fff9e6">
76
87 <center>
98 <img src="arrow1.gif" alt="[Previous]" align=bottom>
2625 This program reads two big integers <tt>a</tt> and <tt>b</tt>,
2726 and prints <tt>(a+1)*(b+1)</tt>.
2827
29 <p>
30 <pre>
31 #include &lt;NTL/ZZ.h&gt;
28 <!-- STARTPLAIN
29 #include <NTL/ZZ.h>
3230
3331 using namespace std;
3432 using namespace NTL;
3735 {
3836 ZZ a, b, c;
3937
40 cin &gt;&gt; a;
41 cin &gt;&gt; b;
38 cin >> a;
39 cin >> b;
4240 c = (a+1)*(b+1);
43 cout &lt;&lt; c &lt;&lt; "\n";
41 cout << c << "\n";
4442 }
45 </pre>
46
47 <p>
43 ENDPLAIN -->
44 <!-- STARTPRETTY {{{ -->
45 <p><p><table cellPadding=10px><tr><td><font color="#000000">
46 <font face="monospace">
47 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/ZZ.h&gt;</font><br>
48 <br>
49 using namespace std;<br>
50 using namespace NTL;<br>
51 <br>
52 <font color="#008b00"><b>int</b></font>&nbsp;main()<br>
53 {<br>
54 &nbsp;&nbsp; ZZ a, b, c;&nbsp;<br>
55 <br>
56 &nbsp;&nbsp; cin &gt;&gt; a;&nbsp;<br>
57 &nbsp;&nbsp; cin &gt;&gt; b;&nbsp;<br>
58 &nbsp;&nbsp; c = (a+<font color="#ff8c00">1</font>)*(b+<font color="#ff8c00">1</font>);<br>
59 &nbsp;&nbsp; cout &lt;&lt; c &lt;&lt;&nbsp;<font color="#4a708b">&quot;</font><font color="#8a2be2">\n</font><font color="#4a708b">&quot;</font>;<br>
60 }<br>
61 </font>
62 </font></td></tr></table><p><p>
63 <!-- }}} ENDPRETTY -->
64
65
4866
4967 This program declares three variables <tt>a</tt>, <tt>b</tt>,
5068 and <tt>c</tt> of type <tt>ZZ</tt>.
7795 Here's a program that reads a list of integers from standard
7896 input and prints the sum of their squares.
7997
80 <p>
81 <pre>
82 #include &lt;NTL/ZZ.h&gt;
98 <!-- STARTPLAIN
99 #include <NTL/ZZ.h>
83100
84101
85102 using namespace std;
92109
93110 acc = 0;
94111 while (SkipWhiteSpace(cin)) {
95 cin &gt;&gt; val;
112 cin >> val;
96113 acc += val*val;
97114 }
98115
99 cout &lt;&lt; acc &lt;&lt; "\n";
116 cout << acc << "\n";
100117 }
101 </pre>
102
103 <p>
118 ENDPLAIN -->
119 <!-- STARTPRETTY {{{ -->
120 <p><p><table cellPadding=10px><tr><td><font color="#000000">
121 <font face="monospace">
122 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/ZZ.h&gt;</font><br>
123 <br>
124 <br>
125 using namespace std;<br>
126 using namespace NTL;<br>
127 <br>
128 <br>
129 <font color="#008b00"><b>int</b></font>&nbsp;main()<br>
130 {<br>
131 &nbsp;&nbsp; ZZ acc, val;<br>
132 <br>
133 &nbsp;&nbsp; acc =&nbsp;<font color="#ff8c00">0</font>;<br>
134 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>while</b></font>&nbsp;(SkipWhiteSpace(cin)) {<br>
135 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin &gt;&gt; val;<br>
136 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc += val*val;<br>
137 &nbsp;&nbsp; }<br>
138 <br>
139 &nbsp;&nbsp; cout &lt;&lt; acc &lt;&lt;&nbsp;<font color="#4a708b">&quot;</font><font color="#8a2be2">\n</font><font color="#4a708b">&quot;</font>;<br>
140 }<br>
141 </font>
142 </font></td></tr></table><p><p>
143 <!-- }}} ENDPRETTY -->
144
145
104146
105147 The function <tt>SkipWhiteSpace</tt> is defined by NTL.
106148 It skips over white space, and returns 1 if there is something
126168 <tt>a^e mod n</tt>.
127169 NTL already provides a more sophisticated one, though.
128170
129 <p>
130 <pre>
131 ZZ PowerMod(const ZZ&amp; a, const ZZ&amp; e, const ZZ&amp; n)
171 <!-- STARTPLAIN
172 ZZ PowerMod(const ZZ& a, const ZZ& e, const ZZ& n)
132173 {
133174 if (e == 0) return ZZ(1);
134175
137178 ZZ res;
138179 res = 1;
139180
140 for (long i = k-1; i &gt;= 0; i--) {
181 for (long i = k-1; i >= 0; i~~) {
141182 res = (res*res) % n;
142183 if (bit(e, i) == 1) res = (res*a) % n;
143184 }
144185
145 if (e &lt; 0)
186 if (e < 0)
146187 return InvMod(res, n);
147188 else
148189 return res;
149190 }
150 </pre>
151 <p>
191 ENDPLAIN -->
192 <!-- STARTPRETTY {{{ -->
193 <p><p><table cellPadding=10px><tr><td><font color="#000000">
194 <font face="monospace">
195 ZZ PowerMod(<font color="#008b00"><b>const</b></font>&nbsp;ZZ&amp; a,&nbsp;<font color="#008b00"><b>const</b></font>&nbsp;ZZ&amp; e,&nbsp;<font color="#008b00"><b>const</b></font>&nbsp;ZZ&amp; n)<br>
196 {<br>
197 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(e ==&nbsp;<font color="#ff8c00">0</font>)&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;ZZ(<font color="#ff8c00">1</font>);<br>
198 <br>
199 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;k = NumBits(e);<br>
200 <br>
201 &nbsp;&nbsp; ZZ res;<br>
202 &nbsp;&nbsp; res =&nbsp;<font color="#ff8c00">1</font>;<br>
203 <br>
204 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>for</b></font>&nbsp;(<font color="#008b00"><b>long</b></font>&nbsp;i = k-<font color="#ff8c00">1</font>; i &gt;=&nbsp;<font color="#ff8c00">0</font>; i--) {<br>
205 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res = (res*res) % n;<br>
206 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(bit(e, i) ==&nbsp;<font color="#ff8c00">1</font>) res = (res*a) % n;<br>
207 &nbsp;&nbsp; }<br>
208 <br>
209 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(e &lt;&nbsp;<font color="#ff8c00">0</font>)<br>
210 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;InvMod(res, n);<br>
211 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>else</b></font><br>
212 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;res;<br>
213 }<br>
214 </font>
215 </font></td></tr></table><p><p>
216 <!-- }}} ENDPRETTY -->
217
152218
153219 Note that as an alternative, we could implement the inner loop
154220 as follows:
155221
156 <pre>
222 <!-- STARTPLAIN
157223 res = SqrMod(res, n);
158224 if (bit(e, i) == 1) res = MulMod(res, a, n);
159 </pre>
225 ENDPLAIN -->
226 <!-- STARTPRETTY {{{ -->
227 <p><p><table cellPadding=10px><tr><td><font color="#000000">
228 <font face="monospace">
229 &nbsp;&nbsp; res = SqrMod(res, n);<br>
230 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(bit(e, i) ==&nbsp;<font color="#ff8c00">1</font>) res = MulMod(res, a, n);<br>
231 </font>
232 </font></td></tr></table><p><p>
233 <!-- }}} ENDPRETTY -->
234
160235
161236 We could also write this as:
162237
163 <pre>
238 <!-- STARTPLAIN
164239 SqrMod(res, res, n);
165240 if (bit(e, i) == 1) MulMod(res, res, a, n);
166 </pre>
241 ENDPLAIN -->
242 <!-- STARTPRETTY {{{ -->
243 <p><p><table cellPadding=10px><tr><td><font color="#000000">
244 <font face="monospace">
245 &nbsp;&nbsp; SqrMod(res, res, n);<br>
246 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(bit(e, i) ==&nbsp;<font color="#ff8c00">1</font>) MulMod(res, res, a, n);<br>
247 </font>
248 </font></td></tr></table><p><p>
249 <!-- }}} ENDPRETTY -->
250
167251
168252 This illustrates an important point about NTL's programming interface.
169253 For every function in NTL, there is a procedural version that
192276 While we are taking about temporaries, consider the first version
193277 of the inner loop.
194278 Execution of the statement
195 <pre>
279 <!-- STARTPLAIN
196280 res = (res*res) % n;
197 </pre>
281 ENDPLAIN -->
282 <!-- STARTPRETTY {{{ -->
283 <p><p><table cellPadding=10px><tr><td><font color="#000000">
284 <font face="monospace">
285 &nbsp;&nbsp; res = (res*res) % n;<br>
286 </font>
287 </font></td></tr></table><p><p>
288 <!-- }}} ENDPRETTY -->
289
198290 will result in the creation of two temporary objects,
199291 one for the product, and one for the result of the mod operation,
200292 whose value is copied into <tt>res</tt>.
211303 Note that NTL already provides a slightly more sophisticated
212304 primality test.
213305
214 <p>
215 <pre>
216 #include &lt;NTL/ZZ.h&gt;
306 <!-- STARTPLAIN
307 #include <NTL/ZZ.h>
217308
218309 using namespace std;
219310 using namespace NTL;
220311
221 long witness(const ZZ&amp; n, const ZZ&amp; x)
312 long witness(const ZZ& n, const ZZ& x)
222313 {
223314 ZZ m, y, z;
224315 long j, k;
242333 y = z;
243334 z = (y*y) % n;
244335 j++;
245 } while (j &lt; k &amp;&amp; z != 1);
336 } while (j < k && z != 1);
246337
247338 return z != 1 || y != n-1;
248339 }
249340
250341
251 long PrimeTest(const ZZ&amp; n, long t)
342 long PrimeTest(const ZZ& n, long t)
252343 {
253 if (n &lt;= 1) return 0;
344 if (n <= 1) return 0;
254345
255346 // first, perform trial division by primes up to 2000
256347
268359 ZZ x;
269360 long i;
270361
271 for (i = 0; i &lt; t; i++) {
362 for (i = 0; i < t; i++) {
272363 x = RandomBnd(n); // random number between 0 and n-1
273364
274365 if (witness(n, x))
282373 {
283374 ZZ n;
284375
285 cout &lt;&lt; "n: ";
286 cin &gt;&gt; n;
376 cout << "n: ";
377 cin >> n;
287378
288379 if (PrimeTest(n, 10))
289 cout &lt;&lt; n &lt;&lt; " is probably prime\n";
380 cout << n << " is probably prime\n";
290381 else
291 cout &lt;&lt; n &lt;&lt; " is composite\n";
382 cout << n << " is composite\n";
292383 }
293 </pre>
294 <p>
384 ENDPLAIN -->
385 <!-- STARTPRETTY {{{ -->
386 <p><p><table cellPadding=10px><tr><td><font color="#000000">
387 <font face="monospace">
388 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/ZZ.h&gt;</font><br>
389 <br>
390 <font color="#b03060"><b>using</b></font>&nbsp;<font color="#008b00"><b>namespace</b></font>&nbsp;std;<br>
391 <font color="#b03060"><b>using</b></font>&nbsp;<font color="#008b00"><b>namespace</b></font>&nbsp;NTL;<br>
392 <br>
393 <font color="#008b00"><b>long</b></font>&nbsp;witness(<font color="#008b00"><b>const</b></font>&nbsp;ZZ&amp; n,&nbsp;<font color="#008b00"><b>const</b></font>&nbsp;ZZ&amp; x)<br>
394 {<br>
395 &nbsp;&nbsp; ZZ m, y, z;<br>
396 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;j, k;<br>
397 <br>
398 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(x ==&nbsp;<font color="#ff8c00">0</font>)&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;<font color="#ff8c00">0</font>;<br>
399 <br>
400 &nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// compute m, k such that n-1 = 2^k * m, m odd:</i></font><br>
401 <br>
402 &nbsp;&nbsp; k =&nbsp;<font color="#ff8c00">1</font>;<br>
403 &nbsp;&nbsp; m = n/<font color="#ff8c00">2</font>;<br>
404 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>while</b></font>&nbsp;(m %&nbsp;<font color="#ff8c00">2</font>&nbsp;==&nbsp;<font color="#ff8c00">0</font>) {<br>
405 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k++;<br>
406 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m /=&nbsp;<font color="#ff8c00">2</font>;<br>
407 &nbsp;&nbsp; }<br>
408 <br>
409 &nbsp;&nbsp; z = PowerMod(x, m, n);&nbsp;<font color="#0000ee"><i>// z = x^m % n</i></font><br>
410 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(z ==&nbsp;<font color="#ff8c00">1</font>)&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;<font color="#ff8c00">0</font>;<br>
411 <br>
412 &nbsp;&nbsp; j =&nbsp;<font color="#ff8c00">0</font>;<br>
413 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>do</b></font>&nbsp;{<br>
414 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y = z;<br>
415 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;z = (y*y) % n;&nbsp;<br>
416 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j++;<br>
417 &nbsp;&nbsp; }&nbsp;<font color="#b03060"><b>while</b></font>&nbsp;(j &lt; k &amp;&amp; z !=&nbsp;<font color="#ff8c00">1</font>);<br>
418 <br>
419 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;z !=&nbsp;<font color="#ff8c00">1</font>&nbsp;|| y != n-<font color="#ff8c00">1</font>;<br>
420 }<br>
421 <br>
422 <br>
423 <font color="#008b00"><b>long</b></font>&nbsp;PrimeTest(<font color="#008b00"><b>const</b></font>&nbsp;ZZ&amp; n,&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;t)<br>
424 {<br>
425 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(n &lt;=&nbsp;<font color="#ff8c00">1</font>)&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;<font color="#ff8c00">0</font>;<br>
426 <br>
427 &nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// first, perform trial division by primes up to 2000</i></font><br>
428 <br>
429 &nbsp;&nbsp; PrimeSeq s;&nbsp;&nbsp;<font color="#0000ee"><i>// a class for quickly generating primes in sequence</i></font><br>
430 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;p;<br>
431 <br>
432 &nbsp;&nbsp; p = s.next();&nbsp;&nbsp;<font color="#0000ee"><i>// first prime is always 2</i></font><br>
433 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>while</b></font>&nbsp;(p &amp;&amp; p &lt;&nbsp;<font color="#ff8c00">2000</font>) {<br>
434 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;((n % p) ==&nbsp;<font color="#ff8c00">0</font>)&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;(n == p);<br>
435 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p = s.next();&nbsp;&nbsp;<br>
436 &nbsp;&nbsp; }<br>
437 <br>
438 &nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// second, perform t Miller-Rabin tests</i></font><br>
439 <br>
440 &nbsp;&nbsp; ZZ x;<br>
441 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;i;<br>
442 <br>
443 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>for</b></font>&nbsp;(i =&nbsp;<font color="#ff8c00">0</font>; i &lt; t; i++) {<br>
444 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = RandomBnd(n);&nbsp;<font color="#0000ee"><i>// random number between 0 and n-1</i></font><br>
445 <br>
446 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(witness(n, x))&nbsp;<br>
447 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;<font color="#ff8c00">0</font>;<br>
448 &nbsp;&nbsp; }<br>
449 <br>
450 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;<font color="#ff8c00">1</font>;<br>
451 }<br>
452 <br>
453 <font color="#008b00"><b>int</b></font>&nbsp;main()<br>
454 {<br>
455 &nbsp;&nbsp; ZZ n;<br>
456 <br>
457 &nbsp;&nbsp; cout &lt;&lt;&nbsp;<font color="#4a708b">&quot;n: &quot;</font>;<br>
458 &nbsp;&nbsp; cin &gt;&gt; n;<br>
459 <br>
460 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(PrimeTest(n,&nbsp;<font color="#ff8c00">10</font>))<br>
461 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; n &lt;&lt;&nbsp;<font color="#4a708b">&quot; is probably prime</font><font color="#8a2be2">\n</font><font color="#4a708b">&quot;</font>;<br>
462 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>else</b></font><br>
463 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; n &lt;&lt;&nbsp;<font color="#4a708b">&quot; is composite</font><font color="#8a2be2">\n</font><font color="#4a708b">&quot;</font>;<br>
464 }<br>
465 </font>
466 </font></td></tr></table><p><p>
467 <!-- }}} ENDPRETTY -->
468
295469
296470 Note that in NTL, there are typically a number of ways to
297471 compute the same thing.
299473 in function <tt>witness</tt>.
300474 We could have written it thusly:
301475
302 <pre>
476 <!-- STARTPLAIN
303477 k = 1;
304 m = n &gt;&gt; 1;
478 m = n >> 1;
305479 while (!IsOdd(m)) {
306480 k++;
307 m &gt;&gt;= 1;
481 m >>= 1;
308482 }
309 </pre>
483 ENDPLAIN -->
484 <!-- STARTPRETTY {{{ -->
485 <p><p><table cellPadding=10px><tr><td><font color="#000000">
486 <font face="monospace">
487 &nbsp;&nbsp; k =&nbsp;<font color="#ff8c00">1</font>;<br>
488 &nbsp;&nbsp; m = n &gt;&gt;&nbsp;<font color="#ff8c00">1</font>;<br>
489 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>while</b></font>&nbsp;(!IsOdd(m)) {<br>
490 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k++;<br>
491 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m &gt;&gt;=&nbsp;<font color="#ff8c00">1</font>;<br>
492 &nbsp;&nbsp; }<br>
493 </font>
494 </font></td></tr></table><p><p>
495 <!-- }}} ENDPRETTY -->
496
310497
311498 It turns out that this is actually not significantly more
312499 efficient than the original version, because the implementation
316503
317504 The following is more efficient:
318505
319 <pre>
506 <!-- STARTPLAIN
320507 k = 1;
321508 while (bit(n, k) == 0) k++;
322 m = n &gt;&gt; k;
323 </pre>
509 m = n >> k;
510 ENDPLAIN -->
511 <!-- STARTPRETTY {{{ -->
512 <p><p><table cellPadding=10px><tr><td><font color="#000000">
513 <font face="monospace">
514 &nbsp;&nbsp; k =&nbsp;<font color="#ff8c00">1</font>;<br>
515 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>while</b></font>&nbsp;(bit(n, k) ==&nbsp;<font color="#ff8c00">0</font>) k++;<br>
516 &nbsp;&nbsp; m = n &gt;&gt; k;<br>
517 </font>
518 </font></td></tr></table><p><p>
519 <!-- }}} ENDPRETTY -->
520
324521
325522 As it happens, there is a built-in NTL routine that does just what we want:
326523
327 <pre>
524 <!-- STARTPLAIN
328525 m = n-1;
329526 k = MakeOdd(m);
330 </pre>
527 ENDPLAIN -->
528 <!-- STARTPRETTY {{{ -->
529 <p><p><table cellPadding=10px><tr><td><font color="#000000">
530 <font face="monospace">
531 &nbsp;&nbsp; m = n-<font color="#ff8c00">1</font>;<br>
532 &nbsp;&nbsp; k = MakeOdd(m);<br>
533 </font>
534 </font></td></tr></table><p><p>
535 <!-- }}} ENDPRETTY -->
536
331537
332538
333539
356562
357563 <p>
358564 One can also assign a value of type <tt>long</tt> to a <tt>ZZ</tt>:
359 <pre>
565 <!-- STARTPLAIN
360566 ZZ x;
361567 x = 1;
362 </pre>
568 ENDPLAIN -->
569 <!-- STARTPRETTY {{{ -->
570 <p><p><table cellPadding=10px><tr><td><font color="#000000">
571 <font face="monospace">
572 &nbsp;&nbsp; ZZ x;<br>
573 &nbsp;&nbsp; x =&nbsp;<font color="#ff8c00">1</font>;<br>
574 </font>
575 </font></td></tr></table><p><p>
576 <!-- }}} ENDPRETTY -->
577
363578
364579 <p>
365580 Note that one cannot write
366 <pre>
581 <!-- STARTPLAIN
367582 ZZ x = 1; // error
368 </pre>
583 ENDPLAIN -->
584 <!-- STARTPRETTY {{{ -->
585 <p><p><table cellPadding=10px><tr><td><font color="#000000">
586 <font face="monospace">
587 &nbsp;&nbsp; ZZ x =&nbsp;<font color="#ff8c00">1</font>;&nbsp;&nbsp;<font color="#0000ee"><i>// error</i></font><br>
588 </font>
589 </font></td></tr></table><p><p>
590 <!-- }}} ENDPRETTY -->
591
369592 to initialize a <tt>ZZ</tt>.
370593 Instead, one could write
371 <pre>
594 <!-- STARTPLAIN
372595 ZZ x = ZZ(1);
373596 ZZ y(1);
374597 ZZ z{1}; // C++11 only
375 </pre>
598 ENDPLAIN -->
599 <!-- STARTPRETTY {{{ -->
600 <p><p><table cellPadding=10px><tr><td><font color="#000000">
601 <font face="monospace">
602 &nbsp;&nbsp; ZZ x = ZZ(<font color="#ff8c00">1</font>);<br>
603 &nbsp;&nbsp; ZZ y(<font color="#ff8c00">1</font>);<br>
604 &nbsp;&nbsp; ZZ z{<font color="#ff8c00">1</font>};&nbsp;<font color="#0000ee"><i>// C++11 only</i></font><br>
605 </font>
606 </font></td></tr></table><p><p>
607 <!-- }}} ENDPRETTY -->
608
376609 Using the comstructor that allows one to <i>explicitly</i>
377610 construct a <tt>ZZ</tt> from a <tt>long</tt>.
378611
379612 <p>
380613 Alternatively, one could write this as:
381 <pre>
382 ZZ x = conv&lt;ZZ&gt;(1);
383 </pre>
614 <!-- STARTPLAIN
615 ZZ x = conv<ZZ>(1);
616 ENDPLAIN -->
617 <!-- STARTPRETTY {{{ -->
618 <p><p><table cellPadding=10px><tr><td><font color="#000000">
619 <font face="monospace">
620 &nbsp;&nbsp; ZZ x = conv&lt;ZZ&gt;(<font color="#ff8c00">1</font>);<br>
621 </font>
622 </font></td></tr></table><p><p>
623 <!-- }}} ENDPRETTY -->
624
384625 This is an example of one of NTL's conversion routines.
385626 For very large constants, one can write:
386 <pre>
387 ZZ x = conv&lt;ZZ&gt;("99999999999999999999");
388 </pre>
627 <!-- STARTPLAIN
628 ZZ x = conv<ZZ>("99999999999999999999");
629 ENDPLAIN -->
630 <!-- STARTPRETTY {{{ -->
631 <p><p><table cellPadding=10px><tr><td><font color="#000000">
632 <font face="monospace">
633 &nbsp;&nbsp; ZZ x = conv&lt;ZZ&gt;(<font color="#4a708b">&quot;99999999999999999999&quot;</font>);<br>
634 </font>
635 </font></td></tr></table><p><p>
636 <!-- }}} ENDPRETTY -->
637
389638 These examples illustrate conversion rountines in their
390639 functional forms.
391 <pre>
640 <!-- STARTPLAIN
392641 ZZ x;
393642 conv(x, 1);
394643 conv(x, "99999999999999999999");
395 </pre>
644 ENDPLAIN -->
645 <!-- STARTPRETTY {{{ -->
646 <p><p><table cellPadding=10px><tr><td><font color="#000000">
647 <font face="monospace">
648 &nbsp;&nbsp; ZZ x;<br>
649 &nbsp;&nbsp; conv(x,&nbsp;<font color="#ff8c00">1</font>);<br>
650 &nbsp;&nbsp; conv(x,&nbsp;<font color="#4a708b">&quot;99999999999999999999&quot;</font>);<br>
651 </font>
652 </font></td></tr></table><p><p>
653 <!-- }}} ENDPRETTY -->
654
396655
397656 <p>
398657 <b>
466725
467726 <p>
468727 There are other functions as well.
469 See <a href="ZZ.txt"><tt>ZZ.txt</tt></a> for complete details.
470 Also see <a href="tools.txt"><tt>tools.txt</tt></a> for some basic
728 See <a href="ZZ.cpp.html"><tt>ZZ.txt</tt></a> for complete details.
729 Also see <a href="tools.cpp.html"><tt>tools.txt</tt></a> for some basic
471730 services provided by NTL.
472731
473732
33 A Tour of NTL: Examples: Vectors and Matrices </title>
44 </head>
55
6 <body bgcolor="#fff9e6">
76 <center>
87 <a href="tour-ex1.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
98 <a href="tour-examples.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a>
2423 The following routine sums up the
2524 numbers in a vector of <tt>ZZ</tt>'s.
2625
27 <pre>
28 #include &lt;NTL/ZZ.h&gt;
29 #include &lt;NTL/vector.h&gt;
26 <!-- STARTPLAIN
27 #include <NTL/ZZ.h>
28 #include <NTL/vector.h>
3029
3130 using namespace std;
3231 using namespace NTL;
3332
34 ZZ sum(const Vec&lt;ZZ&gt;&amp; v)
33 ZZ sum(const Vec<ZZ>& v)
3534 {
3635 ZZ acc;
3736
3837 acc = 0;
3938
40 for (long i = 0; i &lt; v.length(); i++)
39 for (long i = 0; i < v.length(); i++)
4140 acc += v[i];
4241
4342 return acc;
4443 }
45 </pre>
44 ENDPLAIN -->
45 <!-- STARTPRETTY {{{ -->
46 <p><p><table cellPadding=10px><tr><td><font color="#000000">
47 <font face="monospace">
48 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/ZZ.h&gt;</font><br>
49 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/vector.h&gt;</font><br>
50 <br>
51 using namespace std;<br>
52 using namespace NTL;<br>
53 <br>
54 ZZ sum(<font color="#008b00"><b>const</b></font>&nbsp;Vec&lt;ZZ&gt;&amp; v)<br>
55 {<br>
56 &nbsp;&nbsp; ZZ acc;<br>
57 <br>
58 &nbsp;&nbsp; acc =&nbsp;<font color="#ff8c00">0</font>;<br>
59 <br>
60 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>for</b></font>&nbsp;(<font color="#008b00"><b>long</b></font>&nbsp;i =&nbsp;<font color="#ff8c00">0</font>; i &lt; v.length(); i++)<br>
61 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc += v[i];<br>
62 <br>
63 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;acc;<br>
64 }<br>
65 </font>
66 </font></td></tr></table><p><p>
67 <!-- }}} ENDPRETTY -->
68
4669
4770 <p>
4871 The class <tt>Vec&lt;ZZ&gt;</tt> is a dynamic-length array of <tt>ZZ</tt>s;
6790 The generic vector class allows for this;
6891 the above example could be written as follows.
6992
70 <pre>
71 #include &lt;NTL/ZZ.h&gt;
72 #include &lt;NTL/vector.h&gt;
93 <!-- STARTPLAIN
94 #include <NTL/ZZ.h>
95 #include <NTL/vector.h>
7396
7497 using namespace std;
7598 using namespace NTL;
7699
77 ZZ sum(ZZ&amp; s, const Vec&lt;ZZ&gt;&amp; v)
100 ZZ sum(ZZ& s, const Vec<ZZ>& v)
78101 {
79102 ZZ acc;
80103
81104 acc = 0;
82105
83 for (long i = 1; i &lt;= v.length(); i++)
106 for (long i = 1; i <= v.length(); i++)
84107 acc += v(i);
85108
86109 return acc;
87110 }
88 </pre>
111 ENDPLAIN -->
112 <!-- STARTPRETTY {{{ -->
113 <p><p><table cellPadding=10px><tr><td><font color="#000000">
114 <font face="monospace">
115 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/ZZ.h&gt;</font><br>
116 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/vector.h&gt;</font><br>
117 <br>
118 using namespace std;<br>
119 using namespace NTL;<br>
120 <br>
121 ZZ sum(ZZ&amp; s,&nbsp;<font color="#008b00"><b>const</b></font>&nbsp;Vec&lt;ZZ&gt;&amp; v)<br>
122 {<br>
123 &nbsp;&nbsp; ZZ acc;<br>
124 <br>
125 &nbsp;&nbsp; acc =&nbsp;<font color="#ff8c00">0</font>;<br>
126 <br>
127 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>for</b></font>&nbsp;(<font color="#008b00"><b>long</b></font>&nbsp;i =&nbsp;<font color="#ff8c00">1</font>; i &lt;= v.length(); i++)<br>
128 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc += v(i);&nbsp;<br>
129 <br>
130 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;acc;<br>
131 }<br>
132 </font>
133 </font></td></tr></table><p><p>
134 <!-- }}} ENDPRETTY -->
135
89136
90137 <p>
91138 Note that by default, NTL does not perform range checks on
101148 This program reads a <tt>Vec&lt;ZZ&gt;</tt>,
102149 and then creates and prints a "palindrome".
103150
104 <pre>
105 #include &lt;NTL/ZZ.h&gt;
106 #include &lt;NTL/vector.h&gt;
151 <!-- STARTPLAIN
152 #include <NTL/ZZ.h>
153 #include <NTL/vector.h>
107154
108155 using namespace std;
109156 using namespace NTL;
110157
111158 int main()
112159 {
113 Vec&lt;ZZ&gt; v;
114 cin &gt;&gt; v;
160 Vec<ZZ> v;
161 cin >> v;
115162
116163 long n = v.length();
117164 v.SetLength(2*n);
120167 for (i = 0 ; i < n; i++)
121168 v[n+i] = v[n-1-i];
122169
123 cout &lt;&lt; v &lt;&lt "\n";
170 cout << v <&lt "\n";
124171 }
125 </pre>
172 ENDPLAIN -->
173 <!-- STARTPRETTY {{{ -->
174 <p><p><table cellPadding=10px><tr><td><font color="#000000">
175 <font face="monospace">
176 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/ZZ.h&gt;</font><br>
177 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/vector.h&gt;</font><br>
178 <br>
179 using namespace std;<br>
180 using namespace NTL;<br>
181 <br>
182 <font color="#008b00"><b>int</b></font>&nbsp;main()<br>
183 {<br>
184 &nbsp;&nbsp; Vec&lt;ZZ&gt; v;<br>
185 &nbsp;&nbsp; cin &gt;&gt; v;<br>
186 <br>
187 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;n = v.length();<br>
188 &nbsp;&nbsp; v.SetLength(<font color="#ff8c00">2</font>*n);<br>
189 <br>
190 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;i;<br>
191 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>for</b></font>&nbsp;(i =&nbsp;<font color="#ff8c00">0</font>&nbsp;; i &lt; n; i++)<br>
192 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v[n+i] = v[n-<font color="#ff8c00">1</font>-i];<br>
193 <br>
194 &nbsp;&nbsp; cout &lt;&lt; v &lt;&amp;lt&nbsp;<font color="#4a708b">&quot;</font><font color="#8a2be2">\n</font><font color="#4a708b">&quot;</font>;<br>
195 }<br>
196 </font>
197 </font></td></tr></table><p><p>
198 <!-- }}} ENDPRETTY -->
199
126200
127201 <p>
128202
143217
144218 <p>
145219
146 See <a href="vector.txt"><tt>vector.txt</tt></a> for
220 See <a href="vector.cpp.html"><tt>vector.txt</tt></a> for
147221 complete details of NTL's generic vector mechanism.
148222
149223 Also note that for several fundamental vector types, such as
152226 a number of basic arithmetic operations,
153227 as well as provides the typedef
154228 typedef <tt>vec_ZZ</tt> for backward compatibilty.
155 See <a href="vec_ZZ.txt"><tt>vec_ZZ.txt</tt></a> for
229 See <a href="vec_ZZ.cpp.html"><tt>vec_ZZ.txt</tt></a> for
156230 complete details on the arithmetic operations for <tt>Vec&lt;ZZ&gt;</tt>'s
157231 provided by NTL.
158232
176250 Here is a matrix multiplication routine,
177251 which in fact is already provided by NTL.
178252
179 <pre>
180 #include &lt;NTL/ZZ.h&gt;
181 #include &lt;NTL/matrix.h&gt;
253 <!-- STARTPLAIN
254 #include <NTL/ZZ.h>
255 #include <NTL/matrix.h>
182256
183257 using namespace std;
184258 using namespace NTL;
185259
186 void mul(Mat&lt;ZZ&gt;&amp; X, const Mat&lt;ZZ&gt;&amp; A, const Mat&lt;ZZ&gt;&amp; B)
260 void mul(Mat<ZZ>& X, const Mat<ZZ>& A, const Mat<ZZ>& B)
187261 {
188262 long n = A.NumRows();
189263 long l = A.NumCols();
197271 long i, j, k;
198272 ZZ acc, tmp;
199273
200 for (i = 1; i &lt;= n; i++) {
201 for (j = 1; j &lt;= m; j++) {
274 for (i = 1; i <= n; i++) {
275 for (j = 1; j <= m; j++) {
202276 acc = 0;
203 for(k = 1; k &lt;= l; k++) {
277 for(k = 1; k <= l; k++) {
204278 mul(tmp, A(i,k), B(k,j));
205279 add(acc, acc, tmp);
206280 }
208282 }
209283 }
210284 }
211 </pre>
285 ENDPLAIN -->
286 <!-- STARTPRETTY {{{ -->
287 <p><p><table cellPadding=10px><tr><td><font color="#000000">
288 <font face="monospace">
289 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/ZZ.h&gt;</font><br>
290 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/matrix.h&gt;</font><br>
291 <br>
292 using namespace std;<br>
293 using namespace NTL;<br>
294 <br>
295 <font color="#008b00"><b>void</b></font>&nbsp;mul(Mat&lt;ZZ&gt;&amp; X,&nbsp;<font color="#008b00"><b>const</b></font>&nbsp;Mat&lt;ZZ&gt;&amp; A,&nbsp;<font color="#008b00"><b>const</b></font>&nbsp;Mat&lt;ZZ&gt;&amp; B)<br>
296 {<br>
297 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;n = A.NumRows();<br>
298 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;l = A.NumCols();<br>
299 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;m = B.NumCols();<br>
300 <br>
301 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(l != B.NumRows())<br>
302 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Error(<font color="#4a708b">&quot;matrix mul: dimension mismatch&quot;</font>);<br>
303 <br>
304 &nbsp;&nbsp; X.SetDims(n, m);&nbsp;<font color="#0000ee"><i>// make X have n rows and m columns</i></font><br>
305 <br>
306 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;i, j, k;<br>
307 &nbsp;&nbsp; ZZ acc, tmp;<br>
308 <br>
309 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>for</b></font>&nbsp;(i =&nbsp;<font color="#ff8c00">1</font>; i &lt;= n; i++) {<br>
310 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>for</b></font>&nbsp;(j =&nbsp;<font color="#ff8c00">1</font>; j &lt;= m; j++) {<br>
311 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; acc =&nbsp;<font color="#ff8c00">0</font>;<br>
312 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>for</b></font>(k =&nbsp;<font color="#ff8c00">1</font>; k &lt;= l; k++) {<br>
313 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mul(tmp, A(i,k), B(k,j));<br>
314 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add(acc, acc, tmp);<br>
315 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
316 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; X(i,j) = acc;<br>
317 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
318 &nbsp;&nbsp; }<br>
319 }<br>
320 </font>
321 </font></td></tr></table><p><p>
322 <!-- }}} ENDPRETTY -->
323
212324
213325 <p>
214326 In case of a dimension mismatch, the routine calls the
235347
236348 <p>
237349 NTL provides several matrix types.
238 See <a href="matrix.txt"><tt>matrix.txt</tt></a>
350 See <a href="matrix.cpp.html"><tt>matrix.txt</tt></a>
239351 for complete details on NTL's generic matrix mechanism.
240 Also see <a href="mat_ZZ.txt"><tt>mat_ZZ.txt</tt></a> for
352 Also see <a href="mat_ZZ.cpp.html"><tt>mat_ZZ.txt</tt></a> for
241353 complete details on the arithmetic operations for <tt>Mat&lt;ZZ&gt;</tt>'s
242354 provideed by NTL (including basic linear algebra).
243 Also see <a href="LLL.txt"><tt>LLL.txt</tt></a>
355 Also see <a href="LLL.cpp.html"><tt>LLL.txt</tt></a>
244356 for details on routines for lattice basis reduction
245357 (as well as routines for finding the kernel and image of a matrix).
246358
33 A Tour of NTL: Examples: Polynomials </title>
44 </head>
55
6 <body bgcolor="#fff9e6">
76
87 <center>
98 <a href="tour-ex2.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
2928 The following program reads a polynomial,
3029 factors it, and prints the factorization.
3130
32 <p>
33 <pre>
34 #include &lt;NTL/ZZXFactoring.h&gt;
31 <!-- STARTPLAIN
32 #include <NTL/ZZXFactoring.h>
3533
3634 using namespace std;
3735 using namespace NTL;
4038 {
4139 ZZX f;
4240
43 cin &gt;&gt; f;
44
45 Vec&lt; Pair&lt; ZZX, long &gt; &gt; factors;
41 cin >> f;
42
43 Vec< Pair< ZZX, long > > factors;
4644 ZZ c;
4745
4846 factor(c, factors, f);
4947
50 cout &lt;&lt; c &lt;&lt; "\n";
51 cout &lt;&lt; factors &lt;&lt; "\n";
48 cout << c << "\n";
49 cout << factors << "\n";
5250 }
53 </pre>
54 <p>
51 ENDPLAIN -->
52 <!-- STARTPRETTY {{{ -->
53 <p><p><table cellPadding=10px><tr><td><font color="#000000">
54 <font face="monospace">
55 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/ZZXFactoring.h&gt;</font><br>
56 <br>
57 using namespace std;<br>
58 using namespace NTL;<br>
59 <br>
60 <font color="#008b00"><b>int</b></font>&nbsp;main()<br>
61 {<br>
62 &nbsp;&nbsp; ZZX f;<br>
63 <br>
64 &nbsp;&nbsp; cin &gt;&gt; f;<br>
65 <br>
66 &nbsp;&nbsp; Vec&lt; Pair&lt; ZZX,&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;&gt; &gt; factors;<br>
67 &nbsp;&nbsp; ZZ c;<br>
68 <br>
69 &nbsp;&nbsp; factor(c, factors, f);<br>
70 <br>
71 &nbsp;&nbsp; cout &lt;&lt; c &lt;&lt;&nbsp;<font color="#4a708b">&quot;</font><font color="#8a2be2">\n</font><font color="#4a708b">&quot;</font>;<br>
72 &nbsp;&nbsp; cout &lt;&lt; factors &lt;&lt;&nbsp;<font color="#4a708b">&quot;</font><font color="#8a2be2">\n</font><font color="#4a708b">&quot;</font>;<br>
73 }<br>
74 </font>
75 </font></td></tr></table><p><p>
76 <!-- }}} ENDPRETTY -->
77
5578
5679 When this program is compiled an run on input
5780
89112 is an NTL vector whose base type is <tt>Pair&lt; ZZX, long &gt;</tt>.
90113 The type <tt>Pair&lt; ZZX, long &gt;</tt> is the instantiation
91114 of a template "pair" type defined by NTL.
92 See <a href="pair.txt"><tt>pair.txt</tt></a> for more details.
115 See <a href="pair.cpp.html"><tt>pair.txt</tt></a> for more details.
93116
94117
95118
98121 Here is another example.
99122 The following program prints out the first 100 cyclotomic polynomials.
100123
101 <pre>
102
103 #include &lt;NTL/ZZX.h&gt;
124 <!-- STARTPLAIN
125
126 #include <NTL/ZZX.h>
104127
105128 using namespace std;
106129 using namespace NTL;
107130
108131 int main()
109132 {
110 Vec&lt;ZZX&gt; phi(INIT_SIZE, 100);
111
112 for (long i = 1; i &lt;= 100; i++) {
133 Vec<ZZX> phi(INIT_SIZE, 100);
134
135 for (long i = 1; i <= 100; i++) {
113136 ZZX t;
114137 t = 1;
115138
116 for (long j = 1; j &lt;= i-1; j++)
139 for (long j = 1; j <= i-1; j++)
117140 if (i % j == 0)
118141 t *= phi(j);
119142
120143 phi(i) = (ZZX(INIT_MONO, i) - 1)/t;
121144
122 cout &lt;&lt; phi(i) &lt;&lt; "\n";
145 cout << phi(i) << "\n";
123146 }
124147 }
125 </pre>
148 ENDPLAIN -->
149 <!-- STARTPRETTY {{{ -->
150 <p><p><table cellPadding=10px><tr><td><font color="#000000">
151 <font face="monospace">
152 <br>
153 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/ZZX.h&gt;</font><br>
154 <br>
155 using namespace std;<br>
156 using namespace NTL;<br>
157 <br>
158 <font color="#008b00"><b>int</b></font>&nbsp;main()<br>
159 {<br>
160 &nbsp;&nbsp; Vec&lt;ZZX&gt; phi(INIT_SIZE,&nbsp;<font color="#ff8c00">100</font>);&nbsp;&nbsp;<br>
161 <br>
162 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>for</b></font>&nbsp;(<font color="#008b00"><b>long</b></font>&nbsp;i =&nbsp;<font color="#ff8c00">1</font>; i &lt;=&nbsp;<font color="#ff8c00">100</font>; i++) {<br>
163 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ZZX t;<br>
164 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t =&nbsp;<font color="#ff8c00">1</font>;<br>
165 <br>
166 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>for</b></font>&nbsp;(<font color="#008b00"><b>long</b></font>&nbsp;j =&nbsp;<font color="#ff8c00">1</font>; j &lt;= i-<font color="#ff8c00">1</font>; j++)<br>
167 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(i % j ==&nbsp;<font color="#ff8c00">0</font>)<br>
168 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t *= phi(j);<br>
169 <br>
170 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;phi(i) = (ZZX(INIT_MONO, i) -&nbsp;<font color="#ff8c00">1</font>)/t;&nbsp;&nbsp;<br>
171 <br>
172 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; phi(i) &lt;&lt;&nbsp;<font color="#4a708b">&quot;</font><font color="#8a2be2">\n</font><font color="#4a708b">&quot;</font>;<br>
173 &nbsp;&nbsp; }<br>
174 }<br>
175 </font>
176 </font></td></tr></table><p><p>
177 <!-- }}} ENDPRETTY -->
178
126179
127180 <p>
128181 To illustrate more of the NTL interface, let's look at alternative ways
130183
131184 <p>
132185 First, instead of
133 <pre>
134 Vec&lt;ZZX&gt; phi(INIT_SIZE, 100);
135 </pre>
186 <!-- STARTPLAIN
187 Vec<ZZX> phi(INIT_SIZE, 100);
188 ENDPLAIN -->
189 <!-- STARTPRETTY {{{ -->
190 <p><p><table cellPadding=10px><tr><td><font color="#000000">
191 <font face="monospace">
192 &nbsp;&nbsp; Vec&lt;ZZX&gt; phi(INIT_SIZE,&nbsp;<font color="#ff8c00">100</font>);&nbsp;&nbsp;<br>
193 </font>
194 </font></td></tr></table><p><p>
195 <!-- }}} ENDPRETTY -->
196
136197 one can write
137 <pre>
138 Vec&lt;ZZX&gt; phi;
198 <!-- STARTPLAIN
199 Vec<ZZX> phi;
139200 phi.SetLength(100);
140 </pre>
201 ENDPLAIN -->
202 <!-- STARTPRETTY {{{ -->
203 <p><p><table cellPadding=10px><tr><td><font color="#000000">
204 <font face="monospace">
205 &nbsp;&nbsp; Vec&lt;ZZX&gt; phi;<br>
206 &nbsp;&nbsp; phi.SetLength(<font color="#ff8c00">100</font>);<br>
207 </font>
208 </font></td></tr></table><p><p>
209 <!-- }}} ENDPRETTY -->
210
141211
142212 <p>
143213 Second,
144214 instead of
145 <pre>
215 <!-- STARTPLAIN
146216 t *= phi(j);
147 </pre>
217 ENDPLAIN -->
218 <!-- STARTPRETTY {{{ -->
219 <p><p><table cellPadding=10px><tr><td><font color="#000000">
220 <font face="monospace">
221 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t *= phi(j);<br>
222 </font>
223 </font></td></tr></table><p><p>
224 <!-- }}} ENDPRETTY -->
225
148226 one can write this as
149 <pre>
227 <!-- STARTPLAIN
150228 mul(t, t, phi(j));
151 </pre>
229 ENDPLAIN -->
230 <!-- STARTPRETTY {{{ -->
231 <p><p><table cellPadding=10px><tr><td><font color="#000000">
232 <font face="monospace">
233 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mul(t, t, phi(j));<br>
234 </font>
235 </font></td></tr></table><p><p>
236 <!-- }}} ENDPRETTY -->
237
152238 or
153 <pre>
239 <!-- STARTPLAIN
154240 t = t * phi(j);
155 </pre>
241 ENDPLAIN -->
242 <!-- STARTPRETTY {{{ -->
243 <p><p><table cellPadding=10px><tr><td><font color="#000000">
244 <font face="monospace">
245 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t = t * phi(j);<br>
246 </font>
247 </font></td></tr></table><p><p>
248 <!-- }}} ENDPRETTY -->
249
156250 Also, one can write <tt>phi[j-1]</tt> in place of <tt>phi(j)</tt>.
157251
158252 <p>
159253 Third, instead of
160 <pre>
254 <!-- STARTPLAIN
161255 phi(i) = (ZZX(INIT_MONO, i) - 1)/t;
162 </pre>
256 ENDPLAIN -->
257 <!-- STARTPRETTY {{{ -->
258 <p><p><table cellPadding=10px><tr><td><font color="#000000">
259 <font face="monospace">
260 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;phi(i) = (ZZX(INIT_MONO, i) -&nbsp;<font color="#ff8c00">1</font>)/t;&nbsp;&nbsp;<br>
261 </font>
262 </font></td></tr></table><p><p>
263 <!-- }}} ENDPRETTY -->
264
163265 one can write
164 <pre>
266 <!-- STARTPLAIN
165267 ZZX t1;
166268 SetCoeff(t1, i);
167269 SetCoeff(t1, 0, -1);
168270 div(phi(i), t1, t);
169 </pre>
271 ENDPLAIN -->
272 <!-- STARTPRETTY {{{ -->
273 <p><p><table cellPadding=10px><tr><td><font color="#000000">
274 <font face="monospace">
275 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ZZX t1;<br>
276 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetCoeff(t1, i);<br>
277 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetCoeff(t1,&nbsp;<font color="#ff8c00">0</font>, -<font color="#ff8c00">1</font>);<br>
278 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;div(phi(i), t1, t);<br>
279 </font>
280 </font></td></tr></table><p><p>
281 <!-- }}} ENDPRETTY -->
282
170283 Alternatively, one could directly access the coefficient vector as follows:
171 <pre>
284 <!-- STARTPLAIN
172285 ZZX t1;
173286 t1.SetLength(i+1); // all vector elements are initialized to zero
174287 t1[i] = 1;
175288 t1[0] = -1;
176289 t1.normalize(); // not necessary here, but good practice in general
177290 div(phi(i), t1, t);
178 </pre>
291 ENDPLAIN -->
292 <!-- STARTPRETTY {{{ -->
293 <p><p><table cellPadding=10px><tr><td><font color="#000000">
294 <font face="monospace">
295 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ZZX t1;<br>
296 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t1.SetLength(i+<font color="#ff8c00">1</font>);&nbsp;<font color="#0000ee"><i>// all vector elements are initialized to zero</i></font><br>
297 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t1[i] =&nbsp;<font color="#ff8c00">1</font>;<br>
298 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t1[<font color="#ff8c00">0</font>] = -<font color="#ff8c00">1</font>;<br>
299 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t1.normalize();&nbsp;&nbsp;<font color="#0000ee"><i>// not necessary here, but good practice in general</i></font><br>
300 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;div(phi(i), t1, t);<br>
301 </font>
302 </font></td></tr></table><p><p>
303 <!-- }}} ENDPRETTY -->
304
179305 Generally, you can freely access the coefficient vector
180306 of a polynomial, as above.
181307 However, after fiddling with this vector, you should "normalize"
187313 You can always use the <tt>SetCoeff</tt> routine above to set or
188314 change coefficients, and you can always read the value of a coefficient
189315 using the routine <tt>coeff</tt>, e.g.,
190 <pre>
316 <!-- STARTPLAIN
191317 ... f[i] == 1 ...
192 </pre>
318 ENDPLAIN -->
319 <!-- STARTPRETTY {{{ -->
320 <p><p><table cellPadding=10px><tr><td><font color="#000000">
321 <font face="monospace">
322 &nbsp;&nbsp; ... f[i] ==&nbsp;<font color="#ff8c00">1</font>&nbsp;...<br>
323 </font>
324 </font></td></tr></table><p><p>
325 <!-- }}} ENDPRETTY -->
326
193327 is equivalent to
194 <pre>
328 <!-- STARTPLAIN
195329 ... coeff(f, i) == 1 ...
196 </pre>
330 ENDPLAIN -->
331 <!-- STARTPRETTY {{{ -->
332 <p><p><table cellPadding=10px><tr><td><font color="#000000">
333 <font face="monospace">
334 &nbsp;&nbsp; ... coeff(f, i) ==&nbsp;<font color="#ff8c00">1</font>&nbsp;...<br>
335 </font>
336 </font></td></tr></table><p><p>
337 <!-- }}} ENDPRETTY -->
338
197339 except that in the latter case, a read-only reference to zero is returned
198340 if the index <tt>i</tt> is out of range.
199341 There are also special-purpose read-only access routines <tt>LeadCoeff(f)</tt>
206348 All of the basic operations support a "promotion logic" similar
207349 to that for <tt>ZZ</tt>, except that inputs of <i>both</i> types
208350 <tt>long</tt> and <tt>ZZ</tt> are promoted to <tt>ZZX</tt>.
209 See <a href="ZZX.txt"><tt>ZZX.txt</tt></a> for details,
210 and see <a href="ZZXFactoring.txt"><tt>ZZXFactoring.txt</tt></a> for details
351 See <a href="ZZX.cpp.html"><tt>ZZX.txt</tt></a> for details,
352 and see <a href="ZZXFactoring.cpp.html"><tt>ZZXFactoring.txt</tt></a> for details
211353 on the polynomial factoring routines.
212354
213355 <p>
33 A Tour of NTL: Examples: Modular Arithmetic </title>
44 </head>
55
6 <body bgcolor="#fff9e6">
76 <center>
87 <a href="tour-ex3.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
98 <a href="tour-examples.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a>
3433 Here is a program that reads a prime number <tt>p</tt>,
3534 and a polynomial <tt>f</tt> modulo <tt>p</tt>, and factors it.
3635
37 <p>
38 <pre>
39 #include &lt;NTL/ZZ_pXFactoring.h&gt;
36 <!-- STARTPLAIN
37 #include <NTL/ZZ_pXFactoring.h>
4038
4139 using namespace std;
4240 using namespace NTL;
4442 int main()
4543 {
4644 ZZ p;
47 cin &gt;&gt; p;
45 cin >> p;
4846 ZZ_p::init(p);
4947
5048 ZZ_pX f;
51 cin &gt;&gt; f;
52
53 Vec&lt; Pair&lt; ZZ_pX, long &gt; &gt; factors;
49 cin >> f;
50
51 Vec< Pair< ZZ_pX, long > > factors;
5452
5553 CanZass(factors, f); // calls "Cantor/Zassenhaus" algorithm
5654
57 cout &lt;&lt; factors &lt;&lt; "\n";
55 cout << factors << "\n";
5856
5957 }
60 </pre>
58 ENDPLAIN -->
59 <!-- STARTPRETTY {{{ -->
60 <p><p><table cellPadding=10px><tr><td><font color="#000000">
61 <font face="monospace">
62 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/ZZ_pXFactoring.h&gt;</font><br>
63 <br>
64 using namespace std;<br>
65 using namespace NTL;<br>
66 <br>
67 <font color="#008b00"><b>int</b></font>&nbsp;main()<br>
68 {<br>
69 &nbsp;&nbsp; ZZ p;<br>
70 &nbsp;&nbsp; cin &gt;&gt; p;<br>
71 &nbsp;&nbsp; ZZ_p::init(p);<br>
72 <br>
73 &nbsp;&nbsp; ZZ_pX f;<br>
74 &nbsp;&nbsp; cin &gt;&gt; f;<br>
75 <br>
76 &nbsp;&nbsp; Vec&lt; Pair&lt; ZZ_pX,&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;&gt; &gt; factors;<br>
77 <br>
78 &nbsp;&nbsp; CanZass(factors, f);&nbsp;&nbsp;<font color="#0000ee"><i>// calls &quot;Cantor/Zassenhaus&quot; algorithm</i></font><br>
79 <br>
80 &nbsp;&nbsp; cout &lt;&lt; factors &lt;&lt;&nbsp;<font color="#4a708b">&quot;</font><font color="#8a2be2">\n</font><font color="#4a708b">&quot;</font>;<br>
81 &nbsp;&nbsp;&nbsp;&nbsp;<br>
82 }<br>
83 </font>
84 </font></td></tr></table><p><p>
85 <!-- }}} ENDPRETTY -->
86
6187
6288 <p>
6389 As a program is running, NTL keeps track of a "current modulus"
81107 classes.
82108 The first is a vector addition routine (already supplied by NTL):
83109
84 <p>
85 <pre>
86 #include &lt;NTL/ZZ_p.h&gt;
110 <!-- STARTPLAIN
111 #include <NTL/ZZ_p.h>
87112
88113 using namespace std;
89114 using namespace NTL;
90115
91 void add(Vec&lt;ZZ_p&gt;&amp; x, const Vec&lt;ZZ_p&gt;&amp; a, const Vec&lt;ZZ_p&gt;&amp; b)
116 void add(Vec<ZZ_p>& x, const Vec<ZZ_p>& a, const Vec<ZZ_p>& b)
92117 {
93118 long n = a.length();
94119 if (b.length() != n) Error("vector add: dimension mismatch");
98123 for (i = 0; i < n; i++)
99124 add(x[i], a[i], b[i]);
100125 }
101 </pre>
126 ENDPLAIN -->
127 <!-- STARTPRETTY {{{ -->
128 <p><p><table cellPadding=10px><tr><td><font color="#000000">
129 <font face="monospace">
130 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/ZZ_p.h&gt;</font><br>
131 <br>
132 using namespace std;<br>
133 using namespace NTL;<br>
134 <br>
135 <font color="#008b00"><b>void</b></font>&nbsp;add(Vec&lt;ZZ_p&gt;&amp; x,&nbsp;<font color="#008b00"><b>const</b></font>&nbsp;Vec&lt;ZZ_p&gt;&amp; a,&nbsp;<font color="#008b00"><b>const</b></font>&nbsp;Vec&lt;ZZ_p&gt;&amp; b)<br>
136 {<br>
137 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;n = a.length();<br>
138 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(b.length() != n) Error(<font color="#4a708b">&quot;vector add: dimension mismatch&quot;</font>);<br>
139 <br>
140 &nbsp;&nbsp; x.SetLength(n);<br>
141 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;i;<br>
142 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>for</b></font>&nbsp;(i =&nbsp;<font color="#ff8c00">0</font>; i &lt; n; i++)<br>
143 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add(x[i], a[i], b[i]);<br>
144 }<br>
145 </font>
146 </font></td></tr></table><p><p>
147 <!-- }}} ENDPRETTY -->
148
102149
103150 <p>
104151
105152 The second example is an inner product routine (also supplied by NTL):
106153
107 <p>
108 <pre>
109 #include &lt;NTL/ZZ_p.h&gt;
154 <!-- STARTPLAIN
155 #include <NTL/ZZ_p.h>
110156
111157 using namespace std;
112158 using namespace NTL;
113159
114 void InnerProduct(ZZ_p&amp; x, const Vec&lt;ZZ_p&gt;&amp; a, const Vec&lt;ZZ_p&gt;&amp; b)
160 void InnerProduct(ZZ_p& x, const Vec<ZZ_p>& a, const Vec<ZZ_p>& b)
115161 {
116162 long n = min(a.length(), b.length());
117163 long i;
118164 ZZ accum, t;
119165
120166 accum = 0;
121 for (i = 0; i &lt; n; i++) {
167 for (i = 0; i < n; i++) {
122168 mul(t, rep(a[i]), rep(b[i]));
123169 add(accum, accum, t);
124170 }
125171
126172 conv(x, accum);
127173 }
128 </pre>
129
130 <p>
174 ENDPLAIN -->
175 <!-- STARTPRETTY {{{ -->
176 <p><p><table cellPadding=10px><tr><td><font color="#000000">
177 <font face="monospace">
178 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/ZZ_p.h&gt;</font><br>
179 <br>
180 using namespace std;<br>
181 using namespace NTL;<br>
182 <br>
183 <font color="#008b00"><b>void</b></font>&nbsp;InnerProduct(ZZ_p&amp; x,&nbsp;<font color="#008b00"><b>const</b></font>&nbsp;Vec&lt;ZZ_p&gt;&amp; a,&nbsp;<font color="#008b00"><b>const</b></font>&nbsp;Vec&lt;ZZ_p&gt;&amp; b)<br>
184 {<br>
185 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;n = min(a.length(), b.length());<br>
186 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;i;<br>
187 &nbsp;&nbsp; ZZ accum, t;<br>
188 <br>
189 &nbsp;&nbsp; accum =&nbsp;<font color="#ff8c00">0</font>;<br>
190 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>for</b></font>&nbsp;(i =&nbsp;<font color="#ff8c00">0</font>; i &lt; n; i++) {<br>
191 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mul(t, rep(a[i]), rep(b[i]));<br>
192 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add(accum, accum, t);<br>
193 &nbsp;&nbsp; }<br>
194 <br>
195 &nbsp;&nbsp; conv(x, accum);<br>
196 }<br>
197 </font>
198 </font></td></tr></table><p><p>
199 <!-- }}} ENDPRETTY -->
200
201
131202 This second example illustrates two things.
132203 First, it illustrates the use of function <tt>rep</tt> which
133204 returns a read-only reference to the representation of a <tt>ZZ_p</tt>
161232 promoting both <tt>long</tt> and <tt>ZZ_p</tt> to <tt>ZZ_pX</tt>.
162233
163234 <p>
164 See <a href="ZZ_p.txt"><tt>ZZ_p.txt</tt></a> for details on <tt>ZZ_p</tt>;
165 see <a href="ZZ_pX.txt"><tt>ZZ_pX.txt</tt></a> for details on <tt>ZZ_pX</tt>;
166 see <a href="ZZ_pXFactoring.txt"><tt>ZZ_pXFactoring.txt</tt></a> for details on
235 See <a href="ZZ_p.cpp.html"><tt>ZZ_p.txt</tt></a> for details on <tt>ZZ_p</tt>;
236 see <a href="ZZ_pX.cpp.html"><tt>ZZ_pX.txt</tt></a> for details on <tt>ZZ_pX</tt>;
237 see <a href="ZZ_pXFactoring.cpp.html"><tt>ZZ_pXFactoring.txt</tt></a> for details on
167238 the routines for factoring polynomials over <tt>ZZ_p</tt>;
168 see <a href="vec_ZZ_p.txt"><tt>vec_ZZ_p.txt</tt></a> for details
239 see <a href="vec_ZZ_p.cpp.html"><tt>vec_ZZ_p.txt</tt></a> for details
169240 on mathematical operations on <tt>Vec&lt;ZZ_p&gt;</tt>'s;
170 see <a href="mat_ZZ_p.txt"><tt>mat_ZZ_p.txt</tt></a> for details on
241 see <a href="mat_ZZ_p.cpp.html"><tt>mat_ZZ_p.txt</tt></a> for details on
171242 mathematical operations on <tt>Mat&lt;ZZ_p&gt;</tt>'s.
172243
173244 <p> <hr> <p>
178249 and a prime, and tests if the polynomial is irreducible modulo
179250 the prime.
180251
181 <p>
182 <pre>
183 #include &lt;NTL/ZZX.h&gt;
184 #include &lt;NTL/ZZ_pXFactoring.h&gt;
252 <!-- STARTPLAIN
253 #include <NTL/ZZX.h>
254 #include <NTL/ZZ_pXFactoring.h>
185255
186256 using namespace std;
187257 using namespace NTL;
188258
189 long IrredTestMod(const ZZX&amp; f, const ZZ&amp; p)
259 long IrredTestMod(const ZZX& f, const ZZ& p)
190260 {
191261 ZZ_pPush push(p); // save current modulus and install p
192262 // as current modulus
193263
194 return DetIrredTest(conv&lt;ZZ_pX&gt;(f));
264 return DetIrredTest(conv<ZZ_pX>(f));
195265
196266 // old modulus is restored automatically when push is destroyed
197267 // upon return
198268 }
199 </pre>
200
201 <p>
269 ENDPLAIN -->
270 <!-- STARTPRETTY {{{ -->
271 <p><p><table cellPadding=10px><tr><td><font color="#000000">
272 <font face="monospace">
273 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/ZZX.h&gt;</font><br>
274 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/ZZ_pXFactoring.h&gt;</font><br>
275 <br>
276 using namespace std;<br>
277 using namespace NTL;<br>
278 <br>
279 <font color="#008b00"><b>long</b></font>&nbsp;IrredTestMod(<font color="#008b00"><b>const</b></font>&nbsp;ZZX&amp; f,&nbsp;<font color="#008b00"><b>const</b></font>&nbsp;ZZ&amp; p)<br>
280 {<br>
281 &nbsp;&nbsp; ZZ_pPush push(p);&nbsp;<font color="#0000ee"><i>// save current modulus and install p</i></font><br>
282 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// as current modulus</i></font><br>
283 <br>
284 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;DetIrredTest(conv&lt;ZZ_pX&gt;(f));<br>
285 <br>
286 &nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// old modulus is restored automatically when push is destroyed</i></font><br>
287 &nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// upon return</i></font><br>
288 }<br>
289 </font>
290 </font></td></tr></table><p><p>
291 <!-- }}} ENDPRETTY -->
292
293
202294 The modulus switching mechanism is actually quite a bit
203295 more general and flexible than this example illustrates.
204296
206298 Noe the use of the conversion function
207299 <tt>conv&lt;ZZ_pX&gt;</tt>.
208300 We could also have used the equivalent procedural form:
209 <pre>
301 <!-- STARTPLAIN
210302 ZZ_pX f1;
211303 conv(f1, f);
212304 return DetIrredTest(f1);
213 </pre>
214
215
216
305 ENDPLAIN -->
306 <!-- STARTPRETTY {{{ -->
307 <p><p><table cellPadding=10px><tr><td><font color="#000000">
308 <font face="monospace">
309 &nbsp;&nbsp; ZZ_pX f1;<br>
310 &nbsp;&nbsp; conv(f1, f);<br>
311 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;DetIrredTest(f1);<br>
312 </font>
313 </font></td></tr></table><p><p>
314 <!-- }}} ENDPRETTY -->
217315
218316
219317
234332 as follows.
235333
236334
237 <p>
238 <pre>
239 #include &lt;NTL/ZZX.h&gt;
240 #include &lt;NTL/lzz_pXFactoring.h&gt;
335 <!-- STARTPLAIN
336 #include <NTL/ZZX.h>
337 #include <NTL/lzz_pXFactoring.h>
241338
242339 using namespace std;
243340 using namespace NTL;
244341
245 long IrredTestMod(const ZZX&amp; f, long p)
342 long IrredTestMod(const ZZX& f, long p)
246343 {
247344 zz_pPush push(p);
248 return DetIrredTest(conv&lt;zz_pX&gt;(f));
345 return DetIrredTest(conv<zz_pX>(f));
249346 }
250 </pre>
347 ENDPLAIN -->
348 <!-- STARTPRETTY {{{ -->
349 <p><p><table cellPadding=10px><tr><td><font color="#000000">
350 <font face="monospace">
351 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/ZZX.h&gt;</font><br>
352 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/lzz_pXFactoring.h&gt;</font><br>
353 <br>
354 using namespace std;<br>
355 using namespace NTL;<br>
356 <br>
357 <font color="#008b00"><b>long</b></font>&nbsp;IrredTestMod(<font color="#008b00"><b>const</b></font>&nbsp;ZZX&amp; f,&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;p)<br>
358 {<br>
359 &nbsp;&nbsp; zz_pPush push(p);<br>
360 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;DetIrredTest(conv&lt;zz_pX&gt;(f));<br>
361 }<br>
362 </font>
363 </font></td></tr></table><p><p>
364 <!-- }}} ENDPRETTY -->
365
251366
252367 <p> <hr> <p>
253368
258373 The small primes are specially chosen "FFT primes", which are of
259374 a special form that allows for particular fast polynomial arithmetic.
260375
261 <p>
262 <pre>
263 #include &lt;NTL/ZZX.h&gt;
376 <!-- STARTPLAIN
377 #include <NTL/ZZX.h>
264378
265379 using namespace std;
266380 using namespace NTL;
267381
268 void GCD(ZZX&amp; d, const ZZX&amp; a, const ZZX&amp; b)
382 void GCD(ZZX& d, const ZZX& a, const ZZX& b)
269383 {
270384 if (a == 0) {
271385 d = b;
272 if (LeadCoeff(d) &lt; 0) negate(d, d);
386 if (LeadCoeff(d) < 0) negate(d, d);
273387 return;
274388 }
275389
276390 if (b == 0) {
277391 d = a;
278 if (LeadCoeff(d) &lt; 0) negate(d, d);
392 if (LeadCoeff(d) < 0) negate(d, d);
279393 return;
280394 }
281395
324438 break;
325439 }
326440
327 if (FirstTime || deg(G) &lt; deg(g)) {
441 if (FirstTime || deg(G) < deg(g)) {
328442 prod = 1;
329443 g = 0;
330444 FirstTime = 0;
331445 }
332 else if (deg(G) &gt; deg(g)) {
446 else if (deg(G) > deg(g)) {
333447 continue;
334448 }
335449
336450 if (!CRT(g, prod, G)) {
337451 PrimitivePart(res, g);
338 if (divide(f1, res) &amp;&amp; divide(f2, res))
452 if (divide(f1, res) && divide(f2, res))
339453 break;
340454 }
341455
342456 }
343457
344458 mul(d, res, c);
345 if (LeadCoeff(d) &lt; 0) negate(d, d);
459 if (LeadCoeff(d) < 0) negate(d, d);
346460 }
347 </pre>
348
349
350 <p>
351 See <a href="lzz_p.txt"><tt>lzz_p.txt</tt></a> for details on <tt>zz_p</tt>;
352 see <a href="lzz_pX.txt"><tt>lzz_pX.txt</tt></a> for details on <tt>zz_pX</tt>;
353 see <a href="lzz_pXFactoring.txt"><tt>lzz_pXFactoring.txt</tt></a> for details on
461 ENDPLAIN -->
462 <!-- STARTPRETTY {{{ -->
463 <p><p><table cellPadding=10px><tr><td><font color="#000000">
464 <font face="monospace">
465 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/ZZX.h&gt;</font><br>
466 <br>
467 using namespace std;<br>
468 using namespace NTL;<br>
469 <br>
470 <font color="#008b00"><b>void</b></font>&nbsp;GCD(ZZX&amp; d,&nbsp;<font color="#008b00"><b>const</b></font>&nbsp;ZZX&amp; a,&nbsp;<font color="#008b00"><b>const</b></font>&nbsp;ZZX&amp; b)<br>
471 {<br>
472 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(a ==&nbsp;<font color="#ff8c00">0</font>) {<br>
473 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d = b;<br>
474 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(LeadCoeff(d) &lt;&nbsp;<font color="#ff8c00">0</font>) negate(d, d);<br>
475 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>return</b></font>;<br>
476 &nbsp;&nbsp; }<br>
477 <br>
478 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(b ==&nbsp;<font color="#ff8c00">0</font>) {<br>
479 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d = a;<br>
480 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(LeadCoeff(d) &lt;&nbsp;<font color="#ff8c00">0</font>) negate(d, d);<br>
481 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>return</b></font>;<br>
482 &nbsp;&nbsp; }<br>
483 <br>
484 &nbsp;&nbsp; ZZ c1, c2, c;<br>
485 &nbsp;&nbsp; ZZX f1, f2;<br>
486 <br>
487 &nbsp;&nbsp; content(c1, a);<br>
488 &nbsp;&nbsp; divide(f1, a, c1);<br>
489 <br>
490 &nbsp;&nbsp; content(c2, b);<br>
491 &nbsp;&nbsp; divide(f2, b, c2);<br>
492 <br>
493 &nbsp;&nbsp; GCD(c, c1, c2);<br>
494 <br>
495 &nbsp;&nbsp; ZZ ld;<br>
496 &nbsp;&nbsp; GCD(ld, LeadCoeff(f1), LeadCoeff(f2));<br>
497 <br>
498 &nbsp;&nbsp; ZZX g, res;<br>
499 <br>
500 &nbsp;&nbsp; ZZ prod;<br>
501 <br>
502 &nbsp;&nbsp; zz_pPush push();&nbsp;<font color="#0000ee"><i>// save current modulus, restore upon return</i></font><br>
503 <br>
504 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;FirstTime =&nbsp;<font color="#ff8c00">1</font>;<br>
505 <br>
506 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;i;<br>
507 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>for</b></font>&nbsp;(i =&nbsp;<font color="#ff8c00">0</font>; ;i++) {<br>
508 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zz_p::FFTInit(i);<br>
509 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;p = zz_p::modulus();<br>
510 <br>
511 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(divide(LeadCoeff(f1), p) || divide(LeadCoeff(f2), p))&nbsp;<font color="#b03060"><b>continue</b></font>;<br>
512 <br>
513 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zz_pX G, F1, F2;<br>
514 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zz_p&nbsp;&nbsp;LD;<br>
515 <br>
516 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conv(F1, f1);<br>
517 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conv(F2, f2);<br>
518 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conv(LD, ld);<br>
519 <br>
520 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GCD(G, F1, F2);<br>
521 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mul(G, G, LD);<br>
522 <br>
523 <br>
524 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(deg(G) ==&nbsp;<font color="#ff8c00">0</font>) {&nbsp;<br>
525 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; res =&nbsp;<font color="#ff8c00">1</font>;<br>
526 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>break</b></font>;<br>
527 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
528 <br>
529 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(FirstTime || deg(G) &lt; deg(g)) {<br>
530 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prod =&nbsp;<font color="#ff8c00">1</font>;<br>
531 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; g =&nbsp;<font color="#ff8c00">0</font>;<br>
532 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FirstTime =&nbsp;<font color="#ff8c00">0</font>;<br>
533 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
534 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>else</b></font>&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(deg(G) &gt; deg(g)) {<br>
535 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>continue</b></font>;<br>
536 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
537 <br>
538 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(!CRT(g, prod, G)) {<br>
539 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PrimitivePart(res, g);<br>
540 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(divide(f1, res) &amp;&amp; divide(f2, res))<br>
541 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>break</b></font>;<br>
542 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
543 <br>
544 &nbsp;&nbsp; }<br>
545 <br>
546 &nbsp;&nbsp; mul(d, res, c);<br>
547 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(LeadCoeff(d) &lt;&nbsp;<font color="#ff8c00">0</font>) negate(d, d);<br>
548 }<br>
549 </font>
550 </font></td></tr></table><p><p>
551 <!-- }}} ENDPRETTY -->
552
553
554
555 <p>
556 See <a href="lzz_p.cpp.html"><tt>lzz_p.txt</tt></a> for details on <tt>zz_p</tt>;
557 see <a href="lzz_pX.cpp.html"><tt>lzz_pX.txt</tt></a> for details on <tt>zz_pX</tt>;
558 see <a href="lzz_pXFactoring.cpp.html"><tt>lzz_pXFactoring.txt</tt></a> for details on
354559 the routines for factoring polynomials over <tt>zz_p</tt>;
355 see <a href="vec_lzz_p.txt"><tt>vec_lzz_p.txt</tt></a> for details on <tt>vec_zz_p</tt>;
356 see <a href="mat_lzz_p.txt"><tt>mat_lzz_p.txt</tt></a> for details on <tt>mat_zz_p</tt>.
560 see <a href="vec_lzz_p.cpp.html"><tt>vec_lzz_p.txt</tt></a> for details on <tt>vec_zz_p</tt>;
561 see <a href="mat_lzz_p.cpp.html"><tt>mat_lzz_p.txt</tt></a> for details on <tt>mat_zz_p</tt>.
357562
358563
359564 <p> <hr> <p>
398603 is irreducible using linear algebra.
399604 NTL's built-in irreducibility test is to be preferred, however.
400605
401 <pre>
402
403 #include &lt;NTL/GF2X.h&gt;
404 #include &lt;NTL/mat_GF2.h&gt;
606 <!-- STARTPLAIN
607 #include <NTL/GF2X.h>
608 #include <NTL/mat_GF2.h>
405609
406610 using namespace std;
407611 using namespace NTL;
408612
409 long MatIrredTest(const GF2X&amp; f)
613 long MatIrredTest(const GF2X& f)
410614 {
411615 long n = deg(f);
412616
413 if (n &lt;= 0) return 0;
617 if (n <= 0) return 0;
414618 if (n == 1) return 1;
415619
416620 if (GCD(f, diff(f)) != 1) return 0;
417621
418 Mat&lt;GF2&gt; M;
622 Mat<GF2> M;
419623
420624 M.SetDims(n, n);
421625
424628 GF2X g;
425629 g = 1;
426630
427 for (long i = 0; i &lt; n; i++) {
631 for (long i = 0; i < n; i++) {
428632 VectorCopy(M[i], g, n);
429633 M[i][i] += 1;
430634 g = (g * x_squared) % f;
437641 else
438642 return 0;
439643 }
440 </pre>
644 ENDPLAIN -->
645 <!-- STARTPRETTY {{{ -->
646 <p><p><table cellPadding=10px><tr><td><font color="#000000">
647 <font face="monospace">
648 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/GF2X.h&gt;</font><br>
649 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/mat_GF2.h&gt;</font><br>
650 <br>
651 using namespace std;<br>
652 using namespace NTL;<br>
653 <br>
654 <font color="#008b00"><b>long</b></font>&nbsp;MatIrredTest(<font color="#008b00"><b>const</b></font>&nbsp;GF2X&amp; f)<br>
655 {<br>
656 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;n = deg(f);<br>
657 <br>
658 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(n &lt;=&nbsp;<font color="#ff8c00">0</font>)&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;<font color="#ff8c00">0</font>;<br>
659 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(n ==&nbsp;<font color="#ff8c00">1</font>)&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;<font color="#ff8c00">1</font>;<br>
660 <br>
661 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(GCD(f, diff(f)) !=&nbsp;<font color="#ff8c00">1</font>)&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;<font color="#ff8c00">0</font>;<br>
662 <br>
663 &nbsp;&nbsp; Mat&lt;GF2&gt; M;<br>
664 <br>
665 &nbsp;&nbsp; M.SetDims(n, n);<br>
666 <br>
667 &nbsp;&nbsp; GF2X x_squared = GF2X(INIT_MONO,&nbsp;<font color="#ff8c00">2</font>);<br>
668 <br>
669 &nbsp;&nbsp; GF2X g;<br>
670 &nbsp;&nbsp; g =&nbsp;<font color="#ff8c00">1</font>;<br>
671 <br>
672 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>for</b></font>&nbsp;(<font color="#008b00"><b>long</b></font>&nbsp;i =&nbsp;<font color="#ff8c00">0</font>; i &lt; n; i++) {<br>
673 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VectorCopy(M[i], g, n);<br>
674 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;M[i][i] +=&nbsp;<font color="#ff8c00">1</font>;<br>
675 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g = (g * x_squared) % f;<br>
676 &nbsp;&nbsp; }<br>
677 <br>
678 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;rank = gauss(M);<br>
679 <br>
680 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(rank == n-<font color="#ff8c00">1</font>)<br>
681 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;<font color="#ff8c00">1</font>;<br>
682 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>else</b></font><br>
683 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;<font color="#ff8c00">0</font>;<br>
684 }<br>
685 </font>
686 </font></td></tr></table><p><p>
687 <!-- }}} ENDPRETTY -->
688
441689
442690 <p>
443691 Note that the statement
444 <pre>
692 <!-- STARTPLAIN
445693 g = (g * x_squared) % f;
446 </pre>
694 ENDPLAIN -->
695 <!-- STARTPRETTY {{{ -->
696 <p><p><table cellPadding=10px><tr><td><font color="#000000">
697 <font face="monospace">
698 &nbsp;&nbsp; g = (g * x_squared) % f;<br>
699 </font>
700 </font></td></tr></table><p><p>
701 <!-- }}} ENDPRETTY -->
702
447703 could be replace d by the more efficient code sequence
448 <pre>
704 <!-- STARTPLAIN
449705 MulByXMod(g, g, f);
450706 MulByXMod(g, g, f);
451 </pre>
707 ENDPLAIN -->
708 <!-- STARTPRETTY {{{ -->
709 <p><p><table cellPadding=10px><tr><td><font color="#000000">
710 <font face="monospace">
711 &nbsp;&nbsp; MulByXMod(g, g, f);<br>
712 &nbsp;&nbsp; MulByXMod(g, g, f);<br>
713 </font>
714 </font></td></tr></table><p><p>
715 <!-- }}} ENDPRETTY -->
716
452717 but this would not significantly impact the overall
453718 running time, since it is the Gaussian elimination that
454719 dominates the running time.
455720
456721 <p>
457 See <a href="GF2.txt"><tt>GF2.txt</tt></a> for details on <tt>GF2</tt>;
458 see <a href="GF2X.txt"><tt>GF2X.txt</tt></a> for details on <tt>GF2X</tt>;
459 see <a href="GF2XFactoring.txt"><tt>GF2XFactoring.txt</tt></a> for details on
722 See <a href="GF2.cpp.html"><tt>GF2.txt</tt></a> for details on <tt>GF2</tt>;
723 see <a href="GF2X.cpp.html"><tt>GF2X.txt</tt></a> for details on <tt>GF2X</tt>;
724 see <a href="GF2XFactoring.cpp.html"><tt>GF2XFactoring.txt</tt></a> for details on
460725 the routines for factoring polynomials over <tt>GF2</tt>;
461 see <a href="vec_GF2.txt"><tt>vec_GF2.txt</tt></a> for details on <tt>vec_GF2</tt>;
462 see <a href="mat_GF2.txt"><tt>mat_GF2.txt</tt></a> for details on <tt>mat_GF2</tt>.
726 see <a href="vec_GF2.cpp.html"><tt>vec_GF2.txt</tt></a> for details on <tt>vec_GF2</tt>;
727 see <a href="mat_GF2.cpp.html"><tt>mat_GF2.txt</tt></a> for details on <tt>mat_GF2</tt>.
463728
464729 <p>
465730
33 A Tour of NTL: Examples: Extension Rings and Fields </title>
44 </head>
55
6 <body bgcolor="#fff9e6">
76 <center>
87 <a href="tour-ex4.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
98 <a href="tour-examples.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a>
2221 and polynomial arithmetic over such extensions.
2322 Here is a little program that illustrates this.
2423
25 <p>
26 <pre>
27 #include &lt;NTL/ZZ_pXFactoring.h&gt;
28 #include &lt;NTL/ZZ_pEX.h&gt;
24 <!-- STARTPLAIN
25 #include <NTL/ZZ_pXFactoring.h>
26 #include <NTL/ZZ_pEX.h>
2927
3028 NTL_CLIENT
3129
5351 if (CompMod(g, h, f) != 0) // check that g(h) = 0 mod f
5452 Error("oops (2)");
5553 }
56 </pre>
54 ENDPLAIN -->
55 <!-- STARTPRETTY {{{ -->
56 <p><p><table cellPadding=10px><tr><td><font color="#000000">
57 <font face="monospace">
58 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/ZZ_pXFactoring.h&gt;</font><br>
59 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/ZZ_pEX.h&gt;</font><br>
60 <br>
61 NTL_CLIENT<br>
62 <br>
63 <font color="#008b00"><b>int</b></font>&nbsp;main()<br>
64 {<br>
65 &nbsp;&nbsp; ZZ_p::init(ZZ(<font color="#ff8c00">17</font>));&nbsp;<font color="#0000ee"><i>// define GF(17)</i></font><br>
66 <br>
67 &nbsp;&nbsp; ZZ_pX P;<br>
68 &nbsp;&nbsp; BuildIrred(P,&nbsp;<font color="#ff8c00">10</font>);&nbsp;<font color="#0000ee"><i>// generate an irreducible polynomial P</i></font><br>
69 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// of degree 10 over GF(17)</i></font><br>
70 <br>
71 &nbsp;&nbsp; ZZ_pE::init(P);&nbsp;<font color="#0000ee"><i>// define GF(17^10)</i></font><br>
72 <br>
73 &nbsp;&nbsp; ZZ_pEX f, g, h;&nbsp;&nbsp;<font color="#0000ee"><i>// declare polynomials over GF(17^10)</i></font><br>
74 <br>
75 &nbsp;&nbsp; random(f,&nbsp;<font color="#ff8c00">20</font>);&nbsp;&nbsp;<font color="#0000ee"><i>// f is a random, monic polynomial of degree 20</i></font><br>
76 &nbsp;&nbsp; SetCoeff(f,&nbsp;<font color="#ff8c00">20</font>);<br>
77 <br>
78 &nbsp;&nbsp; random(h,&nbsp;<font color="#ff8c00">20</font>);&nbsp;<font color="#0000ee"><i>// h is a random polynomial of degree less than 20</i></font><br>
79 <br>
80 &nbsp;&nbsp; g = MinPolyMod(h, f);&nbsp;<font color="#0000ee"><i>// compute the minimum polynomial of h modulo f</i></font><br>
81 <br>
82 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(g ==&nbsp;<font color="#ff8c00">0</font>) Error(<font color="#4a708b">&quot;oops (1)&quot;</font>);&nbsp;<font color="#0000ee"><i>// check that g != 0</i></font><br>
83 <br>
84 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(CompMod(g, h, f) !=&nbsp;<font color="#ff8c00">0</font>)&nbsp;<font color="#0000ee"><i>// check that g(h) = 0 mod f</i></font><br>
85 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Error(<font color="#4a708b">&quot;oops (2)&quot;</font>);<br>
86 }<br>
87 </font>
88 </font></td></tr></table><p><p>
89 <!-- }}} ENDPRETTY -->
90
5791
5892 <p>
5993 This example illustrates building extension rings over <tt>ZZ_p</tt>.
6195 the syntax is exactly the same.
6296
6397 <p>
64 See <a href="ZZ_pE.txt"><tt>ZZ_pE.txt</tt></a> for the basics of the extension
98 See <a href="ZZ_pE.cpp.html"><tt>ZZ_pE.txt</tt></a> for the basics of the extension
6599 ring <tt>ZZ_pE</tt> over <tt>ZZ_p</tt>.
66 Also see <a href="ZZ_pEX.txt"><tt>ZZ_pEX.txt</tt></a> for polynomial
100 Also see <a href="ZZ_pEX.cpp.html"><tt>ZZ_pEX.txt</tt></a> for polynomial
67101 arithmetic over <tt>ZZ_pE</tt>, and
68 <a href="ZZ_pEXFactoring.txt"><tt>ZZ_pEXFactoring.txt</tt></a> for factoring
102 <a href="ZZ_pEXFactoring.cpp.html"><tt>ZZ_pEXFactoring.txt</tt></a> for factoring
69103 routines over <tt>ZZ_pE</tt>.
70 See <a href="vec_ZZ_pE.txt"><tt>vec_ZZ_pE.txt</tt></a> for vectors over <tt>ZZ_pE</tt>,
71 and <a href="mat_ZZ_pE.txt"><tt>mat_ZZ_pE.txt</tt></a> for matrices over <tt>ZZ_pE</tt>.
104 See <a href="vec_ZZ_pE.cpp.html"><tt>vec_ZZ_pE.txt</tt></a> for vectors over <tt>ZZ_pE</tt>,
105 and <a href="mat_ZZ_pE.cpp.html"><tt>mat_ZZ_pE.txt</tt></a> for matrices over <tt>ZZ_pE</tt>.
72106
73107 <p>
74 See <a href="lzz_pE.txt"><tt>lzz_pE.txt</tt></a> for the basics of the extension
108 See <a href="lzz_pE.cpp.html"><tt>lzz_pE.txt</tt></a> for the basics of the extension
75109 ring <tt>zz_pE</tt> over <tt>zz_p</tt>.
76 Also see <a href="lzz_pEX.txt"><tt>lzz_pEX.txt</tt></a> for polynomial
110 Also see <a href="lzz_pEX.cpp.html"><tt>lzz_pEX.txt</tt></a> for polynomial
77111 arithmetic over <tt>zz_pE</tt>, and
78 <a href="lzz_pEXFactoring.txt"><tt>lzz_pEXFactoring.txt</tt></a> for factoring
112 <a href="lzz_pEXFactoring.cpp.html"><tt>lzz_pEXFactoring.txt</tt></a> for factoring
79113 routines over <tt>zz_pE</tt>.
80 See <a href="vec_lzz_pE.txt"><tt>vec_lzz_pE.txt</tt></a> for vectors over <tt>zz_pE</tt>,
81 and <a href="mat_lzz_pE.txt"><tt>mat_lzz_pE.txt</tt></a> for matrices over <tt>zz_pE</tt>.
114 See <a href="vec_lzz_pE.cpp.html"><tt>vec_lzz_pE.txt</tt></a> for vectors over <tt>zz_pE</tt>,
115 and <a href="mat_lzz_pE.cpp.html"><tt>mat_lzz_pE.txt</tt></a> for matrices over <tt>zz_pE</tt>.
82116
83117 <p>
84 See <a href="GF2E.txt"><tt>GF2E.txt</tt></a> for the basics of the extension
118 See <a href="GF2E.cpp.html"><tt>GF2E.txt</tt></a> for the basics of the extension
85119 ring <tt>GF2E</tt> over <tt>GF2</tt>.
86 Also see <a href="GF2EX.txt"><tt>GF2EX.txt</tt></a> for polynomial
120 Also see <a href="GF2EX.cpp.html"><tt>GF2EX.txt</tt></a> for polynomial
87121 arithmetic over <tt>GF2E</tt>, and
88 <a href="GF2EXFactoring.txt"><tt>GF2EXFactoring.txt</tt></a> for factoring
122 <a href="GF2EXFactoring.cpp.html"><tt>GF2EXFactoring.txt</tt></a> for factoring
89123 routines over <tt>GF2E</tt>.
90 See <a href="vec_GF2E.txt"><tt>vec_GF2E.txt</tt></a> for vectors over <tt>GF2E</tt>,
91 and <a href="mat_GF2E.txt"><tt>mat_GF2E.txt</tt></a> for matrices over <tt>GF2E</tt>.
124 See <a href="vec_GF2E.cpp.html"><tt>vec_GF2E.txt</tt></a> for vectors over <tt>GF2E</tt>,
125 and <a href="mat_GF2E.cpp.html"><tt>mat_GF2E.txt</tt></a> for matrices over <tt>GF2E</tt>.
92126
93127
94128 <center>
33 A Tour of NTL: Examples: Floating Point Classes </title>
44 </head>
55
6 <body bgcolor="#fff9e6">
76 <center>
87 <a href="tour-ex5.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
98 <a href="tour-examples.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a>
3130
3231 Here again is a program that reads a list of numbers from the input,
3332 and outputs the sum of their squares, using the class <tt>RR</tt>.
34 <p>
3533
36 <pre>
37 #include &lt;NTL/RR.h&gt;
34 <!-- STARTPLAIN
35 #include <NTL/RR.h>
3836
3937 int main()
4038 {
4240
4341 acc = 0;
4442 while (SkipWhiteSpace(cin)) {
45 cin &gt;&gt; val;
43 cin >> val;
4644 acc += val*val;
4745 }
4846
49 cout &lt;&lt; acc &lt;&lt; "\n";
47 cout << acc << "\n";
5048 }
51 </pre>
49 ENDPLAIN -->
50 <!-- STARTPRETTY {{{ -->
51 <p><p><table cellPadding=10px><tr><td><font color="#000000">
52 <font face="monospace">
53 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/RR.h&gt;</font><br>
54 <br>
55 <font color="#008b00"><b>int</b></font>&nbsp;main()<br>
56 {<br>
57 &nbsp;&nbsp; RR acc, val;<br>
58 <br>
59 &nbsp;&nbsp; acc =&nbsp;<font color="#ff8c00">0</font>;<br>
60 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>while</b></font>&nbsp;(SkipWhiteSpace(cin)) {<br>
61 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin &gt;&gt; val;<br>
62 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc += val*val;<br>
63 &nbsp;&nbsp; }<br>
64 <br>
65 &nbsp;&nbsp; cout &lt;&lt; acc &lt;&lt;&nbsp;<font color="#4a708b">&quot;</font><font color="#8a2be2">\n</font><font color="#4a708b">&quot;</font>;<br>
66 }<br>
67 </font>
68 </font></td></tr></table><p><p>
69 <!-- }}} ENDPRETTY -->
70
5271
5372 <p>
5473
5574 The precision used for the computation can be set by executing
56 <pre>
75 <!-- STARTPLAIN
5776 RR::SetPrecision(p);
58 </pre>
77 ENDPLAIN -->
78 <!-- STARTPRETTY {{{ -->
79 <p><p><table cellPadding=10px><tr><td><font color="#000000">
80 <font face="monospace">
81 &nbsp;&nbsp; RR::SetPrecision(p);<br>
82 </font>
83 </font></td></tr></table><p><p>
84 <!-- }}} ENDPRETTY -->
85
5986 which sets the effective precision to <tt>p</tt> bits.
6087 By default, <tt>p=150</tt>.
6188 All of the basic arithmetic operations compute their results
6895
6996 The number of <i>decimal</i> digits of precision that are used when
7097 printing an <tt>RR</tt> can be set be executing
71 <pre>
98 <!-- STARTPLAIN
7299 RR::SetOutputPrecision(d);
73 </pre>
100 ENDPLAIN -->
101 <!-- STARTPRETTY {{{ -->
102 <p><p><table cellPadding=10px><tr><td><font color="#000000">
103 <font face="monospace">
104 &nbsp;&nbsp; RR::SetOutputPrecision(d);<br>
105 </font>
106 </font></td></tr></table><p><p>
107 <!-- }}} ENDPRETTY -->
108
74109 which sets the output precision to <tt>d</tt>.
75110 By default, <tt>d=10</tt>.
76111
77112 <p>
78 See <a href="RR.txt"><tt>RR.txt</tt></a> for details.
113 See <a href="RR.cpp.html"><tt>RR.txt</tt></a> for details.
79114
80115 <p>
81116
84119 other floating point classes.
85120 The output precision for these two classes can be controlled just
86121 as with <tt>RR</tt>.
87 See <a href="quad_float.txt"><tt>quad_float.txt</tt></a> and
88 <a href="xdouble.txt"><tt>xdouble.txt</tt></a>
122 See <a href="quad_float.cpp.html"><tt>quad_float.txt</tt></a> and
123 <a href="xdouble.cpp.html"><tt>xdouble.txt</tt></a>
89124 for details.
90125
91126 <p>
33 A Tour of NTL: Examples </title>
44 </head>
55
6 <body bgcolor="#fff9e6">
76 <center>
87 <a href="tour-intro.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
98 <a href="tour.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a>
33 A Tour of NTL: Using NTL with the gf2x library </title>
44 </head>
55
6 <body bgcolor="#fff9e6">
76
87 <center>
98 <a href="tour-gmp.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
3332 for very large degree polynomials.
3433 If you use NTL if the <tt>gf2x</tt> library,
3534 then multiplication, division, GCD, and minimum polynomal
36 calculations for the <a href="GF2X.txt">GF2X</a> class will
35 calculations for the <a href="GF2X.cpp.html">GF2X</a> class will
3736 be faster for large degree polymials.
3837
3938
33 A Tour of NTL: Using NTL with GMP </title>
44 </head>
55
6 <body bgcolor="#fff9e6">
76
87 <center>
98 <a href="tour-impl.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
33 A Tour of NTL: NTL Implementation and Portability </title>
44 </head>
55
6 <body bgcolor="#fff9e6">
76 <center>
87 <a href="tour-tips.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
98 <a href="tour.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a>
232231 for the <tt>quad_float</tt> module, where some desperate hacks,
233232 including assembly code, may be used
234233 to try to work around problems created by "loose" IEEE floating point
235 <a href="quad_float.txt">[more details]</a>.
234 <a href="quad_float.cpp.html">[more details]</a>.
236235 But note that even if the <tt>quad_float</tt> package does not work correctly
237236 because of these problems, the only other routines that are affected
238237 are the <tt>LLL_QP</tt> routines in the <tt>LLL</tt> module -- the
33 A Tour of NTL: Introduction </title>
44 </head>
55
6 <body bgcolor="#fff9e6">
76
87 <center>
98 <img src="arrow1.gif" alt="[Previous]" align=bottom>
33 A Tour of NTL: Summary of NTL's Main Modules </title>
44 </head>
55
6 <body bgcolor="#fff9e6">
76 <center>
87 <a href="tour-struct.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
98 <a href="tour.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a>
33 A Tour of NTL: NTL past, present, and future </title>
44 </head>
55
6 <body bgcolor="#fff9e6">
76 <center>
87 <a href="tour-time.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
98 <a href="tour.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a>
33 A Tour of NTL: Traditional and ISO Modes </title>
44 </head>
55
6 <body bgcolor="#fff9e6">
76 <center>
87 <a href="tour-modules.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
98 <a href="tour.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a>
156155 <p>
157156
158157 Here is a simple example to illustrate namespaces.
159 <p>
160 <pre>
158 <!-- STARTPLAIN
161159
162160 namespace N {
163161 void f(int);
172170 x = 1; // the global x
173171 N::x = 0; // the x in namespace N
174172 N::f(0); // the f in namespace N
175 g(1); // error -- g is not visible here
176 }
177
178 </pre>
173 g(1); // error ~~ g is not visible here
174 }
175
176 ENDPLAIN -->
177 <!-- STARTPRETTY {{{ -->
178 <p><p><table cellPadding=10px><tr><td><font color="#000000">
179 <font face="monospace">
180 <br>
181 namespace N {<br>
182 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>void</b></font>&nbsp;f(<font color="#008b00"><b>int</b></font>);<br>
183 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>void</b></font>&nbsp;g(<font color="#008b00"><b>int</b></font>);<br>
184 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>int</b></font>&nbsp;x;<br>
185 }<br>
186 <br>
187 <font color="#008b00"><b>int</b></font>&nbsp;x;<br>
188 <br>
189 <font color="#008b00"><b>void</b></font>&nbsp;h()<br>
190 {<br>
191 &nbsp;&nbsp; x =&nbsp;<font color="#ff8c00">1</font>;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// the global x</i></font><br>
192 &nbsp;&nbsp; N::x =&nbsp;<font color="#ff8c00">0</font>;&nbsp;<font color="#0000ee"><i>// the x in namespace N</i></font><br>
193 &nbsp;&nbsp; N::f(<font color="#ff8c00">0</font>);&nbsp;&nbsp;<font color="#0000ee"><i>// the f in namespace N</i></font><br>
194 &nbsp;&nbsp; g(<font color="#ff8c00">1</font>);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// error -- g is not visible here</i></font><br>
195 }<br>
196 <br>
197 </font>
198 </font></td></tr></table><p><p>
199 <!-- }}} ENDPRETTY -->
200
179201
180202 <p>
181203 All of this explicit qualification business
187209
188210 Here is a variation on the previous example, with a using directive.
189211
190 <p>
191 <pre>
192
212 <!-- STARTPLAIN
193213 namespace N {
194214 void f(int);
195215 void g(int);
202222
203223 void h()
204224 {
205 x = 1; // error -- ambiguous: the global x or the x in namespace N?
225 x = 1; // error ~~ ambiguous: the global x or the x in namespace N?
206226 ::x = 1; // the global x
207227 N::x = 0; // the x in namespace N
208228 N::f(0); // the f in namespace N
209 f(0); // OK -- N::f(int) is visible here
210 g(1); // OK -- N::g(int) is visible here
211 }
212
213 </pre>
229 f(0); // OK ~~ N::f(int) is visible here
230 g(1); // OK ~~ N::g(int) is visible here
231 }
232
233 ENDPLAIN -->
234 <!-- STARTPRETTY {{{ -->
235 <p><p><table cellPadding=10px><tr><td><font color="#000000">
236 <font face="monospace">
237 namespace N {<br>
238 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>void</b></font>&nbsp;f(<font color="#008b00"><b>int</b></font>);<br>
239 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>void</b></font>&nbsp;g(<font color="#008b00"><b>int</b></font>);<br>
240 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>int</b></font>&nbsp;x;<br>
241 }<br>
242 <br>
243 <font color="#008b00"><b>int</b></font>&nbsp;x;<br>
244 <br>
245 using namespace N;<br>
246 <br>
247 <font color="#008b00"><b>void</b></font>&nbsp;h()<br>
248 {<br>
249 &nbsp;&nbsp; x =&nbsp;<font color="#ff8c00">1</font>;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// error -- ambiguous: the global x or the x in namespace N?</i></font><br>
250 &nbsp;&nbsp; ::x =&nbsp;<font color="#ff8c00">1</font>;&nbsp;&nbsp;<font color="#0000ee"><i>// the global x</i></font><br>
251 &nbsp;&nbsp; N::x =&nbsp;<font color="#ff8c00">0</font>;&nbsp;<font color="#0000ee"><i>// the x in namespace N</i></font><br>
252 &nbsp;&nbsp; N::f(<font color="#ff8c00">0</font>);&nbsp;&nbsp;<font color="#0000ee"><i>// the f in namespace N</i></font><br>
253 &nbsp;&nbsp; f(<font color="#ff8c00">0</font>);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// OK -- N::f(int) is visible here</i></font><br>
254 &nbsp;&nbsp; g(<font color="#ff8c00">1</font>);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// OK -- N::g(int) is visible here</i></font><br>
255 }<br>
256 <br>
257 </font>
258 </font></td></tr></table><p><p>
259 <!-- }}} ENDPRETTY -->
260
214261
215262 <p>
216263 Here is another example.
217264
218 <p>
219 <pre>
265 <!-- STARTPLAIN
220266
221267 namespace N1 {
222268 int x;
236282
237283 void h()
238284 {
239 x = 1; // error -- ambiguous: N1::x or N2::x?
285 x = 1; // error ~~ ambiguous: N1::x or N2::x?
240286 N1::x = 1; // OK
241287 N2::x = 1; // OK
242 y = 1; // OK -- this is N2::y
243 g(0); // error -- ambiguous: N1::g(int) or N2::g(int)?
244 f(0); // OK -- N1::f(int), because it is the "best" match
245 f(0.0); // OK -- N2::f(double), because it is the "best" match
246 }
247
248 </pre>
288 y = 1; // OK ~~ this is N2::y
289 g(0); // error ~~ ambiguous: N1::g(int) or N2::g(int)?
290 f(0); // OK ~~ N1::f(int), because it is the "best" match
291 f(0.0); // OK ~~ N2::f(double), because it is the "best" match
292 }
293
294 ENDPLAIN -->
295 <!-- STARTPRETTY {{{ -->
296 <p><p><table cellPadding=10px><tr><td><font color="#000000">
297 <font face="monospace">
298 <br>
299 namespace N1 {<br>
300 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>int</b></font>&nbsp;x;<br>
301 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>void</b></font>&nbsp;f(<font color="#008b00"><b>int</b></font>);<br>
302 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>void</b></font>&nbsp;g(<font color="#008b00"><b>int</b></font>);<br>
303 }<br>
304 <br>
305 namespace N2 {<br>
306 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>int</b></font>&nbsp;x;<br>
307 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>int</b></font>&nbsp;y;<br>
308 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>void</b></font>&nbsp;f(<font color="#008b00"><b>double</b></font>);<br>
309 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>void</b></font>&nbsp;g(<font color="#008b00"><b>int</b></font>);<br>
310 }<br>
311 <br>
312 using namespace N1;<br>
313 using namespace N2;<br>
314 <br>
315 <font color="#008b00"><b>void</b></font>&nbsp;h()<br>
316 {<br>
317 &nbsp;&nbsp; x =&nbsp;<font color="#ff8c00">1</font>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// error -- ambiguous: N1::x or N2::x?</i></font><br>
318 &nbsp;&nbsp; N1::x =&nbsp;<font color="#ff8c00">1</font>;&nbsp;<font color="#0000ee"><i>// OK</i></font><br>
319 &nbsp;&nbsp; N2::x =&nbsp;<font color="#ff8c00">1</font>;&nbsp;<font color="#0000ee"><i>// OK</i></font><br>
320 &nbsp;&nbsp; y =&nbsp;<font color="#ff8c00">1</font>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// OK&nbsp;&nbsp;-- this is N2::y</i></font><br>
321 &nbsp;&nbsp; g(<font color="#ff8c00">0</font>);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// error -- ambiguous: N1::g(int) or N2::g(int)?</i></font><br>
322 &nbsp;&nbsp; f(<font color="#ff8c00">0</font>);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// OK -- N1::f(int), because it is the &quot;best&quot; match&nbsp;</i></font><br>
323 &nbsp;&nbsp; f(<font color="#ff8c00">0.0</font>);&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// OK&nbsp;&nbsp;-- N2::f(double), because it is the &quot;best&quot; match</i></font><br>
324 }<br>
325 <br>
326 </font>
327 </font></td></tr></table><p><p>
328 <!-- }}} ENDPRETTY -->
329
249330
250331 <p>
251332 This example illustrates the interaction between using declarations
271352 and NTL is "wrapped" in namespace <tt>NTL</tt>.
272353 Thus, the header file <tt>&lt;NTL/ZZ.h&gt;</tt> in ISO mode looks
273354 something like this:
274 <pre>
275
355
356 <!-- STARTPLAIN
276357 namespace NTL {
277358
278359 // ...
281362
282363 // ...
283364
284 ZZ operator+(const ZZ&amp; a, const ZZ&amp; b);
285 ZZ operator*(const ZZ&amp; a, const ZZ&amp; b);
286
287 std::istream&amp; operator>>(std::istream&amp; s, ZZ&amp; x);
288 std::ostream&amp; operator<<(std::ostream&amp; s, const ZZ&amp; a);
365 ZZ operator+(const ZZ& a, const ZZ& b);
366 ZZ operator*(const ZZ& a, const ZZ& b);
367
368 std::istream& operator>>(std::istream& s, ZZ& x);
369 std::ostream& operator<<(std::ostream& s, const ZZ& a);
289370
290371 // ...
291372
292373
293374 }
294375
295 </pre>
376 ENDPLAIN -->
377 <!-- STARTPRETTY {{{ -->
378 <p><p><table cellPadding=10px><tr><td><font color="#000000">
379 <font face="monospace">
380 namespace NTL {<br>
381 <br>
382 &nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// ...</i></font><br>
383 <br>
384 &nbsp;&nbsp; class ZZ {&nbsp;<font color="#0000ee"><i>/*</i></font><font color="#0000ee"><i>&nbsp;...&nbsp;</i></font><font color="#0000ee"><i>*/</i></font>&nbsp;};<br>
385 <br>
386 &nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// ...</i></font><br>
387 <br>
388 &nbsp;&nbsp; ZZ operator+(<font color="#008b00"><b>const</b></font>&nbsp;ZZ&amp; a,&nbsp;<font color="#008b00"><b>const</b></font>&nbsp;ZZ&amp; b);<br>
389 &nbsp;&nbsp; ZZ operator*(<font color="#008b00"><b>const</b></font>&nbsp;ZZ&amp; a,&nbsp;<font color="#008b00"><b>const</b></font>&nbsp;ZZ&amp; b);<br>
390 <br>
391 &nbsp;&nbsp; std::istream&amp; operator&gt;&gt;(std::istream&amp; s, ZZ&amp; x);<br>
392 &nbsp;&nbsp; std::ostream&amp; operator&lt;&lt;(std::ostream&amp; s,&nbsp;<font color="#008b00"><b>const</b></font>&nbsp;ZZ&amp; a);<br>
393 <br>
394 &nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// ...</i></font><br>
395 <br>
396 &nbsp;&nbsp;<br>
397 }<br>
398 <br>
399 </font>
400 </font></td></tr></table><p><p>
401 <!-- }}} ENDPRETTY -->
402
296403
297404 Therefore, one must explicitly qualify all names, or use appropriate
298405 using directives.
300407 of the tour in
301408 ISO mode.
302409
303 <pre>
304
305 #include &lt;NTL/ZZ.h&gt;
410 <!-- STARTPLAIN
411 #include <NTL/ZZ.h>
306412
307413 int main()
308414 {
309415 NTL::ZZ a, b, c;
310416
311 std::cin &gt;&gt; a;
312 std::cin &gt;&gt; b;
417 std::cin >> a;
418 std::cin >> b;
313419 c = (a+1)*(b+1);
314 std::cout &lt;&lt; c &lt;&lt; "\n";
315 }
316
317 </pre>
420 std::cout << c << "\n";
421 }
422
423 ENDPLAIN -->
424 <!-- STARTPRETTY {{{ -->
425 <p><p><table cellPadding=10px><tr><td><font color="#000000">
426 <font face="monospace">
427 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/ZZ.h&gt;</font><br>
428 <br>
429 <font color="#008b00"><b>int</b></font>&nbsp;main()<br>
430 {<br>
431 &nbsp;&nbsp; NTL::ZZ a, b, c;&nbsp;<br>
432 <br>
433 &nbsp;&nbsp; std::cin &gt;&gt; a;&nbsp;<br>
434 &nbsp;&nbsp; std::cin &gt;&gt; b;&nbsp;<br>
435 &nbsp;&nbsp; c = (a+<font color="#ff8c00">1</font>)*(b+<font color="#ff8c00">1</font>);<br>
436 &nbsp;&nbsp; std::cout &lt;&lt; c &lt;&lt;&nbsp;<font color="#4a708b">&quot;</font><font color="#8a2be2">\n</font><font color="#4a708b">&quot;</font>;<br>
437 }<br>
438 <br>
439 </font>
440 </font></td></tr></table><p><p>
441 <!-- }}} ENDPRETTY -->
442
318443
319444 <p>
320445 Notice how everything is explicitly qualified.
331456 be a bit tedious.
332457 Here is the same example, this time with using directives.
333458
334 <pre>
335
336 #include &lt;NTL/ZZ.h&gt;
459 <!-- STARTPLAIN
460 #include <NTL/ZZ.h>
337461
338462 using namespace NTL;
339463 using namespace std;
342466 {
343467 ZZ a, b, c;
344468
345 cin &gt;&gt; a;
346 cin &gt;&gt; b;
469 cin >> a;
470 cin >> b;
347471 c = (a+1)*(b+1);
348 cout &lt;&lt; c &lt;&lt; "\n";
349 }
350
351 </pre>
472 cout << c << "\n";
473 }
474
475 ENDPLAIN -->
476 <!-- STARTPRETTY {{{ -->
477 <p><p><table cellPadding=10px><tr><td><font color="#000000">
478 <font face="monospace">
479 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/ZZ.h&gt;</font><br>
480 <br>
481 using namespace NTL;<br>
482 using namespace std;<br>
483 <br>
484 <font color="#008b00"><b>int</b></font>&nbsp;main()<br>
485 {<br>
486 &nbsp;&nbsp; ZZ a, b, c;&nbsp;<br>
487 <br>
488 &nbsp;&nbsp; cin &gt;&gt; a;&nbsp;<br>
489 &nbsp;&nbsp; cin &gt;&gt; b;&nbsp;<br>
490 &nbsp;&nbsp; c = (a+<font color="#ff8c00">1</font>)*(b+<font color="#ff8c00">1</font>);<br>
491 &nbsp;&nbsp; cout &lt;&lt; c &lt;&lt;&nbsp;<font color="#4a708b">&quot;</font><font color="#8a2be2">\n</font><font color="#4a708b">&quot;</font>;<br>
492 }<br>
493 <br>
494 </font>
495 </font></td></tr></table><p><p>
496 <!-- }}} ENDPRETTY -->
497
352498
353499 To write NTL client code that will compile smoothly in either
354500 Traditional or ISO mode, one simply does the following:
355501
356 <pre>
357
358 #include &lt;NTL/ZZ.h&gt;
502 <!-- STARTPLAIN
503 #include <NTL/ZZ.h>
359504
360505 NTL_CLIENT
361506
363508 {
364509 ZZ a, b, c;
365510
366 cin &gt;&gt; a;
367 cin &gt;&gt; b;
511 cin >> a;
512 cin >> b;
368513 c = (a+1)*(b+1);
369 cout &lt;&lt; c &lt;&lt; "\n";
370 }
371
372 </pre>
514 cout << c << "\n";
515 }
516
517 ENDPLAIN -->
518 <!-- STARTPRETTY {{{ -->
519 <p><p><table cellPadding=10px><tr><td><font color="#000000">
520 <font face="monospace">
521 <font color="#1874cd">#include&nbsp;</font><font color="#4a708b">&lt;NTL/ZZ.h&gt;</font><br>
522 <br>
523 NTL_CLIENT<br>
524 <br>
525 <font color="#008b00"><b>int</b></font>&nbsp;main()<br>
526 {<br>
527 &nbsp;&nbsp; ZZ a, b, c;&nbsp;<br>
528 <br>
529 &nbsp;&nbsp; cin &gt;&gt; a;&nbsp;<br>
530 &nbsp;&nbsp; cin &gt;&gt; b;&nbsp;<br>
531 &nbsp;&nbsp; c = (a+<font color="#ff8c00">1</font>)*(b+<font color="#ff8c00">1</font>);<br>
532 &nbsp;&nbsp; cout &lt;&lt; c &lt;&lt;&nbsp;<font color="#4a708b">&quot;</font><font color="#8a2be2">\n</font><font color="#4a708b">&quot;</font>;<br>
533 }<br>
534 <br>
535 </font>
536 </font></td></tr></table><p><p>
537 <!-- }}} ENDPRETTY -->
538
373539
374540 <p>
375541 Here, <tt>NTL_CLIENT</tt> is a macro defined by NTL
378544 <tt>NTL_PSTD_NNS</tt>, and <tt>NTL_PSTD_NHF</tt>.
379545 Alternatively, instead of using the <tt>NTL_CLIENT</tt> macro,
380546 you can write:
381 <p>
382
383 <pre>
547
548 <!-- STARTPLAIN
384549 #if (defined(NTL_PSTD_NNS) || defined(NTL_STD_CXX))
385550 using namespace NTL;
386551 #endif
388553 #if (defined(NTL_PSTD_NHF) || defined(NTL_STD_CXX))
389554 using namespace std;
390555 #endif
391 </pre>
556 ENDPLAIN -->
557 <!-- STARTPRETTY {{{ -->
558 <p><p><table cellPadding=10px><tr><td><font color="#000000">
559 <font face="monospace">
560 <font color="#1874cd">#if (defined(NTL_PSTD_NNS) || defined(NTL_STD_CXX))</font><br>
561 &nbsp;&nbsp; using namespace NTL;<br>
562 <font color="#1874cd">#endif</font><br>
563 <br>
564 <font color="#1874cd">#if (defined(NTL_PSTD_NHF) || defined(NTL_STD_CXX))</font><br>
565 &nbsp;&nbsp; using namespace std;<br>
566 <font color="#1874cd">#endif</font><br>
567 </font>
568 </font></td></tr></table><p><p>
569 <!-- }}} ENDPRETTY -->
570
392571
393572 Typically,
394573 when writing a program that uses NTL,
477656 Specifically, the new header files declare several overloaded versions
478657 of some functions.
479658 For example, in the old header files, there was one function
480 <pre>
659 <!-- STARTPLAIN
481660 int abs(int);
482 </pre>
661 ENDPLAIN -->
662 <!-- STARTPRETTY {{{ -->
663 <p><p><table cellPadding=10px><tr><td><font color="#000000">
664 <font face="monospace">
665 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>int</b></font>&nbsp;abs(<font color="#008b00"><b>int</b></font>);<br>
666 </font>
667 </font></td></tr></table><p><p>
668 <!-- }}} ENDPRETTY -->
669
483670 Now there are several, including:
484 <pre>
671 <!-- STARTPLAIN
485672 int abs(int);
486673 long abs(long);
487674 float abs(float);
488675 double abs(double);
489676 long double abs(long double);
490 </pre>
677 ENDPLAIN -->
678 <!-- STARTPRETTY {{{ -->
679 <p><p><table cellPadding=10px><tr><td><font color="#000000">
680 <font face="monospace">
681 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>int</b></font>&nbsp;abs(<font color="#008b00"><b>int</b></font>);<br>
682 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;abs(<font color="#008b00"><b>long</b></font>);<br>
683 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>float</b></font>&nbsp;abs(<font color="#008b00"><b>float</b></font>);<br>
684 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>double</b></font>&nbsp;abs(<font color="#008b00"><b>double</b></font>);<br>
685 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;<font color="#008b00"><b>double</b></font>&nbsp;abs(<font color="#008b00"><b>long</b></font>&nbsp;<font color="#008b00"><b>double</b></font>);<br>
686 </font>
687 </font></td></tr></table><p><p>
688 <!-- }}} ENDPRETTY -->
689
491690 Also, functions like <tt>log</tt> and <tt>sqrt</tt> are also overloaded.
492691 So instead of just
493 <pre>
692 <!-- STARTPLAIN
494693 double log(double);
495 </pre>
694 ENDPLAIN -->
695 <!-- STARTPRETTY {{{ -->
696 <p><p><table cellPadding=10px><tr><td><font color="#000000">
697 <font face="monospace">
698 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>double</b></font>&nbsp;log(<font color="#008b00"><b>double</b></font>);<br>
699 </font>
700 </font></td></tr></table><p><p>
701 <!-- }}} ENDPRETTY -->
702
496703 there are
497 <pre>
704 <!-- STARTPLAIN
498705 float log(float);
499706 double log(double);
500707 long double log(long double);
501 </pre>
708 ENDPLAIN -->
709 <!-- STARTPRETTY {{{ -->
710 <p><p><table cellPadding=10px><tr><td><font color="#000000">
711 <font face="monospace">
712 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>float</b></font>&nbsp;log(<font color="#008b00"><b>float</b></font>);<br>
713 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>double</b></font>&nbsp;log(<font color="#008b00"><b>double</b></font>);<br>
714 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;<font color="#008b00"><b>double</b></font>&nbsp;log(<font color="#008b00"><b>long</b></font>&nbsp;<font color="#008b00"><b>double</b></font>);<br>
715 </font>
716 </font></td></tr></table><p><p>
717 <!-- }}} ENDPRETTY -->
718
502719
503720 <p>
504721 This can lead to compile-time errors in some old codes, such as:
505 <pre>
722 <!-- STARTPLAIN
506723 double log_2 = log(2);
507 </pre>
724 ENDPLAIN -->
725 <!-- STARTPRETTY {{{ -->
726 <p><p><table cellPadding=10px><tr><td><font color="#000000">
727 <font face="monospace">
728 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>double</b></font>&nbsp;log_2 = log(<font color="#ff8c00">2</font>);<br>
729 </font>
730 </font></td></tr></table><p><p>
731 <!-- }}} ENDPRETTY -->
732
508733
509734 <p>
510735 With the old header files, the <tt>int</tt> value 2 would have
511736 been converted to a <tt>double</tt>, and the function
512 <pre>
737 <!-- STARTPLAIN
513738 double log(double);
514 </pre>
739 ENDPLAIN -->
740 <!-- STARTPRETTY {{{ -->
741 <p><p><table cellPadding=10px><tr><td><font color="#000000">
742 <font face="monospace">
743 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>double</b></font>&nbsp;log(<font color="#008b00"><b>double</b></font>);<br>
744 </font>
745 </font></td></tr></table><p><p>
746 <!-- }}} ENDPRETTY -->
747
515748 would have been called.
516749 <p>
517750 With the new header files, the compiler would raise an error,
518751 because the function call is now ambiguous.
519752 <p>
520753 Of course, the fix is trivial:
521 <pre>
754 <!-- STARTPLAIN
522755 double log_2 = log(2.0);
523 </pre>
756 ENDPLAIN -->
757 <!-- STARTPRETTY {{{ -->
758 <p><p><table cellPadding=10px><tr><td><font color="#000000">
759 <font face="monospace">
760 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>double</b></font>&nbsp;log_2 = log(<font color="#ff8c00">2.0</font>);<br>
761 </font>
762 </font></td></tr></table><p><p>
763 <!-- }}} ENDPRETTY -->
764
524765 This will compile correctly with either old or new header files.
525766
526767 <p>
33 A Tour of NTL: Programming Interface </title>
44 </head>
55
6 <body bgcolor="#fff9e6">
76 <center>
87 <a href="tour-examples.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
98 <a href="tour.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a>
137136 a functional form, and a procedural form.
138137 For example:
139138
140 <pre>
139 <!-- STARTPLAIN
141140 ZZ x, a, n;
142141 x = InvMod(a, n); // functional form
143142 InvMod(x, a, n); // procedural form
144 </pre>
143 ENDPLAIN -->
144 <!-- STARTPRETTY {{{ -->
145 <p><p><table cellPadding=10px><tr><td><font color="#000000">
146 <font face="monospace">
147 &nbsp;&nbsp; ZZ x, a, n;<br>
148 &nbsp;&nbsp; x = InvMod(a, n);&nbsp;&nbsp;<font color="#0000ee"><i>// functional form</i></font><br>
149 &nbsp;&nbsp; InvMod(x, a, n);&nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// procedural form</i></font><br>
150 </font>
151 </font></td></tr></table><p><p>
152 <!-- }}} ENDPRETTY -->
153
145154
146155 <p>
147156 This example illustrates the normal way these two forms differ
151160 First, if there is a operator that can play the role of the
152161 functional form, that is the notation used:
153162
154 <pre>
163 <!-- STARTPLAIN
155164 ZZ x, a, b;
156165 x = a + b; // functional form
157166 add(x, a, b); // procedural form
158 </pre>
167 ENDPLAIN -->
168 <!-- STARTPRETTY {{{ -->
169 <p><p><table cellPadding=10px><tr><td><font color="#000000">
170 <font face="monospace">
171 &nbsp;&nbsp; ZZ x, a, b;<br>
172 &nbsp;&nbsp; x = a + b;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// functional form</i></font><br>
173 &nbsp;&nbsp; add(x, a, b);&nbsp;<font color="#0000ee"><i>// procedural form</i></font><br>
174 </font>
175 </font></td></tr></table><p><p>
176 <!-- }}} ENDPRETTY -->
177
159178
160179 Second, if the functional form's name would be ambiguous,
161180 the return type is simply appended to its name:
162181
163 <pre>
182 <!-- STARTPLAIN
164183 ZZ_p x;
165184 x = random_ZZ_p(); // functional form
166185 random(x); // procedural form
167 </pre>
186 ENDPLAIN -->
187 <!-- STARTPRETTY {{{ -->
188 <p><p><table cellPadding=10px><tr><td><font color="#000000">
189 <font face="monospace">
190 &nbsp;&nbsp; ZZ_p x;<br>
191 &nbsp;&nbsp; x = random_ZZ_p();&nbsp;&nbsp;<font color="#0000ee"><i>// functional form</i></font><br>
192 &nbsp;&nbsp; random(x);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// procedural form</i></font><br>
193 </font>
194 </font></td></tr></table><p><p>
195 <!-- }}} ENDPRETTY -->
196
168197
169198 Third, there are a number of conversion functions (see below), whose name
170199 in procedural form is <tt>conv</tt>, but whose name in
171200 functional form is <tt>conv&lt;T&gt;</tt>, where <tt>T</tt> is the return type:
172201
173 <pre>
202 <!-- STARTPLAIN
174203 ZZ x;
175204 double a;
176205
177 x = conv&lt;ZZ&gt;(a); // functional form
206 x = conv<ZZ>(a); // functional form
178207 conv(x, a); // procedural form
179 </pre>
208 ENDPLAIN -->
209 <!-- STARTPRETTY {{{ -->
210 <p><p><table cellPadding=10px><tr><td><font color="#000000">
211 <font face="monospace">
212 &nbsp;&nbsp; ZZ x;&nbsp;&nbsp;<br>
213 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>double</b></font>&nbsp;a;<br>
214 <br>
215 &nbsp;&nbsp; x = conv&lt;ZZ&gt;(a);&nbsp;&nbsp;<font color="#0000ee"><i>// functional form</i></font><br>
216 &nbsp;&nbsp; conv(x, a);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// procedural form</i></font><br>
217 </font>
218 </font></td></tr></table><p><p>
219 <!-- }}} ENDPRETTY -->
220
180221
181222
182223
234275 to get the effect of automatic "promotions".
235276 For example:
236277
237 <pre>
278 <!-- STARTPLAIN
238279 ZZ x, a;
239280
240281 x = a + 1;
241 if (x &lt; 0)
282 if (x < 0)
242283 mul(x, 2, a);
243284 else
244285 x = -1;
245 </pre>
286 ENDPLAIN -->
287 <!-- STARTPRETTY {{{ -->
288 <p><p><table cellPadding=10px><tr><td><font color="#000000">
289 <font face="monospace">
290 &nbsp;&nbsp; ZZ x, a;<br>
291 <br>
292 &nbsp;&nbsp; x = a +&nbsp;<font color="#ff8c00">1</font>;<br>
293 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(x &lt;&nbsp;<font color="#ff8c00">0</font>)&nbsp;<br>
294 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mul(x,&nbsp;<font color="#ff8c00">2</font>, a);<br>
295 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>else</b></font><br>
296 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = -<font color="#ff8c00">1</font>;<br>
297 </font>
298 </font></td></tr></table><p><p>
299 <!-- }}} ENDPRETTY -->
300
246301
247302 <p>
248303
250305 usually using a kind of "short hand" notation.
251306 For example:
252307
253 <pre>
254 ZZ operator+(const ZZ&amp; a, const ZZ&amp; b);
308 <!-- STARTPLAIN
309 ZZ operator+(const ZZ& a, const ZZ& b);
255310
256311 // PROMOTIONS: operator + promotes long to ZZ on (a, b).
257 </pre>
312 ENDPLAIN -->
313 <!-- STARTPRETTY {{{ -->
314 <p><p><table cellPadding=10px><tr><td><font color="#000000">
315 <font face="monospace">
316 ZZ operator+(<font color="#008b00"><b>const</b></font>&nbsp;ZZ&amp; a,&nbsp;<font color="#008b00"><b>const</b></font>&nbsp;ZZ&amp; b);<br>
317 <br>
318 <font color="#0000ee"><i>// PROMOTIONS: operator + promotes long to ZZ on (a, b).</i></font><br>
319 </font>
320 </font></td></tr></table><p><p>
321 <!-- }}} ENDPRETTY -->
322
258323
259324 This means that in addition to the declared function, there
260325 are two other functions that are logically equivalent to the following:
261 <pre>
262 ZZ operator+(long a, const ZZ&amp; b) { return ZZ(a) + b; }
263 ZZ operator+(const ZZ&amp; a, long b) { return a + ZZ(b); }
264 </pre>
326 <!-- STARTPLAIN
327 ZZ operator+(long a, const ZZ& b) { return ZZ(a) + b; }
328 ZZ operator+(const ZZ& a, long b) { return a + ZZ(b); }
329 ENDPLAIN -->
330 <!-- STARTPRETTY {{{ -->
331 <p><p><table cellPadding=10px><tr><td><font color="#000000">
332 <font face="monospace">
333 ZZ operator+(<font color="#008b00"><b>long</b></font>&nbsp;a,&nbsp;<font color="#008b00"><b>const</b></font>&nbsp;ZZ&amp; b) {&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;ZZ(a) + b; }<br>
334 ZZ operator+(<font color="#008b00"><b>const</b></font>&nbsp;ZZ&amp; a,&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;b) {&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;a + ZZ(b); }<br>
335 </font>
336 </font></td></tr></table><p><p>
337 <!-- }}} ENDPRETTY -->
338
265339
266340 <p>
267341 Note that this is not how NTL actually implements these functions.
268342 It is in generally more efficient to write
269 <pre>
343 <!-- STARTPLAIN
270344 x = y + 2;
271 </pre>
345 ENDPLAIN -->
346 <!-- STARTPRETTY {{{ -->
347 <p><p><table cellPadding=10px><tr><td><font color="#000000">
348 <font face="monospace">
349 &nbsp;&nbsp; x = y +&nbsp;<font color="#ff8c00">2</font>;<br>
350 </font>
351 </font></td></tr></table><p><p>
352 <!-- }}} ENDPRETTY -->
353
272354 than it is to write
273 <pre>
355 <!-- STARTPLAIN
274356 x = y + ZZ(2);
275 </pre>
357 ENDPLAIN -->
358 <!-- STARTPRETTY {{{ -->
359 <p><p><table cellPadding=10px><tr><td><font color="#000000">
360 <font face="monospace">
361 &nbsp;&nbsp; x = y + ZZ(<font color="#ff8c00">2</font>);<br>
362 </font>
363 </font></td></tr></table><p><p>
364 <!-- }}} ENDPRETTY -->
365
276366 The former notation avoids the creation and destruction
277367 of a temporary <tt>ZZ</tt>
278368 object to hold the value 2.
279369
280370 <p>
281371 Also, don't have any inhibitions about writing tests like
282 <pre>
372 <!-- STARTPLAIN
283373 if (x == 0) ...
284 </pre>
374 ENDPLAIN -->
375 <!-- STARTPRETTY {{{ -->
376 <p><p><table cellPadding=10px><tr><td><font color="#000000">
377 <font face="monospace">
378 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(x ==&nbsp;<font color="#ff8c00">0</font>) ...<br>
379 </font>
380 </font></td></tr></table><p><p>
381 <!-- }}} ENDPRETTY -->
382
285383 and assignments like
286 <pre>
384 <!-- STARTPLAIN
287385 x = 1;
288 </pre>
386 ENDPLAIN -->
387 <!-- STARTPRETTY {{{ -->
388 <p><p><table cellPadding=10px><tr><td><font color="#000000">
389 <font face="monospace">
390 &nbsp;&nbsp; x =&nbsp;<font color="#ff8c00">1</font>;&nbsp;<br>
391 </font>
392 </font></td></tr></table><p><p>
393 <!-- }}} ENDPRETTY -->
394
289395 These are all optimized, and do not execute significaltly slower
290396 than the "lower level" (and much less natural)
291 <pre>
397 <!-- STARTPLAIN
292398 if (IsZero(x)) ...
293 </pre>
399 ENDPLAIN -->
400 <!-- STARTPRETTY {{{ -->
401 <p><p><table cellPadding=10px><tr><td><font color="#000000">
402 <font face="monospace">
403 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(IsZero(x)) ...<br>
404 </font>
405 </font></td></tr></table><p><p>
406 <!-- }}} ENDPRETTY -->
407
294408 and
295 <pre>
409 <!-- STARTPLAIN
296410 set(x);
297 </pre>
411 ENDPLAIN -->
412 <!-- STARTPRETTY {{{ -->
413 <p><p><table cellPadding=10px><tr><td><font color="#000000">
414 <font face="monospace">
415 &nbsp;&nbsp; set(x);<br>
416 </font>
417 </font></td></tr></table><p><p>
418 <!-- }}} ENDPRETTY -->
419
298420
299421 <p>
300422 Some types have even more promotions.
318440 For a given type, there is a natural, fixed set of types
319441 that promote to it.
320442 Here is the complete list:
321 <pre>
322 destination: source
443 <!-- STARTPLAIN
444 destination source
323445
324 xdouble: double
325 quad_float: double
326 RR: double
327 ZZ: long
328 ZZ_p: long
329 ZZ_pX: long, ZZ_p
330 zz_p: long
331 zz_pX: long, zz_p
332 ZZX: long, ZZ
333 GF2: long
334 GF2X: long, GF2
335 GF2E: long, GF2
336 GF2EX: long, GF2, GF2E
337 ZZ_pE: long, ZZ_p
338 ZZ_pEX: long, ZZ_p, ZZ_pE
339 zz_pE: long, zz_p
340 zz_pEX: long, zz_p, zz_pE
341 </pre>
446 xdouble double
447 quad_float double
448 RR double
449 ZZ long
450 ZZ_p long
451 ZZ_pX long, ZZ_p
452 zz_p long
453 zz_pX long, zz_p
454 ZZX long, ZZ
455 GF2 long
456 GF2X long, GF2
457 GF2E long, GF2
458 GF2EX long, GF2, GF2E
459 ZZ_pE long, ZZ_p
460 ZZ_pEX long, ZZ_p, ZZ_pE
461 zz_pE long, zz_p
462 zz_pEX long, zz_p, zz_pE
463 ENDPLAIN -->
464 <!-- STARTPRETTY {{{ -->
465 <p><p><table cellPadding=10px><tr><td><font color="#000000">
466 <font face="monospace">
467 &nbsp;&nbsp; destination&nbsp;&nbsp;source<br>
468 &nbsp;&nbsp;&nbsp;<br>
469 &nbsp;&nbsp; xdouble&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#008b00"><b>double</b></font><br>
470 &nbsp;&nbsp; quad_float&nbsp;&nbsp;&nbsp;<font color="#008b00"><b>double</b></font><br>
471 &nbsp;&nbsp; RR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#008b00"><b>double</b></font><br>
472 &nbsp;&nbsp; ZZ&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font><br>
473 &nbsp;&nbsp; ZZ_p&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font><br>
474 &nbsp;&nbsp; ZZ_pX&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>, ZZ_p<br>
475 &nbsp;&nbsp; zz_p&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font><br>
476 &nbsp;&nbsp; zz_pX&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>, zz_p<br>
477 &nbsp;&nbsp; ZZX&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>, ZZ<br>
478 &nbsp;&nbsp; GF2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font><br>
479 &nbsp;&nbsp; GF2X&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>, GF2<br>
480 &nbsp;&nbsp; GF2E&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>, GF2<br>
481 &nbsp;&nbsp; GF2EX&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>, GF2, GF2E<br>
482 &nbsp;&nbsp; ZZ_pE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>, ZZ_p<br>
483 &nbsp;&nbsp; ZZ_pEX&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>, ZZ_p, ZZ_pE<br>
484 &nbsp;&nbsp; zz_pE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>, zz_p<br>
485 &nbsp;&nbsp; zz_pEX&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>, zz_p, zz_pE<br>
486 </font>
487 </font></td></tr></table><p><p>
488 <!-- }}} ENDPRETTY -->
489
342490
343491 <p>
344492 All the promotions are documented, but here
349497 <li>
350498 All classes provide explicit constructors for promoted types.
351499 For example,
352 <pre>
500 <!-- STARTPLAIN
353501 ZZ w = ZZ(1);
354502 ZZ x(1); // allowed
355503 ZZ y{1}; // allowed in C++11
356504 ZZ z = 1; // not allowed
357 </pre>
505 ENDPLAIN -->
506 <!-- STARTPRETTY {{{ -->
507 <p><p><table cellPadding=10px><tr><td><font color="#000000">
508 <font face="monospace">
509 &nbsp;&nbsp; ZZ w = ZZ(<font color="#ff8c00">1</font>);<br>
510 &nbsp;&nbsp; ZZ x(<font color="#ff8c00">1</font>);&nbsp;&nbsp;<font color="#0000ee"><i>// allowed</i></font><br>
511 &nbsp;&nbsp; ZZ y{<font color="#ff8c00">1</font>};&nbsp;&nbsp;<font color="#0000ee"><i>// allowed in C++11</i></font><br>
512 &nbsp;&nbsp; ZZ z =&nbsp;<font color="#ff8c00">1</font>;&nbsp;<font color="#0000ee"><i>// not allowed</i></font><br>
513 </font>
514 </font></td></tr></table><p><p>
515 <!-- }}} ENDPRETTY -->
516
358517
359518 <li>
360519 Promotions apply uniformly to both procedural and functional
361520 forms, as well as to the corresponding assignment operator forms.
362521 E.g.,
363 <pre>
522 <!-- STARTPLAIN
364523 x = x + 2;
365524 add(x, x, 2);
366525 x += 2;
367 </pre>
526 ENDPLAIN -->
527 <!-- STARTPRETTY {{{ -->
528 <p><p><table cellPadding=10px><tr><td><font color="#000000">
529 <font face="monospace">
530 &nbsp;&nbsp; x = x +&nbsp;<font color="#ff8c00">2</font>;<br>
531 &nbsp;&nbsp; add(x, x,&nbsp;<font color="#ff8c00">2</font>);<br>
532 &nbsp;&nbsp; x +=&nbsp;<font color="#ff8c00">2</font>;<br>
533 </font>
534 </font></td></tr></table><p><p>
535 <!-- }}} ENDPRETTY -->
536
368537
369538 <li>
370539 The addition, subtraction, multiplication, equality and comparison
371540 routines always promote both arguments. E.g.,
372 <pre>
541 <!-- STARTPLAIN
373542 x = 2 + y;
374543 add(x, 2, y);
375 if (3 &gt; x || y == 5) ...
376 </pre>
544 if (3 > x || y == 5) ...
545 ENDPLAIN -->
546 <!-- STARTPRETTY {{{ -->
547 <p><p><table cellPadding=10px><tr><td><font color="#000000">
548 <font face="monospace">
549 &nbsp;&nbsp; x =&nbsp;<font color="#ff8c00">2</font>&nbsp;+ y;<br>
550 &nbsp;&nbsp; add(x,&nbsp;<font color="#ff8c00">2</font>, y);<br>
551 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>if</b></font>&nbsp;(<font color="#ff8c00">3</font>&nbsp;&gt; x || y ==&nbsp;<font color="#ff8c00">5</font>) ...<br>
552 </font>
553 </font></td></tr></table><p><p>
554 <!-- }}} ENDPRETTY -->
555
377556
378557 <li>
379558 The assignment operator always promotes the right-hand side.
380559 E.g.,
381 <pre>
560 <!-- STARTPLAIN
382561 x = 2;
383 </pre>
562 ENDPLAIN -->
563 <!-- STARTPRETTY {{{ -->
564 <p><p><table cellPadding=10px><tr><td><font color="#000000">
565 <font face="monospace">
566 &nbsp;&nbsp; x =&nbsp;<font color="#ff8c00">2</font>;<br>
567 </font>
568 </font></td></tr></table><p><p>
569 <!-- }}} ENDPRETTY -->
570
384571
385572 <li>
386573 For non-integer, non-polynomial types, the division routine
387574 promotes both arguments.
388575 E.g.,
389 <pre>
576 <!-- STARTPLAIN
390577 RR x, y, z;
391578 ...
392579 x = 1.0/y;
393580 z = y/2.0;
394 </pre>
581 ENDPLAIN -->
582 <!-- STARTPRETTY {{{ -->
583 <p><p><table cellPadding=10px><tr><td><font color="#000000">
584 <font face="monospace">
585 &nbsp;&nbsp; RR x, y, z;<br>
586 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...<br>
587 &nbsp;&nbsp; x =&nbsp;<font color="#ff8c00">1.0</font>/y;<br>
588 &nbsp;&nbsp; z = y/<font color="#ff8c00">2.0</font>;<br>
589 </font>
590 </font></td></tr></table><p><p>
591 <!-- }}} ENDPRETTY -->
592
395593
396594 For integer or polynomial types, the division routine
397595 promotes the denominator only. E.g.,
405603 <li>
406604 Matrix by scalar and vector by scalar multiplication promote the scalar.
407605 E.g.,
408 <pre>
409 Vec&lt;ZZ&gt; v, w;
606 <!-- STARTPLAIN
607 Vec<ZZ> v, w;
410608 ...
411609 v = w*2;
412610 v = 2*w;
413611 v *= 2;
414 </pre>
612 ENDPLAIN -->
613 <!-- STARTPRETTY {{{ -->
614 <p><p><table cellPadding=10px><tr><td><font color="#000000">
615 <font face="monospace">
616 &nbsp;&nbsp; Vec&lt;ZZ&gt; v, w;<br>
617 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...<br>
618 &nbsp;&nbsp; v = w*<font color="#ff8c00">2</font>;<br>
619 &nbsp;&nbsp; v =&nbsp;<font color="#ff8c00">2</font>*w;<br>
620 &nbsp;&nbsp; v *=&nbsp;<font color="#ff8c00">2</font>;<br>
621 </font>
622 </font></td></tr></table><p><p>
623 <!-- }}} ENDPRETTY -->
624
415625
416626
417627 <li>
419629 and the corresponding <tt>SetCoeff</tt> routines
420630 promote the coefficient argument.
421631 E.g.,
422 <pre>
632 <!-- STARTPLAIN
423633 ZZX f;
424634 f = ZZX(INIT_MONO, 3, 5); // f == 5*X^3
425635 SetCoeff(f, 0, 2); // f == 5*x^3 + 2;
426 </pre>
636 ENDPLAIN -->
637 <!-- STARTPRETTY {{{ -->
638 <p><p><table cellPadding=10px><tr><td><font color="#000000">
639 <font face="monospace">
640 &nbsp;&nbsp; ZZX f;<br>
641 &nbsp;&nbsp; f = ZZX(INIT_MONO,&nbsp;<font color="#ff8c00">3</font>,&nbsp;<font color="#ff8c00">5</font>);&nbsp;&nbsp;<font color="#0000ee"><i>// f == 5*X^3</i></font><br>
642 &nbsp;&nbsp; SetCoeff(f,&nbsp;<font color="#ff8c00">0</font>,&nbsp;<font color="#ff8c00">2</font>);&nbsp;&nbsp;<font color="#0000ee"><i>// f == 5*x^3 + 2;</i></font><br>
643 </font>
644 </font></td></tr></table><p><p>
645 <!-- }}} ENDPRETTY -->
646
427647
428648 <li>
429649 In module <tt>ZZ</tt>, the modular arithmetic routines, as well as
431651 There are also several other routines in module <tt>ZZ</tt>
432652 that have both <tt>ZZ</tt> and <tt>long</tt> versions, e.g.,
433653 <tt>NumBits</tt>, <tt>bit</tt>, <tt>weight</tt>.
434 Check the documentation in <a href="ZZ.txt"><tt>ZZ.txt</tt></a>
654 Check the documentation in <a href="ZZ.cpp.html"><tt>ZZ.cpp.html</tt></a>
435655 for complete details.
436656
437657 </ul>
457677 The safest way to do this is to apply an explicit conversion operator,
458678 and not to rely on promotions.
459679 For example, consider
460 <pre>
680 <!-- STARTPLAIN
461681 ZZ a; double x;
462682
463683 a = a + x;
464 </pre>
684 ENDPLAIN -->
685 <!-- STARTPRETTY {{{ -->
686 <p><p><table cellPadding=10px><tr><td><font color="#000000">
687 <font face="monospace">
688 &nbsp;&nbsp; ZZ a;&nbsp;<font color="#008b00"><b>double</b></font>&nbsp;x;<br>
689 <br>
690 &nbsp;&nbsp; a = a + x;<br>
691 </font>
692 </font></td></tr></table><p><p>
693 <!-- }}} ENDPRETTY -->
694
465695 This is equivialent to
466 <pre>
696 <!-- STARTPLAIN
467697 a = a + long(x);
468 </pre>
698 ENDPLAIN -->
699 <!-- STARTPRETTY {{{ -->
700 <p><p><table cellPadding=10px><tr><td><font color="#000000">
701 <font face="monospace">
702 &nbsp;&nbsp; a = a +&nbsp;<font color="#008b00"><b>long</b></font>(x);<br>
703 </font>
704 </font></td></tr></table><p><p>
705 <!-- }}} ENDPRETTY -->
706
469707 and to
470 <pre>
708 <!-- STARTPLAIN
471709 a = a + ZZ(x);
472 </pre>
710 ENDPLAIN -->
711 <!-- STARTPRETTY {{{ -->
712 <p><p><table cellPadding=10px><tr><td><font color="#000000">
713 <font face="monospace">
714 &nbsp;&nbsp; a = a + ZZ(x);<br>
715 </font>
716 </font></td></tr></table><p><p>
717 <!-- }}} ENDPRETTY -->
718
473719 One could also use an explicit conversion function:
474 <pre>
475 a = a + conv&lt;ZZ&gt;(x);
476 </pre>
720 <!-- STARTPLAIN
721 a = a + conv<ZZ>(x);
722 ENDPLAIN -->
723 <!-- STARTPRETTY {{{ -->
724 <p><p><table cellPadding=10px><tr><td><font color="#000000">
725 <font face="monospace">
726 &nbsp;&nbsp; a = a + conv&lt;ZZ&gt;(x);<br>
727 </font>
728 </font></td></tr></table><p><p>
729 <!-- }}} ENDPRETTY -->
730
477731 This last version guarantees that there is no loss of precision,
478732 and also guarantees that the floor of <tt>x</tt> is computed.
479733 With the first version, one may lose precision when <tt>x</tt>
503757 Another pitfall too avoid is initialzing <tt>ZZ</tt>'s
504758 with integer constants that are too big.
505759 Consider the following:
506 <pre>
760 <!-- STARTPLAIN
507761 ZZ x;
508762 x = 1234567890123456789012;
509 </pre>
763 ENDPLAIN -->
764 <!-- STARTPRETTY {{{ -->
765 <p><p><table cellPadding=10px><tr><td><font color="#000000">
766 <font face="monospace">
767 &nbsp;&nbsp; ZZ x;<br>
768 &nbsp;&nbsp; x =&nbsp;<font color="#ff8c00">1234567890123456789012</font>;<br>
769 </font>
770 </font></td></tr></table><p><p>
771 <!-- }}} ENDPRETTY -->
772
510773 This integer constant is too big, and this overflow
511774 condition may or may not cause your compiler to give
512775 you a warning or an error.
513776 The easiest way to introduce such large constants into your
514777 program is as follows:
515 <pre>
778 <!-- STARTPLAIN
516779 ZZ x;
517 x = conv&lt;ZZ&gt;("1234567890123456789012");
518 </pre>
780 x = conv<ZZ>("1234567890123456789012");
781 ENDPLAIN -->
782 <!-- STARTPRETTY {{{ -->
783 <p><p><table cellPadding=10px><tr><td><font color="#000000">
784 <font face="monospace">
785 &nbsp;&nbsp; ZZ x;<br>
786 &nbsp;&nbsp; x = conv&lt;ZZ&gt;(<font color="#4a708b">&quot;1234567890123456789012&quot;</font>);<br>
787 </font>
788 </font></td></tr></table><p><p>
789 <!-- }}} ENDPRETTY -->
790
519791 Conversion functions are provided for converting <tt>C</tt> character strings
520792 to the types <tt>ZZ</tt>, <tt>RR</tt>, <tt>quad_float</tt>,
521793 and <tt>xdouble</tt>.
590862 It is safe to declare global objects of any NTL type
591863 as long as one uses only the default constructor.
592864 For example, the global declarations
593 <pre>
865 <!-- STARTPLAIN
594866 ZZ global_integer;
595 Vec&lt;ZZ_p&gt; global_vector;
596 </pre>
867 Vec<ZZ_p> global_vector;
868 ENDPLAIN -->
869 <!-- STARTPRETTY {{{ -->
870 <p><p><table cellPadding=10px><tr><td><font color="#000000">
871 <font face="monospace">
872 &nbsp;&nbsp; ZZ global_integer;<br>
873 &nbsp;&nbsp; Vec&lt;ZZ_p&gt; global_vector;<br>
874 </font>
875 </font></td></tr></table><p><p>
876 <!-- }}} ENDPRETTY -->
877
597878 should always work, since their initialization only involves
598879 setting a pointer to 0.
599880 However,
612893 should not matter too much.
613894 There is, however, one possible exception to this.
614895 A programmer might want to have a global constant initialized like this:
615 <pre>
616 const quad_float Pi = conv&lt;quad_float&gt;("3.1415926535897932384626433832795029");
617 </pre>
896 <!-- STARTPLAIN
897 const quad_float Pi = conv<quad_float>("3.1415926535897932384626433832795029");
898 ENDPLAIN -->
899 <!-- STARTPRETTY {{{ -->
900 <p><p><table cellPadding=10px><tr><td><font color="#000000">
901 <font face="monospace">
902 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>const</b></font>&nbsp;quad_float Pi = conv&lt;quad_float&gt;(<font color="#4a708b">&quot;3.1415926535897932384626433832795029&quot;</font>);<br>
903 </font>
904 </font></td></tr></table><p><p>
905 <!-- }}} ENDPRETTY -->
906
618907 While this probably will work fine on most platforms,
619908 it may not be an entirely portable construction,
620909 since it will involve a non-trivial computation before
622911 A more portable strategy
623912 is to define a function returning a read-only
624913 reference:
625 <pre>
626 const quad_float&amp; Pi()
914 <!-- STARTPLAIN
915 const quad_float& Pi()
627916 {
628917 static quad_float pi =
629 conv&lt;quad_float&gt;("3.1415926535897932384626433832795029");
918 conv<quad_float>("3.1415926535897932384626433832795029");
630919 return pi;
631920 }
632 </pre>
921 ENDPLAIN -->
922 <!-- STARTPRETTY {{{ -->
923 <p><p><table cellPadding=10px><tr><td><font color="#000000">
924 <font face="monospace">
925 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>const</b></font>&nbsp;quad_float&amp; Pi()<br>
926 &nbsp;&nbsp; {<br>
927 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#008b00"><b>static</b></font>&nbsp;quad_float pi =&nbsp;<br>
928 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; conv&lt;quad_float&gt;(<font color="#4a708b">&quot;3.1415926535897932384626433832795029&quot;</font>);<br>
929 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;pi;<br>
930 &nbsp;&nbsp; }<br>
931 </font>
932 </font></td></tr></table><p><p>
933 <!-- }}} ENDPRETTY -->
934
633935 and then call the function <tt>Pi()</tt> to get a read-only reference
634936 to this constant value:
635 <pre>
937 <!-- STARTPLAIN
636938 area = Pi()*r*r;
637 </pre>
939 ENDPLAIN -->
940 <!-- STARTPRETTY {{{ -->
941 <p><p><table cellPadding=10px><tr><td><font color="#000000">
942 <font face="monospace">
943 &nbsp;&nbsp; area = Pi()*r*r;<br>
944 </font>
945 </font></td></tr></table><p><p>
946 <!-- }}} ENDPRETTY -->
947
638948 The initialization will then take place the first time <tt>Pi()</tt>
639949 is called, which is presumably after <tt>main()</tt> starts,
640950 and so everything should work fine.
662972 residue class types.
663973 For example, for <tt>ZZ_p</tt>, you can set the current modulus to <tt>p</tt>
664974 as follows:
665 <pre>
975 <!-- STARTPLAIN
666976 ZZ_p::init(p);
667 </pre>
977 ENDPLAIN -->
978 <!-- STARTPRETTY {{{ -->
979 <p><p><table cellPadding=10px><tr><td><font color="#000000">
980 <font face="monospace">
981 &nbsp;&nbsp; ZZ_p::init(p);<br>
982 </font>
983 </font></td></tr></table><p><p>
984 <!-- }}} ENDPRETTY -->
985
668986 The current modulus <i>must</i> be initialized before any operations
669987 on <tt>ZZ_p</tt>'s are performed. The modulus may be changed, and a mechanism is provided
670988 for saving and restoring a modulus.
673991 Here is what you do to save the current modulus, temporarily
674992 set it to p, and automatically restore it:
675993
676 <pre>
994 <!-- STARTPLAIN
677995 {
678996 ZZ_pPush push(p);
679997
680998 ...
681999
6821000 }
683 </pre>
1001 ENDPLAIN -->
1002 <!-- STARTPRETTY {{{ -->
1003 <p><p><table cellPadding=10px><tr><td><font color="#000000">
1004 <font face="monospace">
1005 &nbsp;&nbsp; {&nbsp;<br>
1006 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ZZ_pPush push(p);&nbsp;<br>
1007 <br>
1008 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...<br>
1009 <br>
1010 &nbsp;&nbsp; }<br>
1011 </font>
1012 </font></td></tr></table><p><p>
1013 <!-- }}} ENDPRETTY -->
1014
6841015
6851016 The constructor for push will save the current modulus, and install <tt>p</tt> as the
6861017 current modulus. The destructor for push will restore the old modulus when the
6901021 <p>
6911022 You could also do the following:
6921023
693 <pre>
1024 <!-- STARTPLAIN
6941025 {
6951026 ZZ_pPush push(); // just backup current modulus
6961027
7041035
7051036 // reinstall original modulus as close of scope
7061037 }
707 </pre>
1038 ENDPLAIN -->
1039 <!-- STARTPRETTY {{{ -->
1040 <p><p><table cellPadding=10px><tr><td><font color="#000000">
1041 <font face="monospace">
1042 &nbsp;&nbsp; {<br>
1043 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ZZ_pPush push();&nbsp;<font color="#0000ee"><i>// just backup current modulus</i></font><br>
1044 <br>
1045 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...<br>
1046 <br>
1047 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ZZ_p::init(p1);&nbsp;<font color="#0000ee"><i>// install p1&nbsp;</i></font><br>
1048 <br>
1049 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...<br>
1050 <br>
1051 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ZZ_p::init(p2);&nbsp;<font color="#0000ee"><i>// install p2</i></font><br>
1052 <br>
1053 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ee"><i>// reinstall original modulus as close of scope</i></font><br>
1054 &nbsp;&nbsp; }<br>
1055 </font>
1056 </font></td></tr></table><p><p>
1057 <!-- }}} ENDPRETTY -->
1058
7081059
7091060
7101061 <p>
33 A Tour of NTL: Some Performance Data </title>
44 </head>
55
6 <body bgcolor="#fff9e6">
76
87 <center>
98 <a href="tour-gf2x.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
44 </title>
55 </head>
66
7 <body bgcolor="#fff9e6">
87 <center>
98 <a href="tour-win.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
109 <a href="tour.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a>
6463 In time-critical code, avoid creating unnecessary temporary
6564 objects.
6665 For example, instead of
67 <pre>
66 <!-- STARTPLAIN
6867 ZZ InnerProduct(const ZZ *a, const ZZ *b, long n)
6968 {
7069 long i;
7372 res += a[i] * b[i];
7473 return res;
7574 }
76 </pre>
75 ENDPLAIN -->
76 <!-- STARTPRETTY {{{ -->
77 <p><p><table cellPadding=10px><tr><td><font color="#000000">
78 <font face="monospace">
79 ZZ InnerProduct(<font color="#008b00"><b>const</b></font>&nbsp;ZZ *a,&nbsp;<font color="#008b00"><b>const</b></font>&nbsp;ZZ *b,&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;n)<br>
80 {<br>
81 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;i;<br>
82 &nbsp;&nbsp; ZZ res;<br>
83 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>for</b></font>&nbsp;(i =&nbsp;<font color="#ff8c00">0</font>; i &lt; n; i++)<br>
84 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res += a[i] * b[i];<br>
85 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;res;<br>
86 }<br>
87 </font>
88 </font></td></tr></table><p><p>
89 <!-- }}} ENDPRETTY -->
90
7791 write this as
78 <pre>
92 <!-- STARTPLAIN
7993 ZZ InnerProduct(const ZZ *a, const ZZ *b, long n)
8094 {
8195 long i;
86100 }
87101 return res;
88102 }
89 </pre>
103 ENDPLAIN -->
104 <!-- STARTPRETTY {{{ -->
105 <p><p><table cellPadding=10px><tr><td><font color="#000000">
106 <font face="monospace">
107 ZZ InnerProduct(<font color="#008b00"><b>const</b></font>&nbsp;ZZ *a,&nbsp;<font color="#008b00"><b>const</b></font>&nbsp;ZZ *b,&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;n)<br>
108 {<br>
109 &nbsp;&nbsp;&nbsp;<font color="#008b00"><b>long</b></font>&nbsp;i;<br>
110 &nbsp;&nbsp; ZZ res, t;<br>
111 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>for</b></font>&nbsp;(i =&nbsp;<font color="#ff8c00">0</font>; i &lt; n; i++) {<br>
112 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mul(t, a[i], b[i]);<br>
113 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add(res, res, t);<br>
114 &nbsp;&nbsp; }<br>
115 &nbsp;&nbsp;&nbsp;<font color="#b03060"><b>return</b></font>&nbsp;res;<br>
116 }<br>
117 </font>
118 </font></td></tr></table><p><p>
119 <!-- }}} ENDPRETTY -->
120
90121 The first version of <tt>InnerProduct</tt>
91122 creates and destroys a temporary object, holding the value
92123 <tt>a[i]*b[i]</tt>, in every loop iteration.
101132 too often, as this can be a rather expensive operation.
102133 If you <i>must</i> switch the modulus often,
103134 use the class <tt>ZZ_pContext</tt> to save the information
104 associated with the modulus (see <a href="ZZ_p.txt">ZZ_p.txt</a>).
135 associated with the modulus (see <a href="ZZ_p.cpp.html">ZZ_p.txt</a>).
105136
106137
107138
33 A Tour of NTL: Obtaining and Installing NTL for UNIX </title>
44 </head>
55
6 <body bgcolor="#fff9e6">
76 <center>
87 <a href="tour-stdcxx.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
98 <a href="tour.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a>
44 and other Platforms </title>
55 </head>
66
7 <body bgcolor="#fff9e6">
87 <center>
98 <a href="tour-unix.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
109 <a href="tour.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a>
33 A Tour of NTL </title>
44 </head>
55
6 <body bgcolor="#fff9e6">
76 <h1>
87 <p align=center>
98 A Tour of NTL
0
1 // TODO: add constructor that uses BlockConstructFromObj
20
31 #ifndef NTL_vector__H
42 #define NTL_vector__H
423421 if (n >= allocated()) pos = position(a);
424422 AllocateTo(n);
425423 if (pos != -1) src = elts() + pos;
426 Init(n, a, *src);
424 Init(n, *src);
427425 AdjustLength(n);
428426 }
429427
55
66 #define NTL_MAJOR_VERSION (6)
77 #define NTL_MINOR_VERSION (2)
8 #define NTL_REVISION (0)
8 #define NTL_REVISION (1)
99
1010 #endif
1111
0 ntl-6.2.0
0 ntl-6.2.1
0 4:0:0
0 5:0:0
0 WinNTL-6_2_0
0 WinNTL-6_2_1