File: polynomials.gi

package info (click to toggle)
gap-hap 1.66%2Bds-1
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 55,348 kB
  • sloc: xml: 15,368; sh: 216; javascript: 155; makefile: 126; ansic: 57; perl: 36
file content (1001 lines) | stat: -rw-r--r-- 34,402 bytes parent folder | download | duplicates (3)
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
#############################################################################
##
##  HAPPRIME - polynomials.gi
##  Functions, Operations and Methods to extend GAP's polynomials
##  Paul Smith
##
##  Copyright (C)  2008
##  Paul Smith
##  National University of Ireland Galway
##
##  This file is part of HAPprime. 
## 
##  HAPprime 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 2 of the License, or
##  (at your option) any later version.
## 
##  HAPprime 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/>.
## 
##
#############################################################################


#####################################################################
##  <#GAPDoc Label="TermsOfPolynomial_manPolynomial">
##  <ManSection>
##  <Attr Name="TermsOfPolynomial" Arg="poly"/>
##
##  <Returns>
##    List of pairs
##  </Returns>
##  <Description>
##  Returns a list of the terms in the polynomial.
##  This list is a list of pairs of the form <C>[mon, coeff]</C> where
##  <C>mon</C> is a monomial and <C>coeff</C> is the coefficient of that
##  monomial in the polynomial. The monomials are sorted according to
##  the total degree/lexicographic order (the same as the in 
##  <Ref Oper="MonomialGrLexOrdering" BookName="ref"/>).
##  </Description>
##  </ManSection>
##  <#/GAPDoc>
#####################################################################
InstallMethod(TermsOfPolynomial,
  [IsPolynomial],
  function(poly)
  
    local terms, extrep, fam, one, i, term, mon, coeff;
    
    terms := [];
    extrep := ExtRepPolynomialRatFun(poly);
    if IsEmpty(extrep) then
      return [];
    fi;
    
    fam := FamilyObj(poly);
    i := 1;
    repeat
      mon := extrep[i];
      coeff := extrep[i+1];
      
      term := [PolynomialByExtRep(fam, [mon, One(coeff)]), coeff];
      Add(terms, term);
      i := i + 2;
    until i > Length(extrep);
    return terms;
  end
);        
#####################################################################


#####################################################################
##  <#GAPDoc Label="IsMonomial_manPolynomial">
##  <ManSection>
##  <Attr Name="IsMonomial" Arg="poly" Label="for polynomial"/>
##
##  <Returns>
##    Boolean
##  </Returns>
##  <Description>
##  Returns <K>true</K> if <A>poly</A> is a monomial, i.e. the polynomial 
##  contains only one term.
##  </Description>
##  </ManSection>
##  <#/GAPDoc>
#####################################################################
InstallMethod(IsMonomial,
  "for polynomial",
  [IsPolynomial],
  function(poly)
  
    return Length(ExtRepPolynomialRatFun(poly)) = 2;
  end
);        
#####################################################################


#####################################################################
##  <#GAPDoc Label="UnivariateMonomialsOfMonomial_manPolynomial">
##  <ManSection>
##  <Attr Name="UnivariateMonomialsOfMonomial" Arg="mon"/>
##
##  <Returns>
##    List
##  </Returns>
##  <Description>
##  Returns a list of the univariate monomials of the largest order 
##  whose product equals <A>mon</A>.
##  The univariate monomials are sorted according to
##  their indeterminate number.
##  </Description>
##  </ManSection>
##  <#/GAPDoc>
#####################################################################
InstallMethod(UnivariateMonomialsOfMonomial,
  [IsPolynomial],
  function(poly)

    local extrep, fam, coeffring, one, mon, i, unimons;

    if IsOne(poly) or IsZero(poly) then
      return [poly];
    fi;

    extrep := ExtRepPolynomialRatFun(poly);
    fam := FamilyObj(poly);
    coeffring := Ring(extrep[2]);
    one := One(coeffring);
    
    if Length(extrep) > 2 or extrep[2] <> one then
      Error("<mon> must be a monomial");
    fi;

    mon := extrep[1];

    i := 1;
    unimons := [];
    repeat
      Add(unimons, PolynomialByExtRep(fam, [[mon[i], mon[i+1]], one]));
      i := i + 2;
    until i > Length(mon);
    
    return unimons;
  end
);        
#####################################################################
  

