Ian Jauslin
summaryrefslogtreecommitdiff
blob: 0c5707e28448a743fa58308bc1bb9f6b041f47fe (plain)
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
/*
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.
*/

/*
As of version 1.0, the mean of a monomial is computed directly
*/

#include "mean.h"

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "definitions.cpp"
#include "tools.h"
#include "polynomial.h"
#include "rational.h"
#include "array.h"
#include "fields.h"
#include "number.h"
#include "determinant.h"

// mean of a monomial
int mean(Int_Array monomial, Polynomial* out, Fields_Table fields, Polynomial_Matrix propagator){
  int sign=1;
  // +/- internal fields
  Int_Array internal_plus;
  Int_Array internal_minus;

  // init out
  *out=polynomial_one();

  // sort first
  monomial_sort(monomial, fields, &sign);
  polynomial_multiply_Qscalar(*out, quot(sign,1));
  // get internals
  // (*out).monomials is the first element of out but it only has 1 element
  // first, free (*out).monomials[0]
  free_Int_Array((*out).monomials[0]);
  get_internals(monomial, &internal_plus, &internal_minus, (*out).monomials, fields);

  if(internal_plus.length>0 && internal_minus.length>0){
    mean_internal(internal_plus, internal_minus, out, propagator, fields);
  }

  free_Int_Array(internal_plus);
  free_Int_Array(internal_minus);
  return(0);
}

// compute the mean of a monomial of internal fields (with split + and -)
int mean_internal(Int_Array internal_plus, Int_Array internal_minus, Polynomial* out, Polynomial_Matrix propagator, Fields_Table fields){
  int ret;
  Number num;

  if(internal_plus.length!=internal_minus.length){
    fprintf(stderr,"error: monomial contains unmatched +/- fields\n");
    exit(-1);
  }

  ret=mean_determinant(internal_plus, internal_minus, &num, propagator, fields);
  // cannot compute the mean as a determinant, use permutations
  // can be because some fields are not Fermions
  // can be because the propagator has non-numeric values (inverting polynomials is not implemented, and would be required for the computation of the determinant)
  if(ret==-1){
    mean_permutations(internal_plus, internal_minus, out, propagator, fields);
  }
  else{
    polynomial_multiply_scalar(*out, num);
    free_Number(num);
  }

  return(0);
}

// compute the mean of a monomial by computing a determinant
// can only be used if all of the propagators are numbers
int mean_determinant(Int_Array internal_plus, Int_Array internal_minus, Number* out, Polynomial_Matrix propagator, Fields_Table fields){
  Number_Matrix M;
  int n=internal_minus.length;
  int i,j;
  int a,b;
  int sign;

  init_Number_Matrix(&M,n);
  
  // extra sign: the monomial is sorted in such a way that minus fields are on the left of plus fields, but the determinant formula requires the fields to be alternated +-
  if((n+1)/2%2==1){
    sign=-1;
  }
  else{
    sign=1;
  }

  // construct matrix
  for(i=0;i<n;i++){
    a=intlist_find_err(propagator.indices, propagator.length, internal_plus.values[i]);
    for(j=0;j<n;j++){
      b=intlist_find_err(propagator.indices, propagator.length, -internal_minus.values[j]);
      // ignore 0
      if(propagator.matrix[a][b].length!=0){
	// check whether the fields are Fermions, and whether the entry is a number
	if(is_fermion(internal_plus.values[i], fields)==0 || is_fermion(internal_minus.values[j], fields)==0 || polynomial_is_number(propagator.matrix[a][b])==0){
	  free_Number_Matrix(M);
	  return(-1);
	}

	number_add_chain(propagator.matrix[a][b].nums[0], M.matrix[i]+j);
      }
    }
  }

  // compute determinant
  determinant_inplace(M, out);

  number_Qprod_chain(quot(sign,1), out);

  free_Number_Matrix(M);

  return(0);
}


