File: polynomials.tex

package info (click to toggle)
form 4.2.1%2Bgit20200217-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,500 kB
  • sloc: ansic: 101,613; cpp: 9,375; sh: 1,582; makefile: 505
file content (622 lines) | stat: -rw-r--r-- 18,767 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622

\chapter{Polynomials and Factorization}
\label{polynomials}

\noindent Starting with version 4, FORM is equipped with powerful handling 
of rational polynomials and with factorization capabilities. Because this 
creates many new possibilities, it brings a whole new category of commands 
with it. We will list most of these here.

\noindent First there are the rational polynomials. These work a bit like 
the PolyFun~\ref{substapolyfun}, but now with two arguments: a numerator and 
a denominator. Instead of PolyFun the function is designated as 
PolyRatFun~\ref{substapolyratfun} as in the example below:
\begin{verbatim}
    Symbol x,y;
    CFunction rat;
    PolyRatFun rat;
    L   F = rat(x+y,x-y)+rat(x-y,x+y);
    Print;
    .end

   F =
      rat(2*x^2 + 2*y^2,x^2 - y^2);
\end{verbatim}
Dealing with a PolyRatFun can be very handy, but one should realize that 
there is a limit to the size of the arguments, because the PolyRatFun with 
its arguments is part of a term and hence is limited by the maximum size of 
a term~\ref{setupmaxtermsize}. One should also take into account that the 
manipulation of multivariate polynomials, and in particular the GCD 
operation, can be rather time consuming.

\noindent The PolyRatFun has one limitation as compared to the regular 
PolyFun: in its arguments one may use only symbols. Of course FORM is 
equipped with a mechanism to replace other objects by extra internally 
generated symbols~\ref{substaextrasymbols}. One could imagine FORM to 
automatically convert these objects to symbols, do the polynomial 
arithmetic and then convert back. This is done with factorization and the 
gcd\_~\ref{fungcd}\index{gcd\_}\index{function!gcd\_}, 
div\_~\ref{fundiv}\index{div\_}\index{function!div\_} 
and rem\_~\ref{funrem}\index{rem\_}\index{function!rem\_} functions. But 
because the addition of PolyRatFun's is such a frequent event, this would 
be very costly in time. Hence it is better that the user does this once 
in a controlled way.

\noindent The PolyFun and PolyRatFun declarations are mutually exclusive. 
The PolyRatFun is considered a special type of PolyFun and there can be 
only one PolyFun at any moment. If one wants to switch back to a mode in 
which there is neither a PolyFun nor a PolyRatFun one can use
\begin{verbatim}
   PolyRatFun;
\end{verbatim}
to indicate that after this there is no function with that status.

\noindent When a PolyRatFun has only a single argument, this argument is 
interpreted as the numerator of a fraction. FORM will add automatically a 
second argument which has the value 1.

\noindent The second important polynomial facility is factorization. This 
is not necessarily something trivial. First of all, with very lengthy 
multivariate input, this can be unpractically slow. Second of all, there 
are various types of objects that we may factorize and each has its special 
needs. One of those needs is access to the factors, which is different for 
the factors of function arguments, of \$-expressions or even complete 
expressions. In addition \$-expressions should be factorizable either from 
the preprocessor or on a term by term basis. Let us start with function 
arguments.

\noindent One can factorize function arguments with the FactArg 
statement~\ref{substafactarg}. The factors are each represented by a 
separate argument as in
\begin{verbatim}
    Symbol x,y;
    CFunction f1,f2;
    Local F = f1(x^4-y^4)+f2(3*y^4-3*x^4);
    FactArg,f1,f2;
    Print;
    .end

   F=
      f1(y-x,y+x,y^2+x^2,-1)+f2(y-x,y+x,y^2+x^2,3);
\end{verbatim}
Overall constants and overall signs are taken separately as one can see. If 
one wants the factors in separate functions one can use the 
ChainOut~\ref{substachainout} command as in
\begin{verbatim}
    Symbol x,y;
    CFunction f1,f2;
    Local F = f2(3*y^4-3*x^4);
    FactArg,f2;
    Print;
    .sort

   F=
      f2(y-x,y+x,y^2+x^2,3);

    ChainOut,f2;
    id f2(x?number_) = x;
    Print;
    .end

   F=
      3*f2(y-x)*f2(y+x)*f2(y^2+x^2);