#####################################################################
##  <#GAPDoc Label="IndeterminateAndExponentOfUnivariateMonomial_manPolynomial">
##  <ManSection>
##  <Attr Name="IndeterminateAndExponentOfUnivariateMonomial" Arg="mon"/>
##
##  <Returns>
##    List
##  </Returns>
##  <Description>
##  Returns a list <C>[indet, exp]</C> where <C>indet</C> is the indeterminate 
##  of the univariate monomial <A>mon</A> and <C>exp</C> is the exponent
##  of that indeterminate in the monomial. If <A>mon</A> is an element in the 
##  coefficient ring (i.e. the monomial contains no indeterminates) then the 
##  first element will be <A>mon</A> with an exponent of zero.
##  If <A>mon</A> is not a univariate monomial, then <K>fail</K> is returned.
##  </Description>
##  </ManSection>
##  <#/GAPDoc>
#####################################################################
InstallMethod(IndeterminateAndExponentOfUnivariateMonomial,
  [IsPolynomial],
  function(poly)

    local extrep, one, fam;

    extrep := ExtRepPolynomialRatFun(poly);
    if IsEmpty(extrep[1]) then
      return [extrep[2], 0];
    fi;


    if Length(extrep) <> 2 then
      return fail;
    fi;
    one := One(extrep[2]);
    fam := FamilyObj(poly);

    return [PolynomialByExtRep(fam, [[extrep[1][1], 1], one]), extrep[1][2]];
  end
);        
#####################################################################
  
  


#####################################################################
##  <#GAPDoc Label="IndeterminatesOfPolynomial_manPolynomial">
##  <ManSection>
##  <Attr Name="IndeterminatesOfPolynomial" Arg="poly"/>
##
##  <Returns>
##    List
##  </Returns>
##  <Description>
##  Returns a list of the indeterminates used in the polynomial <A>poly</A>.
##  </Description>
##  </ManSection>
##  <#/GAPDoc>
#####################################################################
InstallMethod(IndeterminatesOfPolynomial,
  [IsPolynomial],
  function(poly)

    # Is it a constant?
    if IsEmpty(ExtRepPolynomialRatFun(poly)) or 
      IsEmpty(ExtRepPolynomialRatFun(poly)[1]) then
        return [];
    else
      return IndeterminatesOfPolynomialRing(DefaultRing(poly));
    fi;
  end
);        
#####################################################################
  
  


#####################################################################
##  <#GAPDoc Label="ReduceIdeal_manPolynomial">
##  <ManSection>
##  <Heading>ReduceIdeal</Heading>
##  <Oper Name="ReduceIdeal" Arg="I, O" Label="for Ideal"/>
##  <Oper Name="ReduceIdeal" Arg="rels, O" Label="for list of relations"/>
##
##  <Returns>
##    Ideal or list 
##  </Returns>
##  <Description>
##  For an ideal <A>I</A> returns an ideal containing a reduced generating set 
##  for the ideal, i.e. one in which no monomial in a relation in <A>I</A> is 
##  divisible by the leading term of another polynomial in <A>I</A>. 
##  The monomial ordering to be used is
##  specified by <A>O</A> (see <Ref Sect="Monomial Orderings" BookName="ref"/>).
##  The ideal can instead be specified by a list of relations <A>rels</A>, 
##  in which case a reduced list of relations is returned.
##  </Description>
##  </ManSection>
##  <#/GAPDoc>
#####################################################################
InstallOtherMethod(ReduceIdeal, 
  "for empty ideal",
  [IsEmpty, IsMonomialOrdering],
  function(ideal, order)
    return [];
  end
);
#####################################################################
InstallOtherMethod(ReduceIdeal, 
  "for Ideal",
  [IsPolynomialRingIdeal, IsMonomialOrdering],
  function(ideal, order)
    return Ideal(
      LeftActingRingOfIdeal(ideal), 
      ReduceIdeal(GeneratorsOfIdeal(ideal), order));
  end
);
#####################################################################
InstallMethod(ReduceIdeal, 
  "for list of relations",
  [IsHomogeneousList and IsRationalFunctionCollection, IsMonomialOrdering],
  function(I, order)

    local ideal, i, len, changed, poly;

    ideal := ShallowCopy(I);

    len := Length(ideal);
    repeat
      i := 1;
      changed := false;
      while i <= len do
        poly := PolynomialReducedRemainder(
          ideal[i], ideal{Difference([1..len],[i])}, order);
        if poly <> ideal[i] then 
          changed := true;
        fi;
        if IsZero(poly) then
          ideal[i] := ideal[len];
          Unbind(ideal[len]);
          len := len - 1;
        else 
          ideal[i] := poly;
          i := i + 1;
        fi;
      od;
    until not changed;
    for i in [1..Length(ideal)] do
      ideal[i] := ideal[i] / LeadingCoefficientOfPolynomial(ideal[i], order);
    od;

    return ideal;

  end
);
#####################################################################


