/
notation.c
1202 lines (1154 loc) · 27.8 KB
/
notation.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 notation.c
*
* Contains the functions that deal with the rewriting and manipulation
* of expressions/terms in polynomial representation.
*/
/* #[ License : */
/*
* Copyright (C) 1984-2013 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 :
*/
#include "form3.h"
/*
#] Includes :
#[ NormPolyTerm :
Brings a term to normal form.
This routine knows objects of the following types:
SYMBOL
HAAKJE
SNUMBER
LNUMBER
The SNUMBER and LNUMBER are worked into the coefficient.
One of the essences here is that everything can be done in place.
*/
int NormPolyTerm(PHEAD WORD *term)
{
WORD *tcoef, ncoef, *tstop, *tfill, *t, *tt;
int equal, i;
WORD *r1, *r2, *r3, *r4, *r5, *rfirst, rv;
WORD *lnum = AT.n_llnum+1, nnum; /* Scratch, originally for factorials */
/*
One: find the coefficient
*/
tcoef = term+*term;
ncoef = tcoef[-1];
tstop = tcoef - ABS(tcoef[-1]);
tfill = t = term + 1;
rfirst = 0;
if ( t >= tstop ) return(*term);
while ( t < tstop ) {
switch ( *t ) {
case SYMBOL:
if ( rfirst == 0 ) {
/*
Here we only need to sort
1: assume no equals. Bubble.
*/
rfirst = t;
r2 = rfirst+4; tt = r3 = t + t[1]; equal = 0;
while ( r2 < r3 ) {
r1 = r2 - 2;
if ( *r2 > *r1 ) { r2 += 2; continue; }
if ( *r2 == *r1 ) { r2 += 2; equal = 1; continue; }
rv = *r1; *r1 = *r2; *r2 = rv;
r1 -= 2; r2 -= 2; r4 = r2 + 2;
while ( r1 > t ) {
if ( *r2 >= *r1 ) { r2 = r4; break; }
rv = *r1; *r1 = *r2; *r2 = rv;
r1 -= 2; r2 -= 2;
}
}
/*
2: hunt down the equal objects
postpone eliminating zero powers.
*/
if ( equal ) {
r1 = t+2; r2 = r1+2;
while ( r2 < r3 ) {
if ( *r1 == *r2 ) {
r1[1] += r2[1];
r4 = r2+2;
while ( r4 < r3 ) *r2++ = *r4++;
t[1] -= 2;
r2 = r1 + 2; r3 -= 2;
}
}
}
}
else {
/*
Here we only need to insert
*/
r1 = t + 2; tt = r3 = t + t[1];
while ( r1 < r3 ) {
r2 = rfirst+2; r4 = rfirst + rfirst[1];
while ( r2 < r4 ) {
if ( *r1 == *r2 ) {
r2[1] += r1[1];
break;
}
else if ( *r2 > *r1 ) {
r5 = r4;
while ( r5 > r2 ) { r5[1] = r5[-1]; r5[0] = r5[-2]; r5 -= 2; }
rfirst[1] += 2;
*r2 = *r1; r2[1] = r1[1];
break;
}
r2 += 2;
}
if ( r2 == r4 ) {
rfirst[1] += 2;
*r2++ = *r1++; *r2++ = *r1++;
}
else r1 += 2;
}
}
t = tt;
break;
case HAAKJE: /* Here we skip brackets */
t += t[1];
break;
case SNUMBER:
if ( t[2] < 0 ) {
t[2] = -t[2];
if ( t[3] & 1 ) ncoef = -ncoef;
}
else if ( t[2] == 0 ) {
if ( t[3] < 0 ) goto NormInf;
goto NormZero;
}
lnum[0] = t[2];
nnum = 1;
if ( t[3] && RaisPow(BHEAD (UWORD *)lnum,&nnum,(UWORD)(ABS(t[3]))) ) goto FromNorm;
ncoef = REDLENG(ncoef);
if ( t[3] < 0 ) {
if ( Divvy(BHEAD (UWORD *)tstop,&ncoef,(UWORD *)lnum,nnum) )
goto FromNorm;
}
else if ( t[3] > 0 ) {
if ( Mully(BHEAD (UWORD *)tstop,&ncoef,(UWORD *)lnum,nnum) )
goto FromNorm;
}
ncoef = INCLENG(ncoef);
t += t[1];
break;
case LNUMBER:
ncoef = REDLENG(ncoef);
if ( Mully(BHEAD (UWORD *)tstop,&ncoef,(UWORD *)(t+3),t[2]) ) goto FromNorm;
ncoef = INCLENG(ncoef);
t += t[1];
break;
default:
MLOCK(ErrorMessageLock);
MesPrint("Illegal code in NormPolyTerm");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
break;
}
}
/*
Now we try to eliminate objects to the power zero.
*/
if ( rfirst ) {
r2 = rfirst+2;
r3 = rfirst + rfirst[1];
while ( r2 < r3 ) {
if ( r2[1] == 0 ) {
r1 = r2 + 2;
while ( r1 < r3 ) { r1[-2] = r1[0]; r1[-1] = r1[1]; r1 += 2; }
r3 -= 2;
rfirst[1] -= 2;
}
else { r2 += 2; }
}
if ( rfirst[1] < 4 ) rfirst = 0;
}
/*
Finally we put the term together
*/
if ( rfirst ) {
i = rfirst[1];
NCOPY(tfill,rfirst,i)
}
i = ABS(ncoef)-1;
NCOPY(tfill,tstop,i)
*tfill++ = ncoef;
*term = tfill - term;
return(*term);
NormZero:
*term = 0;
return(0);
NormInf:
MLOCK(ErrorMessageLock);
MesPrint("0^0 in NormPolyTerm");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
return(-1);
FromNorm:
MLOCK(ErrorMessageLock);
MesCall("NormPolyTerm");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
return(-1);
}
/*
#] NormPolyTerm :
#[ ComparePoly :
*/
/**
* Compares two terms. The answer is:
* 0 equal ( with exception of the coefficient )
* >0 term1 comes first.
* <0 term2 comes first.
* This routine should not return an error condition.
*
* The address of this routine is to be put in AR.CompareRoutine when we
* want to use it for sorting.
* This makes all existing code work properly and we can just replace the
* routine on a thread by thread basis (each thread has its own AR struct).
* Don't forget to put the old routine back afterwards!
*
* @param term1 First input term
* @param term2 Second input term
* @param level Not used for polynomials
* @return 0 equal ( with exception of the coefficient if level == 0. )
* >0 term1 comes first.
* <0 term2 comes first.
*/
#ifdef WITHCOMPAREPOLY
WORD ComparePoly(WORD *term1, WORD *term2, WORD level)
{
WORD *t1, *t2, *t3, *t4, *tstop1, *tstop2;
tstop1 = term1 + *term1;
tstop1 -= ABS(tstop1[-1]);
tstop2 = term2 + *term2;
tstop2 -= ABS(tstop2[-1]);
t1 = term1+1;
t2 = term2+1;
while ( t1 < tstop1 && t2 < tstop2 ) {
if ( *t1 == *t2 ) {
if ( *t1 == HAAKJE ) {
if ( t1[2] != t2[2] ) return(t2[2]-t1[2]);
t1 += t1[1]; t2 += t2[1];
}
else { /* must be type SYMBOL */
t3 = t1 + t1[1]; t4 = t2 + t2[1];
t1 += 2; t2 += 2;
while ( t1 < t3 && t2 < t4 ) {
if ( *t1 != *t2 ) return(*t2-*t1);
if ( t1[1] != t2[1] ) return(t2[1]-t1[1]);
t1 += 2; t2 += 2;
}
if ( t1 < t3 ) return(-1);
if ( t2 < t4 ) return(1);
}
}
else return(*t2-*t1);
}
if ( t1 < tstop1 ) return(-1);
if ( t2 < tstop2 ) return(1);
return(0);
}
#endif
/*
#] ComparePoly :
#[ ConvertToPoly :
*/
/**
* Converts a generic term to polynomial notation in which there are
* only symbols and brackets.
* During conversion there will be only symbols. Brackets are stripped.
* Objects that need 'translation' are put inside a special compiler
* buffer and represented by a symbol. The numbering of the extra
* symbols is down from the maximum. In principle there can be a
* problem when running into the already assigned ones.
* The output overwrites the input.
* comlist is the compiler code. Used for the various options
*/
static int FirstWarnConvertToPoly = 1;
int ConvertToPoly(PHEAD WORD *term, WORD *outterm, WORD *comlist, WORD par)
{
WORD *tout, *tstop, ncoef, *t, *r, *tt, *ttwo = 0;
int i, action = 0;
tt = term + *term;
ncoef = ABS(tt[-1]);
tstop = tt - ncoef;
tout = outterm+1;
t = term + 1;
if ( comlist[2] == DOALL ) {
while ( t < tstop ) {
if ( *t == SYMBOL ) {
r = t+2;
t += t[1];
while ( r < t ) {
if ( r[1] > 0 ) {
*tout++ = SYMBOL;
*tout++ = 4;
*tout++ = r[0];
*tout++ = r[1];
}
else {
tout[1] = SYMBOL;
tout[2] = 4;
tout[3] = r[0];
tout[4] = -1;
i = FindSubterm(tout+1);
*tout++ = SYMBOL;
*tout++ = 4;
*tout++ = MAXVARIABLES-i;
*tout++ = -r[1];
action = 1;
}
r += 2;
}
}
else if ( *t == DOTPRODUCT ) {
r = t + 2;
t += t[1];
while ( r < t ) {
tout[1] = DOTPRODUCT;
tout[2] = 5;
tout[3] = r[0];
tout[4] = r[1];
if ( r[2] < 0 ) {
tout[5] = -1;
}
else {
tout[5] = 1;
}
i = FindSubterm(tout+1);
*tout++ = SYMBOL;
*tout++ = 4;
*tout++ = MAXVARIABLES-i;
*tout++ = ABS(r[2]);
r += 3;
action = 1;
}
}
else if ( *t == VECTOR ) {
r = t + 2;
t += t[1];
while ( r < t ) {
tout[1] = VECTOR;
tout[2] = 4;
tout[3] = r[0];
tout[4] = r[1];
i = FindSubterm(tout+1);
*tout++ = SYMBOL;
*tout++ = 4;
*tout++ = MAXVARIABLES-i;
*tout++ = 1;
r += 2;
action = 1;
}
}
else if ( *t == INDEX ) {
r = t + 2;
t += t[1];
while ( r < t ) {
tout[1] = INDEX;
tout[2] = 3;
tout[3] = r[0];
i = FindSubterm(tout+1);
*tout++ = SYMBOL;
*tout++ = 4;
*tout++ = MAXVARIABLES-i;
*tout++ = 1;
r++;
action = 1;
}
}
else if ( *t == HAAKJE) {
if ( par ) {
tout[0] = 1; tout[1] = 1; tout[2] = 3;
*outterm = (tout+3)-outterm;
if ( NormPolyTerm(BHEAD outterm) < 0 ) return(-1);
tout = outterm + *outterm;
tout -= 3;
i = t[1]; NCOPY(tout,t,i);
ttwo = tout-1;
}
else { t += t[1]; }
}
else if ( *t >= FUNCTION ) {
i = FindSubterm(t);
t += t[1];
*tout++ = SYMBOL;
*tout++ = 4;
*tout++ = MAXVARIABLES-i;
*tout++ = 1;
action = 1;
}
else {
if ( FirstWarnConvertToPoly ) {
MLOCK(ErrorMessageLock);
MesPrint("Illegal object in conversion to polynomial notation");
MUNLOCK(ErrorMessageLock);
FirstWarnConvertToPoly = 0;
}
return(-1);
}
}
NCOPY(tout,tstop,ncoef)
if ( ttwo ) {
WORD hh = *ttwo;
*ttwo = tout-ttwo;
if ( ( i = NormPolyTerm(BHEAD ttwo) ) >= 0 ) i = action;
tout = ttwo + *ttwo;
*ttwo = hh;
*outterm = tout - outterm;
}
else {
*outterm = tout-outterm;
if ( ( i = NormPolyTerm(BHEAD outterm) ) >= 0 ) i = action;
}
}
else if ( comlist[2] == ONLYFUNCTIONS ) {
while ( t < tstop ) {
if ( *t >= FUNCTION ) {
if ( comlist[1] == 3 ) {
i = FindSubterm(t);
t += t[1];
*tout++ = SYMBOL;
*tout++ = 4;
*tout++ = MAXVARIABLES-i;
*tout++ = 1;
action = 1;
}
else {
for ( i = 3; i < comlist[1]; i++ ) {
if ( *t == comlist[i] ) break;
}
if ( i < comlist[1] ) {
i = FindSubterm(t);
t += t[1];
*tout++ = SYMBOL;
*tout++ = 4;
*tout++ = MAXVARIABLES-i;
*tout++ = 1;
action = 1;
}
else {
i = t[1]; NCOPY(tout,t,i);
}
}
}
else {
i = t[1]; NCOPY(tout,t,i);
}
}
NCOPY(tout,tstop,ncoef)
*outterm = tout-outterm;
Normalize(BHEAD outterm);
i = action;
}
else {
MLOCK(ErrorMessageLock);
MesPrint("Illegal internal code in conversion to polynomial notation");
MUNLOCK(ErrorMessageLock);
i = -1;
}
return(i);
}
/*
#] ConvertToPoly :
#[ LocalConvertToPoly :
*/
/**
* Converts a generic term to polynomial notation in which there are
* only symbols and brackets.
* During conversion there will be only symbols. Brackets are stripped.
* Objects that need 'translation' are put inside a special compiler
* buffer and represented by a symbol. The numbering of the extra
* symbols is down from the maximum. In principle there can be a
* problem when running into the already assigned ones.
* This uses the FindTree for searching in the global tree and
* then looks further in the AT.ebufnum. This allows fully parallel
* processing. Hence we need no locks. Cannot be used in the same
* module as ConvertToPoly.
*/
int LocalConvertToPoly(PHEAD WORD *term, WORD *outterm, WORD startebuf, WORD par)
{
WORD *tout, *tstop, ncoef, *t, *r, *tt, *ttwo = 0;
int i, action = 0;
tt = term + *term;
ncoef = ABS(tt[-1]);
tstop = tt - ncoef;
tout = outterm+1;
t = term + 1;
while ( t < tstop ) {
if ( *t == SYMBOL ) {
r = t+2;
t += t[1];
while ( r < t ) {
if ( r[1] > 0 ) {
*tout++ = SYMBOL;
*tout++ = 4;
*tout++ = r[0];
*tout++ = r[1];
}
else {
tout[1] = SYMBOL;
tout[2] = 4;
tout[3] = r[0];
tout[4] = -1;
i = FindLocalSubterm(BHEAD tout+1,startebuf);
*tout++ = SYMBOL;
*tout++ = 4;
*tout++ = MAXVARIABLES-i;
*tout++ = -r[1];
action = 1;
}
r += 2;
}
}
else if ( *t == DOTPRODUCT ) {
r = t + 2;
t += t[1];
while ( r < t ) {
tout[1] = DOTPRODUCT;
tout[2] = 5;
tout[3] = r[0];
tout[4] = r[1];
if ( r[2] < 0 ) {
tout[5] = -1;
}
else {
tout[5] = 1;
}
i = FindLocalSubterm(BHEAD tout+1,startebuf);
*tout++ = SYMBOL;
*tout++ = 4;
*tout++ = MAXVARIABLES-i;
*tout++ = ABS(r[2]);
r += 3;
action = 1;
}
}
else if ( *t == VECTOR ) {
r = t + 2;
t += t[1];
while ( r < t ) {
tout[1] = VECTOR;
tout[2] = 4;
tout[3] = r[0];
tout[4] = r[1];
i = FindLocalSubterm(BHEAD tout+1,startebuf);
*tout++ = SYMBOL;
*tout++ = 4;
*tout++ = MAXVARIABLES-i;
*tout++ = 1;
r += 2;
action = 1;
}
}
else if ( *t == INDEX ) {
r = t + 2;
t += t[1];
while ( r < t ) {
tout[1] = INDEX;
tout[2] = 3;
tout[3] = r[0];
i = FindLocalSubterm(BHEAD tout+1,startebuf);
*tout++ = SYMBOL;
*tout++ = 4;
*tout++ = MAXVARIABLES-i;
*tout++ = 1;
r++;
action = 1;
}
}
else if ( *t == HAAKJE) {
if ( par ) {
tout[0] = 1; tout[1] = 1; tout[2] = 3;
*outterm = (tout+3)-outterm;
if ( NormPolyTerm(BHEAD outterm) < 0 ) return(-1);
tout = outterm + *outterm;
tout -= 3;
i = t[1]; NCOPY(tout,t,i);
ttwo = tout-1;
}
else { t += t[1]; }
}
else if ( *t >= FUNCTION ) {
i = FindLocalSubterm(BHEAD t,startebuf);
t += t[1];
*tout++ = SYMBOL;
*tout++ = 4;
*tout++ = MAXVARIABLES-i;
*tout++ = 1;
action = 1;
}
else {
if ( FirstWarnConvertToPoly ) {
MLOCK(ErrorMessageLock);
MesPrint("Illegal object in conversion to polynomial notation");
MUNLOCK(ErrorMessageLock);
FirstWarnConvertToPoly = 0;
}
return(-1);
}
}
NCOPY(tout,tstop,ncoef)
if ( ttwo ) {
WORD hh = *ttwo;
*ttwo = tout-ttwo;
if ( ( i = NormPolyTerm(BHEAD ttwo) ) >= 0 ) i = action;
tout = ttwo + *ttwo;
*ttwo = hh;
*outterm = tout - outterm;
}
else {
*outterm = tout-outterm;
if ( ( i = NormPolyTerm(BHEAD outterm) ) >= 0 ) i = action;
}
return(i);
}
/*
#] LocalConvertToPoly :
#[ ConvertFromPoly :
Converts a generic term from polynomial notation to the original
in which the extra symbols have been replaced by their values.
The output is in outterm.
We only deal with the extra symbols in the range from < i <= to
The output has to be sent to TestSub because it may contain
subexpressions when extra symbols have been replaced.
*/
int ConvertFromPoly(PHEAD WORD *term, WORD *outterm, WORD from, WORD to, WORD offset, WORD par)
{
WORD *tout, *tstop, *tstop1, ncoef, *t, *r, *tt;
int i;
/* first = 1; */
tt = term + *term;
tout = outterm+1;
ncoef = ABS(tt[-1]);
tstop = tt - ncoef;
/*
r = t = term + 1;
while ( t < tstop ) {
if ( *t == SYMBOL ) {
tstop1 = t + t[1];
tt = t + 2;
while ( tt < tstop1 ) {
if ( ( *tt < MAXVARIABLES - to )
|| ( *tt >= MAXVARIABLES - from ) ) {
tt += 2;
}
else break;
}
if ( tt >= tstop1 ) { t = tstop1; continue; }
while ( r < t ) *tout++ = *r++;
t += 2;
first = 0;
while ( t < tstop1 ) {
if ( ( *t < MAXVARIABLES - to )
|| ( *t >= MAXVARIABLES - from ) ) {
*tout++ = SYMBOL;
*tout++ = 4;
*tout++ = *t++;
*tout++ = *t++;
}
else {
*tout++ = SUBEXPRESSION;
*tout++ = SUBEXPSIZE;
*tout++ = MAXVARIABLES - *t++ + offset;
*tout++ = *t++;
if ( par ) *tout++ = AT.ebufnum;
else *tout++ = AM.sbufnum;
FILLSUB(tout)
}
}
r = t;
}
else {
t += t[1];
}
}
if ( first ) {
i = *term; t = term;
NCOPY(outterm,t,i);
return(*term);
}
while ( r < t ) *tout++ = *r++;
NCOPY(tout,tstop,ncoef)
*outterm = tout-outterm;
*/
t = term + 1;
while ( t < tstop ) {
if ( *t == SYMBOL ) {
tstop1 = t + t[1];
tt = t + 2;
while ( tt < tstop1 ) {
if ( ( *tt < MAXVARIABLES - to )
|| ( *tt >= MAXVARIABLES - from ) ) {
tt += 2;
}
else {
*tout++ = SUBEXPRESSION;
*tout++ = SUBEXPSIZE;
*tout++ = MAXVARIABLES - *tt++ + offset;
*tout++ = *tt++;
if ( par ) *tout++ = AT.ebufnum;
else *tout++ = AM.sbufnum;
FILLSUB(tout)
}
}
r = tout; t += 2;
*tout++ = SYMBOL; *tout++ = 0;
while ( t < tstop1 ) {
if ( ( *t < MAXVARIABLES - to )
|| ( *t >= MAXVARIABLES - from ) ) {
*tout++ = *t++;
*tout++ = *t++;
}
else { t += 2; }
}
r[1] = tout - r;
if ( r[1] <= 2 ) tout = r;
}
else {
i = t[1]; NCOPY(tout,t,i)
}
}
NCOPY(tout,tstop,ncoef)
*outterm = tout-outterm;
return(*outterm);
}
/*
#] ConvertFromPoly :
#[ FindSubterm :
In this routine we look up a variable.
If we don't find it we will enter it in the subterm compiler buffer
Searching is by tree structure.
Adding changes the tree.
Notice that in TFORM we should be in sequential mode.
*/
WORD FindSubterm(WORD *subterm)
{
WORD old[5], *ss, *term, number;
CBUF *C = cbuf + AM.sbufnum;
LONG oldCpointer;
term = subterm-1;
ss = subterm+subterm[1];
/*
Convert to proper term
*/
old[0] = *term; old[1] = ss[0]; old[2] = ss[1]; old[3] = ss[2]; old[4] = ss[3];
ss[0] = 1; ss[1] = 1; ss[2] = 3; ss[3] = 0; *term = subterm[1]+4;
/*
We may have to add the term to the compiler
buffer and then to the tree. This cannot be done in parallel and
hence we have to set a lock.
*/
LOCK(AM.sbuflock);
oldCpointer = C->Pointer-C->Buffer; /* Offset of course !!!!!*/
AddRHS(AM.sbufnum,1);
AddNtoC(AM.sbufnum,*term,term);
AddToCB(C,0)
/*
See whether we have this one already. If not, insert it in the tree.
*/
number = InsTree(AM.sbufnum,C->numrhs);
/*
Restore old values and return what is needed.
*/
if ( number < (C->numrhs) ) { /* It existed already */
C->Pointer = oldCpointer + C->Buffer;
C->numrhs--;
}
else {
GETIDENTITY
WORD dim = DimensionSubterm(subterm);
if ( dim == -MAXPOSITIVE ) { /* Give error message but continue */
WORD *old = AN.currentTerm;
AN.currentTerm = term;
MLOCK(ErrorMessageLock);
MesPrint("Dimension out of range in %t");
MUNLOCK(ErrorMessageLock);
AN.currentTerm = old;
}
/*
Store the dimension
*/
C->dimension[number] = dim;
}
UNLOCK(AM.sbuflock);
*term = old[0]; ss[0] = old[1]; ss[1] = old[2]; ss[2] = old[3]; ss[3] = old[4];
return(number);
}
/*
#] FindSubterm :
#[ FindLocalSubterm :
In this routine we look up a variable.
If we don't find it we will enter it in the subterm compiler buffer
Searching is by tree structure.
Adding changes the tree.
Notice that in TFORM we should be in sequential mode.
*/
WORD FindLocalSubterm(PHEAD WORD *subterm, WORD startebuf)
{
WORD old[5], *ss, *term, number, i, j, *t1, *t2;
CBUF *C = cbuf + AT.ebufnum;
term = subterm-1;
ss = subterm+subterm[1];
/*
Convert to proper term
*/
old[0] = *term; old[1] = ss[0]; old[2] = ss[1]; old[3] = ss[2]; old[4] = ss[3];
ss[0] = 1; ss[1] = 1; ss[2] = 3; ss[3] = 0; *term = subterm[1]+4;
/*
First see whether we have this one already in the global buffer.
*/
number = FindTree(AM.sbufnum,term);
if ( number > 0 ) goto wearehappy;
/*
Now look whether it is in the ebufnum between startebuf and numrhs
Note however that we need an offset of (numxsymbol-startebuf)
*/
for ( i = startebuf+1; i <= C->numrhs; i++ ) {
t1 = C->rhs[i]; t2 = term;
if ( *t1 == *t2 ) {
j = *t1;
while ( *t1 == *t2 && j > 0 ) { t1++; t2++; j--; }
if ( j <= 0 ) {
number = i-startebuf+numxsymbol;
goto wearehappy;
}
}
}
/*
Now we have to add it to cbuf[AT.ebufnum]
*/
AddRHS(AT.ebufnum,1);
AddNtoC(AT.ebufnum,*term,term);
AddToCB(C,0)
number = C->numrhs-startebuf+numxsymbol;
wearehappy:
*term = old[0]; ss[0] = old[1]; ss[1] = old[2]; ss[2] = old[3]; ss[3] = old[4];
return(number);
}
/*
#] FindLocalSubterm :
#[ PrintSubtermList :
Prints all the expressions in the subterm compiler buffer.
The format is such that they give definitions of the temporary
variables of which the contents are stored in this buffer.
These variables have the names Z_123 etc.
*/
void PrintSubtermList(int from,int to)
{
UBYTE buffer[80], *out, outbuffer[300];
int first, i, ii, inc = 1;
WORD *term;
CBUF *C = cbuf + AM.sbufnum;
/*
if ( to < from ) inc = -1;
if ( to == from ) inc = 0;
*/
if ( from <= to ) {
inc = 1; to += inc;
}
else {
inc = -1; to += inc;
}
AO.OutFill = AO.OutputLine = outbuffer;
AO.OutStop = AO.OutputLine+AC.LineLength;
AO.IsBracket = 0;
AO.OutSkip = 3;
if ( AC.OutputMode == FORTRANMODE || AC.OutputMode == PFORTRANMODE ) {
TokenToLine((UBYTE *)" ");
AO.OutSkip = 7;
}
else if ( ( AO.Optimize.debugflags & 1 ) == 1 ) {}
else if ( AO.OutSkip > 0 ) {
for ( i = 0; i < AO.OutSkip; i++ ) TokenToLine((UBYTE *)" ");
}
i = from;
do {
if ( ( AO.Optimize.debugflags & 1 ) == 1 ) {
TokenToLine((UBYTE *)"id ");
for ( ii = 3; ii < AO.OutSkip; ii++ ) TokenToLine((UBYTE *)" ");
}
/*
if ( AC.OutputMode == NORMALFORMAT ) {
TokenToLine((UBYTE *)"id ");
}
*/
else if ( AC.OutputMode == FORTRANMODE || AC.OutputMode == PFORTRANMODE ) {}
else { TokenToLine((UBYTE *)" "); }
out = StrCopy((UBYTE *)AC.extrasym,buffer);
if ( AC.extrasymbols == 0 ) {
out = NumCopy(i,out);
out = StrCopy((UBYTE *)"_",out);
}
else if ( AC.extrasymbols == 1 ) {
out = AddArrayIndex(i,out);
}
out = StrCopy((UBYTE *)"=",out);
TokenToLine(buffer);
term = C->rhs[i];
first = 1;
if ( *term == 0 ) {
out = StrCopy((UBYTE *)"0",buffer);
if ( AC.OutputMode != FORTRANMODE && AC.OutputMode != PFORTRANMODE ) {
out = StrCopy((UBYTE *)";",out);
}
TokenToLine(buffer);
}
else {
while ( *term ) {
if ( WriteInnerTerm(term,first) ) Terminate(-1);
term += *term;
first = 0;
}
if ( AC.OutputMode != FORTRANMODE && AC.OutputMode != PFORTRANMODE ) {
out = StrCopy((UBYTE *)";",buffer);
TokenToLine(buffer);
}
}
/*
There is a problem with FiniLine because it prepares for a
continuation line in fortran mode.
But the next statement should start on a blank line.
*/
/*
FiniLine();
if ( AC.OutputMode == FORTRANMODE || AC.OutputMode == PFORTRANMODE ) {
AO.OutFill = AO.OutputLine;
TokenToLine((UBYTE *)" ");
AO.OutSkip = 7;
}
*/
if ( AC.OutputMode == FORTRANMODE || AC.OutputMode == PFORTRANMODE ) {
AO.OutSkip = 6;
FiniLine();
AO.OutSkip = 7;
}
else {
FiniLine();
}
i += inc;
} while ( i != to );
}
/*
#] PrintSubtermList :
#[ PrintExtraSymbol :
Prints the definition of extra symbol num as the contents
of the expression in terms.
The parameter par has three options:
EXTRASYMBOL num is interpreted as the number of an extra symbol
REGULARSYMBOL num is interpreted as the number of a symbol.
It could still be an extra symbol.