// compute the mean of a monomial by summing over permutations
int mean_permutations(Int_Array internal_plus, Int_Array internal_minus, Polynomial* out, Polynomial_Matrix propagator, Fields_Table fields){
  int n=internal_minus.length;
  // pairing as an array of positions
  int* pairing=calloc(n,sizeof(int));
  // specifies which indices are available for pairing
  int* mask=calloc(n,sizeof(int));
  // signature of the permutation
  int pairing_sign;
  // sign from mixing - and + together
  int mixing_sign;
  Polynomial num;
  Polynomial num_summed=polynomial_zero();
  // propagator matrix indices
  int* indices_minus=calloc(n,sizeof(int));
  int* indices_plus=calloc(n,sizeof(int));
  int i;
  // whether the next pairing exists
  int exists_next=0;

  // indices
  for(i=0;i<n;i++){
    indices_plus[i]=intlist_find_err(propagator.indices, propagator.length, internal_plus.values[i]);
    indices_minus[i]=intlist_find_err(propagator.indices, propagator.length, -internal_minus.values[i]);
  }

  // init pairing and mask
  exists_next=init_pairing(pairing, mask, n, propagator, indices_plus, indices_minus)+1;

  // initial sign
  pairing_sign=permutation_signature(pairing,n);

  // mixing sign (from ordering psi+psi-): (-1)^{n(n+1)/2}
  if((n*(n+1))/2 %2 ==0){
    mixing_sign=1;
  }
  else{
    mixing_sign=-1;
  }

  // loop over pairings
  // loop ends when the first pairing leaves the array
  while(exists_next==1){
    num=polynomial_one();
    // propagator product for the current pairing (only simplify after all pairings)
    for(i=0;i<n;i++){
      polynomial_prod_chain_nosimplify(propagator.matrix[indices_plus[i]][indices_minus[pairing[i]]],&num, fields);
    }
    polynomial_multiply_Qscalar(num,quot(mixing_sign*pairing_sign,1));
    polynomial_concat_noinit_inplace(num,&num_summed);

    exists_next=next_pairing(pairing, mask, n, propagator, indices_plus, indices_minus)+1;
    pairing_sign=permutation_signature(pairing,n);
  }

  // only simplify in mean_virtual_fields
  polynomial_prod_chain_nosimplify(num_summed,out,fields);
  free_Polynomial(num_summed);
  free(pairing);
  free(mask);
  free(indices_plus);
  free(indices_minus);
  return(0);
}

// first pairing with a non-vanishing propagator
int init_pairing(int* pairing, int* mask, int n, Polynomial_Matrix propagator, int* indices_plus, int* indices_minus){
  // index we want to increment
  int move=0;
  int i;
  for(i=0;i<n;i++){
    pairing[i]=-1;
    mask[i]=0;
  }

  // loop until move is out of range
  while(move>=0 && move<n){
    // move
    pairing[move]=next_wick(move, pairing[move], mask, n, propagator, indices_plus, indices_minus);
    // if the next term does not exist, then move previous index
    if(pairing[move]==-1){
      move--;
    }
    // else move next index
    else{
      move++;
    }
  }

  // if move==-1, then there is no first term, return -1
  if(move==-1){
    return(-1);
  }
  // if the first term exists
  return(0);
}

// next pairing with a non-vanishing propagator
int next_pairing(int* pairing, int* mask, int n, Polynomial_Matrix propagator, int* indices_plus, int* indices_minus){
  // index we want to increment
  int move=n-1;

  // last index
  mask[pairing[n-1]]=0;

  // loop until move is out of range
  while(move>=0 && move<n){
    // move
    pairing[move]=next_wick(move, pairing[move], mask, n, propagator, indices_plus, indices_minus);
    // if the next term does not exist, then move previous index
    if(pairing[move]==-1){
      move--;
    }
    // else move next index
    else{
      move++;
    }
  }

  // if move==-1, then there is no next term, return -1
  if(move==-1){
    return(-1);
  }
  // if the next term exists
  return(0);
}

// next term in the Wick expansion
int next_wick(int index, int start, int* mask, int n, Polynomial_Matrix propagator, int* indices_plus, int* indices_minus){
  int i;
  // unset mask
  if(start>=0 && start<n){
    mask[start]=0;
  }
  // find next position
  for(i=start+1;i<n;i++){
    // if the propagator doesn't vanish
    if(mask[i]==0 && polynomial_is_zero(propagator.matrix[indices_plus[index]][indices_minus[i]])==0){
      mask[i]=1;
      return(i);
    }
  }
  // no next term
  return(-1);
}