#####################################################################
##  <#GAPDoc Label="ReducedPolynomialRingPresentation_manPolynomial">
##  <ManSection>
##  <Oper Name="ReducedPolynomialRingPresentation" Arg="R, I[, avoid]"/>
##  <Oper Name="ReducedPolynomialRingPresentationMap" Arg="R, I[, avoid]"/>
##
##  <Returns>
##    List
##  </Returns>
##  <Description>
##  For a polynomial ring <A>R</A> and a list of relations <A>I</A> in that 
##  ring, returns a list <C>[S, J]</C> representing a polynomial quotient ring 
##  <M>S/J</M> which is isomorphic to the ring <M>R/I</M>, but which involves 
##  the minimal number of ring indeterminates. The indeterminates in <C>S</C>
##  will be distinct from thise in <A>R</A>, and an optional argument 
##  <A>avoid</A> can be used to give a list of further indeterminates to avoid 
##  when creating the ring <C>S</C>.
##  <P/>
##  The extended version of this function,
##  <Ref Oper="ReducedPolynomialRingPresentationMap"/>, returns an additional
##  third element to the list, which contains two lists giving the mapping 
##  between the new ring indeterminates and the old ring indeterminates. The
##  first list is of polynomials in the original indeterminates, the 
##  second the equivalent polynomials in the new ring indeterminates.
##  </Description>
##  </ManSection>
##  <#/GAPDoc>
#####################################################################
InstallMethod(ReducedPolynomialRingPresentation, 
  "with nothing to avoid",
  [IsPolynomialRing, IsHomogeneousList and IsRationalFunctionCollection, IsHomogeneousList],
  function(ring, I, avoid)
    return ReducedPolynomialRingPresentationMap(ring, I, avoid){[1..2]};
  end
  );
#####################################################################
InstallOtherMethod(ReducedPolynomialRingPresentation, 
  "with nothing to avoid",
  [IsPolynomialRing, IsHomogeneousList and IsRationalFunctionCollection],
  function(ring, I)
    return ReducedPolynomialRingPresentationMap(ring, I, []){[1..2]};
  end
  );
#####################################################################
InstallOtherMethod(ReducedPolynomialRingPresentation, 
  "for empty relations",
  [IsPolynomialRing, IsEmpty, IsHomogeneousList],
  function(ring, I, avoid)
    local newring, newrelations, map;
    # Just copy the ring
    newring := PolynomialRing(
      CoefficientsRing(ring), Length(IndeterminatesOfPolynomialRing(ring)),
      Concatenation(avoid, IndeterminatesOfPolynomialRing(ring)));

    newrelations := HAPPRIME_SwitchPolynomialIndeterminates(
      ring, newring, I);

      return [newring, newrelations ];
  end
  );
#####################################################################
InstallOtherMethod(ReducedPolynomialRingPresentationMap, 
  "with nothing to avoid",
  [IsPolynomialRing, IsHomogeneousList and IsRationalFunctionCollection],
  function(ring, I)
    return ReducedPolynomialRingPresentationMap(ring, I, []);
  end
  );
#####################################################################
InstallOtherMethod(ReducedPolynomialRingPresentationMap, 
  "for empty relations and nothing to avoid",
  [IsPolynomialRing, IsEmpty],
  function(ring, I)
    return ReducedPolynomialRingPresentationMap(ring, I, []);
  end
  );
#####################################################################
InstallOtherMethod(ReducedPolynomialRingPresentationMap, 
  "for empty relations",
  [IsPolynomialRing, IsEmpty, IsHomogeneousList],
  function(ring, I, avoid)
    local newring, newrelations, map;
    # Just copy the ring
    newring := PolynomialRing(
      CoefficientsRing(ring), Length(IndeterminatesOfPolynomialRing(ring)),
      Concatenation(avoid, IndeterminatesOfPolynomialRing(ring)));

    newrelations := HAPPRIME_SwitchPolynomialIndeterminates(
      ring, newring, I);

      return [newring, newrelations, 
        [IndeterminatesOfPolynomialRing(ring), 
          IndeterminatesOfPolynomialRing(newring)] ];
  end
  );
