forked from vermaseren/form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern.c
2387 lines (2352 loc) · 61 KB
/
pattern.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 pattern.c
*
* Top level pattern matching routines.
* More pattern matching is found in findpat.c, function.c, symmetr.c
* and smart.c. The last three files contain the matching inside functions.
* The file pattern.c contains also the very important routine Substitute.
* All regular pattern matching is just the finding of the pattern and
* indicating what are the wildcards etc. The routine Substitute does
* the actual removal of the pattern and replaces it by a subterm of the
* type SUBEXPRESSION.
*/
/* #[ License : */
/*
* Copyright (C) 1984-2022 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 : */
/*
!!! Notice the change in OnePV in FindAll (7-may-2008 JV).
#[ Includes : pattern.c
*/
#include "form3.h"
/*
#] Includes :
#[ Patterns :
#[ Rules :
There are several rules governing the allowable replacements.
1: Multi with anything but symbols or dotproducts reverts
to many.
2: Each symbol can have only one (wildcard) power, so
x^2*x^n? is illegal.
3: when a single vector is used it replaces all occurences
of the vector. Therefore q*q(mu) or q*q(mu) cannot occur.
Also q*q cannot be done.
4: Loose vector elements are replaced with p(mu), dotproducts
with p?.q.
5: p?.q? is allowed.
6: x^n? can revert to n = 0 if there is no power of x.
7: x?^n? must match some x. There could be an ambiguity otherwise.
#] Rules :
#[ TestMatch : WORD TestMatch(term,level)
*/
/**
This routine governs the pattern matching. If it decides
that a substitution should be made, this can be either the
insertion of a right hand side (C->rhs) or the automatic generation
of terms as a result of an operation (like trace).
The object to be replaced is removed from term and a subexpression
pointer is inserted. If the substitution is made more than once
there can be more subexpression pointers. Its number is positive
as it corresponds to the level at which the C->rhs can be found
in the compiler output. The subexpression pointer contains the
wildcard substitution information. The power is found in *AT.TMout.
For operations the subexpression pointer is negative and corresponds
to an address in the array AT.TMout. In this array are then the
instructions for the routine to be called and its number in
the array 'Operations'
The format is here:
length,functionnumber,length-2 parameters
There is a certain complexity wrt repeat levels.
Another complication is the poking of the wildcard values in the
subexpression prototype in the compiler buffer. This was how things were
done in the past with sequential FORM, but with the advent of TFORM this
cannot be maintained. Now, for TFORM we make a copy of it.
7-may-2008 (JV):
We cannot yet guarantee that this has been done 100% correctly. There
are errors that occur in TFORM only and that may indicate problems.
*/
WORD TestMatch(PHEAD WORD *term, WORD *level)
{
GETBIDENTITY
WORD *ll, *m, *w, *llf, *OldWork, *StartWork, *ww, *mm, *t, *OldTermBuffer = 0;
WORD power = 0, match = 0, i, msign = 0, ll2;
int numdollars = 0, protosize, oldallnumrhs;
CBUF *C = cbuf+AM.rbufnum, *CC;
AT.idallflag = 0;
do {
/*
#[ Preliminaries :
*/
ll = C->lhs[*level];
if ( *ll == TYPEEXPRESSION ) {
/*
Expressions are not subject to anything.
*/
return(0);
}
else if ( *ll == TYPEREPEAT ) {
*++AN.RepPoint = 0;
return(0); /* Will force the next level */
}
else if ( *ll == TYPEENDREPEAT ) {
if ( *AN.RepPoint ) {
AN.RepPoint[-1] = 1; /* Mark the higher level as dirty */
*AN.RepPoint = 0;
*level = ll[2]; /* Level to jump back to */
}
else {
AN.RepPoint--;
if ( AN.RepPoint < AT.RepCount ) {
MLOCK(ErrorMessageLock);
MesPrint("Internal problems with REPEAT count");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
}
return(0); /* Force the next level */
}
else if ( *ll == TYPEOPERATION ) {
/*
Operations have always their own level.
*/
if ( (*(FG.OperaFind[ll[2]]))(BHEAD term,ll) ) return(-1);
else return(0);
}
/*
#] Preliminaries :
*/
OldWork = AT.WorkPointer;
if ( AT.WorkPointer < term + *term ) AT.WorkPointer = term + *term;
ww = AT.WorkPointer;
/*
Here we need to make a copy of the subexpression object because we
will be writing the values of the wildcards in it.
Originally we copied it into the private version of the compiler buffer
that is used for scratch space (ebufnum). This caused errors in the
routines like ScanFunctions when the ebufnum Buffer was expanded
and inpat was still pointing at the old Buffer. This expansion
could be done in AddWild and hence cannot be fixed at > 100 places.
The solution is to use AN.patternbuffer (JV 16-mar-2009).
*/
{
WORD *ta = ll, *ma;
int ja = ta[1];
/*
New code (16-mar-2009) JV
*/
if ( ( ja + 2 ) > AN.patternbuffersize ) {
if ( AN.patternbuffer ) M_free(AN.patternbuffer,"AN.patternbuffer");
AN.patternbuffersize = 2 * ja + 2;
AN.patternbuffer = (WORD *)Malloc1(AN.patternbuffersize * sizeof(WORD),
"AN.patternbuffer");
}
ma = AN.patternbuffer;
m = ma + IDHEAD;
NCOPY(ma,ta,ja);
*ma = 0;
}
AN.FullProto = m;
AN.WildValue = w = m + SUBEXPSIZE;
protosize = IDHEAD + m[1];
m += m[1];
AN.WildStop = m;
StartWork = ww;
ll2 = ll[2];
/*
#[ Expand dollars :
*/
if ( ( ll[4] & DOLLARFLAG ) != 0 ) { /* We have at least one dollar in the pattern */
WORD oldRepPoint = *AN.RepPoint, olddefer = AR.DeferFlag;
AR.Eside = LHSIDEX;
/*
Copy into WorkSpace. This means that AN.patternbuffer will be free.
*/
ww = AT.WorkPointer; i = m[0]; mm = m;
NCOPY(ww,mm,i);
*StartWork += 3;
*ww++ = 1; *ww++ = 1; *ww++ = 3;
AT.WorkPointer = ww;
AR.DeferFlag = 0;
NewSort(BHEAD0);
if ( Generator(BHEAD StartWork,AR.Cnumlhs) ) {
LowerSortLevel();
AT.WorkPointer = OldWork;
AR.DeferFlag = olddefer;
return(-1);
}
AT.WorkPointer = ww;
if ( EndSort(BHEAD ww,0) < 0 ) {}
AR.DeferFlag = olddefer;
if ( *ww == 0 || *(ww+*ww) != 0 ) {
if ( AP.lhdollarerror == 0 ) {
/*
If race condition we just get more error messages
*/
MLOCK(ErrorMessageLock);
MesPrint("&LHS must be one term");
MUNLOCK(ErrorMessageLock);
AP.lhdollarerror = 1;
}
AT.WorkPointer = OldWork;
return(-1);
}
m = ww; ww = m + *m;
if ( m[*m-1] < 0 ) { msign = 1; m[*m-1] = -m[*m-1]; }
if ( *ww || m[*m-1] != 3 || m[*m-2] != 1 || m[*m-3] != 1 ) {
MLOCK(ErrorMessageLock);
MesPrint("Dollar variable develops into an illegal pattern in id-statement");
MUNLOCK(ErrorMessageLock);
return(-1);
}
*m -= m[*m-1];
if ( ( *m + 1 + protosize ) > AN.patternbuffersize ) {
if ( AN.patternbuffer ) M_free(AN.patternbuffer,"AN.patternbuffer");
AN.patternbuffersize = 2 * (*m) + 2 + protosize;
AN.patternbuffer = (WORD *)Malloc1(AN.patternbuffersize * sizeof(WORD),
"AN.patternbuffer");
mm = ll; ww = AN.patternbuffer; i = protosize;
NCOPY(ww,mm,i);
AN.FullProto = AN.patternbuffer + IDHEAD;
AN.WildValue = w = AN.FullProto + SUBEXPSIZE;
AN.WildStop = AN.patternbuffer + protosize;
}
mm = AN.patternbuffer + protosize;
i = *m;
NCOPY(mm,m,i);
m = AN.patternbuffer + protosize;
AR.Eside = RHSIDE;
*mm = 0;
/*
Test the pattern. If only wildcard powers -> SUBONCE
*/
{
WORD *mmm = m + *m, *m1 = m+1, jm, noveto = 0;
while ( m1 < mmm ) {
if ( *m1 == SYMBOL ) {
for ( jm = 2; jm < m1[1]; jm+=2 ) {
if ( m1[jm+1] < MAXPOWER && m1[jm+1] > -MAXPOWER ) break;
}
if ( jm < m1[1] ) { noveto = 1; break; }
}
else if ( *m1 == DOTPRODUCT ) {
for ( jm = 2; jm < m1[1]; jm+=3 ) {
if ( m1[jm+2] < MAXPOWER && m1[jm+2] > -MAXPOWER ) break;
}
if ( jm < m1[1] ) { noveto = 1; break; }
}
else { noveto = 1; break; }
m1 += m1[1];
}
if ( noveto == 0 ) {
ll2 = ll2 & ~SUBMASK;
ll2 |= SUBONCE;
}
}
AT.WorkPointer = ww = StartWork;
*AN.RepPoint = oldRepPoint;
}
/*
#] Expand dollars :
In case of id,all we have to check at this point that there are only
functions in the pattern.
*/
if ( ( ll2 & SUBMASK ) == SUBALL ) {
WORD *t = AN.patternbuffer+IDHEAD, *tt;
WORD *tstop, *ttstop, ii;
t += t[1]; tstop = t + *t; t++;
while ( t < tstop ) {
if ( *t < FUNCTION ) break;
t += t[1];
}
if ( t < tstop ) {
MLOCK(ErrorMessageLock);
MesPrint("Error: id,all can only be used with (products of) functions and/or tensors.");
MUNLOCK(ErrorMessageLock);
return(-1);
}
OldTermBuffer = AN.termbuffer;
AN.termbuffer = TermMalloc("id,all");
/*
Now make sure that only regular functions and tensors can take part.
*/
tt = term; ttstop = tt+*tt; ttstop -= ABS(ttstop[-1]); tt++;
t = AN.termbuffer+1;
while ( tt < ttstop ) {
if ( *tt >= FUNCTION && *tt != AR.PolyFun && *tt != AR.PolyFunInv ) {
ii = tt[1]; NCOPY(t,tt,ii);
}
else tt += tt[1];
}
*t++ = 1; *t++ = 1; *t++ = 3; AN.termbuffer[0] = t-AN.termbuffer;
}
/*
To be puristic, we need to check that all wildcards in the prototype
are actually present. If the LHS contained a replace_ this may not be
the case.
*/
ClearWild(BHEAD0);
while ( w < AN.WildStop ) {
if ( *w == LOADDOLLAR ) numdollars++;
w += w[1];
}
AN.RepFunNum = 0;
/* rep = */ AN.RepFunList = AT.WorkPointer;
AT.WorkPointer = (WORD *)(((UBYTE *)(AT.WorkPointer)) + AM.MaxTer/2);
if ( AT.WorkPointer >= AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
AN.DisOrderFlag = ll2 & SUBDISORDER;
AN.nogroundlevel = 0;
switch ( ll2 & SUBMASK ) {
case SUBONLY :
/* Must be an exact match */
AN.UseFindOnly = 1; AN.ForFindOnly = 0;
if ( FindRest(BHEAD term,m) && ( AN.UsedOtherFind ||
FindOnly(BHEAD term,m) ) ) {
power = 1;
if ( msign ) term[term[0]-1] = -term[term[0]-1];
}
else power = 0;
break;
case SUBMANY :
AN.UseFindOnly = -1;
if ( ( power = FindRest(BHEAD term,m) ) > 0 ) {
if ( ( power = FindOnce(BHEAD term,m) ) > 0 ) {
AN.UseFindOnly = 0;
do {
if ( msign ) term[term[0]-1] = -term[term[0]-1];
Substitute(BHEAD term,m,1);
if ( numdollars ) {
WildDollars(BHEAD (WORD *)0);
numdollars = 0;
}
if ( ww < term+term[0] ) ww = term+term[0];
ClearWild(BHEAD0);
AT.WorkPointer = ww;
/* if ( rep < ww ) {*/
AN.RepFunNum = 0;
/* rep = */ AN.RepFunList = ww;
AT.WorkPointer = (WORD *)(((UBYTE *)(AT.WorkPointer)) + AM.MaxTer/2);
if ( AT.WorkPointer >= AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
/*
}
else {
AN.RepFunList = rep;
AN.RepFunNum = 0;
}
*/
AN.nogroundlevel = 0;
} while ( FindRest(BHEAD term,m) && ( AN.UsedOtherFind ||
FindOnce(BHEAD term,m) ) );
match = 1;
}
else if ( power < 0 ) {
do {
if ( msign ) term[term[0]-1] = -term[term[0]-1];
Substitute(BHEAD term,m,1);
if ( numdollars ) {
WildDollars(BHEAD (WORD *)0);
numdollars = 0;
}
if ( ww < term+term[0] ) ww = term+term[0];
ClearWild(BHEAD0);
AT.WorkPointer = ww;
/* if ( rep < ww ) { */
AN.RepFunNum = 0;
/* rep = */ AN.RepFunList = ww;
AT.WorkPointer = (WORD *)(((UBYTE *)(AT.WorkPointer)) + AM.MaxTer/2);
if ( AT.WorkPointer >= AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
/*
}
else {
AN.RepFunList = rep;
AN.RepFunNum = 0;
}
*/
} while ( FindRest(BHEAD term,m) );
match = 1;
}
}
else if ( power < 0 ) {
if ( FindOnce(BHEAD term,m) ) {
do {
if ( msign ) term[term[0]-1] = -term[term[0]-1];
Substitute(BHEAD term,m,1);
if ( numdollars ) {
WildDollars(BHEAD (WORD *)0);
numdollars = 0;
}
if ( ww < term+term[0] ) ww = term+term[0];
ClearWild(BHEAD0);
AT.WorkPointer = ww;
/* if ( rep < ww ) { */
AN.RepFunNum = 0;
/* rep = */ AN.RepFunList = ww;
AT.WorkPointer = (WORD *)(((UBYTE *)(AT.WorkPointer)) + AM.MaxTer/2);
if ( AT.WorkPointer >= AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
/*
}
else {
AN.RepFunList = rep;
AN.RepFunNum = 0;
}
*/
} while ( FindOnce(BHEAD term,m) );
match = 1;
}
}
if ( match ) {
if ( ( ll2 & SUBAFTER ) != 0 ) *level = AC.Labels[ll[3]];
}
else {
if ( ( ll2 & SUBAFTERNOT ) != 0 ) *level = AC.Labels[ll[3]];
}
goto nextlevel;
case SUBONCE :
AN.UseFindOnly = 0;
if ( FindRest(BHEAD term,m) && ( AN.UsedOtherFind || FindOnce(BHEAD term,m) ) ) {
power = 1;
if ( msign ) term[term[0]-1] = -term[term[0]-1];
}
else power = 0;
break;
case SUBMULTI :
power = FindMulti(BHEAD term,m);
if ( ( power & 1 ) != 0 && msign ) term[term[0]-1] = -term[term[0]-1];
break;
case SUBVECTOR :
while ( ( power = FindAll(BHEAD term,m,*level,(WORD *)0) ) != 0 ) {
if ( ( power & 1 ) != 0 && msign ) term[term[0]-1] = -term[term[0]-1];
match = 1;
}
break;
case SUBSELECT :
llf = ll + IDHEAD; llf += llf[1]; llf += *llf;
AN.UseFindOnly = 1; AN.ForFindOnly = llf;
if ( FindRest(BHEAD term,m) && ( AN.UsedOtherFind || FindOnly(BHEAD term,m) ) ) {
if ( msign ) term[term[0]-1] = -term[term[0]-1];
/*
The following code needs to be hacked a bit to allow for
all types of sets and for occurrence anywhere in the term
The code at the end of FindOnly is a bit mysterious.
*/
if ( llf[1] > 2 ) {
WORD *t1, *t2;
if ( *term > AN.sizeselecttermundo ) {
if ( AN.selecttermundo ) M_free(AN.selecttermundo,"AN.selecttermundo");
AN.sizeselecttermundo = *term +10;
AN.selecttermundo = (WORD *)Malloc1(
AN.sizeselecttermundo*sizeof(WORD),"AN.selecttermundo");
}
t1 = term; t2 = AN.selecttermundo; i = *term;
NCOPY(t2,t1,i);
}
power = 1;
Substitute(BHEAD term,m,power);
if ( llf[1] > 2 ) {
if ( TestSelect(term,llf) ) {
WORD *t1, *t2;
power = 0;
t1 = term; t2 = AN.selecttermundo; i = *t2;
NCOPY(t1,t2,i);
#if IDHEAD > 3
if ( ( ll2 & SUBAFTERNOT ) != 0 ) {
*level = AC.Labels[ll[3]];
}
#endif
goto nextlevel;
}
}
if ( numdollars ) {
WildDollars(BHEAD (WORD *)0);
numdollars = 0;
}
match = 1;
if ( ( ll2 & SUBAFTER ) != 0 ) {
*level = AC.Labels[ll[3]];
}
}
else {
if ( ( ll2 & SUBAFTERNOT ) != 0 ) {
*level = AC.Labels[ll[3]];
}
power = 0;
}
goto nextlevel;
case SUBALL:
AN.UseFindOnly = 0;
CC = cbuf+AT.allbufnum;
oldallnumrhs = CC->numrhs;
t = AddRHS(AT.allbufnum,1);
*t = 0;
AT.idallflag = 1;
AT.idallmaxnum = ll[5];
AT.idallnum = 0;
if ( FindRest(BHEAD AN.termbuffer,m) || AT.idallflag > 1 ) {
WORD *t, *tstop, *tt, first = 1, ii;
power = 1;
*CC->Pointer++ = 0;
if ( msign ) term[term[0]-1] = -term[term[0]-1];
/*
If we come here the matches are all already in the
compiler buffer. All we need to do is take out all
functions and replace them by a SUBEXPRESSION that
points to this buffer.
Note: the PolyFun/PolyRatFun should be excluded from this.
This works because each match writes incrementally to
the buffer using the routine SubsInAll.
The call to WildDollars should be made in Generator.....
*/
t = term; tstop = t + *t; ii = ABS(tstop[-1]); tstop -= ii;
tt = AT.WorkPointer+1;
t++;
while ( t < tstop ) {
if ( *t >= FUNCTION && *t != AR.PolyFun && *t != AR.PolyFunInv ) {
if ( first ) { /* SUBEXPRESSION */
*tt++ = SUBEXPRESSION;
*tt++ = SUBEXPSIZE;
*tt++ = CC->numrhs;
*tt++ = 1;
*tt++ = AT.allbufnum;
FILLSUB(tt)
first = 0;
}
t += t[1];
}
else {
i = t[1]; NCOPY(tt,t,i);
}
}
if ( ( ll[4] & NORMALIZEFLAG ) != 0 ) {
/*
In case of the normalization option, we have to divide
by AT.idallnum;
*/
WORD na = t[ii-1];
na = REDLENG(na);
for ( i = 0; i < ii; i++ ) tt[i] = t[i];
Divvy(BHEAD (UWORD *)tt,&na,(UWORD *)(&(AT.idallnum)),1);
na = INCLENG(na);
ii = ABS(na);
tt[ii-1] = na;
tt += ii;
}
else {
NCOPY(tt,t,ii);
}
ii = tt-AT.WorkPointer;
*(AT.WorkPointer) = ii;
tt = AT.WorkPointer; t = term;
NCOPY(t,tt,ii);
if ( ( ll2 & SUBAFTER ) != 0 ) { /* ifmatch -> */
*level = AC.Labels[ll[3]];
}
TermFree(AN.termbuffer,"id,all");
AN.termbuffer = OldTermBuffer;
AT.WorkPointer = AN.RepFunList;
AT.idallflag = 0;
CC->Pointer[0] = 0;
TransferBuffer(AT.aebufnum,AT.ebufnum,AT.allbufnum);
return(1);
}
AT.idallflag = 0;
power = 0;
CC->numrhs = oldallnumrhs;
TermFree(AN.termbuffer,"id,all");
AN.termbuffer = OldTermBuffer;
break;
default :
break;
}
if ( power ) {
Substitute(BHEAD term,m,power);
if ( numdollars ) {
WildDollars(BHEAD (WORD *)0);
numdollars = 0;
}
match = 1;
if ( ( ll2 & SUBAFTER ) != 0 ) { /* ifmatch -> */
*level = AC.Labels[ll[3]];
}
}
else {
AT.WorkPointer = AN.RepFunList;
if ( ( ll2 & SUBAFTERNOT ) != 0 ) { /* ifnomatch -> */
*level = AC.Labels[ll[3]];
}
}
nextlevel:;
} while ( (*level)++ < AR.Cnumlhs && C->lhs[*level][0] == TYPEIDOLD );
(*level)--;
AT.WorkPointer = AN.RepFunList;
return(match);
}
/*
#] TestMatch :
#[ Substitute : VOID Substitute(term,pattern,power)
*/
VOID Substitute(PHEAD WORD *term, WORD *pattern, WORD power)
{
GETBIDENTITY
WORD *TemTerm;
WORD *t, *m;
WORD *tstop, *mstop;
WORD *xstop, *ystop;
WORD nt, *fill, nq, mt;
WORD *q, *subterm, *tcoef, oldval1 = 0, newval3, i = 0;
WORD PutExpr = 0, sign = 0;
TemTerm = AT.WorkPointer;
if ( ( (WORD *)(((UBYTE *)(AT.WorkPointer)) + AM.MaxTer*2) ) > AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
m = pattern;
mstop = m + *m;
m++;
t = term;
t += *term - 1;
tcoef = t;
tstop = t - ABS(*t) + 1;
t = term;
t++;
fill = TemTerm;
fill++;
if ( m < mstop ) { do {
/*
#[ SYMBOLS :
*/
if ( *m == SYMBOL ) {
ystop = m + m[1];
m += 2;
while ( *t != SYMBOL && t < tstop ) {
nq = t[1];
NCOPY(fill,t,nq);
}
if ( t >= tstop ) goto SubCoef;
*fill++ = SYMBOL;
fill++;
subterm = fill;
xstop = t + t[1];
t += 2;
do {
if ( *m == *t && t < xstop ) {
nt = t[1];
mt = m[1];
if ( mt >= 2*MAXPOWER ) {
if ( CheckWild(BHEAD mt-2*MAXPOWER,SYMTONUM,-MAXPOWER,&newval3) ) {
nt -= AN.oldvalue;
goto SubsL1;
}
}
else if ( mt <= -2*MAXPOWER ) {
if ( CheckWild(BHEAD -mt-2*MAXPOWER,SYMTONUM,-MAXPOWER,&newval3) ) {
nt += AN.oldvalue;
goto SubsL1;
}
}
else {
nt -= mt * power;
SubsL1: if ( nt ) {
*fill++ = *t;
*fill++ = nt;
}
}
m += 2; t+= 2;
}
else if ( *m >= 2*MAXPOWER ) {
while ( t < xstop ) { *fill++ = *t++; *fill++ = *t++; }
nq = WORDDIF(fill,subterm);
fill = subterm;
while ( nq > 0 ) {
if ( !CheckWild(BHEAD *m-2*MAXPOWER,SYMTOSYM,*fill,&newval3) ) {
mt = m[1];
if ( mt >= 2*MAXPOWER ) {
if ( CheckWild(BHEAD mt-2*MAXPOWER,SYMTONUM,-MAXPOWER,&newval3) ) {
if ( fill[1] -= AN.oldvalue ) goto SubsL2;
}
}
else if ( mt <= -2*MAXPOWER ) {
if ( CheckWild(BHEAD -mt-2*MAXPOWER,SYMTONUM,-MAXPOWER,&newval3) ) {
if ( fill[1] += AN.oldvalue ) goto SubsL2;
}
}
else {
if ( fill[1] -= mt * power ) {
SubsL2: fill += nq;
nq = 0;
}
}
break;
}
nq -= 2;
fill += 2;
}
if ( nq ) {
nq -= 2;
q = fill + 2;
while ( --nq >= 0 ) *fill++ = *q++;
}
m += 2;
}
else if ( *m < *t || t >= xstop ) { m += 2; }
else { *fill++ = *t++; *fill++ = *t++; }
} while ( m < ystop );
while ( t < xstop ) *fill++ = *t++;
nq = WORDDIF(fill,subterm);
if ( nq > 0 ) {
nq += 2;
subterm[-1] = nq;
}
else { fill = subterm; fill -= 2; }
}
/*
#] SYMBOLS :
#[ DOTPRODUCTS :
*/
else if ( *m == DOTPRODUCT ) {
ystop = m + m[1];
m += 2;
while ( *t > DOTPRODUCT && t < tstop ) {
nq = t[1];
NCOPY(fill,t,nq);
}
if ( t >= tstop ) goto SubCoef;
if ( *t != DOTPRODUCT ) {
m = ystop;
goto EndLoop;
}
*fill++ = DOTPRODUCT;
fill++;
subterm = fill;
xstop = t + t[1];
t += 2;
do {
if ( *m == *t && m[1] == t[1] && t < xstop ) {
nt = t[2];
mt = m[2];
if ( mt >= 2*MAXPOWER ) {
if ( CheckWild(BHEAD mt-2*MAXPOWER,SYMTONUM,-MAXPOWER,&newval3) ) {
nt -= AN.oldvalue;
goto SubsL3;
}
}
else if ( mt <= -2*MAXPOWER ) {
if ( CheckWild(BHEAD -mt-2*MAXPOWER,SYMTONUM,-MAXPOWER,&newval3) ) {
nt += AN.oldvalue;
goto SubsL3;
}
}
else {
nt -= mt * power;
SubsL3: if ( nt ) {
*fill++ = *t++;
*fill++ = *t;
*fill++ = nt;
t += 2;
}
else t += 3;
}
m += 3;
}
else if ( *m >= (AM.OffsetVector+WILDOFFSET) ) {
while ( t < xstop ) {
*fill++ = *t++; *fill++ = *t++; *fill++ = *t++;
}
oldval1 = 1;
goto SubsL4;
}
else if ( m[1] >= (AM.OffsetVector+WILDOFFSET) ) {
while ( *m >= *t && t < xstop ) {
*fill++ = *t++; *fill++ = *t++; *fill++ = *t++;
}
oldval1 = 0;
SubsL4: nq = WORDDIF(fill,subterm);
fill = subterm;
while ( nq > 0 ) {
if ( ( oldval1 && ( (
!CheckWild(BHEAD *m-WILDOFFSET,VECTOVEC,*fill,&newval3)
&& !CheckWild(BHEAD m[1]-WILDOFFSET,VECTOVEC,fill[1],&newval3)
) || (
!CheckWild(BHEAD m[1]-WILDOFFSET,VECTOVEC,*fill,&newval3)
&& !CheckWild(BHEAD *m-WILDOFFSET,VECTOVEC,fill[1],&newval3)
) ) ) || ( !oldval1 && ( (
*m == *fill
&& !CheckWild(BHEAD m[1]-WILDOFFSET,VECTOVEC,fill[1],&newval3)
) || (
!CheckWild(BHEAD m[1]-WILDOFFSET,VECTOVEC,*fill,&newval3)
&& *m == fill[1] ) ) ) ) {
mt = m[2];
if ( mt >= 2*MAXPOWER ) {
if ( CheckWild(BHEAD mt-2*MAXPOWER,SYMTONUM,-MAXPOWER,&newval3) ) {
if ( fill[2] -= AN.oldvalue )
goto SubsL5;
}
}
else if ( mt <= -2*MAXPOWER ) {
if ( CheckWild(BHEAD -mt-2*MAXPOWER,SYMTONUM,-MAXPOWER,&newval3) ) {
if ( fill[2] += AN.oldvalue )
goto SubsL5;
}
}
else {
if ( fill[2] -= mt * power ) {
SubsL5: fill += nq;
nq = 0;
}
}
m += 3;
break;
}
fill += 3; nq -= 3;
}
if ( nq ) {
nq -= 3;
q = fill + 3;
while ( --nq >= 0 ) *fill++ = *q++;
}
}
else if ( t >= xstop || *m < *t || ( *m == *t && m[1] < t[1] ) )
{ m += 3; }
else {
*fill++ = *t++; *fill++ = *t++; *fill++ = *t++;
}
} while ( m < ystop );
while ( t < xstop ) *fill++ = *t++;
nq = WORDDIF(fill,subterm);
if ( nq > 0 ) {
nq += 2;
subterm[-1] = nq;
}
else { fill = subterm; fill -= 2; }
}
/*
#] DOTPRODUCTS :
#[ FUNCTIONS :
*/
else if ( *m >= FUNCTION ) {
while ( *t >= FUNCTION || *t == SUBEXPRESSION ) {
nt = WORDDIF(t,term);
for ( mt = 0; mt < AN.RepFunNum; mt += 2 ) {
if ( nt == AN.RepFunList[mt] ) break;
}
if ( mt >= AN.RepFunNum ) {
nq = t[1];
NCOPY(fill,t,nq);
}
else {
WORD *oldt = 0;
if ( *m == GAMMA && m[1] != FUNHEAD+1 ) {
oldt = t;
if ( ( i = AN.RepFunList[mt+1] ) > 0 ) {
*fill++ = GAMMA;
*fill++ = i + FUNHEAD+1;
FILLFUN(fill)
nq = i + 1;
t += FUNHEAD;
NCOPY(fill,t,nq);
}
t = oldt;
}
else if ( ( *t == LEVICIVITA ) || ( *t >= FUNCTION
&& (functions[*t-FUNCTION].symmetric & ~REVERSEORDER) == ANTISYMMETRIC )
) sign += AN.RepFunList[mt+1];
else if ( *m >= FUNCTION+WILDOFFSET
&& (functions[*m-FUNCTION-WILDOFFSET].symmetric & ~REVERSEORDER) == ANTISYMMETRIC
) sign += AN.RepFunList[mt+1];
if ( !PutExpr ) {
xstop = t + t[1];
t = AN.FullProto;
nq = t[1];
t[3] = power;
NCOPY(fill,t,nq);
t = xstop;
PutExpr = 1;
}
else t += t[1];
if ( *m == GAMMA && m[1] != FUNHEAD+1 ) {
i = oldt[1] - m[1] - i;
if ( i > 0 ) {
*fill++ = GAMMA;
*fill++ = i + FUNHEAD+1;
FILLFUN(fill)
*fill++ = oldt[FUNHEAD];
t = t - i;
NCOPY(fill,t,i);
}
}
break;
}
}
m += m[1];
}
/*
#] FUNCTIONS :
#[ VECTORS :
*/
else if ( *m == VECTOR ) {
while ( *t > VECTOR ) {
nq = t[1];
NCOPY(fill,t,nq);
}
xstop = t + t[1];
ystop = m + m[1];
t += 2;
m += 2;
*fill++ = VECTOR;
fill++;
subterm = fill;
do {
if ( *m == *t && m[1] == t[1] ) {
m += 2; t += 2;
}
else if ( *m >= (AM.OffsetVector+WILDOFFSET) ) {
while ( t < xstop ) *fill++ = *t++;
nq = WORDDIF(fill,subterm);
fill = subterm;
if ( m[1] < (AM.OffsetIndex+WILDOFFSET) ) {
do {
if ( m[1] == fill[1] &&
!CheckWild(BHEAD *m-WILDOFFSET,VECTOVEC,*fill,&newval3) )
break;
fill += 2;
nq -= 2;
} while ( nq > 0 );
}
else { /* Double wildcard */
do {
if ( !CheckWild(BHEAD m[1]-WILDOFFSET,INDTOIND,fill[1],&newval3)
&& !CheckWild(BHEAD *m-WILDOFFSET,VECTOVEC,*fill,&newval3) )
break;
if ( *fill == oldval1 && fill[1] == AN.oldvalue ) break;
fill += 2;
nq -= 2;
} while ( nq > 0 );
}
nq -= 2;
q = fill + 2;
if ( nq > 0 ) { NCOPY(fill,q,nq); }
m += 2;
}
else if ( *m <= *t &&
m[1] >= (AM.OffsetIndex + WILDOFFSET) ) {
while ( *m == *t && t < xstop )
{ *fill++ = *t++; *fill++ = *t++; }
nq = WORDDIF(fill,subterm);
fill = subterm;
do {
if ( *m == *fill &&
!CheckWild(BHEAD m[1]-WILDOFFSET,INDTOIND,fill[1],&newval3) )
break;
nq -= 2;
fill += 2;
} while ( nq > 0 );
nq -= 2;
q = fill + 2;
if ( nq > 0 ) { NCOPY(fill,q,nq); }
m += 2;