Ian Jauslin
summaryrefslogtreecommitdiff
blob: 1efe8fd6cf514f7bc3e3321d3b577201fec499e1 (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
/*
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 "coefficient.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
// define MPFR_USE_VA_LIST to enable the use of mpfr_inits and mpfr_clears
#define MPFR_USE_VA_LIST
#include <mpfr.h>
#include "definitions.cpp"
#include "rational.h"
#include "istring.h"
#include "array.h"
#include "number.h"
#include "tools.h"


// allocate memory
int init_Coefficient(Coefficient* coef,int size){
  (*coef).factors=calloc(size,sizeof(Int_Array));
  (*coef).nums=calloc(size,sizeof(Number));
  (*coef).denoms=calloc(size,sizeof(coef_denom));
  (*coef).length=0;
  (*coef).memory=size;

  return(0);
}

// free memory
int free_Coefficient(Coefficient coef){
  int i;
  for(i=0;i<coef.length;i++){
    free_Int_Array(coef.factors[i]);
    free_Number(coef.nums[i]);
  }
  free(coef.factors);
  free(coef.nums);
  free(coef.denoms);

  return(0);
}

// copy a coefficient
int coefficient_cpy(Coefficient input, Coefficient* output){
  init_Coefficient(output,input.length);
  coefficient_cpy_noinit(input,output);
  return(0);
}
int coefficient_cpy_noinit(Coefficient input, Coefficient* output){
  int i;
  
  // if output does not have enough memory allocated to it
  if(input.length>(*output).memory){
    resize_Coefficient(output,input.length);
  }

  for(i=0;i<input.length;i++){
    int_array_cpy(input.factors[i],(*output).factors+i);
    number_cpy(input.nums[i],(*output).nums+i);
    (*output).denoms[i]=input.denoms[i];
  }
  (*output).length=input.length;
  return(0);
}


// resize the memory allocated to a coefficient
int resize_Coefficient(Coefficient* coefficient,int new_size){
  Coefficient new_coef;
  int i;

  init_Coefficient(&new_coef,new_size);
  for(i=0;i<(*coefficient).length;i++){
    new_coef.factors[i]=(*coefficient).factors[i];
    new_coef.nums[i]=(*coefficient).nums[i];
    new_coef.denoms[i]=(*coefficient).denoms[i];
  }
  new_coef.length=(*coefficient).length;

  free((*coefficient).factors);
  free((*coefficient).nums);
  free((*coefficient).denoms);

  *coefficient=new_coef;
  return(0);
}


// append an element to a coefficient
int coefficient_append(Int_Array factor,Number num, coef_denom denom, Coefficient* output){
  int offset=(*output).length;

  if((*output).length>=(*output).memory){
    resize_Coefficient(output,2*(*output).memory+1);
  }
  // copy and allocate
  int_array_cpy(factor,(*output).factors+offset);
  number_cpy(num,(*output).nums+offset);
  (*output).denoms[offset]=denom;
  // increment length
  (*output).length++;
  return(0);
}
// append an element to a coefficient without allocating memory
int coefficient_append_noinit(Int_Array factor, Number num, coef_denom denom, Coefficient* output){
  int offset=(*output).length;

  if((*output).length>=(*output).memory){
    resize_Coefficient(output,2*(*output).memory+1);
  }
  // copy without allocating
  (*output).factors[offset]=factor;
  (*output).nums[offset]=num;
  (*output).denoms[offset]=denom;
  // increment length
  (*output).length++;
  return(0);
}

// concatenate coefficients and simplify result
int coefficient_concat(Coefficient input, Coefficient* output){
  int i;
  for(i=0;i<input.length;i++){
    coefficient_append(input.factors[i],input.nums[i],input.denoms[i],output);
  }

  coefficient_simplify(output);
  return(0);
}
int coefficient_concat_noinit(Coefficient input, Coefficient* output){
  int i;
  for(i=0;i<input.length;i++){
    coefficient_append_noinit(input.factors[i],input.nums[i],input.denoms[i],output);
  }

  coefficient_simplify(output);

  // free input arrays
  free(input.factors);
  free(input.nums);
  free(input.denoms);
  return(0);
}


// simplify a Coefficient
int coefficient_simplify(Coefficient* coefficient){
  int i;
  Coefficient output;
  init_Coefficient(&output,(*coefficient).length);
  // the combination of numerical factors
  Number new_num;
  init_Number(&new_num,NUMBER_SIZE);

  // sort the factors in the coefficient
  for(i=0;i<(*coefficient).length;i++){
    int_array_sort((*coefficient).factors[i],0,(*coefficient).factors[i].length-1);
  }
  // in order to perform a simplification, the list of terms must be
  // sorted (so that terms that are proportional are next to each other)
  sort_coefficient(coefficient,0,(*coefficient).length-1);

  for(i=0;i<(*coefficient).length;i++){
    // if the term actually exists
    if(number_is_zero((*coefficient).nums[i])!=1){
      // combine numerical factors
      number_add_chain((*coefficient).nums[i],&new_num);
    }
    // if the number is 0, the previous terms that may have the same factors should still be added, hence the 'if' ends here

    // if the factor is different from the next then add term
    if(i==(*coefficient).length-1 || (int_array_cmp((*coefficient).factors[i],(*coefficient).factors[i+1])!=0) || coef_denom_cmp((*coefficient).denoms[i],(*coefficient).denoms[i+1])!=0){
      // check that the coefficient is not trivial
      if(number_is_zero(new_num)!=1){
	coefficient_append((*coefficient).factors[i],new_num,(*coefficient).denoms[i],&output);
      }

      // reset new numerical factor
      free_Number(new_num);
      init_Number(&new_num,NUMBER_SIZE);
    }
  }

  free_Number(new_num);
  free_Coefficient(*coefficient);
  *coefficient=output;
  return(0);
}

// put all terms under a common denominator and simplify the resulting fraction
int coefficient_simplify_rational(Coefficient constant, Coefficient* coefficient){
  int ret;
  Coefficient remainder;
  Coefficient quotient;
  Coefficient quotient_prev;
  Coefficient out;
  int power;
  int max_power;

  // common denominator
  coefficient_common_denominator(constant, coefficient);

  // init
  init_Coefficient(&out, COEF_SIZE);

  // simplify, one power at a time
  // largest power (larger powers are at the end)
  max_power=(*coefficient).denoms[(*coefficient).length-1].power;

  quotient_prev=*coefficient;
  for(power=max_power;power>=1;power--){
    ret=coefficient_simplify_fraction(constant, quotient_prev, &remainder, &quotient);

    // if fail to simplify, stop
    if(ret<0){
      if(power<max_power){
	coefficient_concat_noinit(quotient_prev, &out);
      }
      else{
	coefficient_concat(quotient_prev, &out);
      }
      break;
    }

    // add to output
    coefficient_concat_noinit(remainder, &out);
  }
  // if the factorization always succeeded
  if(max_power>=1 && power==0){
    coefficient_concat_noinit(quotient, &out);
  }

  coefficient_simplify(&out);

  // set coefficient to out
  free_Coefficient(*coefficient);
  *coefficient=out;

  return 0;
}

// put all terms under a common denominator
// only supports coefficients with only one constant
int coefficient_common_denominator(Coefficient constant, Coefficient* coefficient){
  int max_power;
  int i,j;
  Coefficient tmp;
  Coefficient out;
  Coefficient* C_n;

  init_Coefficient(&out, COEF_SIZE);

  // largest power (larger powers are at the end)
  max_power=(*coefficient).denoms[(*coefficient).length-1].power;

  // store powers of the constant
  C_n=calloc(sizeof(Coefficient), max_power-1);
  for(i=0;i<max_power-1;i++){
    // start from previous product
    if(i==0){
      coefficient_cpy(constant, C_n+i);
    }
    else{
      coefficient_cpy(C_n[i-1], C_n+i);
    }
    // multiply by constant
    coefficient_prod_chain(constant, C_n+i);
  }

  // multiply each term
  for (i=0;i<(*coefficient).length;i++){
    init_Coefficient(&tmp, COEF_SIZE);
    // start with numerator
    coefficient_append_noinit((*coefficient).factors[i], (*coefficient).nums[i], (*coefficient).denoms[i], &tmp);
    // multiply
    if((*coefficient).denoms[i].power<max_power){
      if((*coefficient).denoms[i].power==max_power-1){
	coefficient_prod_chain(constant, &tmp);
      }
      else{
	coefficient_prod_chain(C_n[max_power-(*coefficient).denoms[i].power-2], &tmp);
      }
    }

    // set denom
    for(j=0;j<tmp.length;j++){
      tmp.denoms[j].power=max_power;
    }

    // add to out
    coefficient_concat_noinit(tmp, &out);
  }

  // free C_n
  for(i=0;i<max_power-1;i++){
    free_Coefficient(C_n[i]);
  }
  free(C_n);

  // free coefficient vectors
  free((*coefficient).factors);
  free((*coefficient).nums);
  free((*coefficient).denoms);

  // set output
  *coefficient=out;

  coefficient_simplify(coefficient);

  return(0);
}

// simplify coefficient / constant
// returns both the remainder and the quotient
// assumes both coefficient and constant are ordered with the highest order terms last
int coefficient_simplify_fraction(Coefficient constant, Coefficient coefficient, Coefficient* remainder, Coefficient* out){
  Coefficient tmp;
  int step_counter=0;
  int max_order;
  int i,j,k;
  Int_Array rfactors;

  if(constant.length==0){
    // nothing to do
    return 0;
  }

  coefficient_cpy(coefficient, remainder);
  init_Coefficient(out, COEF_SIZE);

  // continue until (*remainder) is of lower order than constant
  while((*remainder).length>0 && (*remainder).factors[(*remainder).length-1].length>=constant.factors[constant.length-1].length){
    step_counter++;

    // interrupt if too long
    if(step_counter>=coefficient.length*100){
      free_Coefficient(*remainder);
      free_Coefficient(*out);
      return -1;
    }

    // try to find a term in the constant that divides the last term of the (*remainder)
    rfactors=(*remainder).factors[(*remainder).length-1];

    // highest order in constant
    max_order=constant.factors[constant.length-1].length;

    // start from one of the highest order term and look for a common factor
    for(i=constant.length-1; i>=0; i--){
      // fail: no highest order terms have been matched
      if(constant.factors[i].length<max_order){
	free_Coefficient(*remainder);
	free_Coefficient(*out);
	return -2;
      }

      // check whether the term can be a factor of the last term of the (*remainder)
      if(int_array_is_subarray_ordered(constant.factors[i], rfactors)==1){
	// extract the factors that are not in constant
	init_Coefficient(&tmp, constant.length);

	// init with one term
	tmp.length=1;
	init_Int_Array(tmp.factors,MONOMIAL_SIZE);

	for(j=0,k=0;j<rfactors.length;j++){
	  // check that index is not in constant
	  if(k<constant.factors[i].length){
	    if(rfactors.values[j]!=constant.factors[i].values[k]){
	      int_array_append(rfactors.values[j],tmp.factors);
	    }
	    else{
	      // move to next term in constant
	      k++;
	    }
	  }
	}

	// numerical prefactor: term in the (*remainder) / term in the constant
	number_quot((*remainder).nums[(*remainder).length-1], constant.nums[i], tmp.nums);
	// denominator (dummy)
	tmp.denoms[0]=(*remainder).denoms[(*remainder).length-1];

	// add to out
	coefficient_concat(tmp, out);

	// multiply by -1
	Q minus_1;
	minus_1.numerator=-1;
	minus_1.denominator=1;
	number_Qprod_chain(minus_1, tmp.nums);

	// multiply by constant
	coefficient_prod_chain(constant, &tmp);

	// add to remainder
	coefficient_concat(tmp, remainder);

	// free memory
	free_Coefficient(tmp);

	// simplify
	coefficient_simplify(remainder);

	break;
      }
    }
  }

  // success!
  // decrease power of constant
  for(i=0;i<(*out).length;i++){
    (*out).denoms[i].power=(*out).denoms[i].power-1;
  }

  return(0);
}

// sort the terms in an equation (quicksort algorithm)
int sort_coefficient(Coefficient* coefficient, int begin, int end){
  int i;
  int index;
  // the pivot: middle of the array
  int pivot=(begin+end)/2;
  // if the array is non trivial
  if(begin<end){
    // send pivot to the end
    exchange_coefficient_terms(pivot,end,coefficient);
    // loop over the others
    for(i=begin, index=begin;i<end;i++){
      // compare with pivot
      if(coef_denom_cmp((*coefficient).denoms[i],(*coefficient).denoms[end])<0 || ( coef_denom_cmp((*coefficient).denoms[i],(*coefficient).denoms[end])==0 && (int_array_cmp((*coefficient).factors[i],(*coefficient).factors[end])<0)) ){
	// if smaller, exchange with reference index
	exchange_coefficient_terms(i,index,coefficient);
	// move reference index
	index++;
      }
    }
    // put pivot (which we had sent to the end) in the right place
    exchange_coefficient_terms(index,end,coefficient);
    // recurse
    sort_coefficient(coefficient, begin, index-1);
    sort_coefficient(coefficient, index+1, end);
  }
  return(0);
}

// exchange two terms (for the sorting algorithm)
int exchange_coefficient_terms(int i, int j, Coefficient* coefficient){
  Int_Array ptmp;
  Number tmpq;
  coef_denom tmpc;

  ptmp=(*coefficient).factors[j];
  (*coefficient).factors[j]=(*coefficient).factors[i];
  (*coefficient).factors[i]=ptmp;

  tmpq=(*coefficient).nums[j];
  (*coefficient).nums[j]=(*coefficient).nums[i];
  (*coefficient).nums[i]=tmpq;

  tmpc=(*coefficient).denoms[j];
  (*coefficient).denoms[j]=(*coefficient).denoms[i];
  (*coefficient).denoms[i]=tmpc;
  return(0);
}

// differentiate a coefficient with respect to an index
int coefficient_deriv_noinit(Coefficient input, int index, Coefficient* output){
  int i,j;
  // temp list of indices
  Int_Array factor;
  // number of times index was found
  int match_count;
  // whether the output contains at least one factor
  int at_least_one=0;
  coef_denom denom;

  // loop over monomials
  for(i=0;i<input.length;i++){
    init_Int_Array(&factor,input.factors[i].length);
    // init match count
    match_count=0;
    // loop over indices
    for(j=0;j<input.factors[i].length;j++){
      // if found
      if(input.factors[i].values[j]==index){
	// if it's the first match, don't add it
	if(match_count!=0){
	  int_array_append(index,&factor);
	}
	match_count++;
      }
      // add the index
      else{
	int_array_append(input.factors[i].values[j],&factor);
      }
    }

    denom=input.denoms[i];
    // if the index is that of 1/C
    if(index==input.denoms[i].index){
      // if no C in the numerator (which is normal behavior)
      if(match_count==0){
	denom.power++;
      }
      match_count-=input.denoms[i].power;
    }


    // if the derivative doesn't vanish, add it to the coefficient
    if(match_count!=0){
      at_least_one=1;
      coefficient_append_noinit(factor,number_Qprod_ret(quot(match_count,1),input.nums[i]), denom, output);
    }
    else{
      free_Int_Array(factor);
    }
  }

  if(at_least_one==1){
    coefficient_simplify(output);
  }
  else{
    // add a trivial element to the coefficient
    init_Int_Array(&factor,0);
    denom.index=-1;
    denom.power=0;
    coefficient_append_noinit(factor,number_zero(),denom,output);
  }

  return(0);
}

int coefficient_deriv(Coefficient input, int index, Coefficient* output){
  init_Coefficient(output, COEF_SIZE);
  coefficient_deriv_noinit(input, index, output);
  return(0);
}

/*
// differentiate a coefficient with respect to an index (as a polynomial) (does not differentiate the 1/(1+C)^p )
int coefficient_deriv_noinit(Coefficient input, int index, Coefficient* output){
  int i;
  // temp list of indices
  Int_Array factor;
  // number of times index was found
  int match_count;
  // whether the output contains at least one factor
  int at_least_one=0;
  coef_denom denom;

  // loop over monomials
  for(i=0;i<input.length;i++){
    // derivative of monomial
    monomial_deriv(input.factors[i], index, &factor, &match_count);

    // if the derivative doesn't vanish, add it to the coefficient
    if(match_count>0){
      at_least_one=1;
      coefficient_append_noinit(factor,number_Qprod_ret(quot(match_count,1),input.nums[i]), input.denoms[i],output);
    }
    else{
      free_Int_Array(factor);
    }
  }

  if(at_least_one>0){
    coefficient_simplify(output);
  }
  else{
    // add a trivial element to the coefficient
    init_Int_Array(&factor,0);
    denom.index=-1;
    denom.power=0;
    coefficient_append_noinit(factor,number_zero(),denom,output);
  }

  return(0);
}

// differentiate a monomial with respect to an index
int monomial_deriv(Int_Array factor, int index, Int_Array* out_factor, int* match_count){
  int j;

  init_Int_Array(out_factor,factor.length);
  // init match count
  *match_count=0;

  // loop over indices
  for(j=0;j<factor.length;j++){
    // if found
    if(factor.values[j]==index){
      // if it's the first match, don't add it
      if(*match_count!=0){
	int_array_append(index,out_factor);
      }
      (*match_count)++;
    }
    // add the index
    else{
      int_array_append(factor.values[j],out_factor);
    }
  }

  return(0);
}
*/
  


// product of two coefficients
int coefficient_prod(Coefficient coef1, Coefficient coef2, Coefficient* output){
  int i,j;
  // tmp factor
  Int_Array factor;
  coef_denom denom;

  // init
  init_Coefficient(output,COEF_SIZE);

  // loop over coef1
  for(i=0;i<coef1.length;i++){
    // loop over coef2
    for(j=0;j<coef2.length;j++){
      init_Int_Array(&factor,coef1.factors[i].length+coef2.factors[j].length);
      int_array_concat(coef1.factors[i],&factor);
      int_array_concat(coef2.factors[j],&factor);
      
      // don't throw an error if the power is 0
      if(coef2.denoms[j].power==0){
	coef2.denoms[j].index=coef1.denoms[i].index;
      }
      else if(coef1.denoms[i].power==0){
	coef1.denoms[i].index=coef2.denoms[j].index;
      }
      if(coef1.denoms[i].index!=coef2.denoms[j].index){
	fprintf(stderr,"error: cannot multiply flow equations with different constants: got %d and %d\n", coef1.denoms[i].index, coef2.denoms[j].index);
	exit(-1);
      }
      denom=coef1.denoms[i];
      denom.power+=coef2.denoms[j].power;
      coefficient_append_noinit(factor,number_prod_ret(coef1.nums[i],coef2.nums[j]), denom, output);
    }
  }

  // simplify output
  coefficient_simplify(output);
  return(0);
}

// product of coefficients, output replaces the second coefficient
int coefficient_prod_chain(Coefficient in, Coefficient* inout){
  Coefficient tmp_coef;
  coefficient_prod(in,*inout,&tmp_coef);
  free_Coefficient(*inout);
  *inout=tmp_coef;
  return(0);
}


// print coefficient
// offset specifies the amount of blank space to the left of the terms after the first
// prepend indices by ind_pre
int coefficient_sprint(Coefficient coef, Char_Array* output, int offset, char index_pre){
  Char_Array buffer;
  int i,j,k;
  int dcount;
  
  if(coef.length==0){
    char_array_snprintf(output, "  (0)\n");
  }
  
  for(i=0;i<coef.length;i++){
    if(i==0){
      char_array_snprintf(output, "  ");
    }
    else{
      for(j=0;j<=offset;j++){
	char_array_append(' ',output);
      }
      char_array_append('+',output);
    }

    // print numerical coefficient
    char_array_append('(',output);
    init_Char_Array(&buffer, STR_SIZE);
    number_sprint(coef.nums[i], &buffer);
    char_array_concat(buffer, output);
    free_Char_Array(buffer);
    char_array_append(')',output);

    // print factors
    for(j=0;j<coef.factors[i].length;j++){
      // constant indices
      if(coef.factors[i].values[j]<0){
	// count derivatives
	dcount=-coef.factors[i].values[j]/DOFFSET;
	char_array_append('[',output);
	for(k=0;k<dcount;k++){
	  char_array_append('d',output);
	}
	char_array_snprintf(output,"C%d]",-coef.factors[i].values[j]-dcount*DOFFSET);
      }
      else{
	// count derivatives
	dcount=coef.factors[i].values[j]/DOFFSET;
	char_array_append('[',output);
	for(k=0;k<dcount;k++){
	  char_array_append('d',output);
	}
	char_array_snprintf(output,"%c%d]",index_pre,coef.factors[i].values[j]-dcount*DOFFSET);
      }
    }

    // print constant denominators
    if(coef.denoms[i].power!=0){
      char_array_snprintf(output,"[/C%d^%d]",-coef.denoms[i].index,coef.denoms[i].power);
    }

    char_array_append('\n',output);
  }

  return(0);
}


// read from a string
#define PP_NULL_MODE 0
#define PP_BRACKET_MODE 1
#define PP_INDICES_MODE 2
#define PP_POWER_MODE 3
#define PP_COMMENT_MODE 4
#define PP_NUMBER_MODE 5
#define PP_CONSTANT_MODE 6
#define PP_CONSTANT_DENOM_MODE 7
int char_array_to_Coefficient(Char_Array str_coef, Coefficient* output){
  // buffer
  char* buffer=calloc(str_coef.length+1,sizeof(char));
  char* buffer_ptr=buffer;
  Int_Array indices;
  coef_denom denom;
  Number num, tmp1_num;
  int mode;
  int i,j;
  int parenthesis_count=0;
  int dcount=0;

  // allocate memory
  init_Coefficient(output,COEF_SIZE);

  // init
  init_Int_Array(&indices, MONOMIAL_SIZE);
  num=number_one();
  denom.index=-1;
  denom.power=0;

  *buffer_ptr='\0';
  // loop over the input polynomial
  // start in null mode
  mode=PP_NULL_MODE;
  for(j=0;j<str_coef.length;j++){
    if(mode==PP_COMMENT_MODE){
      if(str_coef.str[j]=='\n'){
	mode=PP_NULL_MODE;
      }
    }
    else{
      switch(str_coef.str[j]){
      // new indices
      case '+':
	if(mode==PP_NULL_MODE){
	  coefficient_append_noinit(indices, num, denom, output);
	  // reset indices, num
	  init_Int_Array(&indices, MONOMIAL_SIZE);
	  num=number_one();
	  denom.index=-1;
	  denom.power=0;
	}
	break;

      // enter indices or power mode
      case '[':
	if(mode==PP_NULL_MODE){
	  mode=PP_BRACKET_MODE;
	  // reset derivatives count
	  dcount=0;
	}
	break;
      // indices mode
      case '%':
	if(mode==PP_BRACKET_MODE){
	  mode=PP_INDICES_MODE;
	  buffer_ptr=buffer;
	  *buffer_ptr='\0';
	}
	break;
      case 'C':
	if(mode==PP_BRACKET_MODE){
	  mode=PP_CONSTANT_MODE;
	  buffer_ptr=buffer;
	  *buffer_ptr='\0';
	}
	break;
      case '/':
	if(mode==PP_BRACKET_MODE){
	  mode=PP_CONSTANT_DENOM_MODE;
	  buffer_ptr=buffer;
	  *buffer_ptr='\0';
	}
	else if(mode!=PP_NULL_MODE){
	  // write to buffer
	  buffer_ptr=str_addchar(buffer_ptr,str_coef.str[j]);
	}
	break;
      // derivatives
      case 'd':
	if(mode==PP_BRACKET_MODE || mode==PP_INDICES_MODE || mode==PP_CONSTANT_MODE){
	  dcount++;
	}
	break;
      // power mode
      case '^':
	if(mode==PP_CONSTANT_DENOM_MODE){
	  sscanf(buffer,"%d",&i);
	  denom.index=-i;
	  mode=PP_POWER_MODE;
	  buffer_ptr=buffer;
	  *buffer_ptr='\0';
	}
	else{
	  buffer_ptr=str_addchar(buffer_ptr,str_coef.str[j]);
	}
	break;
      // read indices or power
      case ']':
	sscanf(buffer,"%d",&i);
	if(mode==PP_INDICES_MODE){
	  int_array_append(i+dcount*DOFFSET,&indices);
	}
	else if(mode==PP_CONSTANT_MODE){
	  int_array_append(-i-dcount*DOFFSET,&indices);
	}
	else if(mode==PP_POWER_MODE){
	  denom.power=i;
	}
	// switch back to null mode
	mode=PP_NULL_MODE;
	break;
	
      // numerical pre-factor
      case '(':
	if(mode==PP_NULL_MODE){
	  mode=PP_NUMBER_MODE;
	  parenthesis_count=0;
	  buffer_ptr=buffer;
	  *buffer_ptr='\0';
	}
	else if(mode==PP_NUMBER_MODE){
	  // match parentheses
	  parenthesis_count++;
	  buffer_ptr=str_addchar(buffer_ptr,str_coef.str[j]);
	}
	break;
      case ')':
	if(mode==PP_NUMBER_MODE){
	  if(parenthesis_count==0){
	    // write num
	    str_to_Number(buffer,&tmp1_num);
	    number_prod_chain(tmp1_num,&num);
	    free_Number(tmp1_num);
	    // back to null mode
	    mode=PP_NULL_MODE;
	  }
	  else{
	    parenthesis_count--;
	    buffer_ptr=str_addchar(buffer_ptr,str_coef.str[j]);
	  }
	}
	break;

      // characters to ignore
      case ' ':break;
      case '&':break;
      case '\n':break;
      
      // comments
      case '#':
	mode=PP_COMMENT_MODE;
	break;

      default:
	if(mode!=PP_NULL_MODE){
	  // write to buffer
	  buffer_ptr=str_addchar(buffer_ptr,str_coef.str[j]);
	}
	break;
      }
    }
  }

  // last term
  coefficient_append_noinit(indices, num, denom, output);

  free(buffer);
  return(0);
}

int str_to_Coefficient(char* str, Coefficient* output){
  Char_Array array;
  array.length=str_len(str);
  array.str=str;
  char_array_to_Coefficient(array, output);
  return(0);
}

// compare coefficient denominators
int coef_denom_cmp(coef_denom denom1, coef_denom denom2){
  if(denom1.index<denom2.index){
    return(1);
  }
  else if(denom1.index>denom2.index){
    return(-1);
  }

  if(denom1.power<denom2.power){
    return(-1);
  }
  else if(denom1.power>denom2.power){
    return(1);
  }

  return(0);
}


// evaluate a coefficient on a vector
int evalcoef(RCC rccs, Coefficient coef, long double* out){
  int i,j;
  long double num_factor;

  *out=0.;

  // for each monomial
  for(i=0;i<coef.length;i++){
    // product of factors
    for(j=0, num_factor=1.;j<coef.factors[i].length;j++){
      num_factor*=rccs.values[intlist_find_err(rccs.indices,rccs.length,coef.factors[i].values[j])];
    }
    // denominator
    if(coef.denoms[i].power>0){
      num_factor/=dpower(rccs.values[intlist_find_err(rccs.indices,rccs.length,coef.denoms[i].index)],coef.denoms[i].power);
    }
    *out+=num_factor*number_double_val(coef.nums[i]);
  }
  return(0);
}

// evaluate a coefficient on a vector (using mpfr floats)
int evalcoef_mpfr(RCC_mpfr rccs, Coefficient coef, mpfr_t out){
  int i,j;
  mpfr_t num_factor;
  // tmp number (do not initialize Z)
  mpfr_t x, y, Z;

  // init numbers
  mpfr_inits(num_factor, x, y, (mpfr_ptr) NULL);

  mpfr_init(out);
  mpfr_set_zero(out, 1);

  // for each monomial
  for(i=0;i<coef.length;i++){
    // product of factors
    mpfr_set_flt(num_factor, 1., MPFR_RNDN);
    for(j=0;j<coef.factors[i].length;j++){
      mpfr_mul(x,num_factor,rccs.values[intlist_find_err(rccs.indices,rccs.length,coef.factors[i].values[j])], MPFR_RNDN);
      mpfr_set(num_factor,x, MPFR_RNDN);
    }
    // denominator
    if(coef.denoms[i].power>0){
      mpfr_pow_si(y, rccs.values[intlist_find_err(rccs.indices,rccs.length,coef.denoms[i].index)], coef.denoms[i].power, MPFR_RNDN);
      mpfr_div(x, num_factor, y, MPFR_RNDN);
      mpfr_set(num_factor, x, MPFR_RNDN);
    }

    number_mpfr_val(Z, coef.nums[i]);
    mpfr_mul(x, num_factor, Z, MPFR_RNDN);
    mpfr_add(y, x, out, MPFR_RNDN);
    mpfr_set(out, y, MPFR_RNDN);
    mpfr_clear(Z);
  }

  // free numbers
  mpfr_clears(num_factor, x, y, (mpfr_ptr)NULL);

  return(0);
}