/* Older function: propagator as number
// compute the mean of a monomial of internal fields (with split + and -)
// compute all contractions
int mean_internal_slow(Int_Array internal_plus, Int_Array internal_minus, Number* outnum, Polynomial_Matrix propagator){
  if(internal_plus.length!=internal_minus.length){
    fprintf(stderr,"error: monomial contains unmatched +/- fields\n");
    exit(-1);
  }
  int n=internal_minus.length;
  // pairing as an array of positions
  int* pairing=calloc(n,sizeof(int));
  // specifies which indices are available for pairing
  int* mask=calloc(n,sizeof(int));
  // signature of the permutation
  int pairing_sign;
  // sign from mixing - and + together
  int mixing_sign;
  Number num;
  Number num_summed=number_zero();
  // propagator matrix indices
  int index1, index2;
  int i,j,k,l;

  // init pairing and mask
  for(i=0;i<n-1;i++){
    pairing[i]=i;
    mask[i]=1;
  }
  pairing[n-1]=n-1;
  pairing_sign=1;

  // mixing sign: (-1)^{n(n+1)/2}
  if((n*(n+1))/2 %2 ==0){
    mixing_sign=1;
  }
  else{
    mixing_sign=-1;
  }

  // loop over pairings
  // loop ends when the first pairing leaves the array
  while(pairing[0]<n){
    num=number_one();
    // propagator product for the current pairing
    for(i=0;i<n;i++){
      // indices within the propagator matrix
      index1=intlist_find_err(propagator.indices, propagator.length, internal_plus.values[i]);
      index2=intlist_find_err(propagator.indices, propagator.length, -internal_minus.values[pairing[i]]);

      number_prod_chain(propagator.matrix[index1][index2],&num);
    }
    number_Qprod_chain(quot(mixing_sign*pairing_sign,1),&num);
    number_add_chain(num,&num_summed);
    free_Number(num);

    // next pairing
    // last element of the pairing that we can move
    for(i=n-1;i>=0;i--){
      // move i-th
      mask[pairing[i]]=0;
      // search for next possible position
      for(j=pairing[i]+1;j<n;j++){
	if(mask[j]==0){
	  break;
	}
      }

      // if the next position exists
      if(j<n){
	// sign correction: change sign by (-1)^{1+(n-i)(n-i-1)/2}
	// actually (-1)^{1+(n-1-i)(n-1-i-1)/2
	if(((n-i-1)*(n-i-2))/2 % 2==0){
	  pairing_sign*=-1;
	}

	pairing[i]=j;
	mask[j]=1;
	// put the remaining pairings at their left-most possible values
	if(i<n-1){
	  k=i+1;
	  for(l=0;l<n;l++){
	    if(mask[l]==0){
	      mask[l]=1;
	      pairing[k]=l;
	      k++;
	      // if exhausted all indices
	      if(k>=n){
		break;
	      }
	    }
	  }
	}
	// if the next position was found, then don't try to move the previous pairings
	break;
      }
      // if no next position is found, store the pairing outside the array (so the algorithm stops when the first pairing is outside the array)
      else{
	pairing[i]=n;
      }
    }
  }

  number_prod_chain(num_summed,outnum);
  free_Number(num_summed);
  free(pairing);
  free(mask);
  return(0);
}
*/


// get lists of internal fields from a monomial (separate + and -)
// requires the monomial to be sorted (for the sign to be correct)
int get_internals(Int_Array monomial, Int_Array* internal_plus, Int_Array* internal_minus, Int_Array* others, Fields_Table fields){
  int i;
  init_Int_Array(internal_plus, monomial.length);
  init_Int_Array(internal_minus, monomial.length);
  init_Int_Array(others, monomial.length);
  for (i=0;i<monomial.length;i++){
    if(int_array_find(abs(monomial.values[i]),fields.internal)>=0){
      // split +/- fields
      if(monomial.values[i]>0){
	int_array_append(monomial.values[i],internal_plus);
      }
      else{
	int_array_append(monomial.values[i],internal_minus);
      }
    }
    else{
      int_array_append(monomial.values[i], others);
    }
  }
  return(0);
}


