File: nmz_integrate.h

package info (click to toggle)
regina-normal 7.4.1-1.1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 154,244 kB
  • sloc: cpp: 295,026; xml: 9,992; sh: 1,344; python: 1,225; perl: 616; ansic: 138; makefile: 26
file content (960 lines) | stat: -rw-r--r-- 30,766 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2007-2022  W. Bruns, B. Ichim, Ch. Soeger, U. v. d. Ohe
 * This program 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.
 *
 * This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * As an exception, when this program is distributed through (i) the App Store
 * by Apple Inc.; (ii) the Mac App Store by Apple Inc.; or (iii) Google Play
 * by Google Inc., then that store may impose any digital rights management,
 * device limits and/or redistribution restrictions that are required by its
 * terms of service.
 */

#ifndef LIBNORMALIZ_NMZ_INTEGRATE_H
#define LIBNORMALIZ_NMZ_INTEGRATE_H

#ifdef NMZ_COCOA
#include "CoCoA/library.H"
#endif

#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <gmpxx.h>

#include "libnormaliz/dynamic_bitset.h"
#include "libnormaliz/general.h"
#include "libnormaliz/HilbertSeries.h"
#include "libnormaliz/matrix.h"

namespace libnormaliz {

using namespace std;


#ifdef NMZ_COCOA

using namespace CoCoA;

//  conversion from CoCoA types to GMP
inline mpz_class mpz(const BigInt& B) {
    return (mpz_class(mpzref(B)));
}

inline mpq_class mpq(const BigRat& B) {
    return (mpq_class(mpqref(B)));
}

inline mpz_class ourFactorial(const long& n) {
    mpz_class fact = 1;
    for (long i = 1; i <= n; ++i)
        fact *= i;
    return (fact);
}


typedef unsigned int key_type;

extern bool verbose_INT;

struct SIMPLINEXDATA_INT {     // local data of excluded faces
    dynamic_bitset GenInFace;  // indicator for generators of simplex in face
    long mult;                 // multiplicity of this face
    size_t card;               // the cardinality of the face
    bool done;                 // indicates that this face has been done for a given offset
    vector<long> denom;
    vector<long> degrees;
    vector<long> key;
};

class ourFactorization {
   public:
    vector<RingElem> myFactors;
    vector<long> myMultiplicities;
    RingElem myRemainingFactor;

    ourFactorization(const vector<RingElem>& myFactors, const vector<long>& myMultiplicities, const RingElem& myRemainingFactor);
    ourFactorization(const factorization<RingElem>& FF);
    ourFactorization();
};
// end class

class CyclRatFunct {
    // class for rational functions whose denominator is a product
    // of cyclotomic polynomials
    // We work with denominators that are products of factors 1-t^i
    // which is of course equivalent
    // the numerator is a polynomial in its ring
    // the denominator is an integer vector that at index i
    // gives the multiplicity of 1-t^i in the denominator
    // (the entry at index 0 is not used and must always be equal to 0)
   public:
    RingElem num;
    vector<long> denom;