\end{verbatim}

\noindent Factorization of expressions is a bit more complicated. Clearly 
this cannot be a command at the term level. Hence we had two options on how 
to implement this. One would have been as a preprocessor instruction, which 
we did not select, and the other is as some type of format statement, which 
is what we did opt for. In the case we factorize an expression, the 
original unfactorized expression is replaced by the factorized version. 
After that we keep the factorized version only and that may bring some 
restrictions with it. Of course, in the same way one can factorize an 
expression, one can unfactorize it. The corresponding statements are 
Factorize~\ref{substafactorize}, NFactorize~\ref{substanfactorize}, 
UnFactorize~\ref{substaunfactorize} and 
NUnFactorize~\ref{substanunfactorize}. These statements are used at the end 
of the module in the same place as one might use the bracket 
statement~\ref{substabracket}. It should be noticed however that a 
factorized expression will never apply the bracket mechanism. They are 
mutually exclusive, because internally we use the bracket mechanism with a 
built in symbol factor\_ to indicate the factors. Here is an example:
\begin{verbatim}
    Symbol x,y;
    Local F = x^4-y^4;
    Print;
    .sort

Time =       0.00 sec    Generated terms =          2
               F         Terms in output =          2
                         Bytes used      =         64

   F=
      -y^4+x^4;

    Print;
    Factorize F;
    .end

Time =       0.00 sec    Generated terms =          2
               F         Terms in output =          2
                         Bytes used      =         64

Time =       0.00 sec    Generated terms =          7
               F         Terms in output =          7
               factorize Bytes used      =        288

   F=
       (-1)
      *(y-x)
      *(y+x)
      *(y^2+x^2);
\end{verbatim}
We have printed the statistics in this example to show that the 
factorization prints its own statistics. This factorization is executed 
after the expression has been completed and before manipulations on the 
next expression start. This way it is possible to overwrite the first 
output by the factorized output and we do not loose diskspace 
unnecessarily.

\noindent The next question is of course how to find out how many factors 
an expression has and how to access individual factors. There is a function 
numfactors\_ which gives the number of factors in an expression:
\begin{verbatim}
    Symbol x,y;
    Local F1 = x^4-y^4;
    Local F2 = 0;
    Local F3 = 1;
    Local F4 = x^4-y^4;
    Print;
    Factorize F1,F2,F3;
    .sort

   F1=
       (-1)
      *(y-x)
      *(y+x)
      *(y^2+x^2);
    
   F2=0;

   F3=
       (1);

   F4=
      -y^4+x^4;
    #do i = 1,4
    #$n`i' = numfactors_(F`i');
    #message expression F`i' has `$n`i'' factors
~~~expression F1 has 4 factors
    #enddo
~~~expression F2 has 1 factors
~~~expression F3 has 1 factors
~~~expression F4 has 0 factors
    .end
\end{verbatim}
As we see, an expression that is zero still gives one factor when it is 
factorized. When the expression is not factorized it will return 0 in all 
cases. The factors can be accessed easily once one knows that the factors 
are stored by means of the bracket mechanism and the n-th factor is the 
bracket with the n-th power of the symbol factor\_ outside the bracket:
\begin{verbatim}
    Symbol x,y;
    Local F = x^4-y^4;
    Factorize F;
    .sort
    #$n = numfactors_(F);
    #do i = 1,`$n'
    Local F`i' = F[factor_^`i'];
    #enddo
    Print;
    .end

   F=
       (-1)
      *(y-x)
      *(y+x)
      *(y^2+x^2);

   F1=
      -1;

   F2=
      y-x;

   F3=
      y+x;

   F4=
      y^2+x^2;
\end{verbatim}

\noindent It is also possible to put an expression in the input in a 
factorized format. For this we have the 
LocalFactorized~\ref{substalfactorized} and 
GlobalFactorized~\ref{substagfactorized} commands. These commands can 
be abbreviated to LFactorized, GFactorized or even LF and GF. One should 
notice that these commands do not execute a factorization. They accept the 
factors as the user provides them:
\begin{verbatim}
    Symbol x,y;
    LocalFactorize E = -(x+1)*(x+2)*((x+3)*(x+4));
    Print;
    .end

   E =
         (  - 1 )
       * ( 1 + x )
       * ( 2 + x )
       * ( 12 + 7*x + x^2 );