// compute the mean of a monomial containing virtual fields
// keep track of which means were already computed
int mean_virtual_fields(Int_Array monomial, Polynomial* output, Fields_Table fields, Polynomial_Matrix propagator, Groups groups, Identities* computed){
  Int_Array virtual_field_list;
  int i;
  int power;
  int* current_term;
  Polynomial mean_num;
  Int_Array tmp_monomial;
  Number tmp_num;
  Int_Array base_monomial;
  int sign;
  // whether or not the next term exists
  int exists_next=0;
  // simplify polynomial periodically
  int simplify_freq=1;
  Polynomial mean_poly;

  init_Polynomial(output, POLY_SIZE);

  // check whether the mean was already computed
  for(i=0;i<(*computed).length;i++){
    if(int_array_cmp((*computed).lhs[i], monomial)==0){
      // write polynomial
      polynomial_concat((*computed).rhs[i], output);
      return(0);
    }
  }

  init_Int_Array(&virtual_field_list, monomial.length);
  init_Int_Array(&base_monomial, monomial.length);

  // generate virtual_fields list
  for(i=0;i<monomial.length;i++){
    if(field_type(monomial.values[i], fields)==FIELD_VIRTUAL){
      int_array_append(intlist_find_err(fields.virtual_fields.indices, fields.virtual_fields.length, monomial.values[i]), &virtual_field_list);
    }
    else{
      int_array_append(monomial.values[i], &base_monomial);
    }
  }
  power=virtual_field_list.length;

  // trivial case
  if(power==0){
    mean(monomial, &mean_num, fields, propagator);
    polynomial_concat_noinit(mean_num, output);
    
    free_Int_Array(virtual_field_list);
    free_Int_Array(base_monomial);
    return(0);
  }
  else{
    // initialize current term to a position that has no repetitions
    current_term=calloc(power,sizeof(int));
    exists_next=init_prod(current_term, virtual_field_list, fields, power, base_monomial)+1;
  }

  // loop over terms; the loop stops when all the pointers are at the end of the first virtual field
  while(exists_next==1){
    // construct monomial
    int_array_cpy(base_monomial, &tmp_monomial);
    tmp_num=number_one();
    for(i=0;i<power;i++){
      int_array_concat(fields.virtual_fields.expr[virtual_field_list.values[i]].monomials[current_term[i]], &tmp_monomial);
      number_prod_chain(fields.virtual_fields.expr[virtual_field_list.values[i]].nums[current_term[i]], &tmp_num);
    }
    // check whether the monomial vanishes
    if(check_monomial_match(tmp_monomial, fields)==1){
      // sort monomial
      sign=1;
      monomial_sort(tmp_monomial, fields, &sign);
      number_Qprod_chain(quot(sign,1), &tmp_num);

      // mean
      mean_groups(tmp_monomial, &mean_poly, fields, propagator, groups, computed);

      // write to output
      polynomial_multiply_scalar(mean_poly,tmp_num);
      polynomial_concat_noinit_inplace(mean_poly, output);
    }

    free_Number(tmp_num);
    free_Int_Array(tmp_monomial);

    // next term
    exists_next=next_prod(current_term, virtual_field_list, fields, power, base_monomial)+1;

    
    // simplfiy every 25 steps (improves both memory usage and performance)
    if(simplify_freq %25 ==0){
      polynomial_simplify(output, fields);
      simplify_freq=0;
    }
    simplify_freq++;
  }

  // simplify
  polynomial_simplify(output, fields);

  // write computed
  identities_append(monomial, *output, computed);

  // free memory
  free(current_term);
  free_Int_Array(virtual_field_list);
  free_Int_Array(base_monomial);
  return(0);
}

// first term in product with no repetitions
int init_prod(int* current_term, Int_Array virtual_field_list, Fields_Table fields, int power, Int_Array base_monomial){
  // index we want to increment
  int move=0;
  // tmp monomial
  Int_Array monomial;
  int i;

  init_Int_Array(&monomial, base_monomial.length+5*power);
  int_array_cpy_noinit(base_monomial, &monomial);
  // init current term
  for(i=0;i<power;i++){
    current_term[i]=-1;
  }

  // loop until move is out of range
  while(move>=0 && move<power){
    // move
    current_term[move]=next_term_norepeat(current_term[move], fields.virtual_fields.expr[virtual_field_list.values[move]], &monomial, fields);
    // if the next term does not exist, then move previous index
    if(current_term[move]==-1){
      move--;
    }
    // else move next index
    else{
      move++;
    }
  }

  free_Int_Array(monomial);
  // if move==-1, then there is no first term, return -1
  if(move==-1){
    return(-1);
  }
  // if the next term exists
  return(0);
}

