forked from vermaseren/form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolywrap.cc
1876 lines (1560 loc) · 46.8 KB
/
polywrap.cc
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file polywrap.cc
*
* Contains methods to call the polynomial methods (written in C++)
* from the rest of Form (written in C). These include polynomial
* gcd computation, factorization and polyratfuns.
*/
/* #[ License : */
/*
* Copyright (C) 1984-2023 J.A.M. Vermaseren
* When using this file you are requested to refer to the publication
* J.A.M.Vermaseren "New features of FORM" math-ph/0010025
* This is considered a matter of courtesy as the development was paid
* for by FOM the Dutch physics granting agency and we would like to
* be able to track its scientific use to convince FOM of its value
* for the community.
*
* This file is part of FORM.
*
* FORM is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* FORM is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with FORM. If not, see <http://www.gnu.org/licenses/>.
*/
/* #] License : */
#include "poly.h"
#include "polygcd.h"
#include "polyfact.h"
#include <iostream>
#include <vector>
#include <map>
#include <climits>
#include <cassert>
//#define DEBUG
#ifdef DEBUG
#include "mytime.h"
#endif
// denompower is increased by this factor when divmod_heap fails
const int POLYWRAP_DENOMPOWER_INCREASE_FACTOR = 2;
using namespace std;
/*
#[ poly_determine_modulus :
*/
/** Modulus for polynomial algebra
*
* Description
* ===========
* This method determines whether polynomial algebra is done with a
* modulus or not. This depends on AC.ncmod. If only_funargs is set
* it also depends on (AC.modmode & ALSOFUNARGS).
*
* The program terminates if the feature is not
* implemented. Polynomial algebra modulo M > WORDSIZE in not
* implemented. If multi_error is set, multivariate algebra mod M is
* not implemented.
* Notes
* =====
* - If AC.ncmod>0 and only_funargs=true and
* AC.modmode&ALSOFUNARGS=false, AN.ncmod is set to zero, for
* otherwise RaisPow calculates mod M.
*/
WORD poly_determine_modulus (PHEAD bool multi_error, bool is_fun_arg, string message) {
if (AC.ncmod==0) return 0;
if (!is_fun_arg || (AC.modmode & ALSOFUNARGS)) {
if (ABS(AC.ncmod)>1) {
MLOCK(ErrorMessageLock);
MesPrint ((char*)"ERROR: %s with modulus > WORDSIZE not implemented",message.c_str());
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
if (multi_error && AN.poly_num_vars>1) {
MLOCK(ErrorMessageLock);
MesPrint ((char*)"ERROR: multivariate %s with modulus not implemented",message.c_str());
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
return *AC.cmod;
}
AN.ncmod = 0;
return 0;
}
/*
#] poly_determine_modulus :
#[ poly_gcd :
*/
/** Polynomial gcd
*
* Description
* ===========
* This method calculates the greatest common divisor of two
* polynomials, given by two zero-terminated Form-style term lists.
*
* Notes
* =====
* - The result is written at newly allocated memory
* - Called from ratio.c
* - Calls polygcd::gcd
*/
WORD *poly_gcd(PHEAD WORD *a, WORD *b, WORD fit) {
#ifdef DEBUG
cout << "*** [" << thetime() << "] CALL : poly_gcd" << endl;
#endif
//
//MesPrint("Calling poly_gcd with:");
//{
// WORD *at = a;
// MesPrint(" a:");
// while ( *at ) {
// MesPrint(" %a",*at,at);
// at += *at;
// }
// MesPrint(" b:");
// at = b;
// while ( *at ) {
// MesPrint(" %a",*at,at);
// at += *at;
// }
//}
// Extract variables
vector<WORD *> e;
e.push_back(a);
e.push_back(b);
poly::get_variables(BHEAD e, false, true);
// Check for modulus calculus
WORD modp=poly_determine_modulus(BHEAD true, true, "polynomial GCD");
// Convert to polynomials
poly pa(poly::argument_to_poly(BHEAD a, false, true), modp, 1);
poly pb(poly::argument_to_poly(BHEAD b, false, true), modp, 1);
// Calculate gcd
poly gcd(polygcd::gcd(pa,pb));
// Allocate new memory and convert to Form notation
int newsize = (gcd.size_of_form_notation()+1);
WORD *res;
if ( fit ) {
if ( newsize*sizeof(WORD) >= (size_t)(AM.MaxTer) ) {
MLOCK(ErrorMessageLock);
MesPrint("poly_gcd: Term too complex. Maybe increasing MaxTermSize can help");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
res = TermMalloc("poly_gcd");
}
else {
res = (WORD *)Malloc1(newsize*sizeof(WORD), "poly_gcd");
}
poly::poly_to_argument(gcd, res, false);
poly_free_poly_vars(BHEAD "AN.poly_vars_qcd");
// reset modulo calculation
AN.ncmod = AC.ncmod;
return res;
}
/*
#] poly_gcd :
#[ poly_divmod :
if fit == 1 the answer must fit inside a term.
*/
WORD *poly_divmod(PHEAD WORD *a, WORD *b, int divmod, WORD fit) {
#ifdef DEBUG
cout << "*** [" << thetime() << "] CALL : poly_divmod" << endl;
#endif
// check for modulus calculus
WORD modp=poly_determine_modulus(BHEAD false, true, "polynomial division");
// get variables
vector<WORD *> e;
e.push_back(a);
e.push_back(b);
poly::get_variables(BHEAD e, false, false);
// add extra variables to keep track of denominators
const int DENOMSYMBOL = MAXPOSITIVE;
// WORD *new_poly_vars = (WORD *)Malloc1((AN.poly_num_vars+1)*sizeof(WORD), "AN.poly_vars");
// WCOPY(new_poly_vars, AN.poly_vars, AN.poly_num_vars);
// new_poly_vars[AN.poly_num_vars] = DENOMSYMBOL;
// if (AN.poly_num_vars > 0)
// M_free(AN.poly_vars, "AN.poly_vars");
// AN.poly_num_vars++;
// AN.poly_vars = new_poly_vars;
AN.poly_vars[AN.poly_num_vars++] = DENOMSYMBOL;
// convert to polynomials
poly dena(BHEAD 0);
poly denb(BHEAD 0);
poly pa(poly::argument_to_poly(BHEAD a, false, true, &dena), modp, 1);
poly pb(poly::argument_to_poly(BHEAD b, false, true, &denb), modp, 1);
// remove contents
poly numres(polygcd::integer_content(pa));
poly denres(polygcd::integer_content(pb));
pa /= numres;
pb /= denres;
if (divmod==0) {
numres *= denb;
denres *= dena;
}
else {
denres = dena;
}
poly gcdres(polygcd::integer_gcd(numres,denres));
numres /= gcdres;
denres /= gcdres;
// determine lcoeff(b)
poly lcoeffb(pb.integer_lcoeff());
poly pres(BHEAD 0);
if (!lcoeffb.is_one()) {
if (AN.poly_num_vars > 2) {
// the original polynomial is multivariate (one dummy variable has
// been added), so it is not trivial to determine which power of
// lcoeff(b) can be in the answer
int denompower = 0, prevdenompower = 0;
poly pq(BHEAD 0);
poly pr(BHEAD 0);
// try denompower = 0, if this fails increase denompower until division succeeds
bool div_fail = true;
do
{
if(denompower < prevdenompower)
{
// denompower increased beyond INT_MAX
MLOCK(ErrorMessageLock);
MesPrint ((char*)"ERROR: pseudo-division failed in poly_divmod (denompower > INT_MAX)");
MUNLOCK(ErrorMessageLock);
Terminate(1);
}
if(denompower != 0)
{
// multiply a by lcoeffb^(denompower-prevdenompower)
WORD n = lcoeffb[lcoeffb[1]];
RaisPow(BHEAD (UWORD *)&lcoeffb[2+AN.poly_num_vars], &n, denompower-prevdenompower);
lcoeffb[1] = 2 + AN.poly_num_vars + ABS(n);
lcoeffb[0] = 1 + lcoeffb[1];
lcoeffb[lcoeffb[1]] = n;
pa *= lcoeffb;
denres *= lcoeffb;
}
// try division
poly ppow(BHEAD 0);
poly::divmod_heap(pa,pb,pq,pr,false,true,div_fail); // sets div_fail
// increase denompower for next iteration
prevdenompower = denompower;
denompower = (denompower==0 ? 1 : denompower*POLYWRAP_DENOMPOWER_INCREASE_FACTOR+1 ); // generates 2^n-1 when POLYWRAP_DENOMPOWER_INCREASE_FACTOR = 2
}
while(div_fail);
pres = (divmod==0 ? pq : pr);
}
else {
// one variable, so the power is the difference of the degrees
int denompower = MaX(0, pa.degree(0) - pb.degree(0) + 1);
// multiply a by that power
WORD n = lcoeffb[lcoeffb[1]];
RaisPow(BHEAD (UWORD *)&lcoeffb[2+AN.poly_num_vars], &n, denompower);
lcoeffb[1] = 2 + AN.poly_num_vars + ABS(n);
lcoeffb[0] = 1 + lcoeffb[1];
lcoeffb[lcoeffb[1]] = n;
pa *= lcoeffb;
denres *= lcoeffb;
pres = (divmod==0 ? pa/pb : pa%pb);
}
}
else {
pres = (divmod==0 ? pa/pb : pa%pb);
}
// convert to Form notation
// NOTE: this part can be rewritten with poly::size_of_form_notation_with_den()
// and poly::poly_to_argument_with_den().
WORD *res;
// special case: a=0
if (pres[0]==1) {
if ( fit ) {
res = TermMalloc("poly_divmod");
}
else {
res = (WORD *)Malloc1(sizeof(WORD), "poly_divmod");
}
res[0] = 0;
}
else {
pres *= numres;
WORD nden;
UWORD *den = (UWORD *)NumberMalloc("poly_divmod");
// allocate the memory; note that this overestimates the size,
// since the estimated denominators are too large
int ressize = pres.size_of_form_notation() + pres.number_of_terms()*2*ABS(denres[denres[1]]) + 1;
if ( fit ) {
if ( ressize*sizeof(WORD) > (size_t)(AM.MaxTer) ) {
MLOCK(ErrorMessageLock);
MesPrint("poly_divmod: Term too complex. Maybe increasing MaxTermSize can help");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
res = TermMalloc("poly_divmod");
}
else {
res = (WORD *)Malloc1(ressize*sizeof(WORD), "poly_divmod");
}
int L=0;
for (int i=1; i!=pres[0]; i+=pres[i]) {
res[L]=1; // length
bool first = true;
for (int j=0; j<AN.poly_num_vars; j++)
if (pres[i+1+j] > 0) {
if (first) {
first = false;
res[L+1] = 1; // symbols
res[L+2] = 2; // length
}
res[L+1+res[L+2]++] = AN.poly_vars[j]; // symbol
res[L+1+res[L+2]++] = pres[i+1+j]; // power
}
if (!first) res[L] += res[L+2]; // fix length
// numerator
WORD nnum = pres[i+pres[i]-1];
WCOPY(&res[L+res[L]], &pres[i+pres[i]-1-ABS(nnum)], ABS(nnum));
// calculate denominator
nden = denres[denres[1]];
WCOPY(den, &denres[2+AN.poly_num_vars], ABS(nden));
if (nden!=1 || den[0]!=1)
Simplify(BHEAD (UWORD *)&res[L+res[L]], &nnum, den, &nden); // gcd(num,den)
Pack((UWORD *)&res[L+res[L]], &nnum, den, nden); // format
res[L] += 2*ABS(nnum)+1; // fix length
res[L+res[L]-1] = SGN(nnum)*(2*ABS(nnum)+1); // length of coefficient
L += res[L]; // fix length
}
res[L] = 0;
NumberFree(den,"poly_divmod");
}
// clean up
poly_free_poly_vars(BHEAD "AN.poly_vars_divmod");
// reset modulo calculation
AN.ncmod = AC.ncmod;
return res;
}
/*
#] poly_divmod :
#[ poly_div :
Routine divides the expression in arg1 by the expression in arg2.
We did not take out special cases.
The arguments are zero terminated sequences of term(s).
The action is to divide arg1 by arg2: [arg1/arg2].
The answer should be a buffer (allocated by Malloc1) with a zero
terminated sequence of terms (or just zero).
*/
WORD *poly_div(PHEAD WORD *a, WORD *b, WORD fit) {
#ifdef DEBUG
cout << "*** [" << thetime() << "] CALL : poly_div" << endl;
#endif
return poly_divmod(BHEAD a, b, 0, fit);
}
/*
#] poly_div :
#[ poly_rem :
Routine divides the expression in arg1 by the expression in arg2
and takes the remainder.
We did not take out special cases.
The arguments are zero terminated sequences of term(s).
The action is to divide arg1 by arg2 and take the remainder: [arg1%arg2].
The answer should be a buffer (allocated by Malloc1) with a zero
terminated sequence of terms (or just zero).
*/
WORD *poly_rem(PHEAD WORD *a, WORD *b, WORD fit) {
#ifdef DEBUG
cout << "*** [" << thetime() << "] CALL : poly_rem" << endl;
#endif
return poly_divmod(BHEAD a, b, 1, fit);
}
/*
#] poly_rem :
#[ poly_ratfun_read :
*/
/** Read a PolyRatFun
*
* Description
* ===========
* This method reads a polyratfun starting at the pointer a. The
* resulting numerator and denominator are written in num and
* den. If MUSTCLEANPRF, the result is normalized.
*
* Notes
* =====
* - Calls polygcd::gcd
*/
void poly_ratfun_read (WORD *a, poly &num, poly &den) {
#ifdef DEBUG
cout << "*** [" << thetime() << "] CALL : poly_ratfun_read" << endl;
#endif
POLY_GETIDENTITY(num);
int modp = num.modp;
WORD *astop = a+a[1];
bool clean = (a[2] & MUSTCLEANPRF) == 0;
a += FUNHEAD;
if (a >= astop) {
MLOCK(ErrorMessageLock);
MesPrint ((char*)"ERROR: PolyRatFun cannot have zero arguments");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
poly den_num(BHEAD 1),den_den(BHEAD 1);
num = poly::argument_to_poly(BHEAD a, true, !clean, &den_num);
num.setmod(modp,1);
NEXTARG(a);
if (a < astop) {
den = poly::argument_to_poly(BHEAD a, true, !clean, &den_den);
den.setmod(modp,1);
NEXTARG(a);
}
else {
den = poly(BHEAD 1, modp, 1);
}
if (a < astop) {
MLOCK(ErrorMessageLock);
MesPrint ((char*)"ERROR: PolyRatFun cannot have more than two arguments");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
// JD: At this point, num and den are certainly sorted into the correct order by
// poly::argument_to_poly, but we can't rely on the clean flag to know if there
// are any negative powers. Check for them, and set clean = false if there are any.
vector<WORD> minpower(AN.poly_num_vars, MAXPOSITIVE);
for (int i=1; i<num[0]; i+=num[i]) {
for (int j=0; j<AN.poly_num_vars; j++) {
minpower[j] = MiN(minpower[j], num[i+1+j]);
}
}
for (int i=1; i<den[0]; i+=den[i]) {
for (int j=0; j<AN.poly_num_vars; j++) {
minpower[j] = MiN(minpower[j], den[i+1+j]);
if ( minpower[j] < 0 ) clean = false;
}
}
if (!clean) {
for (int i=1; i<num[0]; i+=num[i])
for (int j=0; j<AN.poly_num_vars; j++)
num[i+1+j] -= minpower[j];
for (int i=1; i<den[0]; i+=den[i])
for (int j=0; j<AN.poly_num_vars; j++)
den[i+1+j] -= minpower[j];
num *= den_den;
den *= den_num;
poly gcd = polygcd::gcd(num,den);
num /= gcd;
den /= gcd;
}
}
/*
#] poly_ratfun_read :
#[ poly_sort :
*/
/** Sort the polynomial terms
*
* Description
* ===========
* Sorts the terms of a polynomial in Form poly(rat)fun order,
* i.e. lexicographical order with highest degree first.
*
* Notes
* =====
* - Uses Form sort routines with custom compare
*/
void poly_sort(PHEAD WORD *a) {
#ifdef DEBUG
cout << "*** [" << thetime() << "] CALL : poly_sort" << endl;
#endif
if (NewSort(BHEAD0)) { Terminate(-1); }
AR.CompareRoutine = (COMPAREDUMMY)(&CompareSymbols);
for (int i=ARGHEAD; i<a[0]; i+=a[i]) {
if (SymbolNormalize(a+i)<0 || StoreTerm(BHEAD a+i)) {
AR.CompareRoutine = (COMPAREDUMMY)(&Compare1);
LowerSortLevel();
Terminate(-1);
}
}
if (EndSort(BHEAD a+ARGHEAD,1) < 0) {
AR.CompareRoutine = (COMPAREDUMMY)(&Compare1);
Terminate(-1);
}
AR.CompareRoutine = (COMPAREDUMMY)(&Compare1);
a[1] = 0; // set dirty flag to zero
}
/*
#] poly_sort :
#[ poly_ratfun_add :
*/
/** Addition of PolyRatFuns
*
* Description
* ===========
* This method gets two pointers to polyratfuns with up to two
* arguments each and calculates the sum.
*
* Notes
* =====
* - The result is written at the workpointer
* - Called from sort.c and threads.c
* - Calls poly::operators and polygcd::gcd
*/
WORD *poly_ratfun_add (PHEAD WORD *t1, WORD *t2) {
if ( AR.PolyFunExp == 1 ) return PolyRatFunSpecial(BHEAD t1, t2);
#ifdef DEBUG
cout << "*** [" << thetime() << "] CALL : poly_ratfun_add" << endl;
#endif
WORD *oldworkpointer = AT.WorkPointer;
// Extract variables
vector<WORD *> e;
for (WORD *t=t1+FUNHEAD; t<t1+t1[1];) {
e.push_back(t);
NEXTARG(t);
}
for (WORD *t=t2+FUNHEAD; t<t2+t2[1];) {
e.push_back(t);
NEXTARG(t);
}
poly::get_variables(BHEAD e, true, true);
// Check for modulus calculus
WORD modp=poly_determine_modulus(BHEAD true, true, "PolyRatFun");
// Find numerators / denominators
poly num1(BHEAD 0,modp,1), den1(BHEAD 0,modp,1), num2(BHEAD 0,modp,1), den2(BHEAD 0,modp,1);
poly_ratfun_read(t1, num1, den1);
poly_ratfun_read(t2, num2, den2);
poly num(BHEAD 0),den(BHEAD 0),gcd(BHEAD 0);
// Calculate result
if (den1 != den2) {
gcd = polygcd::gcd(den1,den2);
#ifdef OLDADDITION
num = num1*(den2/gcd) + num2*(den1/gcd);
den = (den1/gcd)*den2;
gcd = polygcd::gcd(num,den);
#else
den = den1/gcd;
num = num1*(den2/gcd) + num2*den;
den = den*den2;
gcd = polygcd::gcd(num,gcd);
#endif
}
else {
num = num1 + num2;
den = den1;
gcd = polygcd::gcd(num,den);
}
num /= gcd;
den /= gcd;
// Fix sign
if (den.sign() == -1) { num*=poly(BHEAD -1); den*=poly(BHEAD -1); }
// Check size
if (num.size_of_form_notation() + den.size_of_form_notation() + 3 >= AM.MaxTer/(int)sizeof(WORD)) {
MLOCK(ErrorMessageLock);
MesPrint ("ERROR: PolyRatFun doesn't fit in a term");
MesPrint ("(1) num size = %d, den size = %d, MaxTer = %d",num.size_of_form_notation(),
den.size_of_form_notation(),AM.MaxTer);
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
// Format result in Form notation
WORD *t = oldworkpointer;
*t++ = AR.PolyFun; // function
*t++ = 0; // length (to be determined)
// *t++ &= ~MUSTCLEANPRF; // clean polyratfun
*t++ = 0;
FILLFUN3(t); // header
poly::poly_to_argument(num,t, true); // argument 1 (numerator)
if (*t>0 && t[1]==DIRTYFLAG) // to Form order
poly_sort(BHEAD t);
t += (*t>0 ? *t : 2);
poly::poly_to_argument(den,t, true); // argument 2 (denominator)
if (*t>0 && t[1]==DIRTYFLAG) // to Form order
poly_sort(BHEAD t);
t += (*t>0 ? *t : 2);
oldworkpointer[1] = t - oldworkpointer; // length
AT.WorkPointer = t;
poly_free_poly_vars(BHEAD "AN.poly_vars_ratfun_add");
// reset modulo calculation
AN.ncmod = AC.ncmod;
return oldworkpointer;
}
/*
#] poly_ratfun_add :
#[ poly_ratfun_normalize :
*/
/** Multiplication/normalization of PolyRatFuns
*
* Description
* ===========
* This method searches a term for multiple polyratfuns and
* multiplies their contents. The result is properly
* normalized. Normalization also works for terms with a single
* polyratfun.
*
* Notes
* =====
* - The result overwrites the original term
* - Called from proces.c
* - Calls poly::operators and polygcd::gcd
*/
int poly_ratfun_normalize (PHEAD WORD *term) {
#ifdef DEBUG
cout << "*** [" << thetime() << "] CALL : poly_ratfun_normalize" << endl;
#endif
// Strip coefficient
WORD *tstop = term + *term;
int ncoeff = tstop[-1];
tstop -= ABS(ncoeff);
// if only one clean polyratfun, return immediately
int num_polyratfun = 0;
for (WORD *t=term+1; t<tstop; t+=t[1])
if (*t == AR.PolyFun) {
num_polyratfun++;
if ((t[2] & MUSTCLEANPRF) != 0)
num_polyratfun = INT_MAX;
if (num_polyratfun > 1) break;
}
if (num_polyratfun <= 1) return 0;
WORD oldsorttype = AR.SortType;
AR.SortType = SORTHIGHFIRST;
/*
When there are polyratfun's with only one variable: rename them
temporarily to TMPPOLYFUN.
*/
for (WORD *t=term+1; t<tstop; t+=t[1]) {
if (*t == AR.PolyFun && (t[1] == FUNHEAD+t[FUNHEAD]
|| t[1] == FUNHEAD+2 ) ) { *t = TMPPOLYFUN; }
}
// Extract all variables in the polyfuns
vector<WORD *> e;
for (WORD *t=term+1; t<tstop; t+=t[1]) {
if (*t == AR.PolyFun)
for (WORD *t2 = t+FUNHEAD; t2<t+t[1];) {
e.push_back(t2);
NEXTARG(t2);
}
}
poly::get_variables(BHEAD e, true, true);
// Check for modulus calculus
WORD modp=poly_determine_modulus(BHEAD true, true, "PolyRatFun");
// Accumulate total denominator/numerator and copy the remaining terms
// We start with 'trivial' polynomials
poly num1(BHEAD (UWORD *)tstop, ncoeff/2, modp, 1);
poly den1(BHEAD (UWORD *)tstop+ABS(ncoeff/2), ABS(ncoeff)/2, modp, 1);
WORD *s = term+1;
for (WORD *t=term+1; t<tstop;)
if (*t == AR.PolyFun) {
poly num2(BHEAD 0,modp,1);
poly den2(BHEAD 0,modp,1);
poly_ratfun_read(t,num2,den2);
if ((t[2] & MUSTCLEANPRF) != 0) { // first normalize
poly gcd1(polygcd::gcd(num2,den2));
num2 = num2/gcd1;
den2 = den2/gcd1;
}
t += t[1];
poly gcd1(polygcd::gcd(num1,den2));
poly gcd2(polygcd::gcd(num2,den1));
num1 = (num1 / gcd1) * (num2 / gcd2);
den1 = (den1 / gcd2) * (den2 / gcd1);
}
else {
int i = t[1];
if ( s != t ) { NCOPY(s,t,i) }
else { t += i; s += i; }
}
// Fix sign
if (den1.sign() == -1) { num1*=poly(BHEAD -1); den1*=poly(BHEAD -1); }
// Check size
if (num1.size_of_form_notation() + den1.size_of_form_notation() + 3 >= AM.MaxTer/(int)sizeof(WORD)) {
MLOCK(ErrorMessageLock);
MesPrint ("ERROR: PolyRatFun doesn't fit in a term");
MesPrint ("(2) num size = %d, den size = %d, MaxTer = %d",num1.size_of_form_notation(),
den1.size_of_form_notation(),AM.MaxTer);
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
// Format result in Form notation
WORD *t = s;
*t++ = AR.PolyFun; // function
*t++ = 0; // size (to be determined)
*t++ &= ~MUSTCLEANPRF; // clean polyratfun
FILLFUN3(t); // header
poly::poly_to_argument(num1,t,true); // argument 1 (numerator)
if (*t>0 && t[1]==DIRTYFLAG) // to Form order
poly_sort(BHEAD t);
t += (*t>0 ? *t : 2);
poly::poly_to_argument(den1,t,true); // argument 2 (denominator)
if (*t>0 && t[1]==DIRTYFLAG) // to Form order
poly_sort(BHEAD t);
t += (*t>0 ? *t : 2);
s[1] = t - s; // function length
*t++ = 1; // term coefficient
*t++ = 1;
*t++ = 3;
term[0] = t-term; // term length
poly_free_poly_vars(BHEAD "AN.poly_vars_ratfun_normalize");
// reset modulo calculation
AN.ncmod = AC.ncmod;
tstop = term + *term; tstop -= ABS(tstop[-1]);
for (WORD *t=term+1; t<tstop; t+=t[1]) {
if (*t == TMPPOLYFUN ) *t = AR.PolyFun;
}
AR.SortType = oldsorttype;
return 0;
}
/*
#] poly_ratfun_normalize :
#[ poly_fix_minus_signs :
*/
void poly_fix_minus_signs (factorized_poly &a) {
if ( a.factor.empty() ) return;
POLY_GETIDENTITY(a.factor[0]);
int overall_sign = +1;
// find term with maximum power of highest symbol
for (int i=0; i<(int)a.factor.size(); i++) {
int maxvar = -1;
int maxpow = -1;
int sign = +1;
WORD *tstop = a.factor[i].terms; tstop += *tstop;
for (WORD *t=a.factor[i].terms+1; t<tstop; t+=*t)
for (int j=0; j<AN.poly_num_vars; j++) {
int var = AN.poly_vars[j];
int pow = t[1+j];
if (pow>0 && (var>maxvar || (var==maxvar && pow>maxpow))) {
maxvar = var;
maxpow = pow;
sign = SGN(*(t+*t-1));
}
}
// if negative coefficient, multiply by -1
if (sign==-1) {
a.factor[i] *= poly(BHEAD sign);
if (a.power[i] % 2 == 1) overall_sign*=-1;
}
}
// if overall minus sign
if (overall_sign == -1) {
// look at constant factor and multiply by -1
for (int i=0; i<(int)a.factor.size(); i++)
if (a.factor[i].is_integer()) {
a.factor[i] *= poly(BHEAD -1);
return;
}
// otherwise, add a factor of -1
a.add_factor(poly(BHEAD -1), 1);
}
}
/*
#] poly_fix_minus_signs :
#[ poly_factorize :
*/
/** Factorization of function arguments / dollars
*
* Description
* ===========
* This method factorizes a Form style argument or zero-terminated
* term list.
*
* Notes
* =====
* - Called from poly_factorize_{argument,dollar}
* - Calls polyfact::factorize
*/
WORD *poly_factorize (PHEAD WORD *argin, WORD *argout, bool with_arghead, bool is_fun_arg) {
#ifdef DEBUG
cout << "*** [" << thetime() << "] CALL : poly_factorize" << endl;
#endif
poly::get_variables(BHEAD vector<WORD*>(1,argin), with_arghead, true);
poly den(BHEAD 0);
poly a(poly::argument_to_poly(BHEAD argin, with_arghead, true, &den));
// check for modulus calculus
WORD modp=poly_determine_modulus(BHEAD true, is_fun_arg, "polynomial factorization");
a.setmod(modp,1);
// factorize
factorized_poly f(polyfact::factorize(a));
poly_fix_minus_signs(f);
poly num(BHEAD 1);
for (int i=0; i<(int)f.factor.size(); i++)
if (f.factor[i].is_integer())
num = f.factor[i];
// determine size
int len = with_arghead ? ARGHEAD : 0;
if (!num.is_one() || !den.is_one()) {
len++;
len += MaX(ABS(num[num[1]]), den[den[1]])*2+1;
len += with_arghead ? ARGHEAD : 1;
}
for (int i=0; i<(int)f.factor.size(); i++) {
if (!f.factor[i].is_integer()) {
len += f.power[i] * f.factor[i].size_of_form_notation();
len += f.power[i] * (with_arghead ? ARGHEAD : 1);
}
}
len++;
if (argout != NULL) {
// check size
if (len >= AM.MaxTer) {
MLOCK(ErrorMessageLock);
MesPrint ("ERROR: factorization doesn't fit in a term");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
}
else {
// allocate size
argout = (WORD*) Malloc1(len*sizeof(WORD), "poly_factorize");
}
WORD *old_argout = argout;
// constant factor
if (!num.is_one() || !den.is_one()) {
int n = max(ABS(num[num[1]]), ABS(den[den[1]]));
if (with_arghead) {
*argout++ = ARGHEAD + 2 + 2*n;
for (int i=1; i<ARGHEAD; i++)
*argout++ = 0;
}
*argout++ = 2 + 2*n;
for (int i=0; i<n; i++)