\end{verbatim}
\noindent This can go to some extremes when we feed in expressions 
containing powers and expressions that are potentially already factorized:
\begin{verbatim}
    Symbol x,y;
    LocalFactorize E = -(x+1)*(x+2)*((x+3)*(x+4));
    Local   F = -(x+1)*(x+2)*((x+3)*(x+4));
    Print;
    .sort

   E=
       (-1)
      *(1+x)
      *(2+x)
      *(12+7*x+x^2);

   F=
      -24-50*x-35*x^2-10*x^3-x^4;

    LF G = (x-1)*(x+2)^2*E^2*F^2;
    Print G;
    .end

   G=
       (-1+x)
      *(2+x)
      *(2+x)
      *(-1)
      *(1+x)
      *(2+x)
      *(12+7*x+x^2)
      *(-1)
      *(1+x)
      *(2+x)
      *(12+7*x+x^2)
      *(-24-50*x-35*x^2-10*x^3-x^4)
      *(-24-50*x-35*x^2-10*x^3-x^4);
\end{verbatim}
\noindent To put some order in this one may factorize the new expression 
again:
\begin{verbatim}
    Symbol x,y;
    LocalFactorize E = -(x+1)*(x+2)*((x+3)*(x+4));
    Local   F = -(x+1)*(x+2)*((x+3)*(x+4));
    .sort
    LF G = (x-1)*(x+2)^2*E^2*F^2;
    Print G;
    Factorize G;
    .end

   G=
       (-1+x)
      *(1+x)
      *(1+x)
      *(1+x)
      *(1+x)
      *(2+x)
      *(2+x)
      *(2+x)
      *(2+x)
      *(2+x)
      *(2+x)
      *(3+x)
      *(3+x)
      *(3+x)
      *(3+x)
      *(4+x)
      *(4+x)
      *(4+x)
      *(4+x);
\end{verbatim}
\noindent In this case all constants are multiplied, all factors are 
factorized, and all factors in the new format are sorted.

\noindent The case that one or more factors are zero is special. In 
principle the zero factors are kept as in:
\begin{verbatim}
    Symbol x,y;
    LocalFactorize E = -0*(x+1)*(x+2)*0*((x+3)*(x+4));
    Print;
    .end

   E=
       (-1)
      *(0)
      *(1+x)
      *(2+x)
      *(0)
      *(12+7*x+x^2);
\end{verbatim}
\noindent This way one can see what has happened when a substitution makes 
a factor zero. When we factorize this expression again however the whole 
expression becomes zero. If this is not intended and one would like to 
continue with the factors that are nonzero we have the keepzero option in 
the factorize statement as in:
\begin{verbatim}
    Symbol x,y;
    Format Nospaces;
    LocalFactorize E = -0*3*(x+1)*(x+2)/2*0*((x+3)*(x+4));
    Print;
    .sort

   E=
       (-1)
      *(0)
      *(3)
      *(1+x)
      *(2+x)
      *(1/2)
      *(0)
      *(12+7*x+x^2);
    Print;
    Factorize(keepzero) E;
    .end

   E=
       (0)
      *(-3/2)
      *(1+x)
      *(2+x)
      *(3+x)
      *(4+x);
\end{verbatim}
\noindent We see here that first all constants are separate factors and the 
new factorization combines them. The keepzero option does the same with the 
factors that are zero. The zero factor will always be the first. Hence it 
is rather easy to test for whether the total expression should actually be 
zero. We just have to look whether \verb:E[factor_]: is zero.

\noindent The unfactorize~\ref{substaunfactorize} statement is the opposite 
of the factorize statement. It takes the factorized expression and 
multiplies out the factors. It also uses the current brackets for 
formatting the output.
\begin{verbatim}
    Symbol x,y;
    LFactorized F = (x+1)*(x+y)*(y+1);
    Print;
    .sort

   F=
       (1+x)
      *(y+x)
      *(1+y);

    Print;
    Bracket x;
    UnFactorize F;
    .end

   F=
      +x*(1+2*y+y^2)
      +x^2*(1+y)
      +y+y^2;