// next term in product with no repetitions
int next_prod(int* current_term, Int_Array virtual_field_list, Fields_Table fields, int power, Int_Array base_monomial){
  // index we want to increment
  int move=power-1;
  // tmp monomial
  Int_Array monomial;
  int i;

  // init monomial
  init_Int_Array(&monomial, base_monomial.length+5*power);
  int_array_cpy_noinit(base_monomial, &monomial);
  for(i=0;i<=move;i++){
    int_array_concat(fields.virtual_fields.expr[virtual_field_list.values[i]].monomials[current_term[i]],&monomial);
  }

  // loop until move is out of range
  while(move>=0 && move<power){
    // move
    current_term[move]=next_term_norepeat(current_term[move], fields.virtual_fields.expr[virtual_field_list.values[move]], &monomial, fields);
    // if the next term does not exist, then move previous index
    if(current_term[move]==-1){
      move--;
    }
    // else move next index
    else{
      move++;
    }
  }

  free_Int_Array(monomial);
  // if move==-1, then there is no next term, return -1
  if(move==-1){
    return(-1);
  }
  // if the next term exists
  return(0);
}

// find the next term in a polynomial that can be multiplied to a given monomial and add it to the monomial
int next_term_norepeat(int start, Polynomial polynomial, Int_Array* monomial, Fields_Table fields){
  int i;
  // remove last term from monomial
  if(start>=0 && start<polynomial.length){
    (*monomial).length-=polynomial.monomials[start].length;
  }
  // find next position
  for(i=start+1;i<polynomial.length;i++){
    // if no repetitions
    if(check_monomial_addterm(*monomial,polynomial.monomials[i],fields)==1){
      // append to monomial
      int_array_concat(polynomial.monomials[i], monomial);
      return(i);
    }
  }
  // no next term
  return(-1);
}


// signature of a permutation
int permutation_signature(int* permutation, int n){
  int* tmp_array=calloc(n,sizeof(int));
  int i;
  int ret=1;
  for(i=0;i<n;i++){
    tmp_array[i]=permutation[i];
  }
  sort_fermions(tmp_array, 0, n-1, &ret);
  free(tmp_array);
  return(ret);
}

// sort a list of anti-commuting variables
int sort_fermions(int* array, int begin, int end, int* sign){
  int i;
  int tmp;
  int index;
  // the pivot: middle of the monomial
  int pivot=(begin+end)/2;

  // if the monomial is non trivial
  if(begin<end){
    // send pivot to the end
    if(pivot!=end){
      tmp=array[end];
      array[end]=array[pivot];
      array[pivot]=tmp;
      *sign*=-1;
    }

    // loop over the others
    for(i=begin, index=begin;i<end;i++){
      // compare with pivot
      if(array[i]<array[end]){
	// if smaller, exchange with reference index
	if(i!=index){
	  tmp=array[i];
	  array[i]=array[index];
	  array[index]=tmp;
	  *sign*=-1;
	}
	// move reference index
	index++;
      }
    }
    // put pivot (which we had sent to the end) in the right place
    if(end!=index){
      tmp=array[end];
      array[end]=array[index];
      array[index]=tmp;
      *sign*=-1;
    }

    // recurse
    sort_fermions(array, begin, index-1, sign);
    sort_fermions(array, index+1, end, sign);
  }
  return(0);
}


// mean while factoring groups out
int mean_groups(Int_Array monomial, Polynomial* output, Fields_Table fields, Polynomial_Matrix propagator, Groups groups, Identities* computed){
  Polynomial num_mean;
  Int_Array tmp_monomial;
  int i;
  int group=-2;
  int next_group=-2;
  Polynomial tmp_poly;
  int sign=1;

  init_Polynomial(output, MONOMIAL_SIZE);

  // check whether there are virtual fields
  // IMPORTANT: the virtual fields must be at the end of the monomial
  if(monomial.length==0 || field_type(monomial.values[monomial.length-1], fields)!=FIELD_VIRTUAL){
    // mean
    mean(monomial, &num_mean, fields, propagator);
    // add to output
    polynomial_concat_noinit(num_mean,output);
  }
  else{
    // sort into groups
    if(groups.length>0){
      sign=1;
      monomial_sort_groups(monomial, fields, groups, &sign);
    }
    // construct groups and take mean
    init_Int_Array(&tmp_monomial, MONOMIAL_SIZE);
    for(i=0;i<=monomial.length;i++){
      // new group
      if(i<monomial.length){
	next_group=find_group(monomial.values[i], groups);
      }
      // if group changes, take mean
      if((i>0 && next_group!=group) || i==monomial.length){
	mean_virtual_fields(tmp_monomial, &tmp_poly, fields, propagator, groups, computed);
	// if zero
	if(polynomial_is_zero(tmp_poly)==1){
	  // set output to 0
	  free_Polynomial(*output);
	  init_Polynomial(output, 1);
	  free_Polynomial(tmp_poly);
	  break;
	}
	// add to output
	if(polynomial_is_zero(*output)==1){
	  polynomial_concat(tmp_poly, output);
	}
	else{
	  polynomial_prod_chain_nosimplify(tmp_poly, output, fields);
	}
	free_Polynomial(tmp_poly);
	
	// reset tmp_monomial
	free_Int_Array(tmp_monomial);
	init_Int_Array(&tmp_monomial, MONOMIAL_SIZE);
      }

      // add to monomial
      if(i<monomial.length){
	int_array_append(monomial.values[i], &tmp_monomial);
      }
      group=next_group;
    }

    // sign correction
    if(sign==-1){
      polynomial_multiply_Qscalar(*output,quot(sign,1));
    }

    free_Int_Array(tmp_monomial);

  }

  return(0);
}