    void extendDenom(const vector<long>& target);
    void addCRF(const CyclRatFunct& r);
    void multCRF(const CyclRatFunct& r);
    void simplifyCRF();
    void set2(const RingElem& f, const vector<long>& d);
    void set2(const RingElem& f);
    void showCRF();
    void showCoprimeCRF();
    CyclRatFunct(const RingElem& c);
    CyclRatFunct(const RingElem& c, const vector<long>& d);
};
// class end *****************************************************************

// manipulation of denominators
vector<long> lcmDenom(const vector<long>& df, const vector<long>& dg);
vector<long> prodDenom(const vector<long>& df, const vector<long>& dg);
vector<long> degrees2denom(const vector<long>& d);
vector<long> denom2degrees(const vector<long>& d);
RingElem denom2poly(const SparsePolyRing& P, const vector<long>& d);
vector<long> makeDenom(long k, long n);


inline ourFactorization::ourFactorization(const vector<RingElem>& myFactors,
                                   const vector<long>& myMultiplicities,
                                   const RingElem& myRemainingFactor) {
    this->myFactors = myFactors;
    this->myMultiplicities = myMultiplicities;
    this->myRemainingFactor = myRemainingFactor;
}

inline ourFactorization::ourFactorization() {
}
/*
ourFactorization::ourFactorization(const factorization<RingElem>& FF) {
    ourFactorization(FF.myFactors(), FF.myMultiplicities(), FF.myRemainingFactor());
}

RingElem binomial(const RingElem& f, long k)
// computes binomial coefficient (f choose k)
{
    const SparsePolyRing& P = owner(f);
    RingElem g(P);
    g = 1;
    for (int i = 0; i < k; i++)
        g *= (f - i) / (i + 1);
    return (g);
}
*/

inline RingElem ascFact(const RingElem& f, long k)
// computes (f+1)*...*(f+k)
{
    const SparsePolyRing& P = owner(f);
    RingElem g(P);
    g = 1;
    for (int i = 0; i < k; i++)
        g *= (f + i + 1);
    return (g);
}

/*
RingElem descFact(const RingElem& f, long k)
// computes f*(f-1)*...*(f-k+1)
{
    const SparsePolyRing& P = owner(f);
    RingElem g(P);
    g = 1;
    for (int i = 0; i < k; i++)
        g *= (f - i);
    return (g);
}
*/

inline bool compareLength(const RingElem& p, const RingElem& q) {
    return (NumTerms(p) > NumTerms(q));
}

inline vector<RingElem> ourCoeffs(const RingElem& F, const long j) {
    // our version of expanding a poly nomial wrt to indeterminate j
    // The return value is the vector of coefficients of x[j]^i
    vector<RingElem> c;
    const SparsePolyRing& P = owner(F);
    RingElem x = indets(P)[j];
    if (F == 0) {
        c.push_back(zero(P));
        return (c);
    }

    vector<long> v(NumIndets(P));
    long k, cs;

    SparsePolyIter i = BeginIter(F);
    for (; !IsEnded(i); ++i) {
        exponents(v, PP(i));
        k = v[j];
        cs = c.size();
        if (k > cs - 1)
            c.resize(k + 1, zero(P));
        v[j] = 0;
        // c[k]+=monomial(P,coeff(i),v);
        PushBack(c[k], coeff(i), v);
    }
    return (c);
}

inline RingElem mySubstitution(const RingElem& F, const vector<RingElem>& w) {
    const SparsePolyRing& R = owner(F);
    RingElem G(zero(R));
    RingElem H(one(R));
    vector<long> v(NumIndets(R));
    vector<long> Z(NumIndets(R));

    SparsePolyIter i = BeginIter(F);
    for (; !IsEnded(i); ++i) {
        exponents(v, PP(i));
        H = zero(R);
        PushBack(H, coeff(i), Z);
        for (size_t j = 0; j < v.size(); ++j)
            H *= power(w[j], v[j]);
        G += H;
    }
    return G;
}

template <typename Number>
vector<long> MxV(const vector<vector<Number> >& M, vector<Number> V) {
    // matrix*vector
    vector<Number> P(M.size());
    for (size_t i = 0; i < M.size(); ++i) {
        long s = 0;
        for (size_t j = 0; j < V.size(); ++j)
            s += M[i][j] * V[j];
        P[i] = s;
    }
    return (P);
}

template <typename Number>
vector<RingElem> VxM(const vector<RingElem>& V, const vector<vector<Number> >& M) {
    // vector*matrix
    const SparsePolyRing& R = owner(V[0]);
    RingElem s(zero(R));
    vector<RingElem> P(M[0].size(), zero(R));
    for (size_t j = 0; j < M[0].size(); ++j) {
        s = 0;
        for (size_t i = 0; i < M.size(); ++i)
            s += V[i] * M[i][j];
        P[j] = s;
    }
    return (P);
}

/*
RingElem affineLinearSubstitution(const RingElem& F,const vector<vector<long> >& A,
                     const vector<long>& b, const long& denom){
// NOT IN USE
    size_t i;
    const SparsePolyRing& R=owner(F);
    size_t m=A.size();
    // long n=A[0].size();
    vector<RingElem> v(m,zero(R));
    RingElem g(zero(R));

    for(i=0;i<m;i++)
    {
        g=b[i];
        g=g/denom;
        v[i]=g+indets(R)[i+1];
    }
    vector<RingElem> w=VxM(v,A);
    vector<RingElem> w1(w.size()+1,zero(R));
    w1[0]=indets(R)[0];
    for(i=1;i<w1.size();++i)
        w1[i]=w[i-1];

    RingHom phi=PolyAlgebraHom(R,R,w1);
    RingElem G=phi(F);
    return(G);
}
*/

// bool DDD = false;

inline vector<long> shiftVars(const vector<long>& v, const vector<long>& key) {
    // selects components of v and reorders them according to key
    vector<long> w(v.size(), 0);
    for (size_t i = 0; i < key.size(); ++i) {
        w[i] = v[key[i]];
    }
    return (w);
}

inline void makeLocalDegreesAndKey(const dynamic_bitset& indicator,
                            const vector<long>& degrees,
                            vector<long>& localDeg,
                            vector<long>& key) {
    localDeg.clear();
    key.clear();
    key.push_back(0);
    for (size_t i = 0; i < indicator.size(); ++i)
        if (indicator.test(i))
            key.push_back(i + 1);
    for (size_t i = 0; i < key.size() - 1; ++i)
        localDeg.push_back(degrees[key[i + 1] - 1]);
}

inline void makeStartEnd(const vector<long>& localDeg, vector<long>& St, vector<long>& End) {
    vector<long> denom = degrees2denom(localDeg);  // first we must find the blocks of equal degree
    if (denom.size() == 0)
        return;
    St.push_back(1);
    for (size_t i = 0; i < denom.size(); ++i)
        if (denom[i] != 0) {
            End.push_back(St[St.size() - 1] + denom[i] - 1);
            if (i < denom.size() - 1)
                St.push_back(End[End.size() - 1] + 1);
        }
    /* if(St.size()!=End.size()){
        for (size_t i=0;i<denom.size(); ++i){
            verboseOutput() << denom.size() << endl;
                    verboseOutput() << denom[i] << " ";
            verboseOutput() << endl;
        }
    }*/
}

inline vector<long> orderExposInner(vector<long>& vin, const vector<long>& St, vector<long>& End) {
    vector<long> v = vin;
    long p, s, pend, pst;
    bool ordered;
    if (St.size() != End.size()) {
        verboseOutput() << St.size() << " " << End.size() << " " << vin.size() << endl;
        verboseOutput() << St[0] << endl;
        for (size_t i = 0; i < vin.size(); ++i) {
            verboseOutput() << vin[i] << " ";
        }
        verboseOutput() << endl;
        assert(false);
    }
    for (size_t j = 0; j < St.size(); ++j) {  // now we go over the blocks
        pst = St[j];
        pend = End[j];
        while (1) {
            ordered = true;
            for (p = pst; p < pend; ++p) {
                if (v[p] < v[p + 1]) {
                    ordered = false;
                    s = v[p];
                    v[p] = v[p + 1];
                    v[p + 1] = s;
                }
            }
            if (ordered)
                break;
            pend--;
        }
    }
    return (v);
}

inline RingElem orderExpos(const RingElem& F, const vector<long>& degrees, const dynamic_bitset& indicator, bool compactify) {
    // orders the exponent vectors v of the terms of F
    // the exponents v[i] and v[j], i < j,  are swapped if
    // (1) degrees[i]==degrees[j] and (2) v[i] < v[j]
    // so that the exponents are descending in each degree block
    // the ordered exponent vectors are inserted into a map
    // and their coefficients are added
    // at the end the polynomial is rebuilt from the map
    // If compactify==true, the exponents will be shifted to the left in order to keep the correspondence
    // of variables to degrees
    // compactification not used at present (occurs only in restrictToFaces)

    const SparsePolyRing& P = owner(F);
    vector<long> v(NumIndets(P));
    vector<long> key, localDeg;
    key.reserve(v.size() + 1);
    localDeg.reserve(degrees.size() + 1);

    if (compactify) {
        makeLocalDegreesAndKey(indicator, degrees, localDeg, key);
    }
    else {
        localDeg = degrees;
    }

    vector<long> St, End;
    makeStartEnd(localDeg, St, End);

    // now the main job

    map<vector<long>, RingElem> orderedMons;  // will take the ordered exponent vectors

    SparsePolyIter mon = BeginIter(F);  // go over the given polynomial
    for (; !IsEnded(mon); ++mon) {
        exponents(v, PP(mon));  // this function gives the exponent vector back as v
        if (compactify)
            v = shiftVars(v, key);
        v = orderExposInner(v, St, End);
        auto ord_mon = orderedMons.find(v);  // insert into map or add coefficient
        if (ord_mon != orderedMons.end()) {
            ord_mon->second += coeff(mon);
        }
        else {
            orderedMons.insert(pair<vector<long>, RingElem>(v, coeff(mon)));
        }
    }

    // now we must reconstruct the polynomial
    // we use that the natural order of vectors in C++ STL is inverse
    // to lex. Therefore push_front

    RingElem r(zero(P));
    // JAA   verboseOutput() << "Loop start " << orderedMons.size() <<  endl;
    // JAA   size_t counter=0;
    for (const auto& ord_mon : orderedMons) {
        // JAA       verboseOutput() << counter++ << ord_mon.first << endl;
        // JAA       try {
        PushFront(r, ord_mon.second, ord_mon.first);
        // JAA        }
        // JAA       catch(const std::exception& exc){verboseOutput() << "Caught exception: " << exc.what() << endl;}
    }
    // JAA    verboseOutput() << "Loop end" << endl;
    return (r);
}

inline void restrictToFaces(const RingElem& G,
                     RingElem& GOrder,
                     vector<RingElem>& GRest,
                     const vector<long> degrees,
                     const vector<SIMPLINEXDATA_INT>& inExSimplData) {
    // Computesd the restrictions of G to the faces in inclusion-exclusion.
    // All terms are simultaneously compactified and exponentwise ordered
    // Polynomials returned in GRest
    // Ordering is also applied to G itself, returned in GOrder
    // Note: degrees are given for the full simplex. Therefore "local" degrees must be made
    // (depend only on face and not on offset, but generation here is cheap)

    const SparsePolyRing& P = owner(G);

    vector<long> v(NumIndets(P));
    vector<long> w(NumIndets(P));
    vector<long> localDeg;
    localDeg.reserve(v.size());
    size_t dim = NumIndets(P) - 1;

    // first we make the facewise data that are needed for the compactification and otrdering
    // of exponent vectors
    vector<vector<long> > St(inExSimplData.size()), End(inExSimplData.size()), key(inExSimplData.size());
    vector<long> active;
    for (size_t i = 0; i < inExSimplData.size(); ++i)
        if (!inExSimplData[i].done) {
            active.push_back(i);
            makeLocalDegreesAndKey(inExSimplData[i].GenInFace, degrees, localDeg, key[i]);
            makeStartEnd(localDeg, St[i], End[i]);
        }

    // now the same for the full simplex (localDeg=degrees)
    dynamic_bitset fullSimpl(dim);
    fullSimpl.set();
    vector<long> StSimpl, EndSimpl;
    makeStartEnd(degrees, StSimpl, EndSimpl);

    vector<map<vector<long>, RingElem> > orderedMons(inExSimplData.size());  // will take the ordered exponent vectors
    map<vector<long>, RingElem> orderedMonsSimpl;

    dynamic_bitset indicator(dim);

    // now we go over the terms of G
    SparsePolyIter term = BeginIter(G);
    // PPMonoid TT = PPM(owner(G));
    for (; !IsEnded(term); ++term) {
        // PPMonoidElem mon(PP(term));
        exponents(v, PP(term));
        w = v;
        indicator.reset();
        for (size_t j = 0; j < dim; ++j)
            if (v[j + 1] != 0)  // we must add 1 since the 0-th indeterminate is irrelevant here
                indicator.set(j);
        for (size_t i = 0; i < active.size(); ++i) {
            int j = active[i];
            if (indicator.is_subset_of(inExSimplData[j].GenInFace)) {
                w = shiftVars(v, key[j]);
                w = orderExposInner(w, St[j], End[j]);
                // w=shiftVars(v,key[j]);
                auto ord_mon = orderedMons[j].find(w);  // insert into map or add coefficient
                if (ord_mon != orderedMons[j].end()) {
                    ord_mon->second += coeff(term);
                }
                else {
                    orderedMons[j].insert(pair<vector<long>, RingElem>(w, coeff(term)));
                }
            }
        }  // for i

        v = orderExposInner(v, StSimpl, EndSimpl);
        auto ord_mon = orderedMonsSimpl.find(v);  // insert into map or add coefficient
        if (ord_mon != orderedMonsSimpl.end()) {
            ord_mon->second += coeff(term);
        }
        else {
            orderedMonsSimpl.insert(pair<vector<long>, RingElem>(v, coeff(term)));
        }
    }  // loop over term

    // now we must make the resulting polynomials from the maps

    for (size_t i = 0; i < active.size(); ++i) {
        int j = active[i];
        for (const auto& ord_mon : orderedMons[j]) {
            PushFront(GRest[j], ord_mon.second, ord_mon.first);
        }
        // verboseOutput() << "GRest[j] " << j << " " << NumTerms(GRest[j]) << endl;
    }

    for (const auto& ord_mon : orderedMonsSimpl) {
        PushFront(GOrder, ord_mon.second, ord_mon.first);
    }
}

// long nrActiveFaces = 0;
// long nrActiveFacesOld = 0;

inline void all_contained_faces(const RingElem& G,
                         RingElem& GOrder,
                         const vector<long>& degrees,
                         dynamic_bitset& indicator,
                         long Deg,
                         vector<SIMPLINEXDATA_INT>& inExSimplData,
                         deque<pair<vector<long>, RingElem> >& facePolysThread) {
    const SparsePolyRing& R = owner(G);
    vector<RingElem> GRest;
    // size_t dim=indicator.size();
    for (size_t i = 0; i < inExSimplData.size(); ++i) {
        GRest.push_back(zero(R));

        if (!indicator.is_subset_of(inExSimplData[i].GenInFace))
            inExSimplData[i].done = true;  // done if face cannot contribute to result for this offset
        else
            inExSimplData[i].done = false;  // not done otherwise
    }
    restrictToFaces(G, GOrder, GRest, degrees, inExSimplData);

    for (size_t j = 0; j < inExSimplData.size(); ++j) {
        if (inExSimplData[j].done)
            continue;
// #pragma omp atomic
        // nrActiveFaces++;
        // verboseOutput() << "Push back " << NumTerms(GRest[j]);
        GRest[j] = power(indets(R)[0], Deg) * inExSimplData[j].mult *
                   GRest[j];  // shift by degree of offset amd multiply by mult of face
        facePolysThread.push_back(pair<vector<long>, RingElem>(inExSimplData[j].degrees, GRest[j]));
        // verboseOutput() << " Now " << facePolysThread.size() << endl;
    }
}

template <typename Number>
RingElem affineLinearSubstitutionFL(const ourFactorization& FF,
                                    const vector<vector<Number> >& A,
                                    const vector<Number>& b,
                                    const Number& denom,
                                    const SparsePolyRing& R,
                                    const vector<Number>& degrees,
                                    const BigInt& lcmDets,
                                    vector<SIMPLINEXDATA_INT>& inExSimplData,
                                    deque<pair<vector<Number>, RingElem> >& facePolysThread) {
    // applies linear substitution y --> lcmDets*A(y+b/denom) to all factors in FF
    // and returns the product of the modified factorsafter ordering the exponent vectors

    size_t i;
    size_t m = A.size();
    size_t dim = m;  // TO DO: eliminate this duplication
    vector<RingElem> v(m, zero(R));
    RingElem g(zero(R));

    for (i = 0; i < m; i++) {
        g = b[i] * (lcmDets / denom);
        v[i] = g + lcmDets * indets(R)[i + 1];
    }
    vector<RingElem> w = VxM(v, A);
    vector<RingElem> w1(w.size() + 1, zero(R));
    w1[0] = RingElem(R, lcmDets);
    for (i = 1; i < w1.size(); ++i)
        w1[i] = w[i - 1];

    // RingHom phi=PolyAlgebraHom(R,R,w1);

    RingElem G1(zero(R));
    list<RingElem> sortedFactors;
    for (i = 0; i < FF.myFactors.size(); ++i) {
        // G1=phi(FF.myFactors[i]);
        G1 = mySubstitution(FF.myFactors[i], w1);
        for (int nn = 0; nn < FF.myMultiplicities[i]; ++nn)
            sortedFactors.push_back(G1);
    }

    sortedFactors.sort(compareLength);

    RingElem G(one(R));

    for (const auto& sf : sortedFactors)
        G *= sf;

    if (inExSimplData.size() == 0) {  // not really necessary, but a slight shortcut
        dynamic_bitset dummyInd;
        return (orderExpos(G, degrees, dummyInd, false));
    }

    // if(inExSimplData.size()!=0){
    long Deg = 0;
    dynamic_bitset indicator(dim);  // indicates the non-zero components of b
    indicator.reset();
    for (size_t i = 0; i < dim; ++i)
        if (b[i] != 0) {
            indicator.set(i);
            Deg += degrees[i] * b[i];
        }
    Deg /= denom;
    RingElem Gorder(zero(R));
    all_contained_faces(G, Gorder, degrees, indicator, Deg, inExSimplData, facePolysThread);
    return (Gorder);
    // }
}

inline vector<RingElem> homogComps(const RingElem& F) {
    // returns the vector of homogeneous components of F
    // w.r.t. standard grading

    const SparsePolyRing& P = owner(F);
    long dim = NumIndets(P);
    vector<long> v(dim);
    vector<RingElem> c(deg(F) + 1, zero(P));
    long j, k;

    // TODO there is a leading_term() function coming in cocoalib
    // TODO maybe there will be even a "splice_leading_term"
    SparsePolyIter i = BeginIter(F);
    for (; !IsEnded(i); ++i) {
        exponents(v, PP(i));
        k = 0;
        for (j = 0; j < dim; j++)
            k += v[j];
        PushBack(c[k], coeff(i), v);
    }
    return (c);
}

inline RingElem homogenize(const RingElem& F) {
    // homogenizes F wrt the zeroth variable and returns the
    // homogenized polynomial

    SparsePolyRing P = owner(F);
    int d = deg(F);
    vector<RingElem> c(d + 1, zero(P));
    c = homogComps(F);
    RingElem h(zero(P));
    for (int i = 0; i <= d; ++i)
        h += c[i] * power(indets(P)[0], d - i);
    return (h);
}

inline RingElem makeZZCoeff(const RingElem& F, const SparsePolyRing& RZZ) {
    // F is a polynomial over RingQQ with integral coefficients
    // This function converts it into a polynomial over RingZZ

    SparsePolyIter mon = BeginIter(F);  // go over the given polynomial
    RingElem G(zero(RZZ));
    for (; !IsEnded(mon); ++mon) {
        // cout << num(coeff(mon)) << endl;
        vector<long> v;
        exponents(v, PP(mon));
        PushBack(G, num(coeff(mon)), v);
    }
    return (G);
}

inline RingElem makeQQCoeff(const RingElem& F, const SparsePolyRing& R) {
    // F is a polynomial over RingZZ
    // This function converts it into a polynomial over RingQQ
    SparsePolyIter mon = BeginIter(F);  // go over the given polynomial
    RingElem G(zero(R));
    for (; !IsEnded(mon); ++mon) {
        PushBack(G, RingElem(RingQQ(), coeff(mon)), PP(mon));
    }
    return (G);
}

inline CyclRatFunct genFunct(const vector<vector<CyclRatFunct> >& GFP, const RingElem& F, const vector<long>& degrees)
// writes \sum_{x\in\ZZ_+^n} f(x,t) T^x
// under the specialization T_i --> t^g_i
// as a rational function in t
{
    const SparsePolyRing& P = owner(F);
    RingElem t = indets(P)[0];

    CyclRatFunct s(F);  // F/1

    CyclRatFunct g(zero(P)), h(zero(P));

    long nd = degrees.size();
    long i, k, mg;
    vector<RingElem> c;

    for (k = 1; k <= nd; k++) {
        c = ourCoeffs(s.num, k);  // we split the numerator according
                                  // to powers of var k
        mg = c.size();            // max degree+1 in  var k

        h.set2(zero(P));
        for (i = 0; i < mg; i++)  // now we replace the powers of var k
        {                         // by the corresponding rational function,
                                  // multiply, and sum the products

            h.num = (1 - power(t, degrees[k - 1])) * h.num + GFP[degrees[k - 1]][i].num * c[i];
            h.denom = GFP[degrees[k - 1]][i].denom;
        }
        s.num = h.num;
        s.denom = prodDenom(s.denom, h.denom);
    }
    return (s);
}

inline vector<RingElem> power2ascFact(const SparsePolyRing& P, const long& k)
// computes the representation of the power x^n as the linear combination
// of (x+1)_n,...,(x+1)_0
// return value is the vector of coefficients (they belong to ZZ)
{
    RingElem t = indets(P)[0];
    const vector<long> ONE(NumIndets(P));
    RingElem f(P), g(P), h(P);
    f = power(t, k);
    long m;
    vector<RingElem> c(k + 1, zero(P));
    while (f != 0) {
        m = deg(f);
        h = monomial(P, LC(f), ONE);
        c[m] = h;
        f -= h * ascFact(t, m);
    }
    return (c);
}

inline CyclRatFunct genFunctPower1(const SparsePolyRing& P, long k, long n)
// computes the generating function for
//  \sum_j j^n (t^k)^j
{
    vector<RingElem> a = power2ascFact(P, n);
    RingElem b(P);
    vector<long> u;
    CyclRatFunct g(zero(P)), h(zero(P));
    long i, s = a.size();
    for (i = 0; i < s; ++i) {
        u = makeDenom(k, i + 1);
        b = a[i] * factorial(i);
        g.set2(b, u);
        h.addCRF(g);
    }
    return (h);
}

inline void CyclRatFunct::extendDenom(const vector<long>& target)
// extends the denominator to target
// by multiplying the numrerator with the remaining factor
{
    RingElem t = indets(owner(num))[0];
    long i, ns = target.size(), nf = denom.size();
    for (i = 1; i < ns; ++i) {
        if (i > nf - 1)
            num *= power(1 - power(t, i), (target[i]));
        else if (target[i] > denom[i])
            num *= power(1 - power(t, i), (target[i] - denom[i]));
    }
    denom = target;
}

inline vector<long> lcmDenom(const vector<long>& df, const vector<long>& dg) {
    // computes the lcm of ztwo denominators as used in CyclRatFunct
    // (1-t^i and 1-t^j, i != j, are considered as coprime)
    size_t nf = df.size(), ng = dg.size(), i;
    size_t n = max(nf, ng);
    vector<long> dh = df;
    dh.resize(n);
    for (i = 1; i < n; ++i)
        if (i < ng && dh[i] < dg[i])
            dh[i] = dg[i];
    return (dh);
}

inline vector<long> prodDenom(const vector<long>& df, const vector<long>& dg) {
    // as above, but computes the profduct
    size_t nf = df.size(), ng = dg.size(), i;
    size_t n = max(nf, ng);
    vector<long> dh = df;
    dh.resize(n);
    for (i = 1; i < n; ++i)
        if (i < ng)
            dh[i] += dg[i];
    return (dh);
}

inline vector<long> degrees2denom(const vector<long>& d) {
    // converts a vector of degrees to a "denominator"
    // listing at position i the multiplicity of i in d
    long m = 0;
    size_t i;
    if (d.size() == 0)
        return vector<long>(0);
    for (i = 0; i < d.size(); ++i)
        m = max(m, d[i]);
    vector<long> e(m + 1);
    for (i = 0; i < d.size(); ++i)
        e[d[i]]++;
    return (e);
}

inline vector<long> denom2degrees(const vector<long>& d) {
    // the converse operation
    vector<long> denomDeg;
    for (size_t i = 0; i < d.size(); ++i)
        for (long j = 0; j < d[i]; ++j)
            denomDeg.push_back(i);
    return (denomDeg);
}

inline RingElem denom2poly(const SparsePolyRing& P, const vector<long>& d) {
    // converts a denominator into a real polynomial
    // the variable for the denominator is x[0]
    RingElem t = indets(P)[0];
    RingElem f(one(P));
    for (size_t i = 1; i < d.size(); ++i)
        f *= power(1 - power(t, i), d[i]);
    return (f);
}

inline vector<long> makeDenom(long k, long n)
// makes the denominator (1-t^k)^n
{
    vector<long> d(k + 1);
    d[k] = n;
    return (d);
}

inline void CyclRatFunct::addCRF(const CyclRatFunct& r) {
    // adds r to *this, r is preserved in its given form
    CyclRatFunct s(zero(owner(num)));
    const vector<long> lcmden(lcmDenom(denom, r.denom));
    s = r;
    s.extendDenom(lcmden);
    extendDenom(lcmden);
    num += s.num;
}

/*
void CyclRatFunct::multCRF(const CyclRatFunct& r) {
    // nmultiplies *this by r
    num *= r.num;
    denom = prodDenom(denom, r.denom);
}
*/

inline void CyclRatFunct::showCRF() {
    if (!verbose_INT)
        return;

    verboseOutput() << num << endl;
    for (size_t i = 1; i < denom.size(); ++i)
        verboseOutput() << denom[i] << " ";
    verboseOutput() << endl;
}

inline void CyclRatFunct::showCoprimeCRF() {
    // shows *this also with coprime numerator and denominator
    // makes only sense if only x[0] appears in the numerator (not checked)

    if (!verbose_INT)
        return;

    verboseOutput() << "--------------------------------------------" << endl << endl;
    verboseOutput() << "Given form" << endl << endl;
    showCRF();
    verboseOutput() << endl;
    const SparsePolyRing& R = owner(num);
    SparsePolyRing P = NewPolyRing_DMPI(RingQQ(), symbols("t"));
    vector<RingElem> Im(NumIndets(R), zero(P));
    Im[0] = indets(P)[0];
    RingHom phi = PolyAlgebraHom(R, P, Im);
    RingElem f(phi(num));
    RingElem g(denom2poly(P, denom));
    RingElem h = CoCoA::gcd(f, g);
    f /= h;
    g /= h;
    verboseOutput() << "Coprime numerator (for denom with remaining factor 1)" << endl << endl;
    factorization<RingElem> gf = factor(g);
    verboseOutput() << f / gf.myRemainingFactor() << endl << endl << "Factorization of denominator" << endl << endl;
    size_t nf = gf.myFactors().size();
    for (size_t i = 0; i < nf; ++i)
        verboseOutput() << gf.myFactors()[i] << "  mult " << gf.myMultiplicities()[i] << endl;
    verboseOutput() << "--------------------------------------------" << endl;
}

inline void CyclRatFunct::simplifyCRF() {
    // cancels factors 1-t^i from the denominator that appear there explicitly
    // (and not just as factors of 1-t^j for some j)

    const SparsePolyRing& R = owner(num);
    long nd = denom.size();
    for (long i = 1; i < nd; i++) {
        while (denom[i] > 0) {
            if (!IsDivisible(num, 1 - power(indets(R)[0], i)))
                break;
            num /= 1 - power(indets(R)[0], i);
            denom[i]--;
        }
    }
}

inline void CyclRatFunct::set2(const RingElem& f, const vector<long>& d) {
    num = f;
    denom = d;
}

inline void CyclRatFunct::set2(const RingElem& f) {
    num = f;
    denom.resize(1, 0);
}

inline CyclRatFunct::CyclRatFunct(const RingElem& c)
    : num(c)
// constructor starting from a RingElem
// initialization necessary because RingElem has no default
// constructor
{
    denom.resize(1, 0);
}

inline CyclRatFunct::CyclRatFunct(const RingElem& c, const vector<long>& d) : num(c), denom(d) {
}
//--------------------------------------

struct PolynomialData {
    ourFactorization FF;
    bool homogeneous;
    long degree;
    vector<BigInt> Factorial;
    vector<BigInt> FactQuot;
    long dimension;

    RingElem F;
};

#endif  // NMZ_COCOA

}  // end namespace libnormaliz


#endif  // NMZ_INTEGRATE_H