\end{verbatim}
\noindent In principle there are various models by which the 
unfactorization can be done in an efficient way. In addition it would be 
less efficient when the master would do all the work as is the case with 
the factorize statement. Currently this statement is still being developed 
internally. It is possible to make ones own emulation of it. Here we give 
the `brute force' way:
\begin{verbatim}
    Symbol x,y;
    LFactorized F = (x+1)*(x+y)*(y+1);
    Print;
    .sort

   F=
       (1+x)
      *(y+x)
      *(1+y);

    #$num = numfactors_(F);
    Local   G = <F[factor_^1]>*...*<F[factor_^`$num']>;
    Bracket x;
    Print;
    .end

   F=
       (1+x)
      *(y+x)
      *(1+y);

   G=
      +x*(1+2*y+y^2)
      +x^2*(1+y)
      +y+y^2;
\end{verbatim}

\noindent Factorization of \$-expressions is yet a different thing. The 
\$-expressions do not have a bracket mechanism. Hence we need different 
ways of storing the factors. In the case of expressions we have to work in 
a way that is potentially disk based. With \$-expressions we work in 
allocated memory. Hence we also store the factors in allocated memory. In 
that case we can keep both the original and the factors. The factors are 
accessed by referring to their number between braces. The number zero 
refers to the number of factors:
\begin{verbatim}
    Symbol x,y;
    CFunction f;
    Off Statistics;
    #$a = x^4-y^4;
    Local F = f(x^4-y^4)+f(x^6-y^6);
    Print;
    .sort

   F=
      f(-y^4+x^4)+f(-y^6+x^6);
    #factdollar $a;
    #do i = 1,`$a[0]'
    #write <> "Factor `i' of `$a' is `$a[`i']'"
Factor 1 of -y^4+x^4 is -1
    #enddo
Factor 2 of -y^4+x^4 is y-x
Factor 3 of -y^4+x^4 is y+x
Factor 4 of -y^4+x^4 is y^2+x^2
    id  f(x?$b) = f(x);
    FactDollar $b;
    do $i = 1,$b[0];
      Print "Factor %$ of %$ is %$",$i,$b,$b[$i];
    enddo;
    Print;
    .end
Factor 1 of -y^4+x^4 is -1
Factor 2 of -y^4+x^4 is y-x
Factor 3 of -y^4+x^4 is y+x
Factor 4 of -y^4+x^4 is y^2+x^2
Factor 1 of -y^6+x^6 is -1
Factor 2 of -y^6+x^6 is y-x
Factor 3 of -y^6+x^6 is y+x
Factor 4 of -y^6+x^6 is y^2-x*y+x^2
Factor 5 of -y^6+x^6 is y^2+x*y+x^2

   F=
      f(-y^4+x^4)+f(-y^6+x^6);
\end{verbatim}
\noindent We see here a variety of new features. The preprocessor can 
factorize \$a with the \#FactDollar instruction. We do indeed pick up the 
number of factors in the preprocessor as `\$a[0]' and the factors 
themselves as `\$a[1]' etc. For the \$-variable that needs to be 
manipulated during running time things as a bit more complicated. We define 
\$b as part of a wildcard pattern matching. This is still rather normal. 
Then we use the FactDollar statement. Notice that for each term we will 
have a different \$b. To access the factors we cannot use the preprocessor 
methods because those are only available at compile time. Hence we cannot 
use the preprocessor \#do instruction and therefore we need an execution 
time do statement. The loop parameter will have to be a \$-variable as 
well. The do statement and the print statement show now how one can use the 
factors. In the output one can see that indeed we had two different 
contents for \$b. And the arguments of the function f remain unaffected.

