-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtour-ex3.html
361 lines (307 loc) · 11.5 KB
/
tour-ex3.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<html>
<head>
<title>
A Tour of NTL: Examples: Polynomials </title>
</head>
<center>
<a href="tour-ex2.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
<a href="tour-examples.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a>
<a href="tour-ex4.html"> <img src="arrow3.gif" alt="[Next]" align=bottom></a>
</center>
<h1>
<p align=center>
A Tour of NTL: Examples: Polynomials
</p>
</h1>
<p> <hr> <p>
NTL provides extensive support for very fast polynomial arithmetic.
In fact, this was the main motivation for creating NTL in the first place,
because existing computer algebra systems and software
libraries had very slow polynomial arithmetic.
The class <tt>ZZX</tt> represents univariate polynomials
with integer coefficients.
The following program reads a polynomial,
factors it, and prints the factorization.
<!-- STARTPLAIN
#include <NTL/ZZXFactoring.h>
using namespace std;
using namespace NTL;
int main()
{
ZZX f;
cin >> f;
Vec< Pair< ZZX, long > > factors;
ZZ c;
factor(c, factors, f);
cout << c << "\n";
cout << factors << "\n";
}
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000"><font face="monospace">
<font color="#1773cc">#include </font><font color="#4a6f8b"><NTL/ZZXFactoring.h></font><br>
<br>
<font color="#b02f60"><b>using</b></font> <font color="#008b00"><b>namespace</b></font> std;<br>
<font color="#b02f60"><b>using</b></font> <font color="#008b00"><b>namespace</b></font> NTL;<br>
<br>
<font color="#008b00"><b>int</b></font> main()<br>
{<br>
ZZX f;<br>
<br>
cin >> f;<br>
<br>
Vec< Pair< ZZX, <font color="#008b00"><b>long</b></font> > > factors;<br>
ZZ c;<br>
<br>
factor(c, factors, f);<br>
<br>
cout << c << <font color="#4a6f8b">"</font><font color="#8a2ae2">\n</font><font color="#4a6f8b">"</font>;<br>
cout << factors << <font color="#4a6f8b">"</font><font color="#8a2ae2">\n</font><font color="#4a6f8b">"</font>;<br>
}<br>
</font></font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
When this program is compiled an run on input
<pre>
[2 10 14 6]
</pre>
which represents the polynomial <tt>2 + 10*X + 14*x^2 +6*X^3</tt>,
the output is
<pre>
2
[[[1 3] 1] [[1 1] 2]]
</pre>
The first line of output is the content of the polynomial, which
is 2 in this case as each coefficient of the input polynomial
is divisible by 2.
The second line is a vector of pairs, the first member of each
pair is an irreducible factor of the input, and the second
is the exponent to which is appears in the factorization.
Thus, all of the above simply means that
<pre>
2 + 10*X + 14*x^2 +6*X^3 = 2 * (1 + 3*X) * (1 + X)^2
</pre>
<p>
Admittedly, I/O in NTL is not exactly user friendly,
but then NTL has no pretensions about being an interactive
computer algebra system: it is a library for programmers.
<p>
In this example, the type <tt>Vec< Pair< ZZX, long > ></tt>
is an NTL vector whose base type is <tt>Pair< ZZX, long ></tt>.
The type <tt>Pair< ZZX, long ></tt> is the instantiation
of a template "pair" type defined by NTL.
See <a href="pair.cpp.html"><tt>pair.txt</tt></a> for more details.
<p> <hr> <p>
Here is another example.
The following program prints out the first 100 cyclotomic polynomials.
<!-- STARTPLAIN
#include <NTL/ZZX.h>
using namespace std;
using namespace NTL;
int main()
{
Vec<ZZX> phi(INIT_SIZE, 100);
for (long i = 1; i <= 100; i++) {
ZZX t;
t = 1;
for (long j = 1; j <= i-1; j++)
if (i % j == 0)
t *= phi(j);
phi(i) = (ZZX(INIT_MONO, i) - 1)/t;
cout << phi(i) << "\n";
}
}
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000"><font face="monospace">
<br>
<font color="#1773cc">#include </font><font color="#4a6f8b"><NTL/ZZX.h></font><br>
<br>
<font color="#b02f60"><b>using</b></font> <font color="#008b00"><b>namespace</b></font> std;<br>
<font color="#b02f60"><b>using</b></font> <font color="#008b00"><b>namespace</b></font> NTL;<br>
<br>
<font color="#008b00"><b>int</b></font> main()<br>
{<br>
Vec<ZZX> phi(INIT_SIZE, <font color="#ff8b00">100</font>); <br>
<br>
<font color="#b02f60"><b>for</b></font> (<font color="#008b00"><b>long</b></font> i = <font color="#ff8b00">1</font>; i <= <font color="#ff8b00">100</font>; i++) {<br>
ZZX t;<br>
t = <font color="#ff8b00">1</font>;<br>
<br>
<font color="#b02f60"><b>for</b></font> (<font color="#008b00"><b>long</b></font> j = <font color="#ff8b00">1</font>; j <= i-<font color="#ff8b00">1</font>; j++)<br>
<font color="#b02f60"><b>if</b></font> (i % j == <font color="#ff8b00">0</font>)<br>
t *= phi(j);<br>
<br>
phi(i) = (ZZX(INIT_MONO, i) - <font color="#ff8b00">1</font>)/t; <br>
<br>
cout << phi(i) << <font color="#4a6f8b">"</font><font color="#8a2ae2">\n</font><font color="#4a6f8b">"</font>;<br>
}<br>
}<br>
</font></font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
<p>
To illustrate more of the NTL interface, let's look at alternative ways
this routine could have been written.
<p>
First, instead of
<!-- STARTPLAIN
Vec<ZZX> phi(INIT_SIZE, 100);
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
Vec<ZZX> phi(INIT_SIZE, <font color="#ff8c00">100</font>); <br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
one can write
<!-- STARTPLAIN
Vec<ZZX> phi;
phi.SetLength(100);
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
Vec<ZZX> phi;<br>
phi.SetLength(<font color="#ff8c00">100</font>);<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
<p>
Second,
instead of
<!-- STARTPLAIN
t *= phi(j);
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
t *= phi(j);<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
one can write this as
<!-- STARTPLAIN
mul(t, t, phi(j));
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
mul(t, t, phi(j));<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
or
<!-- STARTPLAIN
t = t * phi(j);
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
t = t * phi(j);<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
Also, one can write <tt>phi[j-1]</tt> in place of <tt>phi(j)</tt>.
<p>
Third, instead of
<!-- STARTPLAIN
phi(i) = (ZZX(INIT_MONO, i) - 1)/t;
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
phi(i) = (ZZX(INIT_MONO, i) - <font color="#ff8c00">1</font>)/t; <br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
one can write
<!-- STARTPLAIN
ZZX t1;
SetCoeff(t1, i);
SetCoeff(t1, 0, -1);
div(phi(i), t1, t);
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
ZZX t1;<br>
SetCoeff(t1, i);<br>
SetCoeff(t1, <font color="#ff8c00">0</font>, -<font color="#ff8c00">1</font>);<br>
div(phi(i), t1, t);<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
Alternatively, one could directly access the coefficient vector as follows:
<!-- STARTPLAIN
ZZX t1;
t1.SetLength(i+1); // all vector elements are initialized to zero
t1[i] = 1;
t1[0] = -1;
t1.normalize(); // not necessary here, but good practice in general
div(phi(i), t1, t);
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
ZZX t1;<br>
t1.SetLength(i+<font color="#ff8c00">1</font>); <font color="#0000ee"><i>// all vector elements are initialized to zero</i></font><br>
t1[i] = <font color="#ff8c00">1</font>;<br>
t1[<font color="#ff8c00">0</font>] = -<font color="#ff8c00">1</font>;<br>
t1.normalize(); <font color="#0000ee"><i>// not necessary here, but good practice in general</i></font><br>
div(phi(i), t1, t);<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
Generally, you can freely access the coefficient vector
of a polynomial, as above.
However, after fiddling with this vector, you should "normalize"
the polynomial, so that the leading coefficient is non-zero:
this is an invariant which all routines that work with polynomials
expect to hold.
Of course, if you can avoid directly accessing the
coefficient vector, you should do so.
You can always use the <tt>SetCoeff</tt> routine above to set or
change coefficients, and you can always read the value of a coefficient
using the routine <tt>coeff</tt>, e.g.,
<!-- STARTPLAIN
... f[i] == 1 ...
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
... f[i] == <font color="#ff8c00">1</font> ...<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
is equivalent to
<!-- STARTPLAIN
... coeff(f, i) == 1 ...
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
... coeff(f, i) == <font color="#ff8c00">1</font> ...<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
except that in the latter case, a read-only reference to zero is returned
if the index <tt>i</tt> is out of range.
There are also special-purpose read-only access routines <tt>LeadCoeff(f)</tt>
and <tt>ConstTerm(f)</tt>.
<p>
NTL provides a full compliment of arithmetic operations for polynomials
over the integers, in both operator and procedural form.
All of the basic operations support a "promotion logic" similar
to that for <tt>ZZ</tt>, except that inputs of <i>both</i> types
<tt>long</tt> and <tt>ZZ</tt> are promoted to <tt>ZZX</tt>.
See <a href="ZZX.cpp.html"><tt>ZZX.txt</tt></a> for details,
and see <a href="ZZXFactoring.cpp.html"><tt>ZZXFactoring.txt</tt></a> for details
on the polynomial factoring routines.
<p>
<center>
<a href="tour-ex2.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
<a href="tour-examples.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a>
<a href="tour-ex4.html"> <img src="arrow3.gif" alt="[Next]" align=bottom></a>
</center>
</body>
</html>