#####################################################################
InstallMethod(ReducedPolynomialRingPresentationMap, 
  [IsPolynomialRing, IsHomogeneousList and IsRationalFunctionCollection, IsHomogeneousList],
  function(ring, I, avoid)

    local ideal, indetorder, order, indet, i, t, t2, indets, allindets, ord,
    removedindets, removedrelations, removedring, unimons, relation, poly, len, 
    nomod, newring, newrelations, map;

    # Check for unit in the ideal - if so, we return a trivial ring
    if Length(I) = 1 and IsOne(I[1]) then
      return [Ring(Zero(I[1])), [ ] ];
    fi;

    ideal := ShallowCopy(I);
    # indeterminates will be removed from indets and added to removedindets
    # as we do our reduction 
    indets := ShallowCopy(IndeterminatesOfPolynomialRing(ring));
    removedindets := [];
    removedrelations := [];

    # Are any of the leading terms of the ideal single indeterminates?
    # If so then we can (after reduction) remove those indeterminates and those 
    # terms of the ideal
    repeat
      indet := false;
      # Find a relation in the ideal that involves a solitary indeterminate
      for i in [1..Length(ideal)] do
        for t in TermsOfPolynomial(ideal[i]) do
          unimons := UnivariateMonomialsOfMonomial(t[1]);
          if Length(unimons) = 1 and unimons[1] = IndeterminateOfUnivariateRationalFunction(unimons[1]) then
            # This term involves a solitary indeterminate. 
            # Check that no other term in this relation also involves this 
            # indeterminate
            indet := unimons[1];
            for t2 in TermsOfPolynomial(ideal[i] - t[1]*t[2]) do
              if IsOne(DenominatorOfRationalFunction(t2[1] / indet)) then
                indet := false;
                break;
              fi;
            od;
            if indet <> false then
              # We're OK - no other term in this relation involves this
              # indeterminate, so we can go on to remove it
              break;
            fi;
          fi;
        od;
        # Have we found a solitary indeterminate?
        if indet <> false then
          relation := Remove(ideal, i);
          Add(removedrelations, relation);
          break;
        fi;
      od;

      if indet <> false then
        # Remove this indeterminate from the indets list and put it
        # onto the removedindets list instead
        Remove(indets, Position(indets, indet));
        Add(removedindets, indet);

        # And create a new (temporary) order which has indet as the most important
        order := MonomialLexOrdering(Concatenation([indet], indets));
        # And make sure that our relation has a unit coefficient
        relation := relation / LeadingCoefficientOfPolynomial(relation, order);
  
        # Now reduce all the other relations in the ideal with this one, which
        # will get rid of this indeterminate
        i := 1;
        len := Length(ideal);
        while i <= len do
          poly := PolynomialReducedRemainder(ideal[i], [relation], order);
          if IsZero(poly) then
            ideal[i] := ideal[len];
            Unbind(ideal[len]);
            len := len - 1;
          else
            ideal[i] := poly;
            i := i + 1;
          fi;
        od;
      fi;
    until indet = false;
  
    # Tidy up the ideal
    ideal := ReduceIdeal(ideal, MonomialLexOrdering());

    # Now make the new ring and convert the indeterminates
    newring := PolynomialRing(CoefficientsRing(ring), Length(indets),
      Concatenation(avoid, IndeterminatesOfPolynomialRing(ring)));
    newrelations := HAPPRIME_SwitchPolynomialIndeterminates(
      PolynomialRing(CoefficientsRing(ring), indets), newring, ideal);
    # And remember the mapping
    map := [indets, ShallowCopy(IndeterminatesOfPolynomialRing(newring))];

    if not IsEmpty(removedrelations) then
      # Finally, sort out the map between the old and new indeterminates
      # for the removed ones
      # Start off by adding relations that tell us what the new indeterminates are
      ideal := IndeterminatesOfPolynomialRing(newring) - indets;
      # Now add the relations that we have removed
      Append(ideal, removedrelations);
      # Create an ordering that puts the removed relations largest so that 
      # they will be removed as much as possible, and the new relations smallest
      # so they will be kept, then reduce this set of relations
      ord := MonomialLexOrdering(Concatenation(removedindets, indets, 
        IndeterminatesOfPolynomialRing(newring)));
      ideal := ReduceIdeal(ideal, ord);

      # Now add to the map. The polynomials in the ideal that have
      # leading monomials which still involve the removedindets are the ones
      # we want
      removedring := PolynomialRing(CoefficientsRing(ring), removedindets);
      for i in ideal do
        if LeadingMonomialOfPolynomial(i, ord) in removedring then
          # is this a single indeterminate (i.e. a relation of the form x_i)?
          if Length(TermsOfPolynomial(i)) = 1 then
            poly := [[i], [Zero(i)]];
          else
            poly := [[], []];
            for t in TermsOfPolynomial(i) do
              if t[1] in ring then 
                Add(poly[1], t[1]*t[2]);
              elif t[1] in newring then
                Add(poly[2], t[1]*t[2]);
              else
                Error("Relation is not seperable when creating map. Please consult the package maintainer.");
              fi;
            od;
            if IsEmpty(poly[2]) then
              Error("Unexpected relation with no new indeterminates. Please consult the package maintainer.");
            fi;
          fi;
          Add(map[1], Sum(poly[1]));
          Add(map[2], Sum(poly[2]));
        fi;
      od;
    fi;

    return [newring, newrelations, map];
  
  end
);
#####################################################################


