forked from vermaseren/form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratio.c
3703 lines (3578 loc) · 98.4 KB
/
ratio.c
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 ratio.c
*
* A variety of routines:
* The ratio command for partial fractioning
* (rather old. Schoonschip inheritance)
* The sum routines.
*/
/* #[ 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 : */
/*
#[ Includes : ratio.c
*/
#include "form3.h"
/*
#] Includes :
#[ Ratio :
These are the special operations regarding simple polynomials.
The first and most needed is the partial fractioning expansion.
Ratio,x1,x2,x3
The files belonging to the ratio command serve also as a good example
of how to implement a new operation.
#[ RatioFind :
The routine that should locate the need for a ratio command.
If located the corresponding symbols are removed and the
operational parameters are loaded. A subexpression pointer
is inserted and the code for success is returned.
params points at the compiler output block defined in RatioComp.
*/
WORD RatioFind(PHEAD WORD *term, WORD *params)
{
GETBIDENTITY
WORD *t, *m, *r;
WORD x1, x2, i;
WORD *y1, *y2, n1 = 0, n2 = 0;
x1 = params[3];
x2 = params[4];
m = t = term;
m += *m;
m -= ABS(m[-1]);
t++;
if ( t < m ) do {
if ( *t == SYMBOL ) {
y1 = 0;
y2 = 0;
r = t + t[1];
m = t + 2;
do {
if ( *m == x1 ) { y1 = m; n1 = m[1]; }
else if ( *m == x2 ) { y2 = m; n2 = m[1]; }
m += 2;
} while ( m < r );
if ( !y1 || !y2 || ( n1 > 0 && n2 > 0 ) ) return(0);
m -= 2;
if ( y1 > y2 ) { r = y1; y1 = y2; y2 = r; }
*y2 = *m; y2[1] = m[1];
m -= 2;
*y1 = *m; y1[1] = m[1];
i = WORDDIF(m,t);
#if SUBEXPSIZE > 6
We have to revise the code for the second case.
#endif
if ( i > 2 ) { /* Subexpression fits exactly */
t[1] = i;
y1 = term+*term;
y2 = y1+SUBEXPSIZE-4;
r = m+4;
while ( y1 > r ) *--y2 = *--y1;
*m++ = SUBEXPRESSION;
*m++ = SUBEXPSIZE;
*m++ = -1;
*m++ = 1;
*m++ = DUMMYBUFFER;
FILLSUB(m)
*term += SUBEXPSIZE-4;
}
else { /* All symbols are gone. Rest has to be moved */
m -= 2;
*m++ = SUBEXPRESSION;
*m++ = SUBEXPSIZE;
*m++ = -1;
*m++ = 1;
*m++ = DUMMYBUFFER;
FILLSUB(m)
t = term;
t += *t;
*term += SUBEXPSIZE-6;
r = m + 6-SUBEXPSIZE;
do { *m++ = *r++; } while ( r < t );
}
t = AT.TMout; /* Load up the TM out array for the generator */
*t++ = 7;
*t++ = RATIO;
*t++ = x1;
*t++ = x2;
*t++ = params[5];
*t++ = n1;
*t++ = n2;
return(1);
}
t += t[1];
} while ( t < m );
return(0);
}
/*
#] RatioFind :
#[ RatioGen :
The algorithm:
x1^-n1*x2^n2 ==> x2 --> x1 + x3
x1^n1*x2^-n2 ==> x1 --> x2 - x3
x1^-n1*x2^-n2 ==>
+sum(i=0,n1-1){(-1)^i*binom(n2-1+i,n2-1)
*x3^-(n2+i)*x1^-(n1-i)}
+sum(i=0,n2-1){(-1)^(n1)*binom(n1-1+i,n1-1)
*x3^-(n1+i)*x2^-(n2-i)}
Actually there is an amount of arbitrariness in the first two
formulae and the replacement x2 -> x1 + x3 could be made 'by hand'.
It is better to use the nontrivial 'minimal change' formula:
x1^-n1*x2^n2: if ( n1 >= n2 ) {
+sum(i=0,n2){x3^i*x1^-(n1-n2+i)*binom(n2,i)}
}
else {
sum(i=0,n2-n1){x2^(n2-n1-i)*x3^i*binom(n1-1+i,n1-1)}
+sum(i=0,n1-1){x3^(n2-i)*x1^-(n1-i)*binom(n2,i)}
}
x1^n1*x2^-n2: Same but x3 -> -x3.
The contents of the AT.TMout/params array are:
length,type,x1,x2,x3,n1,n2
*/
WORD RatioGen(PHEAD WORD *term, WORD *params, WORD num, WORD level)
{
GETBIDENTITY
WORD *t, *m;
WORD *tstops[3];
WORD n1, n2, i, j;
WORD x1,x2,x3;
UWORD *coef;
WORD ncoef, sign = 0;
coef = (UWORD *)AT.WorkPointer;
t = term;
tstops[2] = m = t + *t;
m -= ABS(m[-1]);
t++;
do {
if ( *t == SUBEXPRESSION && t[2] == num ) break;
t += t[1];
} while ( t < m );
tstops[0] = t;
tstops[1] = t + t[1];
/*
Copying to termout will be from term to tstop1, then the induced part
and finally from tstop2 to tstop3
Now separate the various cases:
*/
t = params + 2;
x1 = *t++;
x2 = *t++;
x3 = *t++;
n1 = *t++;
n2 = *t++;
if ( n1 > 0 ) { /* Flip the variables and indicate -x3 */
n2 = -n2;
sign = 1;
i = n1; n1 = n2; n2 = i;
i = x1; x1 = x2; x2 = i;
goto PosNeg;
}
else if ( n2 > 0 ) {
n1 = -n1;
PosNeg:
if ( n2 <= n1 ) { /* x1 -> x2 + x3 */
*coef = 1;
ncoef = 1;
AT.WorkPointer = (WORD *)(coef + 1);
j = n2;
for ( i = 0; i <= n2; i++ ) {
if ( BinomGen(BHEAD term,level,tstops,x1,x3,n2-n1-i,i,sign&i
,coef,ncoef) ) goto RatioCall;
if ( i < n2 ) {
if ( Product(coef,&ncoef,j) ) goto RatioCall;
if ( Quotient(coef,&ncoef,i+1) ) goto RatioCall;
j--;
AT.WorkPointer = (WORD *)(coef + ABS(ncoef));
}
}
AT.WorkPointer = (WORD *)(coef);
return(0);
}
else {
/*
sum(i=0,n2-n1){x2^(n2-n1-i)*x3^i*binom(n1-1+i,n1-1)}
+sum(i=0,n1-1){x3^(n2-i)*x1^-(n1-i)*binom(n2,i)}
*/
*coef = 1;
ncoef = 1;
AT.WorkPointer = (WORD *)(coef + 1);
j = n2 - n1;
for ( i = 0; i <= j; i++ ) {
if ( BinomGen(BHEAD term,level,tstops,x2,x3,n2-n1-i,i,sign&i
,coef,ncoef) ) goto RatioCall;
if ( i < j ) {
if ( Product(coef,&ncoef,n1+i) ) goto RatioCall;
if ( Quotient(coef,&ncoef,i+1) ) goto RatioCall;
AT.WorkPointer = (WORD *)(coef + ABS(ncoef));
}
}
*coef = 1;
ncoef = 1;
AT.WorkPointer = (WORD *)(coef + 1);
j = n1-1;
for ( i = 0; i <= j; i++ ) {
if ( BinomGen(BHEAD term,level,tstops,x1,x3,i-n1,n2-i,sign&(n2-i)
,coef,ncoef) ) goto RatioCall;
if ( i < j ) {
if ( Product(coef,&ncoef,n2-i) ) goto RatioCall;
if ( Quotient(coef,&ncoef,i+1) ) goto RatioCall;
AT.WorkPointer = (WORD *)(coef + ABS(ncoef));
}
}
AT.WorkPointer = (WORD *)(coef);
return(0);
}
}
else {
n2 = -n2;
n1 = -n1;
/*
+sum(i=0,n1-1){(-1)^i*binom(n2-1+i,n2-1)
*x3^-(n2+i)*x1^-(n1-i)}
+sum(i=0,n2-1){(-1)^(n1)*binom(n1-1+i,n1-1)
*x3^-(n1+i)*x2^-(n2-i)}
*/
*coef = 1;
ncoef = 1;
AT.WorkPointer = (WORD *)(coef + 1);
j = n1-1;
for ( i = 0; i <= j; i++ ) {
if ( BinomGen(BHEAD term,level,tstops,x1,x3,i-n1,-n2-i,i&1
,coef,ncoef) ) goto RatioCall;
if ( i < j ) {
if ( Product(coef,&ncoef,n2+i) ) goto RatioCall;
if ( Quotient(coef,&ncoef,i+1) ) goto RatioCall;
AT.WorkPointer = (WORD *)(coef + ABS(ncoef));
}
}
*coef = 1;
ncoef = 1;
AT.WorkPointer = (WORD *)(coef + 1);
j = n2-1;
for ( i = 0; i <= j; i++ ) {
if ( BinomGen(BHEAD term,level,tstops,x2,x3,i-n2,-n1-i,n1&1
,coef,ncoef) ) goto RatioCall;
if ( i < j ) {
if ( Product(coef,&ncoef,n1+i) ) goto RatioCall;
if ( Quotient(coef,&ncoef,i+1) ) goto RatioCall;
AT.WorkPointer = (WORD *)(coef + ABS(ncoef));
}
}
AT.WorkPointer = (WORD *)(coef);
return(0);
}
RatioCall:
MLOCK(ErrorMessageLock);
MesCall("RatioGen");
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
}
/*
#] RatioGen :
#[ BinomGen :
Routine for the generation of terms in a binomialtype expansion.
*/
WORD BinomGen(PHEAD WORD *term, WORD level, WORD **tstops, WORD x1, WORD x2,
WORD pow1, WORD pow2, WORD sign, UWORD *coef, WORD ncoef)
{
GETBIDENTITY
WORD *t, *r;
WORD *termout;
WORD k;
termout = AT.WorkPointer;
t = termout;
r = term;
do { *t++ = *r++; } while ( r < tstops[0] );
*t++ = SYMBOL;
if ( pow2 == 0 ) {
if ( pow1 == 0 ) t--;
else { *t++ = 4; *t++ = x1; *t++ = pow1; }
}
else if ( pow1 == 0 ) {
*t++ = 4; *t++ = x2; *t++ = pow2;
}
else {
*t++ = 6; *t++ = x1; *t++ = pow1; *t++ = x2; *t++ = pow2;
}
*t++ = LNUMBER;
*t++ = ABS(ncoef) + 3;
*t = ncoef;
if ( sign ) *t = -*t;
t++;
ncoef = ABS(ncoef);
for ( k = 0; k < ncoef; k++ ) *t++ = coef[k];
r = tstops[1];
do { *t++ = *r++; } while ( r < tstops[2] );
*termout = WORDDIF(t,termout);
AT.WorkPointer = t;
if ( AT.WorkPointer > AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
*AN.RepPoint = 1;
AR.expchanged = 1;
if ( Generator(BHEAD termout,level) ) {
MLOCK(ErrorMessageLock);
MesCall("BinomGen");
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
}
AT.WorkPointer = termout;
return(0);
}
/*
#] BinomGen :
#] Ratio :
#[ Sum :
#[ DoSumF1 :
Routine expands a sum_ function.
Its arguments are:
The term in which the function occurs.
The parameter list:
length of parameter field
function number (SUMNUM1)
number of the symbol that is loop parameter
min value
max value
increment
the number of the subexpression to be removed
the level in the generation tree.
Note that the insertion of the loop parameter in the argument
is done via the regular wildcard substitution mechanism.
*/
WORD DoSumF1(PHEAD WORD *term, WORD *params, WORD replac, WORD level)
{
GETBIDENTITY
WORD *termout, *t, extractbuff = AT.TMbuff;
WORD isum, ival, iinc;
LONG from;
CBUF *C;
ival = params[3];
iinc = params[5];
if ( ( iinc > 0 && params[4] >= ival )
|| ( iinc < 0 && params[4] <= ival ) ) {
isum = (params[4] - ival)/iinc + 1;
}
else return(0);
termout = AT.WorkPointer;
AT.WorkPointer = (WORD *)(((UBYTE *)(AT.WorkPointer)) + AM.MaxTer);
if ( AT.WorkPointer > AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
t = term + 1;
while ( *t != SUBEXPRESSION || t[2] != replac || t[4] != extractbuff )
t += t[1];
C = cbuf+t[4];
t += SUBEXPSIZE;
if ( params[2] < 0 ) {
while ( *t != INDTOIND || t[2] != -params[2] ) t += t[1];
*t = INDTOIND;
}
else {
while ( *t > SYMTOSUB || t[2] != params[2] ) t += t[1];
*t = SYMTONUM;
}
do {
t[3] = ival;
from = C->rhs[replac] - C->Buffer;
while ( C->Buffer[from] ) {
if ( InsertTerm(BHEAD term,replac,extractbuff,C->Buffer+from,termout,0) < 0 ) goto SumF1Call;
AT.WorkPointer = termout + *termout;
if ( Generator(BHEAD termout,level) < 0 ) goto SumF1Call;
from += C->Buffer[from];
}
ival += iinc;
} while ( --isum > 0 );
AT.WorkPointer = termout;
return(0);
SumF1Call:
MLOCK(ErrorMessageLock);
MesCall("DoSumF1");
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
}
/*
#] DoSumF1 :
#[ Glue :
Routine multiplies two terms. The second term is subject
to the wildcard substitutions in sub.
Output in the first term. This routine is a variation on
the routine InsertTerm.
*/
WORD Glue(PHEAD WORD *term1, WORD *term2, WORD *sub, WORD insert)
{
GETBIDENTITY
UWORD *coef;
WORD ncoef, *t, *t1, *t2, i, nc2, nc3, old, newer;
coef = (UWORD *)(TermMalloc("Glue"));
t = term1;
t += *t;
i = t[-1];
t -= ABS(i);
old = WORDDIF(t,term1);
ncoef = REDLENG(i);
if ( i < 0 ) i = -i;
i--;
t1 = t;
t2 = (WORD *)coef;
while ( --i >= 0 ) *t2++ = *t1++;
i = *--t;
nc2 = WildFill(BHEAD t,term2,sub);
*t = i;
t += nc2;
nc2 = t[-1];
t -= ABS(nc2);
newer = WORDDIF(t,term1);
if ( MulRat(BHEAD (UWORD *)t,REDLENG(nc2),coef,ncoef,(UWORD *)t,&nc3) ) {
MLOCK(ErrorMessageLock);
MesCall("Glue");
MUNLOCK(ErrorMessageLock);
TermFree(coef,"Glue");
SETERROR(-1)
}
i = (ABS(nc3))*2;
t += i++;
*t++ = (nc3 >= 0)?i:-i;
*term1 = WORDDIF(t,term1);
/*
Switch the new piece with the old tail, so that noncommuting
variables get into their proper spot.
*/
i = old - insert;
t1 = t;
t2 = term1+insert;
NCOPY(t1,t2,i);
i = newer - old;
t1 = term1+insert;
t2 = term1+old;
NCOPY(t1,t2,i);
t2 = t;
i = old - insert;
NCOPY(t1,t2,i);
TermFree(coef,"Glue");
return(0);
}
/*
#] Glue :
#[ DoSumF2 :
*/
WORD DoSumF2(PHEAD WORD *term, WORD *params, WORD replac, WORD level)
{
GETBIDENTITY
WORD *termout, *t, *from, *sub, *to, extractbuff = AT.TMbuff;
WORD isum, ival, iinc, insert, i;
CBUF *C;
ival = params[3];
iinc = params[5];
if ( ( iinc > 0 && params[4] >= ival )
|| ( iinc < 0 && params[4] <= ival ) ) {
isum = (params[4] - ival)/iinc + 1;
}
else return(0);
termout = AT.WorkPointer;
AT.WorkPointer = (WORD *)(((UBYTE *)(AT.WorkPointer)) + AM.MaxTer);
if ( AT.WorkPointer > AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
t = term + 1;
while ( *t != SUBEXPRESSION || t[2] != replac || t[4] != extractbuff ) t += t[1];
insert = WORDDIF(t,term);
from = term;
to = termout;
while ( from < t ) *to++ = *from++;
from += t[1];
sub = term + *term;
while ( from < sub ) *to++ = *from++;
*termout -= t[1];
sub = t;
C = cbuf+t[4];
t += SUBEXPSIZE;
if ( params[2] < 0 ) {
while ( *t != INDTOIND || t[2] != -params[2] ) t += t[1];
*t = INDTOIND;
}
else {
while ( *t > SYMTOSUB || t[2] != params[2] ) t += t[1];
*t = SYMTONUM;
}
t[3] = ival;
for(;;) {
AT.WorkPointer = termout + *termout;
to = AT.WorkPointer;
if ( ( to + *termout ) > AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
from = termout;
i = *termout;
NCOPY(to,from,i);
from = AT.WorkPointer;
AT.WorkPointer = to;
if ( Generator(BHEAD from,level) < 0 ) goto SumF2Call;
if ( --isum <= 0 ) break;
ival += iinc;
t[3] = ival;
if ( Glue(BHEAD termout,C->rhs[replac],sub,insert) < 0 ) goto SumF2Call;
}
AT.WorkPointer = termout;
return(0);
SumF2Call:
MLOCK(ErrorMessageLock);
MesCall("DoSumF2");
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
}
/*
#] DoSumF2 :
#] Sum :
#[ GCDfunction :
#[ GCDfunction :
*/
typedef struct {
WORD *buffer;
DOLLARS dollar;
LONG size;
int type;
int dummy;
} ARGBUFFER;
int GCDfunction(PHEAD WORD *term,WORD level)
{
GETBIDENTITY
WORD *t, *tstop, *tf, *termout, *tin, *tout, *m, *mnext, *mstop, *mm;
int todo, i, ii, j, istart, sign = 1, action = 0;
WORD firstshort = 0, firstvalue = 0, gcdisone = 0, mlength, tlength, newlength;
WORD totargs = 0, numargs, argsdone = 0, *mh, oldval1, *g, *gcdout = 0;
WORD *arg1, *arg2;
UWORD x1,x2,x3;
LONG args;
#if ( FUNHEAD > 4 )
WORD sh[FUNHEAD+5];
#else
WORD sh[9];
#endif
DOLLARS d;
ARGBUFFER *abuf = 0, ab;
/*
#[ Find Function. Count arguments :
First find the proper function
*/
t = term + *term; tlength = t[-1];
tstop = t - ABS(tlength);
t = term + 1;
while ( t < tstop ) {
if ( *t != GCDFUNCTION ) { t += t[1]; continue; }
todo = 1; totargs = 0;
tf = t + FUNHEAD;
while ( tf < t + t[1] ) {
totargs++;
if ( *tf > 0 && tf[1] != 0 ) todo = 0;
NEXTARG(tf);
}
if ( todo ) break;
t += t[1];
}
if ( t >= tstop ) {
MLOCK(ErrorMessageLock);
MesPrint("Internal error. Indicated gcd_ function not encountered.");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
WantAddPointers(totargs);
args = AT.pWorkPointer; AT.pWorkPointer += totargs;
/*
#] Find Function. Count arguments :
#[ Do short arguments :
The function we need, in agreement with TestSub, is now in t
Make first a compilation of the short arguments (except $-s and expressions)
to see whether we need to do much work.
This means that after this scan we can ignore all short arguments with
the exception of unevaluated $-s and expressions.
*/
numargs = 0;
firstshort = 0;
tf = t + FUNHEAD;
while ( tf < t + t[1] ) {
if ( *tf == -SNUMBER && tf[1] == 0 ) { NEXTARG(tf); continue; }
if ( *tf > 0 || *tf == -DOLLAREXPRESSION || *tf == -EXPRESSION ) {
AT.pWorkSpace[args+numargs++] = tf;
NEXTARG(tf); continue;
}
if ( firstshort == 0 ) {
firstshort = *tf;
if ( *tf <= -FUNCTION ) { firstvalue = -(*tf); }
else { firstvalue = tf[1]; }
NEXTARG(tf);
argsdone++;
continue;
}
else if ( *tf != firstshort ) {
if ( *tf != -INDEX && *tf != -VECTOR && *tf != -MINVECTOR ) {
argsdone++; gcdisone = 1; break;
}
if ( firstshort != -INDEX && firstshort != -VECTOR && firstshort != -MINVECTOR ) {
argsdone++; gcdisone = 1; break;
}
if ( tf[1] != firstvalue ) {
argsdone++; gcdisone = 1; break;
}
if ( *t == -MINVECTOR ) { firstshort = -VECTOR; }
if ( firstshort == -MINVECTOR ) { firstshort = -VECTOR; }
}
else if ( *tf > -FUNCTION && *tf != -SNUMBER && tf[1] != firstvalue ) {
argsdone++; gcdisone = 1; break;
}
if ( *tf == -SNUMBER && firstvalue != tf[1] ) {
/*
make a new firstvalue which is gcd_(firstvalue,tf[1])
*/
if ( firstvalue == 1 || tf[1] == 1 ) { gcdisone = 1; break; }
if ( firstvalue < 0 && tf[1] < 0 ) {
x1 = -firstvalue; x2 = -tf[1]; sign = -1;
}
else {
x1 = ABS(firstvalue); x2 = ABS(tf[1]); sign = 1;
}
while ( ( x3 = x1%x2 ) != 0 ) { x1 = x2; x2 = x3; }
firstvalue = ((WORD)x2)*sign;
argsdone++;
if ( firstvalue == 1 ) { gcdisone = 1; break; }
}
NEXTARG(tf);
}
termout = AT.WorkPointer;
AT.WorkPointer = (WORD *)(((UBYTE *)(AT.WorkPointer)) + AM.MaxTer);
if ( AT.WorkPointer > AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
/*
#] Do short arguments :
#[ Do trivial GCD :
Copy head
*/
i = t - term; tin = term; tout = termout;
NCOPY(tout,tin,i);
if ( gcdisone || ( firstshort == -SNUMBER && firstvalue == 1 ) ) {
sign = 1;
gcdone:
tin += t[1]; tstop = term + *term;
while ( tin < tstop ) *tout++ = *tin++;
*termout = tout - termout;
if ( sign < 0 ) tout[-1] = -tout[-1];
AT.WorkPointer = tout;
if ( argsdone && Generator(BHEAD termout,level) < 0 ) goto CalledFrom;
AT.WorkPointer = termout;
AT.pWorkPointer = args;
return(0);
}
/*
#] Do trivial GCD :
#[ Do short argument GCD :
*/
if ( numargs == 0 ) { /* basically we are done */
doshort:
sign = 1;
if ( firstshort == 0 ) goto gcdone;
if ( firstshort == -SNUMBER ) {
*tout++ = SNUMBER; *tout++ = 4; *tout++ = firstvalue; *tout++ = 1;
goto gcdone;
}
else if ( firstshort == -SYMBOL ) {
*tout++ = SYMBOL; *tout++ = 4; *tout++ = firstvalue; *tout++ = 1;
goto gcdone;
}
else if ( firstshort == -VECTOR || firstshort == -INDEX ) {
*tout++ = INDEX; *tout++ = 3; *tout++ = firstvalue; goto gcdone;
}
else if ( firstshort == -MINVECTOR ) {
sign = -1;
*tout++ = INDEX; *tout++ = 3; *tout++ = firstvalue; goto gcdone;
}
else if ( firstshort <= -FUNCTION ) {
*tout++ = firstvalue; *tout++ = FUNHEAD; FILLFUN(tout);
goto gcdone;
}
else {
MLOCK(ErrorMessageLock);
MesPrint("Internal error. Illegal short argument in GCDfunction.");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
}
/*
#] Do short argument GCD :
#[ Convert short argument :
Now we allocate space for the arguments in general notation.
First the special one if there were short arguments
*/
if ( firstshort ) {
switch ( firstshort ) {
case -SNUMBER:
sh[0] = 4; sh[1] = ABS(firstvalue); sh[2] = 1;
if ( firstvalue < 0 ) sh[3] = -3;
else sh[3] = 3;
sh[4] = 0;
break;
case -MINVECTOR:
case -VECTOR:
case -INDEX:
sh[0] = 8; sh[1] = INDEX; sh[2] = 3; sh[3] = firstvalue;
sh[4] = 1; sh[5] = 1;
if ( firstshort == -MINVECTOR ) sh[6] = -3;
else sh[6] = 3;
sh[7] = 0;
break;
case -SYMBOL:
sh[0] = 8; sh[1] = SYMBOL; sh[2] = 4; sh[3] = firstvalue; sh[4] = 1;
sh[5] = 1; sh[6] = 1; sh[7] = 3; sh[8] = 0;
break;
default:
sh[0] = FUNHEAD+4; sh[1] = firstshort; sh[2] = FUNHEAD;
for ( i = 2; i < FUNHEAD; i++ ) sh[i+1] = 0;
sh[FUNHEAD+1] = 1; sh[FUNHEAD+2] = 1; sh[FUNHEAD+3] = 3; sh[FUNHEAD+4] = 0;
break;
}
}
/*
#] Convert short argument :
#[ Sort arguments :
Now we should sort the arguments in a way that the dollars and the
expressions come last. That way we may never need them.
*/
for ( i = 1; i < numargs; i++ ) {
for ( ii = i; ii > 0; ii-- ) {
arg1 = AT.pWorkSpace[args+ii];
arg2 = AT.pWorkSpace[args+ii-1];
if ( *arg1 < 0 ) {
if ( *arg2 < 0 ) {
if ( *arg1 == -EXPRESSION ) break;
if ( *arg2 == -DOLLAREXPRESSION ) break;
AT.pWorkSpace[args+ii] = arg2;
AT.pWorkSpace[args+ii-1] = arg1;
}
else break;
}
else if ( *arg2 < 0 ) {
AT.pWorkSpace[args+ii] = arg2;
AT.pWorkSpace[args+ii-1] = arg1;
}
else {
if ( *arg1 > *arg2 ) {
AT.pWorkSpace[args+ii] = arg2;
AT.pWorkSpace[args+ii-1] = arg1;
}
else break;
}
}
}
/*
#] Sort arguments :
#[ There is a single term argument :
*/
if ( firstshort ) {
mh = sh; istart = 0;
oneterm:;
for ( i = istart; i < numargs; i++ ) {
arg1 = AT.pWorkSpace[args+i];
if ( *arg1 > 0 ) {
oldval1 = arg1[*arg1]; arg1[*arg1] = 0;
m = arg1+ARGHEAD;
while ( *m ) {
GCDterms(BHEAD mh,m,mh); m += *m;
if ( mh[0] == 4 && mh[1] == 1 && mh[2] == 1 && mh[3] == 3 ) {
gcdisone = 1; sign = 1; arg1[*arg1] = oldval1; goto gcdone;
}
}
arg1[*arg1] = oldval1;
}
else if ( *arg1 == -DOLLAREXPRESSION ) {
if ( ( d = DolToTerms(BHEAD arg1[1]) ) != 0 ) {
m = d->where;
while ( *m ) {
GCDterms(BHEAD mh,m,mh); m += *m;
argsdone++;
if ( mh[0] == 4 && mh[1] == 1 && mh[2] == 1 && mh[3] == 3 ) {
gcdisone = 1; sign = 1;
if ( d->factors ) M_free(d->factors,"Dollar factors");
M_free(d,"Copy of dollar variable"); goto gcdone;
}
}
if ( d->factors ) M_free(d->factors,"Dollar factors");
M_free(d,"Copy of dollar variable");
}
}
else {
mm = CreateExpression(BHEAD arg1[1]);
m = mm;
while ( *m ) {
GCDterms(BHEAD mh,m,mh); m += *m;
argsdone++;
if ( mh[0] == 4 && mh[1] == 1 && mh[2] == 1 && mh[3] == 3 ) {
gcdisone = 1; sign = 1; M_free(mm,"CreateExpression"); goto gcdone;
}
}
M_free(mm,"CreateExpression");
}
}
if ( firstshort ) {
if ( mh[0] == 4 ) {
firstshort = -SNUMBER; firstvalue = mh[1] * (mh[3]/3);
}
else if ( mh[1] == SYMBOL ) {
firstshort = -SYMBOL; firstvalue = mh[3];
}
else if ( mh[1] == INDEX ) {
firstshort = -INDEX; firstvalue = mh[3];
if ( mh[6] == -3 ) firstshort = -MINVECTOR;
}
else if ( mh[1] >= FUNCTION ) {
firstshort = -mh[1]; firstvalue = mh[1];
}
goto doshort;
}
else {
/*
We have a GCD that is only a single term.
Paste it in and combine the coefficients.
*/
mh[mh[0]] = 0;
mm = mh;
ii = 0;
goto multiterms;
}
}
/*
Now we have only regular arguments.
But some have not yet been expanded.
Check whether there are proper long arguments and if so if there is
one with just a single term
*/
for ( i = 0; i < numargs; i++ ) {
arg1 = AT.pWorkSpace[args+i];
if ( *arg1 > 0 && arg1[ARGHEAD]+ARGHEAD == *arg1 ) {
/*
We have an argument with a single term
*/
if ( i != 0 ) {
arg2 = AT.pWorkSpace[args];
AT.pWorkSpace[args] = arg1;
AT.pWorkSpace[args+1] = arg2;
}
m = mh = AT.WorkPointer;
mm = arg1+ARGHEAD; i = *mm;
NCOPY(m,mm,i);
AT.WorkPointer = m;
istart = 1;
argsdone++;
goto oneterm;
}
}
/*
#] There is a single term argument :
#[ Expand $ and expr :
We have: 1: regular multiterm arguments
2: dollars
3: expressions.
The sum of them is numargs. Their addresses are in args. The problem is
that expansion will lead to allocations that we have to return and all
these allocations are different in nature.
*/
action = 1;
abuf = (ARGBUFFER *)Malloc1(numargs*sizeof(ARGBUFFER),"argbuffer");
for ( i = 0; i < numargs; i++ ) {
arg1 = AT.pWorkSpace[args+i];
if ( *arg1 > 0 ) {
m = (WORD *)Malloc1(*arg1*sizeof(WORD),"argbuffer type 0");
abuf[i].buffer = m;
abuf[i].type = 0;
mm = arg1+ARGHEAD;
j = *arg1-ARGHEAD;
abuf[i].size = j;
if ( j ) argsdone++;
NCOPY(m,mm,j);
*m = 0;
}
else if ( *arg1 == -DOLLAREXPRESSION ) {
d = DolToTerms(BHEAD arg1[1]);
abuf[i].buffer = d->where;
abuf[i].type = 1;
abuf[i].dollar = d;
m = abuf[i].buffer;
if ( *m ) argsdone++;
while ( *m ) m+= *m;
abuf[i].size = m-abuf[i].buffer;
}
else if ( *arg1 == -EXPRESSION ) {
abuf[i].buffer = CreateExpression(BHEAD arg1[1]);
abuf[i].type = 2;
m = abuf[i].buffer;
if ( *m ) argsdone++;
while ( *m ) m+= *m;
abuf[i].size = m-abuf[i].buffer;
}
else {
MLOCK(ErrorMessageLock);
MesPrint("What argument is this?");
MUNLOCK(ErrorMessageLock);
goto CalledFrom;
}
}
for ( i = 0; i < numargs; i++ ) {
arg1 = abuf[i].buffer;