\noindent One may also ask for the number of factors in a \$-expression 
with the numfactors\_ function as in:
\begin{verbatim}
    Symbol x,y;
    CFunction f;
    Format Nospaces;
    #$a = x^4-y^4;
    #factdollar $a;
    Local F = f(numfactors_($a))
        +f(<$a[1]>,...,<$a[`$a[0]']>);
    Print;
    .end

   F=
      f(-1,y-x,y+x,y^2+x^2)+f(4);
\end{verbatim}
\noindent Note that in the second case we need to use the construction 
`\$a[0]' because the preprocessor needs to substitute the number 
immediately in order to expand the triple dot operator. This cannot wait 
till execution time.

\noindent Some remarks.

\noindent The time needed for a factorization depends strongly on the 
number of variables used. For example factorization of $x^{60}-1$ is much 
faster than factorization of $x^{60}-y^{60}$. One could argue that the 
second formula can be converted into the first, but there is a limit to 
what FORM should do and what the user should do.
\begin{verbatim}
    Symbol x,y;
    Format NoSpaces;
    On ShortStats;
    Local F1 = x^60-1;
    Local F2 = y^60-x^60;
    Factorize F1,F2;
    Print;
    .end
      0.00s        1>         2-->         2:        52 F1 
      0.07s        1>        51-->        51:      1524 F1 factorize
      0.07s        1>         2-->         2:        64 F2 
      1.17s        1>        51-->        51:      1944 F2 factorize

   F1=
       (-1+x)
      *(1-x+x^2)
      *(1-x+x^2-x^3+x^4)
      *(1-x+x^3-x^4+x^5-x^7+x^8)
      *(1+x)
      *(1+x+x^2)
      *(1+x+x^2+x^3+x^4)
      *(1+x-x^3-x^4-x^5+x^7+x^8)
      *(1-x^2+x^4)
      *(1-x^2+x^4-x^6+x^8)
      *(1+x^2)
      *(1+x^2-x^6-x^8-x^10+x^14+x^16);

   F2=
       (y-x)
      *(y+x)
      *(y^2-x*y+x^2)
      *(y^4-x*y^3+x^2*y^2-x^3*y+x^4)
      *(y^4+x*y^3+x^2*y^2+x^3*y+x^4)
      *(y^2+x*y+x^2)
      *(y^2+x^2)
      *(y^8-x*y^7+x^3*y^5-x^4*y^4+x^5*y^3-x^7*y+x^8)
      *(y^8+x*y^7-x^3*y^5-x^4*y^4-x^5*y^3+x^7*y+x^8)
      *(y^8-x^2*y^6+x^4*y^4-x^6*y^2+x^8)
      *(y^4-x^2*y^2+x^4)
      *(y^16+x^2*y^14-x^6*y^10-x^8*y^8-x^10*y^6+x^14*y^2+x^16);
\end{verbatim}

\noindent When one has a factorized expression and one uses the multiply 
statement, all terms in the factorized expression are multiplied the 
specified amount. This may lead to a counterintuitive result:
\begin{verbatim}
    Symbols a,b;
    LF F = (a+b)^2;
    multiply 2;
    Print;
    .end

   F =
       ( 2*b + 2*a )
       * ( 2*b + 2*a );
\end{verbatim}
This is a consequence of the way we store the factors. This way each factor 
will be multiplied by two. If one would like to add a factor one can do 
this by the following simple mechanism:
\begin{verbatim}
    Symbols a,b;
    LF F = (a+b)^2;
    .sort
    LF F = 2*F;
    Print;
    .end

   F =
         ( 2 )
       * ( b + a )
       * ( b + a );
\end{verbatim}

\noindent In version 3 there were some experimental polynomial functions 
like polygcd\_\index{polygcd\_}\index{function!polygcd\_}. These have been 
removed as their functionality has been completely taken over by the new 
functions gcd\_~\ref{fungcd}, div\_~\ref{fundiv} and rem\_~\ref{funrem} and 
some statements like normalize~\ref{substanormalize}, 
makeinteger~\ref{substamakeinteger} and factarg~\ref{substafactarg}. Unlike 
regular functions, the functions gcd\_. div\_ and rem\_ have the 
peculiarity that if one of the arguments is just an expression or a 
\$-expression, this expression is not evaluated until the function is 
evaluated. This means that the evaluated expression does not have to fit 
inside the maximum size reserved for a single term. In some cases, when the 
gcd\_ function is invoked with many arguments, the expression may not have 
to be evaluated at all! The GCD of the other arguments may be one already.

%\begin{verbatim}
%\end{verbatim}
%\begin{verbatim}
%\end{verbatim}
%\begin{verbatim}
%\end{verbatim}