#################################
## This functions new to GAP 4.4.10
## so needs defining if using an earlier version
if not IsBound(EmptyPlist) then
  EmptyPlist := function(n)
    return [];
  end;
fi;
#################################


#####################################################################
##  <#GAPDoc Label="HAPPRIME_SwitchPolynomialIndeterminates_manDTPolynomialInt">
##  <ManSection>
##  <Func Name="HAPPRIME_SwitchPolynomialIndeterminates" Arg="R, S, poly"/>
##
##  <Returns>
##    Polynomial or List of Polynomials
##  </Returns>
##  <Description>
##  Changes the indeterminates in <A>poly</A>, which should be a polynomial or 
##  a list of polynomials, substituting the indeterminates of the polynomial 
##  ring <A>S</A> one-for-one for those in <A>R</A> (from which all polynomials 
##  in <A>poly</A> must come). The returned object is either a polynomial or a 
##  list of polynomials in the new indeterminates, depending on the input object.
##  <P/>
##  See <Ref Func="HAPPRIME_MapPolynomialIndeterminates"/> for a function that
##  can work with more general indeterminate maps.
##  </Description>
##  </ManSection>
##  <#/GAPDoc>
#####################################################################
InstallGlobalFunction(HAPPRIME_SwitchPolynomialIndeterminates,
  function(R, S, polys)
    local onepoly, Rindetnums, Sindetnums, newpolys, fam, p, extrep, extrepnew,
      i, j, e;
    
    if CoefficientsRing(R) <> CoefficientsRing(S) then
      Error("<R> and <S> must have the same coefficient ring");
    fi;

    onepoly := false;
    if Length(IndeterminatesOfPolynomialRing(R)) <> 
      Length(IndeterminatesOfPolynomialRing(S)) then 
      Error("<R> and <S> must have the same number of indeterminates");
    fi;
    
    if IsEmpty(polys) then
      return polys;
    fi;
    if IsPolynomial(polys) then
      polys := [polys];
      onepoly := true;
    elif not IsHomogeneousList(polys) then
      Error("<polys> must be a polynomial or a list of polynomials in <R>");
    fi;

    Rindetnums := List(IndeterminatesOfPolynomialRing(R), 
      IndeterminateNumberOfUnivariateRationalFunction);
    Sindetnums := List(IndeterminatesOfPolynomialRing(S), 
      IndeterminateNumberOfUnivariateRationalFunction);

    newpolys := EmptyPlist(Length(polys));
    fam := FamilyObj(polys[1]);
    for p in polys do
      if not p in R then
        Error("<polys> must be a polynomial or a list of polynomials in <R>");
      fi;

      extrep := ExtRepPolynomialRatFun(p);
      extrepnew := EmptyPlist(Length(extrep));
      i := 1;
      repeat
        e := ShallowCopy(extrep[i]);
        if not IsEmpty(e) then
          j := 1;
          repeat
            # Swap the indeterminate number
            e[j] := Sindetnums[Position(Rindetnums, e[j])];
            j := j+2;
          until j > Length(e);
        fi;
        Add(extrepnew, e);
        Add(extrepnew, extrep[i+1]);
        i := i+2;
      until i > Length(extrep);
      Add(newpolys, PolynomialByExtRep(fam, extrepnew));
    od;


    # And sort out the return type
    if onepoly then
      return newpolys[1];
    else
      return newpolys;
    fi;
end);
######################################################