// mean of a polynomial

// argument struct for multithreaded mean
struct mean_args{
  Polynomial* polynomial;
  Fields_Table fields;
  Polynomial_Matrix propagator;
  Groups groups;
  int print_progress;
};

// multithreaded
int polynomial_mean_multithread(Polynomial* polynomial, Fields_Table fields, Polynomial_Matrix propagator, Groups groups, int threads, int print_progress){
  int i;
  Polynomial thread_polys[threads];
  pthread_t thread_ids[threads];
  struct mean_args args[threads];
  int len=(*polynomial).length;


  // alloc
  for(i=0;i<threads;i++){
    init_Polynomial(thread_polys+i,(*polynomial).length/threads+1);
    // arguments
    args[i].fields=fields;
    args[i].propagator=propagator;
    args[i].groups=groups;
    args[i].print_progress=print_progress;
  }

  // split polynomial
  // randomly choose the thread
  // see random number generator
  srand(time(NULL));
  for(i=0;i<len;i++){
    polynomial_append((*polynomial).monomials[i], (*polynomial).factors[i], (*polynomial).nums[i], thread_polys+(rand() % threads));
  }

  // start threads
  for(i=0;i<threads;i++){
    args[i].polynomial=thread_polys+i;
    pthread_create(thread_ids+i, NULL, polynomial_mean_thread, (void*)(args+i));
  }

  free_Polynomial(*polynomial);
  init_Polynomial(polynomial, len);

  // wait for completion and join
  for(i=0;i<threads;i++){
    pthread_join(thread_ids[i], NULL);
    polynomial_concat_noinit(thread_polys[i], polynomial);
  }

  polynomial_simplify(polynomial, fields);

  return(0);
}

// mean for one of the threads
void* polynomial_mean_thread(void* mean_args){
  struct mean_args *args=mean_args;
  polynomial_mean((*args).polynomial,(*args).fields,(*args).propagator,(*args).groups, (*args).print_progress);
  return(NULL);
}

// single threaded version
int polynomial_mean(Polynomial* polynomial, Fields_Table fields, Polynomial_Matrix propagator, Groups groups, int print_progress){
  int i,j;
  Polynomial output;
  Polynomial tmp_poly;
  // a list of already computed means
  Identities computed;

  init_Polynomial(&output, (*polynomial).length);
  init_Identities(&computed, EQUATION_SIZE);

  remove_unmatched_plusminus(polynomial, fields);

  // mean of each monomial
  for(i=0;i<(*polynomial).length;i++){
    if(print_progress==1){
      fprintf(stderr,"computing %d of %d means\n",i,(*polynomial).length-1);
    }
    mean_groups((*polynomial).monomials[i], &tmp_poly, fields, propagator, groups, &computed);

    // write factors
    for(j=0;j<tmp_poly.length;j++){
      int_array_concat((*polynomial).factors[i], tmp_poly.factors+j);
      number_prod_chain((*polynomial).nums[i], tmp_poly.nums+j);
    }

    // add to output
    polynomial_concat_noinit(tmp_poly, &output);
    // simplify (simplify here in order to keep memory usage low)
    polynomial_simplify(&output, fields);
  }
  free_Identities(computed);

  free_Polynomial(*polynomial);
  *polynomial=output;

  return(0);
}