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
|
/*
Copyright 2015-2022 Ian Jauslin
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "fields.h"
#include "definitions.cpp"
#include <stdio.h>
#include <stdlib.h>
#include "number.h"
#include "tools.h"
#include "polynomial.h"
#include "array.h"
#include "rational.h"
#include "tree.h"
//---------------------------------------------------------------------
//
// Fields_Table
//
//---------------------------------------------------------------------
// init and free for Fields_Table
int init_Fields_Table(Fields_Table* fields){
init_Int_Array(&((*fields).parameter),FIELDS_SIZE);
init_Int_Array(&((*fields).external),FIELDS_SIZE);
init_Int_Array(&((*fields).internal),FIELDS_SIZE);
init_Identities(&((*fields).ids), FIELDS_SIZE);
init_Virtual_fields(&((*fields).virtual_fields), FIELDS_SIZE);
init_Int_Array(&((*fields).fermions),FIELDS_SIZE);
init_Int_Array(&((*fields).noncommuting),FIELDS_SIZE);
return(0);
}
int free_Fields_Table(Fields_Table fields){
free_Int_Array(fields.parameter);
free_Int_Array(fields.external);
free_Int_Array(fields.internal);
free_Identities(fields.ids);
free_Virtual_fields(fields.virtual_fields);
free_Int_Array(fields.fermions);
free_Int_Array(fields.noncommuting);
return(0);
}
// determine field type
int field_type(int index, Fields_Table fields){
if(int_array_find(abs(index), fields.parameter)>=0){
return(FIELD_PARAMETER);
}
else if(int_array_find(abs(index), fields.external)>=0){
return(FIELD_EXTERNAL);
}
else if(int_array_find(abs(index), fields.internal)>=0){
return(FIELD_INTERNAL);
}
else if(intlist_find(fields.virtual_fields.indices, fields.virtual_fields.length, index)>=0){
return(FIELD_VIRTUAL);
}
fprintf(stderr,"error: index %d is neither a parameter nor an external or an internal field, nor a virtual field\n",index);
exit(-1);
}
// check whether a field anticommutes
int is_fermion(int index, Fields_Table fields){
if(int_array_find(abs(index), fields.fermions)>=0){
return(1);
}
else{
return(0);
}
}
// check whether a field is non-commuting
int is_noncommuting(int index, Fields_Table fields){
if(int_array_find(abs(index), fields.noncommuting)>=0){
return(1);
}
else{
return(0);
}
}
//---------------------------------------------------------------------
//
// Identities
//
//---------------------------------------------------------------------
// allocate memory
int init_Identities(Identities* identities,int size){
(*identities).lhs=calloc(size,sizeof(Int_Array));
(*identities).rhs=calloc(size,sizeof(Polynomial));
(*identities).length=0;
(*identities).memory=size;
return(0);
}
// free memory
int free_Identities(Identities identities){
int i;
for(i=0;i<identities.length;i++){
free_Int_Array(identities.lhs[i]);
free_Polynomial(identities.rhs[i]);
}
free(identities.lhs);
free(identities.rhs);
return(0);
}
// resize
int resize_identities(Identities* identities,int new_size){
Identities new_identities;
int i;
init_Identities(&new_identities,new_size);
for(i=0;i<(*identities).length;i++){
new_identities.lhs[i]=(*identities).lhs[i];
new_identities.rhs[i]=(*identities).rhs[i];
}
new_identities.length=(*identities).length;
free((*identities).lhs);
free((*identities).rhs);
*identities=new_identities;
return(0);
}
// copy
int identities_cpy(Identities input, Identities* output){
init_Identities(output,input.length);
identities_cpy_noinit(input,output);
return(0);
}
int identities_cpy_noinit(Identities input, Identities* output){
int i;
if((*output).memory<input.length){
fprintf(stderr,"error: trying to copy an identities collection of length %d to another with memory %d\n",input.length,(*output).memory);
exit(-1);
}
for(i=0;i<input.length;i++){
int_array_cpy(input.lhs[i],(*output).lhs+i);
polynomial_cpy(input.rhs[i],(*output).rhs+i);
}
(*output).length=input.length;
return(0);
}
// append an element to a identities
int identities_append(Int_Array lhs, Polynomial rhs, Identities* output){
int offset=(*output).length;
if((*output).length>=(*output).memory){
resize_identities(output,2*(*output).memory+1);
}
// copy and allocate
int_array_cpy(lhs,(*output).lhs+offset);
polynomial_cpy(rhs,(*output).rhs+offset);
// increment length
(*output).length++;
return(0);
}
// append an element to a identities without allocating memory
int identities_append_noinit(Int_Array lhs, Polynomial rhs, Identities* output){
int offset=(*output).length;
if((*output).length>=(*output).memory){
resize_identities(output,2*(*output).memory+1);
}
// copy without allocating
(*output).lhs[offset]=lhs;
(*output).rhs[offset]=rhs;
// increment length
(*output).length++;
return(0);
}
// concatenate two identitiess
int identities_concat(Identities input, Identities* output){
int i;
for(i=0;i<input.length;i++){
identities_append(input.lhs[i],input.rhs[i],output);
}
return(0);
}
// resolve the identities
// requires both the monomials in polynomial and the ids in fields to be sorted
// IMPORTANT: the sorting must be such that noncommuting fields must come before the other fields
int resolve_ids(Polynomial* polynomial, Fields_Table fields){
int i,j,k,l;
int sign;
int fermion_count;
int first_field;
int at_least_one;
int security;
Int_Array pre_monomial;
Int_Array post_monomial;
Number num;
Number tmp_num;
// loop over monomials
for(i=0;i<(*polynomial).length;i++){
at_least_one=1;
security=0;
// repeat the simplification until the monomial is fully simplified
while(at_least_one>0){
at_least_one=0;
// prevent infinite loops
security++;
if(security>1000000){
fprintf(stderr,"error: 1000000 iterations reached when trying to simplify a monomial\n");
exit(-1);
}
// loop over ids
for(j=0;j<fields.ids.length;j++){
// check whether the monomial matches the id
first_field=int_array_is_subarray_noncommuting(fields.ids.lhs[j],(*polynomial).monomials[i],fields);
if(first_field>=0){
init_Int_Array(&pre_monomial, (*polynomial).monomials[i].length);
init_Int_Array(&post_monomial, (*polynomial).monomials[i].length);
// add whatever is before the first field to pre
for(k=0;k<first_field;k++){
int_array_append((*polynomial).monomials[i].values[k],&pre_monomial);
}
// find others and move them together
// sign from moving the fields
sign=1;
// number of Fermions to jump over
fermion_count=0;
for(l=1,k=first_field+1;k<(*polynomial).monomials[i].length;k++){
// check whether the field is identical to the "current" one in the id
// if l is too large, then keep the field
if(l>=fields.ids.lhs[j].length || (*polynomial).monomials[i].values[k]!=fields.ids.lhs[j].values[l]){
// add to post
int_array_append((*polynomial).monomials[i].values[k],&post_monomial);
// count Fermions to jump
if(is_fermion((*polynomial).monomials[i].values[k],fields)){
fermion_count++;
}
}
else{
// sign correction
if(is_fermion(fields.ids.lhs[j].values[l],fields) && fermion_count % 2 == 1){
sign*=-1;
}
// increment "current" field in the id
l++;
}
}
num=number_Qprod_ret(quot(sign,1),(*polynomial).nums[i]);
// add extra monomials (if there are more than 1)
for(k=1;k<fields.ids.rhs[j].length;k++){
number_prod(num, fields.ids.rhs[j].nums[k], &tmp_num);
polynomial_append(pre_monomial, (*polynomial).factors[i], tmp_num, polynomial);
free_Number(tmp_num);
int_array_concat(fields.ids.rhs[j].monomials[k],(*polynomial).monomials+(*polynomial).length-1);
int_array_concat(post_monomial,(*polynomial).monomials+(*polynomial).length-1);
// re-sort monomial
sign=1;
monomial_sort((*polynomial).monomials[(*polynomial).length-1],fields,&sign);
number_Qprod_chain(quot(sign,1),(*polynomial).nums+(*polynomial).length-1);
}
// correct i-th monomial
free_Number((*polynomial).nums[i]);
(*polynomial).nums[i]=number_prod_ret(num,fields.ids.rhs[j].nums[0]);
free_Int_Array((*polynomial).monomials[i]);
(*polynomial).monomials[i]=pre_monomial;
int_array_concat(fields.ids.rhs[j].monomials[0],(*polynomial).monomials+i);
int_array_concat(post_monomial,(*polynomial).monomials+i);
free_Int_Array(post_monomial);
// re-sort monomial
sign=1;
monomial_sort((*polynomial).monomials[i],fields,&sign);
number_Qprod_chain(quot(sign,1),(*polynomial).nums+i);
// free num
free_Number(num);
// repeat the replacement (in order to perform all of the replacements if several are necessary)
j=0;
if(at_least_one==0){
at_least_one=1;
}
}
}
}
}
return(0);
}
// check whether an array is a sub-array of another
// requires noncommuting elements to be next to each other
// other elements may be separated, but the order must be respected
// returns the first index of the sub-array
// IMPORTANT: the noncommuting elements must precede all others in input and in test_array
int int_array_is_subarray_noncommuting(Int_Array input, Int_Array test_array, Fields_Table fields){
int i,j;
int matches=0;
int post_nc=0;
int match_nc;
int first=-1;
// cannot fit
if(test_array.length<input.length){
return(-1);
}
// bound noncommuting elements
while(is_noncommuting(input.values[post_nc], fields)==1){
post_nc++;
}
for(i=0,match_nc=0;i<test_array.length;i++){
if(test_array.values[i]==input.values[0]){
match_nc=1;
}
for(j=1;j<post_nc;j++){
if(i+j>=test_array.length || test_array.values[i+j]!=input.values[j]){
match_nc=0;
}
}
if(match_nc==1){
first=i;
break;
}
}
if(first<0){
return(-1);
}
if(post_nc>0){
matches=post_nc;
}
else{
matches=1;
}
for(i=first+1;i<test_array.length && matches<input.length;i++){
if(input.values[matches]==test_array.values[i]){
matches++;
}
}
if(matches==input.length){
return(first);
}
else{
return(-1);
}
}
//---------------------------------------------------------------------
//
// Virtual_fields
//
//---------------------------------------------------------------------
// allocate memory
int init_Virtual_fields(Virtual_fields* virtual_fields,int size){
(*virtual_fields).indices=calloc(size,sizeof(int));
(*virtual_fields).expr=calloc(size,sizeof(Polynomial));
(*virtual_fields).length=0;
(*virtual_fields).memory=size;
return(0);
}
// free memory
int free_Virtual_fields(Virtual_fields virtual_fields){
int i;
for(i=0;i<virtual_fields.length;i++){
free_Polynomial(virtual_fields.expr[i]);
}
free(virtual_fields.indices);
free(virtual_fields.expr);
return(0);
}
// resize
int resize_virtual_fields(Virtual_fields* virtual_fields,int new_size){
Virtual_fields new_virtual_fields;
int i;
init_Virtual_fields(&new_virtual_fields,new_size);
for(i=0;i<(*virtual_fields).length;i++){
new_virtual_fields.indices[i]=(*virtual_fields).indices[i];
new_virtual_fields.expr[i]=(*virtual_fields).expr[i];
}
new_virtual_fields.length=(*virtual_fields).length;
free((*virtual_fields).indices);
free((*virtual_fields).expr);
*virtual_fields=new_virtual_fields;
return(0);
}
// copy
int virtual_fields_cpy(Virtual_fields input, Virtual_fields* output){
init_Virtual_fields(output,input.length);
virtual_fields_cpy_noinit(input,output);
return(0);
}
int virtual_fields_cpy_noinit(Virtual_fields input, Virtual_fields* output){
int i;
if((*output).memory<input.length){
fprintf(stderr,"error: trying to copy a virtual fields collection of length %d to another with memory %d\n",input.length,(*output).memory);
exit(-1);
}
for(i=0;i<input.length;i++){
(*output).indices[i]=input.indices[i];
polynomial_cpy(input.expr[i],(*output).expr+i);
}
(*output).length=input.length;
return(0);
}
// append an element to a virtual_fields
int virtual_fields_append(int index, Polynomial expr, Virtual_fields* output){
int offset=(*output).length;
if((*output).length>=(*output).memory){
resize_virtual_fields(output,2*(*output).memory+1);
}
// copy and allocate
(*output).indices[offset]=index;
polynomial_cpy(expr,(*output).expr+offset);
// increment length
(*output).length++;
return(0);
}
// append an element to a virtual_fields without allocating memory
int virtual_fields_append_noinit(int index, Polynomial expr, Virtual_fields* output){
int offset=(*output).length;
if((*output).length>=(*output).memory){
resize_virtual_fields(output,2*(*output).memory+1);
}
// copy without allocating
(*output).indices[offset]=index;
(*output).expr[offset]=expr;
// increment length
(*output).length++;
return(0);
}
// concatenate two virtual_fields
int virtual_fields_concat(Virtual_fields input, Virtual_fields* output){
int i;
for(i=0;i<input.length;i++){
virtual_fields_append(input.indices[i],input.expr[i],output);
}
return(0);
}
//---------------------------------------------------------------------
//
// Groups
//
//---------------------------------------------------------------------
// allocate memory
int init_Groups(Groups* groups,int size){
(*groups).indices=calloc(size,sizeof(Int_Array));
(*groups).length=0;
(*groups).memory=size;
return(0);
}
// free memory
int free_Groups(Groups groups){
int i;
for(i=0;i<groups.length;i++){
free_Int_Array(groups.indices[i]);
}
free(groups.indices);
return(0);
}
// resize
int resize_groups(Groups* groups,int new_size){
Groups new_groups;
int i;
init_Groups(&new_groups,new_size);
for(i=0;i<(*groups).length;i++){
new_groups.indices[i]=(*groups).indices[i];
}
new_groups.length=(*groups).length;
free((*groups).indices);
*groups=new_groups;
return(0);
}
// copy
int groups_cpy(Groups input, Groups* output){
init_Groups(output,input.length);
groups_cpy_noinit(input,output);
return(0);
}
int groups_cpy_noinit(Groups input, Groups* output){
int i;
if((*output).memory<input.length){
fprintf(stderr,"error: trying to copy a groups collection of length %d to another with memory %d\n",input.length,(*output).memory);
exit(-1);
}
for(i=0;i<input.length;i++){
int_array_cpy(input.indices[i],(*output).indices+i);
}
(*output).length=input.length;
return(0);
}
// append an element to a groups
int groups_append(Int_Array indices, Groups* output){
int offset=(*output).length;
if((*output).length>=(*output).memory){
resize_groups(output,2*(*output).memory+1);
}
// copy and allocate
int_array_cpy(indices,(*output).indices+offset);
// increment length
(*output).length++;
return(0);
}
// append an element to a groups without allocating memory
int groups_append_noinit(Int_Array indices, Groups* output){
int offset=(*output).length;
if((*output).length>=(*output).memory){
resize_groups(output,2*(*output).memory+1);
}
// copy without allocating
(*output).indices[offset]=indices;
// increment length
(*output).length++;
return(0);
}
// concatenate two groupss
int groups_concat(Groups input, Groups* output){
int i;
for(i=0;i<input.length;i++){
groups_append(input.indices[i],output);
}
return(0);
}
// find which group an index belongs to
int find_group(int index, Groups groups){
int i,j;
for(i=0;i<groups.length;i++){
for(j=0;j<groups.indices[i].length;j++){
if(groups.indices[i].values[j]==index){
return(i);
}
}
}
return(-1);
}
//---------------------------------------------------------------------
//
// Variables
//
//---------------------------------------------------------------------
// allocate memory
int init_Variables(Variables* variables,int size){
(*variables).var_names=calloc(size,sizeof(Char_Array));
(*variables).symbol_trees=calloc(size,sizeof(Tree));
(*variables).length=0;
(*variables).memory=size;
return(0);
}
// free memory
int free_Variables(Variables variables){
int i;
for(i=0;i<variables.length;i++){
free_Char_Array(variables.var_names[i]);
free_Tree(variables.symbol_trees[i]);
}
free(variables.var_names);
free(variables.symbol_trees);
return(0);
}
// resize
int resize_variables(Variables* variables,int new_size){
Variables new_variables;
int i;
init_Variables(&new_variables,new_size);
for(i=0;i<(*variables).length;i++){
new_variables.var_names[i]=(*variables).var_names[i];
new_variables.symbol_trees[i]=(*variables).symbol_trees[i];
}
new_variables.length=(*variables).length;
free((*variables).var_names);
free((*variables).symbol_trees);
*variables=new_variables;
return(0);
}
// copy
int variables_cpy(Variables input, Variables* output){
init_Variables(output,input.length);
variables_cpy_noinit(input,output);
return(0);
}
int variables_cpy_noinit(Variables input, Variables* output){
int i;
if((*output).memory<input.length){
fprintf(stderr,"error: trying to copy a variables collection of length %d to another with memory %d\n",input.length,(*output).memory);
exit(-1);
}
for(i=0;i<input.length;i++){
char_array_cpy(input.var_names[i], (*output).var_names+i);
tree_cpy(input.symbol_trees[i],(*output).symbol_trees+i);
}
(*output).length=input.length;
return(0);
}
// append an element to a variables collection
int variables_append(Char_Array var_name, Tree symbol_tree, Variables* output){
int offset=(*output).length;
if((*output).length>=(*output).memory){
resize_variables(output,2*(*output).memory+1);
}
// copy and allocate
char_array_cpy(var_name,(*output).var_names+offset);
tree_cpy(symbol_tree,(*output).symbol_trees+offset);
// increment length
(*output).length++;
return(0);
}
// append an element to a variables collection without allocating memory
int variables_append_noinit(Char_Array var_name, Tree symbol_tree, Variables* output){
int offset=(*output).length;
if((*output).length>=(*output).memory){
resize_variables(output,2*(*output).memory+1);
}
// copy without allocating
(*output).var_names[offset]=var_name;
(*output).symbol_trees[offset]=symbol_tree;
// increment length
(*output).length++;
return(0);
}
// concatenate two variables collections
int variables_concat(Variables input, Variables* output){
int i;
for(i=0;i<input.length;i++){
variables_append(input.var_names[i], input.symbol_trees[i], output);
}
return(0);
}
// find a variable matching a var_name
int variables_find_var(Char_Array name, Variables variables, Tree* output){
Char_Array varname;
int i;
// drop inital '$'
char_array_substring(name, 1, name.length-1, &varname);
for(i=0;i<variables.length;i++){
if(char_array_cmp(varname, variables.var_names[i])==1){
tree_cpy(variables.symbol_trees[i], output);
break;
}
}
// error if no variable was found
if(i==variables.length){
fprintf(stderr, "error: variable '$%s' not found\n",char_array_to_str_noinit(&varname));
exit(-1);
}
free_Char_Array(varname);
return(0);
}
// add a polynomials as a new named variable
int add_polynomial_to_variables(char* name, Polynomial polynomial, Variables* variables){
// save polynomial to string (to convert it to a variable, it must first be a string)
Char_Array poly_str;
Char_Array out_name;
Tree out_tree;
init_Char_Array(&poly_str, STR_SIZE);
polynomial_sprint(polynomial, &poly_str);
// convert name to Char_Array
init_Char_Array(&out_name,STR_SIZE);
char_array_append_str(name, &out_name);
// trivial tree containing the polynomial
init_Tree(&out_tree,0,poly_str.length);
tree_set_label(poly_str, &out_tree);
free_Char_Array(poly_str);
// add variable
variables_append(out_name, out_tree, variables);
free_Tree(out_tree);
free_Char_Array(out_name);
return 0;
}
|