#####################################################################
##  <#GAPDoc Label="HAPPRIME_MapPolynomialIndeterminates_manDTPolynomialInt">
##  <ManSection>
##  <Func Name="HAPPRIME_MapPolynomialIndeterminates" Arg="old, new, poly"/>
##
##  <Returns>
##    Polynomial or List of Polynomials
##  </Returns>
##  <Description>
##  Changes the indeterminates in <A>poly</A>, which can be a polynomial or a 
##  list of polynomials, substituting the polynomials in <A>old</A> for those
##  in <A>new</A>. The returned object is either a polynomial or a list of 
##  polynomials in the new indeterminates, depending on the input object.
##  The change of variable arguments, <A>old</A> and <A>new</A>, do not 
##  have to be simply indeterminates: they can be can be lists of polynomials 
##  which are equivalent in the two different sets of indeterminates.
##  If a polynomial cannot be converted (i.e. if it cannot be generated from the
##  polynomials in <A>old</A>) then <K>fail</K> is returned for that polynomial.
##  </Description>
##  </ManSection>
##  <#/GAPDoc>
#####################################################################
InstallGlobalFunction(HAPPRIME_MapPolynomialIndeterminates,
  function(old, new, polys)
  
    local old2, new2, oldindets, newindets, ord, newpolys, p, newp, onepoly, I, 
      lead, newI, ord2;
    
    onepoly := false;
    if not IsHomogeneousList(old) then
      Error("<old> must a list of polynomials");
    fi;
    if not IsHomogeneousList(new) then
      Error("<new> must a list of polynomials");
    fi;
    if Length(old) <> Length(new) then
      Error("<old> and <new> must both be the same length");
    fi;
    if IsPolynomial(polys) then
      polys := [polys];
      onepoly := true;
    elif not IsHomogeneousList(polys) then
      Error("<polys> must be a polynomial or a list of polynomials in the old indeterminates");
    fi;

    oldindets := [];
    for p in old do
      UniteSet(oldindets, IndeterminatesOfPolynomial(p));
    od;
    newindets := [];
    for p in new do
      UniteSet(newindets, IndeterminatesOfPolynomial(p));
    od;
    if not IsEmpty(Intersection(oldindets, newindets)) then
      Error("the indeterminates in <old> and <new> must be distinct sets.");
    fi;
  
    # Order the indeterminates so that the old ones are larger, so will
    # be replaced
    ord := MonomialLexOrdering(Concatenation(oldindets, newindets));

    # Are there any zeros in the old list? If so, remove them and the 
    # corresponding element of the new list
    old2 := ShallowCopy(old);
    new2 := ShallowCopy(new);
    repeat
      p := Position(old2, Zero(old2[1]));
      if p <> fail then
        Remove(old2, p);
        Remove(new2, p);
      fi;
    until p = fail;
    # The conversion is relations in an ideal. Make sure it is a GroebnerBasis
    I := HAPPRIME_SingularGroebnerBasis(old2 - new2, ord);
    
    # Extract out the relations in the new ring
    newI := Filtered(I, i->IsSubset(newindets, IndeterminatesOfPolynomial(i)));
    # and turn this into a Groebner Basis with grevlex ordering
    ## TODO I would like to make it MonomialGrevlexOrdering here,
    ## but a bug in that function (reported by me on 27/6/08) means
    ## that that ordering doesn't work!
    #ord2 := MonomialGrevlexOrdering(); 
    ord2 := MonomialGrlexOrdering();
    newI := HAPPRIME_SingularGroebnerBasis(newI, ord2);

    newpolys := [];
    for p in polys do
      # is this just a constant?
      lead := LeadingMonomial(p);
      if IsEmpty(lead) or lead[2] = infinity or lead[2] = 0 then
        Add(newpolys, p);
      else
        # Reduce this. Given the ordering, this will substitute the 
        # new in place of the old
        # We further reduce this using the grevlex ordering to get the 
        # simplest (smallest degree) form in the new indeterminates
        newp := PolynomialReducedRemainder(
          PolynomialReducedRemainder(p, I, ord), newI, ord2);
        # Check that this really is in the new indeterminates - it might not
        # be if the map is not complete
        if not IsSubset(newindets, IndeterminatesOfPolynomial(newp)) then
          Add(newpolys, fail);
        else
          Add(newpolys, newp);
        fi;
      fi;
    od;

    # And sort out the return type
    if onepoly then
      return newpolys[1];
    else
      return newpolys;
    fi;
end);
######################################################


#####################################################################
##  <#GAPDoc Label="HAPPRIME_CombineIndeterminateMaps_manDTPolynomialInt">
##  <ManSection>
##  <Func Name="HAPPRIME_CombineIndeterminateMaps" Arg="coeff, M, N"/>
##
##  <Returns>
##    List
##  </Returns>
##  <Description>
##  Returns the indeterminate map that results from applying map <A>M</A>
##  followed by map <A>N</A>. An indeterminate map is a list containing two
##  lists, the first of which is a list of polynomials in the original indeterminates, 
##  the second the equivalent polynomials in the new ring indeterminates.
##  </Description>
##  </ManSection>
##  <#/GAPDoc>
#####################################################################
InstallGlobalFunction(HAPPRIME_CombineIndeterminateMaps,
  function(M, N)
      
    local indetsA, indetsB, indetsC, i, ord, I, J, relations, map, poly, t;
    
    # Recreate the three rings
    indetsA := [];
    indetsB := [];
    indetsC := [];
        
    for i in M[1] do
      Append(indetsA, IndeterminatesOfPolynomial(i));
    od;
    indetsA := AsSet(indetsA);
    
    for i in M[2] do
      Append(indetsB, IndeterminatesOfPolynomial(i));
    od;
    indetsB := AsSet(indetsB);
    
    for i in N[1] do
      Append(indetsC, IndeterminatesOfPolynomial(i));
    od;
    indetsC := AsSet(indetsC);
    
    # Check that the second ring in M and the first in N are compatible
    if not IsSubset(indetsC, indetsB) and not IsSubset(indetsB, indetsC) then
      Error("the maps <M> and <N> are incompatible");
    fi;
        
    indetsC := [];
    for i in N[2] do
      Append(indetsC, IndeterminatesOfPolynomial(i));
    od;
    indetsC := AsSet(indetsC);
    
    # And check that the three rings are distinct
    if not IsEmpty(Intersection(indetsA, indetsB)) then 
      Error("the source and target rings of <M> are not different");
    fi;
    if not IsEmpty(Intersection(indetsA, indetsC)) then 
      Error("the source ring of <M> and target ring of <N> are not different");
    fi;
    if not IsEmpty(Intersection(indetsB, indetsC)) then 
      Error("the source and target rings of <N> are not different");
    fi;
        
    # Order the middle indeterminates first so that they are removed
    ord := MonomialLexOrdering(Concatenation(indetsB, indetsA, indetsC));
    # And get the two set of relations
    I := M[1] - M[2];
    J := N[1] - N[2];
    # Now reduce each with the other
    relations := [];
    for i in I do
      Add(relations, PolynomialReducedRemainder(i, J, ord));
    od;
    for i in J do
      Add(relations, PolynomialReducedRemainder(i, I, ord));
    od;
    relations := AsSet(relations);

    # Now create the new map. The polynomials in the ideal have
    # leading monomials which involve the source ring
    map := [[], []];
    for i in relations do
      poly := [[Zero(i)], [Zero(i)]];
      for t in TermsOfPolynomial(i) do
        if IsSubset(indetsA, IndeterminatesOfPolynomial(t[1])) then 
          Add(poly[1], t[1]*t[2]);
        elif IsSubset(indetsC, IndeterminatesOfPolynomial(t[1])) then
          Add(poly[2], t[1]*t[2]);
        else
          # This relation still involves an old indeterminate, so we
          # can't do anything with it - ignore it
          poly := "ignore";
          break;
        fi;
      od;
      if poly <> "ignore" then
        Add(map[1], Sum(poly[1]));
        Add(map[2], Sum(poly[2]));
      fi;
    od;

    return map;
end);
######################################################

    
#####################################################################
##  <#GAPDoc Label="HAPPRIME_SingularGroebnerBasis_manDTGroebnerInt">
##  <ManSection>
##  <Func Name="HAPPRIME_SingularGroebnerBasis" Arg="pols, O"/>
##  <Func Name="HAPPRIME_SingularReducedGroebnerBasis" Arg="pols, O"/>
##
##  <Returns>
##    List
##  </Returns>
##  <Description>
##  Returns the Gröbner basis (or reduced Gröbner basis) with respect to the 
##  ordering <A>O</A> for the ideal generated by the polynomials <A>pols</A>. 
##  This function uses the Gröbner basis implementation 
##  from &singular;, for preference, if available 
##  (<Ref Func="GroebnerBasis" BookName="singular"/>), and if so it also 
##  manuipulates the result to fix a bug in &singular; where the returned 
##  polynomials are not necessarily returned with a value external 
##  representation (see 
##  <Ref Sect="The Defining Attributes of Rational Functions" BookName="ref"/>).
##  <P/>
##  If the option <C>obeyGBASIS</C> is <K>true</K>, then this function will use 
##  whichever algorithm is specified by the <K>GBASIS</K> global variable
##  (see <Ref Var="SINGULARGBASIS" BookName="singular"/>).
##  </Description>
##  </ManSection>
##  <#/GAPDoc>
#####################################################################
#if LoadPackage("singular") = true then
if IsPackageMarkedForLoading("singular","0") then
  InstallGlobalFunction(HAPPRIME_SingularGroebnerBasis,
    function(pols, O)
      local gbasis, ideal, fam;

      # If empty, do nothing
      if IsEmpty(pols) then
        return pols;
      fi;
      # Make sure we use Singular
      gbasis := GBASIS;
      if not ValueOption("obeyGBASIS") = true then
        GBASIS := SINGULARGBASIS;
      fi;
      ideal := GroebnerBasis(pols, O);
      GBASIS := gbasis;

      # Hack to make sure that the polynomials returned by Singular are correct
      fam := FamilyObj(pols[1]);
      return List(ideal, x->PolynomialByExtRep(fam, ExtRepPolynomialRatFun(x)));;
    end
  ); 
  #####################################################################
  InstallGlobalFunction(HAPPRIME_SingularReducedGroebnerBasis,
    function(pols , O)
      local  ipr, mcf, R, I, input, out, fam;
 
    # If empty, do nothing
    if IsEmpty(pols) then
      return pols;
    fi;
    
    if ValueOption("obeyGBASIS") = true and GBASIS = GAPGBASIS then
      return ReducedGroebnerBasis(pols, O);
    fi;
    
    if IsPolynomialRingIdeal(pols)  then
      R := LeftActingRingOfIdeal(pols);
      pols := GeneratorsOfTwoSidedIdeal(pols);
    else
      R := DefaultRing(pols);
    fi;
    
    if IsMonomialOrdering(O) then
      ipr := ShallowCopy(IndeterminatesOfPolynomialRing(R));
      mcf := MonomialComparisonFunction(O);
      Sort(ipr, mcf);
      ipr := Reversed(ipr);
      R := PolynomialRing(LeftActingDomain(R), ipr);
    fi;
    
    if not(HasTermOrdering(R) and 
      IsIdenticalObj(TermOrdering(R), O)) then
        SetTermOrdering(R, O);
        SingularSetBaseRing(R);
    fi;
    
    I := Ideal(R, pols);

    # Now use Singular to find the Groebner Basis
    Info( InfoSingular, 2, "running GroebnerBasis..." );
  
    SingularCommand("", "option(redSB)");
    # preparing the input for Singular
    input := "";
  
    Append( input, "ideal GAP_groebner = simplify( groebner( " );
    Append( input, ParseGapIdealToSingIdeal( I ) );
    Append( input, " ), 1 );\n" );
  
    out := SingularCommand( input, "string (GAP_groebner)" );
  
    SingularCommand("", "option(noredSB)");
    Info( InfoSingular, 2, "done GroebnerBasis." );
  
    I := List( SplitString( out, ',' ), ParseSingPolyToGapPoly );
    
    # Hack to make sure that the polynomials returned by Singular are correct
    fam := FamilyObj(pols[1]);
    return List(I, x->PolynomialByExtRep(fam, ExtRepPolynomialRatFun(x)));;
  end );
else
  InstallGlobalFunction(HAPPRIME_SingularGroebnerBasis,
    function(I, O)
      return GroebnerBasis(I, O);
    end
  ); 
  InstallGlobalFunction(HAPPRIME_SingularReducedGroebnerBasis,
    function(I, O)
      return ReducedGroebnerBasis(I, O);
    end
  ); 
fi;
#####################################################################