source: git/Singular/LIB/multigrading.lib @ 087946

spielwiese
Last change on this file since 087946 was 087946, checked in by Oleksandr Motsak <motsak@…>, 14 years ago
*motsak: new library multigrading.lib git-svn-id: file:///usr/local/Singular/svn/trunk@13259 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 53.7 KB
Line 
1///////////////////////////////////////////////////////////////////
2Version="$Id: multigrading.lib 0.1 2010-02-13$";
3category="Combinatorial Commutative Algebra";
4info="
5LIBRARY:  multigrading.lib          Multigraded Rings
6
7AUTHORS:  Rene Birkner, rbirkner@math.fu-berlin.de
8@*        Lars Kastner, lkastner@math.fu-berlin.de
9@*        Oleksandr Motsak, U@D, where U={motsak}, D={mathematik.uni-kl.de}
10
11
12OVERVIEW: using this library allows one can virtually add multigrading to Singular.
13For more see http://code.google.com/p/convex-singular/wiki/Multigrading
14
15NOTE: this is a proof of concept using Singular attributes.
16
17REFERENCE:
18E. Miller, B. Sturmfels: 'Combinatorial Commutative Algebra',
19M. Kreuzer, L. Robbiano: 'Computational Commutative Algebra'
20
21KEYWORDS:  multigradeding, multidegree, multiweights, multigraded-homogenous
22
23
24REQUIRES: 4ti2 for Hilbert Bases needed for mDegBasis
25
26PROCEDURES:
27setBaseMultigrading(M,T) attach multiweights/torsion matrices to the basering
28getVariableWeights([R])  get matrix of multidegrees of vars attached to a ring
29getTorsion([R])          get torsion matrix attached to a ring
30
31setModuleGrading(M,v)    attach multiweights of units to a module and return it
32getModuleGrading(M)      get multiweights of module units (attached to M)
33
34mDeg(A)                  compute the multidegree of A
35mDegBasis(d)             compute all monomials of multidegree d
36mDegPartition(p)         compute the multigraded-homogenous components of p
37
38isTorsionFree()          test whether the current multigrading is torsion free
39isTorsionElement(p)      test whether p has zero multidegree
40isHomogenous(a)          test whether 'a' is multigraded-homogenous
41
42equalMDeg(e1,e2[,V])     test whether e1==e2 in the current multigrading
43
44mDegGroebner(M)          compute the multigraded GB/SB of M
45mDegSyzygy(M)            compute the multigraded syzygies of M
46mDegResolution(M,l[,m])  compute the multigraded resolution of M
47
48defineHomogenous(p)      get a torsion matrix wrt which p becomes homogenous
49pushForward(f)           find the finest grading on the image ring, homogenizing f
50
51hermite(A)               compute the Hermite Normal Form of a matrix
52
53hilbertSeries(M)         compute the multigraded Hilbert Series of M
54
55           (parameters in square brackets [] are optional)
56";
57
58// finestMDeg(def r)
59// newMap(map F, intmat Q, list #)
60
61
62
63LIB "standard.lib"; // for groebner
64
65/******************************************************/
66proc setBaseMultigrading(intmat M, list #)
67"USAGE:   setBaseMultigrading(M[, T])
68PURPOSE: attach M abd T to the basering, where M is the matrix of the weights of variables, L is the torsion
69RETURN: nothing
70EXAMPLE: example setBaseMultigrading; shows an example
71"
72{
73  string attrMgrad   = "mgrad";
74  string attrTorsion = "torsion";
75  string attrTorsionHNF = "torsion_hermite";
76
77
78  attrib(basering, attrMgrad, M);
79 
80  if( size(#) > 0 and typeof(#[1]) == "intmat" )
81  {
82    def T = #[1];
83  } else
84  {
85    intmat T[nrows(M)][1];
86  }
87
88  if( nrows(T) == nrows(M) )
89  {
90    attrib(basering, attrTorsion, T); 
91    def H;
92    attrib(basering, attrTorsionHNF, H);       
93  }
94  else
95  {
96    ERROR("Incompatible matrix sizes!");
97  }
98}
99example
100{
101  "EXAMPLE:"; echo=2;
102   
103  ring R = 0, (x, y, z), dp;
104
105  // Weights of variables
106  intmat M[3][3] =
107    1, 0, 0,
108    0, 1, 0,
109    0, 0, 1;
110
111  // Torsion:
112  intmat L[3][2] =
113    1, 1,
114    1, 3,
115    1, 5;
116   
117  // attaches M & L to R (==basering):
118  setBaseMultigrading(M, L); // Grading: Z^3/L
119
120  // Weights are accessible via "getVariableWeights()":
121  getVariableWeights() == M;
122  getVariableWeights(R) == M;
123  getVariableWeights(basering) == M;
124
125  // Torsion is accessible via "getTorsion()":
126  getTorsion() == L;
127  getTorsion(R) == L;
128  getTorsion(basering) == L;
129
130  // And its hermite NF via getTorsion("hermite"):
131  intmat H = hermite(L);
132  getTorsion("hermite") == H;
133  getTorsion(R, "hermite") == H;
134  getTorsion(basering, "hermite") == H;
135
136 
137
138  kill L, M;
139
140  // ----------- isomorphic multigrading -------- //
141
142  // Weights of variables
143  intmat M[2][3] =
144    1, -2, 1,
145    1,  1, 0;
146
147  // Torsion:
148  intmat L[2][1] =
149    0,
150    2;
151   
152  // attaches M & L to R (==basering):
153  setBaseMultigrading(M, L); // Grading: Z + (Z/2Z)
154
155  // Weights are accessible via "getVariableWeights()":
156  getVariableWeights() == M;
157
158  // Torsion is accessible via "getTorsion()":
159  getTorsion() == L;
160
161  kill L, M;
162  // ----------- extreme case ------------ //
163
164  // Weights of variables
165  intmat M[1][3] =
166    1,  -1, 10;
167
168  // Torsion:
169  intmat L[1][1] =
170    0;
171   
172  // attaches M & L to R (==basering):
173  setBaseMultigrading(M); // Grading: Z^3
174
175  // Weights are accessible via "getVariableWeights()":
176  getVariableWeights() == M;
177
178  // Torsion is accessible via "getTorsion()":
179  getTorsion() == L;
180}
181
182
183/******************************************************/
184proc getVariableWeights(list #)
185"USAGE: getVariableWeights([R])
186PURPOSE: get associated multigrading matrix for the basering [or R]
187RETURN:  intmat, matrix of multidegrees of variables
188EXAMPLE: example getVariableWeights; shows an example
189"
190{
191  string attrMgrad = "mgrad";
192
193
194  if( size(#) > 0 )
195  {
196    if(( typeof(#[1]) == "ring" ) || ( typeof(#[1]) == "qring" ))
197    {
198      def R = #[1];
199    }
200    else
201    {
202      ERROR("Optional argument must be a ring!");
203    }
204  }
205  else
206  {
207    def R = basering;
208  }
209
210  def M = attrib(R, attrMgrad);
211  if( typeof(M) == "intmat"){ return (M); }
212  ERROR( "Sorry no multigrading matrix!" ); 
213}
214example
215{
216  "EXAMPLE:"; echo=2;
217   
218  ring R = 0, (x, y, z), dp;
219
220  // Weights of variables
221  intmat M[3][3] =
222    1, 0, 0,
223    0, 1, 0,
224    0, 0, 1;
225
226  // Torsion:
227  intmat L[3][2] =
228    1, 1,
229    1, 3,
230    1, 5;
231   
232  // attaches M & L to R (==basering):
233  setBaseMultigrading(M, L); // Grading: Z^3/L
234
235  // Weights are accessible via "getVariableWeights()":
236  getVariableWeights() == M;
237
238  kill L, M;
239
240  // ----------- isomorphic multigrading -------- //
241
242  // Weights of variables
243  intmat M[2][3] =
244    1, -2, 1,
245    1,  1, 0;
246
247  // Torsion:
248  intmat L[2][1] =
249    0,
250    2;
251   
252  // attaches M & L to R (==basering):
253  setBaseMultigrading(M, L); // Grading: Z + (Z/2Z)
254
255  // Weights are accessible via "getVariableWeights()":
256  getVariableWeights() == M;
257
258  kill L, M;
259
260  // ----------- extreme case ------------ //
261
262  // Weights of variables
263  intmat M[1][3] =
264    1,  -1, 10;
265
266  // Torsion:
267  intmat L[1][1] =
268    0;
269   
270  // attaches M & L to R (==basering):
271  setBaseMultigrading(M); // Grading: Z^3
272
273  // Weights are accessible via "getVariableWeights()":
274  getVariableWeights() == M;
275}
276
277/******************************************************/
278proc getTorsion(list #)
279"USAGE: getTorsion([R[,opt]])
280PURPOSE: get associated torsion matrix, i.e. generators (cols) of the Torsion group
281RETURN: intmat, the torsion matrix, or its hermite normal form
282        if an optional argument (\"hermite\") is given
283"
284{
285  string attrTorsion    = "torsion";
286  string attrTorsionHNF = "torsion_hermite";
287
288  int i = 1;
289
290  if( size(#) >= i )
291  {
292    if( ( typeof(#[i]) == "ring" ) or ( typeof(#[i]) == "qring" ) )
293    {
294      def R = #[i];
295      i++;
296    }
297  }
298
299  if( !defined(R) )
300  {
301    def R = basering;
302  }
303
304  if( size(#) >= i )
305  {
306    if( #[i] == "hermite" )
307    {
308      if( typeof(attrib(R, attrTorsionHNF)) != "intmat" )
309      {
310        def M = getTorsion(R);
311        if( typeof(M) != "intmat")
312        {
313          ERROR( "Sorry no torsion matrix!" ); 
314        }
315        attrib(R, attrTorsionHNF, hermite(M)); // this might not work with R != basering...
316      }
317      return (attrib(R, attrTorsionHNF));
318    }
319  }
320
321  def M = attrib(R, attrTorsion);
322  if( typeof(M) != "intmat")
323  {
324    ERROR( "Sorry no torsion matrix!" ); 
325  }
326  return (M);
327}
328example
329{
330  "EXAMPLE:"; echo=2;
331   
332  ring R = 0, (x, y, z), dp;
333
334  // Weights of variables
335  intmat M[3][3] =
336    1, 0, 0,
337    0, 1, 0,
338    0, 0, 1;
339
340  // Torsion:
341  intmat L[3][2] =
342    1, 1,
343    1, 3,
344    1, 5;
345   
346  // attaches M & L to R (==basering):
347  setBaseMultigrading(M, L); // Grading: Z^3/L
348
349  // Torsion is accessible via "getTorsion()":
350  getTorsion() == L;
351
352  // its hermite NF:
353  print(getTorsion("hermite"));
354
355  kill L, M;
356
357  // ----------- isomorphic multigrading -------- //
358
359  // Weights of variables
360  intmat M[2][3] =
361    1, -2, 1,
362    1,  1, 0;
363
364  // Torsion:
365  intmat L[2][1] =
366    0,
367    2;
368   
369  // attaches M & L to R (==basering):
370  setBaseMultigrading(M, L); // Grading: Z + (Z/2Z)
371
372  // Torsion is accessible via "getTorsion()":
373  getTorsion() == L;
374
375  // its hermite NF:
376  print(getTorsion("hermite"));
377
378  kill L, M;
379
380  // ----------- extreme case ------------ //
381
382  // Weights of variables
383  intmat M[1][3] =
384    1,  -1, 10;
385
386  // Torsion:
387  intmat L[1][1] =
388    0;
389   
390  // attaches M & L to R (==basering):
391  setBaseMultigrading(M); // Grading: Z^3
392
393  // Torsion is accessible via "getTorsion()":
394  getTorsion() == L;
395
396  // its hermite NF:
397  print(getTorsion("hermite"));
398}
399
400/******************************************************/
401proc getModuleGrading(def m)
402"USAGE: getModuleGrading(m), 'm' module/vector
403RETURN: integer matrix of the multiweights of free module generators attached to 'm'
404"
405{
406  string attrModuleGrading = "genWeights";
407
408  //  print(m);  typeof(m);  attrib(m);
409
410  def V = attrib(m, attrModuleGrading);
411
412  if( typeof(V) != "intmat" )
413  {
414    if( (typeof(m) == "ideal") or (typeof(m) == "poly") )
415    {
416      intmat M = getVariableWeights();
417      intmat VV[nrows(M)][1];
418      return (VV);
419    }
420     
421    ERROR("Sorry: vector or module need module-grading-matrix! See 'getModuleGrading'.");
422  }
423
424  if( nrows(V) != nrows(getVariableWeights()) )
425  {
426    ERROR("Sorry wrong height of V: " + string(nrows(V)));
427  }
428
429  if( ncols(V) < nrows(m) )
430  {
431    ERROR("Sorry wrong width of V: " + string(ncols(V)));
432  }
433   
434  return (V);
435}
436example
437{
438  "EXAMPLE:"; echo=2;
439   
440   ring R = 0, (x,y), dp;
441   intmat M[2][2]=
442     1, 1,
443     0, 2;
444   intmat T[2][5]=
445     1,  2,  3,  4, 0,
446     0, 10, 20, 30, 1;
447   
448   setBaseMultigrading(M, T);
449   
450   ideal I = x, y, xy^5;
451   isHomogenous(I);
452 
453   intmat V = mDeg(I); print(V);
454
455   module S = syz(I); print(S);
456   
457   S = setModuleGrading(S, V);
458
459   getModuleGrading(S) == V;
460   
461   vector v = setModuleGrading(S[1], V);
462   getModuleGrading(v) == V;
463   isHomogenous(v);
464   print( mDeg(v) );   
465   
466   isHomogenous(S);
467   print( mDeg(S) );
468}
469
470/******************************************************/
471proc setModuleGrading(def m, intmat G)
472"USAGE: setModuleGrading(m, G), m module/vector, G intmat
473PURPOSE: attaches the multiweights of free module generators to 'm'
474WARNING: The method does not verify that the multigrading makes the
475         module/vector homogenous. One can do that using isHomogenous(m).
476"
477{
478  string attrModuleGrading = "genWeights";
479
480  intmat R = getVariableWeights();
481
482  if(nrows(G) != nrows(R)){ ERROR("Incompatible gradings.");}
483  if(ncols(G) < nrows(m)){ ERROR("Multigrading does not fit to module.");}
484
485  attrib(m, attrModuleGrading, G);
486  return(m);
487}
488example
489{
490  "EXAMPLE:"; echo=2;
491   
492   ring R = 0, (x,y), dp;
493   intmat M[2][2]=
494     1, 1,
495     0, 2;
496   intmat T[2][5]=
497     1,  2,  3,  4, 0,
498     0, 10, 20, 30, 1;
499   
500   setBaseMultigrading(M, T);
501   
502   ideal I = x, y, xy^5;
503   intmat V = mDeg(I);
504   
505   // V == M; modulo T
506   print(V);
507
508   module S = syz(I);
509   
510   S = setModuleGrading(S, V);
511   getModuleGrading(S) == V;
512
513   print(S);
514   
515   vector v = S[1]; v = setModuleGrading(v, V);
516   getModuleGrading(v) == V;
517
518   print( mDeg(v) );   
519
520   isHomogenous(S);
521
522   print( mDeg(S) );
523}
524
525
526
527
528/******************************************************/
529proc isTorsionFree()
530"USAGE: isTorsionFree()
531PURPOSE: Determines whether the multigrading attached to the current ring is torsion-free.
532RETURN: boolean, the result of the test
533"
534{
535
536  intmat H = hermite(transpose(getTorsion("hermite"))); // TODO: ?cache it?
537
538  int i, j;
539  int r = nrows(H);
540  int c = ncols(H);
541  int d = 1;
542  for( i = 1; (i <= c) && (i <= r); i++ )
543  {
544    for( j = i; (H[j, i] == 0)&&(j < r); j++ )
545    {
546    }
547
548    if(H[j, i]!=0)
549    {
550      d=d*H[j, i];
551    }
552  }
553
554  if( (d*d)==1 )
555  {
556    return(1==1);
557  }
558  return(0==1);
559}
560example
561{
562  "EXAMPLE:"; echo=2;
563
564   ring R = 0,(x,y),dp;
565   intmat M[2][2]=
566     1,0,
567     0,1;
568   intmat T[2][5]=
569     1, 2, 3, 4, 0,
570     0,10,20,30, 1;
571   
572   setBaseMultigrading(M,T);
573   
574   // Is the resulting group torsion free?
575   isTorsionFree();
576
577   kill R, M, T;
578   ///////////////////////////////////////////
579
580   ring R=0,(x,y,z),dp;
581   intmat A[3][3] =
582     1,0,0,
583     0,1,0,
584     0,0,1;
585   intmat B[3][4]=
586     3,3,3,3,
587     2,1,3,0,
588     1,2,0,3;
589   setBaseMultigrading(A,B);
590   // Is the resulting group torsion free?
591   isTorsionFree();
592
593   kill R, A, B;
594}
595
596
597
598/******************************************************/
599proc hermite(intmat A)
600"USAGE: hermite( A );
601PROCEDURE: Computes the Hermite Normal Form of the matrix A by column operations.
602RETURN: intmat, the Hermite Normal Form of A
603"
604{
605  intmat g;
606  int i,j,k, save, d, a1, a2, c, l;
607  c=0;
608  l=0;
609  for(i=1; ((i+l)<=nrows(A))&&((i+l)<=ncols(A)); i++)
610  {
611    //i;
612    if(A[i+l, i]==0)
613    {
614      for(j=i;j<=ncols(A); j++)
615      {
616        if(A[i+l, j]!=0)
617        {
618          for(k=1;k<=nrows(A);k++)
619          {
620            save=A[k, i];
621            A[k, i]=A[k, j];
622            A[k, j]=save;
623          }
624          break;
625        }
626        if(j==ncols(A)){ c=1; l=l+1; }
627      }
628    }
629
630    if((i+l)>nrows(A)){ break; }
631   
632    if(A[i+l, i]<0)
633    {
634      for(k=1;k<=nrows(A);k++){ A[k, i]=A[k, i]*(-1); }
635    }
636
637    if(c==0)
638    {
639      for(j=i+1;j<=ncols(A);j++)
640      {
641        //A;
642        if(A[i+l, j]<0)
643        {
644          for(k=1;k<=nrows(A);k++){ A[k, j]=(-1)*A[k, j];}
645        }
646
647        if(A[i+l, i]==1)
648        {
649          d=A[i+l, j];
650          for(k=1;k<=nrows(A);k++)
651          {
652            A[k, j]=A[k, j]-d*A[k, i];
653          }
654        }
655        else
656        {
657          while((A[i+l, i] * A[i+l, j])!=0)
658          {
659            if(A[i+l, i]> A[i+l, j])
660            {
661
662              for(k=1;k<=nrows(A);k++)
663              {
664                save=A[k, i];
665                A[k, i]=A[k, j];
666                A[k, j]=save;
667              }
668            }
669            a1=A[i+l, j]%A[i+l,i];
670            a2=A[i+l, j]-a1;
671            d=a2/A[i+l, i];
672            for(k=1;k<=nrows(A);k++)
673            {
674              A[k, j]=A[k, j]- d*A[k, i];
675            }
676          }
677        }
678      }
679      for(j=1;j<i;j++)
680      {
681        a1=A[i+l, j]%A[i+l,i];
682        d=(A[i+l, j]-a1)/A[i+l, i];
683        for(k=1;k<=nrows(A);k++){ A[k, j]=A[k, j]-d*A[k, i];}
684      }
685    }
686    c=0;
687  }
688  return( A);
689}
690example
691{
692  "EXAMPLE:"; echo=2;
693
694   intmat M[2][5] =
695     1, 2, 3, 4, 0,
696     0,10,20,30, 1;
697
698   // Hermite Normal Form of M:
699   print(hermite(M));
700
701   intmat T[3][4] =
702     3,3,3,3,
703     2,1,3,0,
704     1,2,0,3;
705
706   // Hermite Normal Form of T:
707   print(hermite(T));
708
709   intmat A[4][5] =
710     1,2,3,2,2,
711     1,2,3,4,0,
712     0,5,4,2,1,
713     3,2,4,0,2;
714
715   // Hermite Normal Form of A:
716   print(hermite(A));
717}
718
719
720/******************************************************/
721proc isTorsionElement(intvec mdeg)
722"USAGE: isTorsionElement(intvec mdeg);
723PROCEDURE: For a integer vector mdeg representing the multidegree of some polynomial
724or vector this method computes if the multidegree is contained in the torsion
725group, i.e. if it is zero in the multigrading.
726"
727{
728  intmat H = getTorsion("hermite");
729  int x, k, i;
730
731  int r = nrows(H);
732  int c = ncols(H);
733
734  int rr = nrows(mdeg);
735
736  for( i = 1; (i<=r) && (i<=c); i++)
737  {
738    if(H[i, i]!=0)
739    {
740      x = mdeg[i]%H[i, i];
741
742      if(x!=0)
743      {
744        return(1==0);
745      }
746
747      x = mdeg[i] / H[i,i];
748
749      for( k=1; k <= rr; k++)
750      {
751        mdeg[k] = mdeg[k] - x*H[k,i];
752      }
753    }
754  }
755
756  return( mdeg == 0 );
757
758}
759example
760{
761 "EXAMPLE:"; echo=2;
762
763  ring r = 0,(x,y,z),dp;
764
765  intmat g[2][3]=
766    1,0,1,
767    0,1,1;
768  intmat t[2][1]=
769    -2,
770    1;
771
772  setBaseMultigrading(g,t);
773
774  poly a = x10yz;
775  poly b = x8y2z;
776  poly c = x4z2;
777  poly d = y5;
778  poly e = x2y2;
779  poly f = z2;
780
781  intvec v1 = mDeg(a) - mDeg(b);
782  v1;
783  isTorsionElement(v1);
784 
785  intvec v2 = mDeg(a) - mDeg(c);
786  v2;
787  isTorsionElement(v2);
788 
789  intvec v3 = mDeg(e) - mDeg(f);
790  v3;
791  isTorsionElement(v3);
792 
793  intvec v4 = mDeg(c) - mDeg(d);
794  v4;
795  isTorsionElement(v4);
796}
797
798
799/******************************************************/
800proc defineHomogenous(poly f, list #)
801"USAGE: defineHomogenous(f[, G]); polynomial f, integer matrix G
802PURPOSE: Yields a matrix which has to be appended to the torsion matrix to make the
803polynomial f homogenous in the grading by grad.
804"
805{
806  if( size(#) > 0 )
807  {
808    if( typeof(#[1]) == "intmat" )
809    {
810      intmat grad = #[1];
811    }
812  }
813
814  if( !defined(grad) )
815  {
816    intmat grad = getVariableWeights();
817  }
818
819  intmat newtor[nrows(grad)][size(f)-1];
820  int i,j;
821  intvec l = grad*leadexp(f);
822  intvec v;
823  for(i=2; i <= size(f); i++)
824  {
825    v = grad * leadexp(f[i]) - l;
826    for( j=1; j<=size(v); j++)
827    {
828      newtor[j,i-1] = v[j];
829    }
830  }
831  return(newtor);
832}
833example
834{
835  "EXAMPLE:"; echo=2;
836
837  ring r =0,(x,y,z),dp;
838  intmat grad[2][3] =
839    1,0,1,
840    0,1,1;
841
842  setBaseMultigrading(grad);
843
844  poly f = x2y3-z5+x-3zx;
845
846  intmat M = defineHomogenous(f);
847  M;
848  defineHomogenous(f, grad) == M;
849 
850  isHomogenous(f);
851  setBaseMultigrading(grad, M);
852  isHomogenous(f);
853}
854
855/******************************************************/
856proc pushForward(map f)
857"USAGE: pushForward(f);
858PURPOSE: Computes the finest grading of the image ring which makes the map f
859a map of graded rings. The group map between the two grading groups is given
860by transpose( (Id, 0) ). Pay attention that the group spanned by the columns of
861the torsion matrix may not be a subgroup of the grading group. Still all columns
862are needed to find the correct image of the preimage gradings.
863"
864{
865
866  int k,i,j;
867  f;
868
869  intmat oldgrad=getVariableWeights(preimage(f));
870  intmat oldtor=getTorsion(preimage(f));
871
872  int n=nvars(preimage(f));
873  int np=nvars(basering);
874  int p=nrows(oldgrad);
875  int pp=p+np;
876
877  intmat newgrad[pp][np];
878
879  for(i=1;i<=np;i++){ newgrad[p+i,i]=1;}
880
881  //newgrad;
882
883
884
885  list newtor;
886  intmat toadd;
887  int columns=0;
888
889  intmat toadd1[pp][n];
890  intvec v;
891  poly im;
892
893  for(i=1;i<=p;i++){
894    for(j=1;j<=n;j++){ toadd1[i,j]=oldgrad[i,j];}
895  }
896
897  for(i=1;i<=n;i++){
898    im=f[i];
899    //im;
900    toadd = defineHomogenous(im, newgrad);
901    newtor=insert(newtor,toadd);
902    columns=columns+ncols(toadd);
903
904    v=leadexp(f[i]);
905    for(j=p+1;j<=p+np;j++){ toadd1[j,i]=-v[j-p];}
906  }
907
908  newtor=insert(newtor,toadd1);
909  columns=columns+ncols(toadd1);
910
911
912  if(typeof(basering)=="qring"){
913    //"Entering qring";
914    ideal a=ideal(basering);
915    for(i=1;i<=size(a);i++){
916      toadd = defineHomogenous(a[i], newgrad);
917      //toadd;
918      columns=columns+ncols(toadd);
919      newtor=insert(newtor,toadd);
920    }
921  }
922
923  //newtor;
924  intmat imofoldtor[pp][ncols(oldtor)];
925  for(i=1; i<=nrows(oldtor);i++){
926    for(j=1; j<=ncols(oldtor); j++){
927      imofoldtor[i,j]=oldtor[i,j];
928    }
929  }
930
931  columns=columns+ncols(oldtor);
932  newtor=insert(newtor, imofoldtor);
933
934  intmat torsion[pp][columns];
935  columns=0;
936  for(k=1;k<=size(newtor);k++){
937    for(i=1;i<=pp;i++){
938      for(j=1;j<=ncols(newtor[k]);j++){torsion[i,j+columns]=newtor[k][i,j];}
939    }
940    columns=columns+ncols(newtor[k]);
941  }
942
943  torsion=hermite(torsion);
944  intmat result[pp][pp];
945  for(i=1;i<=pp;i++){
946    for(j=1;j<=pp;j++){result[i,j]=torsion[i,j];}
947  }
948
949  setBaseMultigrading(newgrad, result);
950
951}
952example
953{
954  "EXAMPLE:"; echo=2;
955
956  // Setting degrees for preimage ring.;
957  intmat grad[3][3] =
958    1,0,0,
959    0,1,0,
960    0,0,1;
961
962  ring r = 0,(x,y,z),dp;
963  keepring(r);
964  setBaseMultigrading(grad);
965
966  // grading on r:
967  getVariableWeights();
968  getTorsion();
969
970  ring R = 0,(a,b),dp;
971  ideal i=a2-b2+a6-b5+ab3,a7b+b15-ab6+a6b6;
972
973  // The quotient ring by this ideal will become our image ring.;
974  qring Q = std(i);
975  map f = r,-a2b6+b5+a3b+a2+ab,-a2b7-3a2b5+b4+a,a6-b6-b3+a2;
976  f;
977
978  // Pushing forward f:
979  pushForward(f);
980
981  // due to pushForward we have got new grading on Q
982  getVariableWeights();
983  getTorsion();
984 
985  // TODO: Unfortunately this is not a very spectacular example.;
986
987}
988
989
990/******************************************************/
991proc equalMDeg(intvec exp1, intvec exp2, list #)
992"USAGE: equalMDeg(exp1, exp2[, V]) where exp1,exp2 are exponents of terms [, V is multidegree of module components];
993PURPOSE: Tests if the exponent vectors of two monomials represent the same multidegree.
994"
995{
996  if( size(exp1) != size(exp2) )
997  {
998    ERROR("Sorry: we cannot compare exponents comming from a polynomial and a vector yet!");
999  }
1000
1001  if( exp1 == exp2)
1002  {
1003    return (1==1);
1004  }
1005
1006
1007
1008  intmat M = getVariableWeights();
1009
1010  if( nrows(exp1) > ncols(M) ) // vectors => last exponent is the module component!
1011  {
1012    if( (size(#) == 0) or (typeof(#[1])!="intmat") )
1013    {
1014      ERROR("Sorry: wrong or missing module-unit-weights-matrix!");
1015    }
1016    intmat V = #[1];
1017
1018    // typeof(V); print(V);
1019
1020    int N = ncols(M);
1021    int r = nrows(M);
1022
1023    intvec d = intvec(exp1[1..N]) - intvec(exp2[1..N]);
1024    intvec dm = intvec(V[1..r, exp1[N+1]]) - intvec(V[1..r, exp2[N+1]]);
1025
1026    intvec difference = M * d + dm;
1027  }
1028  else
1029  {
1030    intvec d = (exp1 - exp2);
1031    intvec difference = M * d;
1032  }
1033
1034  if (isFreeRepresented()) // no torsion!?
1035  {
1036    return ( difference == 0);
1037  }
1038  return ( isTorsionElement( difference ) );
1039}
1040example
1041{
1042  "EXAMPLE:"; echo=2;
1043
1044  ring r = 0,(x,y,z),dp;
1045
1046  intmat g[2][3]=
1047    1,0,1,
1048    0,1,1;
1049
1050  intmat t[2][1]=
1051    -2,
1052    1;
1053
1054  setBaseMultigrading(g,t);
1055
1056  poly a = x10yz;
1057  poly b = x8y2z;
1058  poly c = x4z2;
1059  poly d = y5;
1060  poly e = x2y2;
1061  poly f = z2;
1062
1063
1064  equalMDeg(leadexp(a), leadexp(b));
1065  equalMDeg(leadexp(a), leadexp(c));
1066  equalMDeg(leadexp(a), leadexp(d));
1067  equalMDeg(leadexp(a), leadexp(e));
1068  equalMDeg(leadexp(a), leadexp(f));
1069
1070  equalMDeg(leadexp(b), leadexp(c));
1071  equalMDeg(leadexp(b), leadexp(d));
1072  equalMDeg(leadexp(b), leadexp(e));
1073  equalMDeg(leadexp(b), leadexp(f));
1074
1075  equalMDeg(leadexp(c), leadexp(d));
1076  equalMDeg(leadexp(c), leadexp(e));
1077  equalMDeg(leadexp(c), leadexp(f));
1078
1079  equalMDeg(leadexp(d), leadexp(e));
1080  equalMDeg(leadexp(d), leadexp(f));
1081
1082  equalMDeg(leadexp(e), leadexp(f));
1083
1084}
1085
1086
1087
1088/******************************************************/
1089static proc isFreeRepresented()
1090"check whether the base muligrading is torsion-free (it is zero).
1091"
1092{
1093  intmat T = getTorsion();
1094
1095  intmat Z[nrows(T)][ncols(T)];
1096
1097  return (T == Z); // no torsion!
1098}
1099
1100
1101/******************************************************/
1102proc isHomogenous(def a, list #)
1103"USAGE: isHomogenous(a[, f]); a polynomial/vector/ideal/module
1104RETURN: boolean, TRUE if a is (multi)homogenous, and FALSE otherwise
1105"
1106{
1107  if( (typeof(a) == "poly") or (typeof(a) == "vector") )
1108  {
1109    return ( size(mDegPartition(a)) <= 1 )
1110  }
1111
1112  if( typeof(a) == "vector" or typeof(a) == "module" )
1113  {
1114    intmat V = getModuleGrading(a);
1115  }
1116
1117  if( (typeof(a) == "ideal") or (typeof(a) == "module") )
1118  {
1119    if(size(#) > 0)
1120    {
1121      if (#[1] == "checkGens")
1122      {
1123        def aa;
1124        for( int i = ncols(a); i > 0; i-- )
1125        {
1126          aa = a[i];
1127
1128          if(defined(V))
1129          {
1130            aa = setModuleGrading(aa, V);
1131          }
1132
1133          if(!isHomogenous(aa))
1134          {
1135            return(0==1);
1136          }
1137        }
1138        return(1==1);
1139      }
1140    }
1141
1142    def g = groebner(a); // !!!!
1143
1144    def b, aa; int j;
1145    for( int i = ncols(a); i > 0; i-- )
1146    {
1147      aa = a[i];
1148
1149      if(defined(V))
1150      {
1151        aa = setModuleGrading(aa, V);
1152      }
1153
1154      b = mDegPartition(aa);
1155      for( j = ncols(b); j > 0; j-- )
1156      {
1157        if(NF(b[j],g) != 0)
1158        {
1159          return(0==1);
1160        }
1161      }
1162    }
1163    return(1==1);
1164  } 
1165}
1166example
1167{
1168  "EXAMPLE:"; echo=2;
1169
1170  ring r = 0,(x,y,z),dp;
1171
1172  //Grading and Torsion matrices:
1173  intmat M[3][3] =
1174    1,0,0,
1175    0,1,0,
1176    0,0,1;
1177
1178  intmat T[3][1] =
1179    1,2,3;
1180
1181  setBaseMultigrading(M,T);
1182
1183  attrib(r);
1184
1185  poly f = x-yz;
1186
1187  mDegPartition(f);
1188  print(mDeg(_));
1189
1190  isHomogenous(f);   // f: is not homogenous
1191
1192  poly g = 1-xy2z3;
1193  isHomogenous(g); // g: is homogenous
1194  mDegPartition(g);
1195
1196  kill T;
1197  /////////////////////////////////////////////////////////
1198  // new Torsion matrix:
1199  intmat T[3][4] =
1200    3,3,3,3,
1201    2,1,3,0,
1202    1,2,0,3;
1203 
1204  setBaseMultigrading(M,T);
1205
1206  f;
1207  isHomogenous(f);
1208  mDegPartition(f);
1209
1210  // ---------------------
1211  g;
1212  isHomogenous(g);
1213  mDegPartition(g);
1214
1215  kill r, T, M;
1216
1217  ring R = 0, (x,y,z), dp;
1218
1219  intmat A[2][3] =
1220    0,0,1,
1221    3,2,1;
1222  intmat T[2][1] =
1223    -1,
1224     4;
1225  setBaseMultigrading(A, T);
1226
1227  isHomogenous(ideal(x2 - y3 -xy +z, x*y-z, x^3 - y^2*z + x^2 -y^3)); // 1
1228  isHomogenous(ideal(x2 - y3 -xy +z, x*y-z, x^3 - y^2*z + x^2 -y^3), "checkGens");
1229  isHomogenous(ideal(x+y, x2 - y2)); // 0
1230
1231  // Degree partition:
1232  mDegPartition(x2 - y3 -xy +z);
1233  mDegPartition(x3 -y2z + x2 -y3 + z + 1);
1234
1235 
1236  module N = gen(1) + (x+y) * gen(2), z*gen(3);
1237
1238  intmat V[2][3] = 0; // 1, 2, 3,  4, 5, 6; //  column-wise weights of components!!??
1239 
1240  vector v1, v2;
1241 
1242  v1 = setModuleGrading(N[1], V); v1;
1243  mDegPartition(v1);
1244  print( mDeg(_) );
1245
1246  v2 = setModuleGrading(N[2], V); v2;
1247  mDegPartition(v2);
1248  print( mDeg(_) );
1249
1250  N = setModuleGrading(N, V);
1251  isHomogenous(N);
1252  print( mDeg(N) );
1253
1254  ///////////////////////////////////////
1255
1256  V =
1257    1, 2, 3, 
1258    4, 5, 6;
1259
1260  v1 = setModuleGrading(N[1], V); v1;
1261  mDegPartition(v1);
1262  print( mDeg(_) );
1263
1264  v2 = setModuleGrading(N[2], V); v2;
1265  mDegPartition(v2);
1266  print( mDeg(_) );
1267
1268  N = setModuleGrading(N, V);
1269  isHomogenous(N);
1270  print( mDeg(N) );
1271
1272  ///////////////////////////////////////
1273
1274  V =
1275    0, 0, 0, 
1276    4, 1, 0;
1277
1278  N = gen(1) + x * gen(2), z*gen(3);
1279
1280  N = setModuleGrading(N, V); N;
1281  isHomogenous(N);
1282  print( mDeg(N) );
1283
1284  v1 = setModuleGrading(N[1], V); v1;
1285  mDegPartition(v1);
1286  print( mDeg(_) );
1287
1288  N = setModuleGrading(N, V); N;
1289  isHomogenous(N);
1290  print( mDeg(N) );
1291}
1292
1293/******************************************************/
1294proc mDeg(def A)
1295"USAGE: mDeg(A); any A
1296PURPOSE: compute multidegree
1297"
1298{
1299  if( defined(attrib(A, "grad")) > 0 )
1300  {
1301    return (attrib(A, "grad"));
1302  }
1303
1304  intmat M = getVariableWeights();
1305  int N = nvars(basering);
1306
1307  if( ncols(M) != N )
1308  {
1309    ERROR("Sorry wrong mgrad-size of M: " + string(ncols(M)));
1310  }
1311
1312  int r = nrows(M);
1313
1314  if( A == 0 )
1315  {
1316    intvec v; v[r] = 0;
1317    return (v);
1318  }
1319
1320  if( (typeof(A) == "vector") or (typeof(A) == "module") )
1321  {
1322    intmat V = getModuleGrading(A);
1323
1324    if( nrows(V) != r )
1325    {
1326      ERROR("Sorry wrong mgrad-size of V: " + string(nrows(V)));
1327    }
1328  }
1329
1330  intvec m; m[r] = 0;
1331
1332  if( typeof(A) == "poly" )
1333  {
1334    intvec v = leadexp(A); //  v;
1335    m = M * v;
1336
1337    // We assume homogeneous input!
1338    return(m);
1339
1340    A = A - lead(A);
1341    while( size(A) > 0 )
1342    {
1343      v = leadexp(A); //  v;
1344      m = max( m, M * v, r ); // ????
1345      A = A - lead(A);
1346    }
1347
1348    return(m);
1349  }
1350
1351
1352  if( typeof(A) == "vector" )
1353  {
1354    intvec v;
1355    v = leadexp(A); //  v;
1356    m = intvec(M * intvec(v[1..N])) + intvec(V[1..r, v[N+1]]);
1357
1358    // We assume homogeneous input!
1359    return(m);
1360
1361    A = A - lead(A);
1362    while( size(A) > 0 )
1363    {
1364      v = leadexp(A); //  v;
1365
1366      // intvec(M * intvec(v[1..N])) + intvec(V[1..r, v[N+1]]);
1367
1368      m = max( m, intvec(M * intvec(v[1..N])) + intvec(V[1..r, v[N+1]]), r ); // ???
1369
1370      A = A - lead(A);
1371    }
1372
1373    return(m);
1374  }
1375
1376  int i, j; intvec d;
1377
1378  if( typeof(A) == "ideal" )
1379  {
1380    intmat G[ r ] [ ncols(A)];
1381    for( i = ncols(A); i > 0; i-- )
1382    {
1383      d = mDeg( A[i] );
1384
1385      for( j = 1; j <= r; j++ ) // see ticket: 253
1386      {
1387        G[j, i] = d[j];
1388      }     
1389    }
1390    return(G);
1391  }
1392
1393  if( typeof(A) == "module" )
1394  {
1395    intmat G[ r ] [ ncols(A)];
1396    vector v;
1397
1398    for( i = ncols(A); i > 0; i-- )
1399    {
1400      v = setModuleGrading(A[i], V);
1401
1402      // G[1..r, i]
1403      d = mDeg(v);
1404
1405      for( j = 1; j <= r; j++ ) // see ticket: 253
1406      {
1407        G[j, i] = d[j];
1408      }     
1409
1410    }
1411
1412    return(G);
1413  }
1414 
1415}
1416example
1417{
1418  "EXAMPLE:"; echo=2;
1419
1420  ring r = 0,(x, y), dp;
1421
1422  intmat A[2][2] = 1, 0, 0, 1;
1423  print(A);
1424
1425  intmat Ta[2][1] = 0, 3;
1426  print(Ta);
1427
1428  //   attrib(A, "torsion", Ta); // to think about
1429
1430//  "poly:";
1431  setBaseMultigrading(A);
1432
1433
1434  mDeg( x*x, A );
1435  mDeg( y*y*y, A );
1436
1437  setBaseMultigrading(A, Ta);
1438 
1439  mDeg( x*x*y );
1440 
1441  mDeg( y*y*y*x );
1442 
1443  mDeg( x*y + x + 1 );
1444
1445  mDegPartition(x*y + x + 1);
1446
1447  print ( mDeg(0) );
1448  poly zero = 0;
1449  print ( mDeg(zero) );
1450
1451//  "ideal:";
1452 
1453  ideal I = y*x*x, x*y*y*y;
1454  print( mDeg(I) );
1455
1456  print ( mDeg(ideal(0)) );
1457  print ( mDeg(ideal(0,0,0)) );
1458
1459//  "vectors:";
1460 
1461  intmat B[2][2] = 0, 1, 1, 0;
1462  print(B);
1463 
1464  mDeg( setModuleGrading(y*y*y*gen(2), B ));
1465  mDeg( setModuleGrading(x*x*gen(1), B ));
1466
1467 
1468  vector V = x*gen(1) + y*gen(2);
1469  V = setModuleGrading(V, B);
1470  mDeg( V );
1471
1472  vector v1 = setModuleGrading([0, 0, 0], B);
1473  print( mDeg( v1 ) );
1474 
1475  vector v2 = setModuleGrading([0], B);
1476  print( mDeg( v2 ) );
1477
1478//  "module:";
1479 
1480  module D = x*gen(1), y*gen(2);
1481  D;
1482  D = setModuleGrading(D, B);
1483  print( mDeg( D ) );
1484 
1485
1486  module DD = [0, 0],[0, 0, 0];
1487  DD = setModuleGrading(DD, B);
1488  print( mDeg( DD ) );
1489
1490  module DDD = [0, 0];
1491  DDD = setModuleGrading(DDD, B);
1492  print( mDeg( DDD ) );
1493
1494};
1495
1496
1497
1498
1499
1500/******************************************************/
1501proc mDegPartition(def p)
1502"USAGE: mDegPartition(def p), p polynomial/vector
1503RETURNS: an ideal/module consisting of multigraded-homogeneous parts of p
1504"
1505{
1506  if( typeof(p) == "poly" )
1507  {
1508    ideal I;     
1509    poly mp, t, tt;
1510  }
1511  else
1512  {
1513    if(  typeof(p) == "vector" )
1514    {
1515      module I;     
1516      vector mp, t, tt;
1517    }
1518    else
1519    {
1520      ERROR("Wrong ARGUMENT type!");
1521    }
1522  }
1523
1524  if( typeof(p) == "vector" )
1525  {
1526    intmat V = getModuleGrading(p);
1527  }
1528  else
1529  {
1530    intmat V;
1531  }
1532
1533  if( size(p) > 1)
1534  {
1535    intvec m;
1536
1537    while( p != 0 )
1538    {
1539      m = leadexp(p);
1540      mp = lead(p);
1541      p = p - lead(p);
1542      tt = p; t = 0;
1543
1544      while( size(tt) > 0 )
1545      {
1546
1547        if( equalMDeg( leadexp(tt), m, V  ) ) // TODO: we make no caching of matrices (M,T,H,V), which remain the same!
1548        {
1549          mp = mp + lead(tt); // "mp", mp;
1550        }
1551        else
1552        {
1553          t = t + lead(tt);  //  "t", t;
1554        }
1555
1556        tt = tt - lead(tt);
1557      }
1558
1559      I[size(I)+1] = mp;
1560
1561      p = t;
1562    }
1563  }
1564  else
1565  {
1566    I[1] = p; // single monom
1567  }
1568
1569  if( typeof(I) == "module" )
1570  {
1571    I = setModuleGrading(I, V);
1572  }
1573
1574  return (I);
1575}
1576example
1577{
1578  "EXAMPLE:"; echo=2;
1579
1580  ring r = 0,(x,y,z),dp;
1581
1582  intmat g[2][3]=
1583    1,0,1,
1584    0,1,1;
1585  intmat t[2][1]=
1586    -2,
1587    1;
1588
1589  setBaseMultigrading(g,t);
1590
1591  poly f = x10yz+x8y2z-x4z2+y5+x2y2-z2+x17z3-y6;
1592
1593  mDegPartition(f);
1594 
1595  vector v = xy*gen(1)-x3y2*gen(2)+x4y*gen(3);
1596  intmat B[2][3]=1,-1,-2,0,0,1;
1597  v = setModuleGrading(v,B);
1598  getModuleGrading(v);
1599 
1600  mDegPartition(v, B);
1601}
1602
1603
1604
1605/******************************************************/
1606static proc unitMatrix(int n)
1607{
1608  intmat A[n][n];
1609 
1610  for( int i = n; i > 0; i-- )
1611  {
1612    A[i,i] = 1;
1613  }
1614
1615  return (A);
1616}
1617
1618
1619
1620/******************************************************/
1621static proc finestMDeg(def r)
1622"
1623USAGE:
1624PURPOSE: finest multigrading
1625"
1626{
1627  def save = basering;
1628  setring (r);
1629
1630  // in basering
1631      ideal I = ideal(basering);
1632
1633  int n = 0; int i; poly p;
1634  for( i = ncols(I); i > 0; i-- )
1635  {
1636    p = I[i];
1637    if( size(p) > 1 )
1638    {
1639      n = n + (size(p) - 1);
1640    }
1641    else
1642    {
1643      I[i] = 0;
1644    }
1645  }
1646
1647  int N = nvars(basering);
1648  intmat A = unitMatrix(N);
1649
1650
1651
1652  if( n > 0)
1653  {
1654
1655    intmat L[N][n];
1656    //  list L;
1657    int j = n;
1658
1659    for(  i = ncols(I); i > 0; i-- )
1660    {
1661      p = I[i];
1662
1663      if( size(p) > 1 )
1664      {
1665        intvec m0 = leadexp(p);
1666        p = p - lead(p);
1667
1668        while( size(p) > 0 )
1669        {
1670          L[ 1..N, j ] = leadexp(p) - m0;
1671          p = p - lead(p);
1672          j--;
1673        }
1674      }
1675    }
1676
1677    print(L);
1678    setBaseMultigrading(A, L);     
1679  }
1680  else
1681  {
1682    setBaseMultigrading(A);
1683  }
1684
1685  //  ERROR("nope");
1686
1687  //  ring T = integer, (x), (C, dp);
1688
1689  setring(save);
1690  return (r);
1691}
1692example
1693{
1694  "EXAMPLE:"; echo=2;
1695
1696  ring r = 0,(x, y), dp;
1697  qring q  = std(x^2 - y);
1698
1699  finestMDeg(q);
1700
1701}
1702
1703
1704
1705
1706/******************************************************/
1707static proc newMap(map F, intmat Q, list #)
1708"
1709USAGE: ?? no use now...
1710PURPOSE: ??
1711"
1712{
1713  attrib(F, "Q", Q);
1714
1715  if( size(#) > 0 and typeof(#[1]) == "intmat" )
1716  {
1717    attrib(F, "P", #[1]);
1718  }
1719  return (F);
1720}
1721
1722
1723/******************************************************/
1724static proc matrix2intmat( matrix M )
1725{
1726  execute( "intmat A[ "+ string(nrows(M)) + "]["+ string(ncols(M)) + "] = " + string(M) + ";" );
1727  return (A);
1728}
1729
1730
1731/******************************************************/
1732static proc leftKernelZ(intmat M)
1733"USAGE:  leftKernel(M);   M a matrix
1734RETURN:  module
1735PURPOSE: computes left kernel of matrix M (a module of all elements v such that vM=0)
1736EXAMPLE: example leftKernel; shows an example
1737"
1738{
1739  if( nameof(basering) != "basering" )
1740  {
1741    def save = basering;
1742  }
1743
1744  ring r = integer, (x), dp;
1745
1746
1747  //  basering;
1748  module N = matrix((M)); // transpose
1749  //  print(N);
1750
1751  def MM = modulo( N, std(0) ) ;
1752  //  print(MM);
1753
1754  intmat R = (  matrix2intmat( MM ) ); // transpose
1755
1756  if( defined(save) > 0 )
1757  {
1758    setring save;
1759  }
1760
1761  kill r;
1762  return( R );
1763}
1764example
1765{
1766  "EXAMPLE:"; echo=2;
1767
1768  ring r= 0,(x,y,z),dp;
1769  matrix M[3][1] = x,y,z;
1770  print(M);
1771  matrix L = leftKernel(M);
1772  print(L);
1773  // check:
1774  print(L*M);
1775};
1776
1777
1778
1779/******************************************************/
1780// the following is taken from "sing4ti2.lib" as we need 'hilbert' from 4ti2
1781
1782static proc hilbert4ti2intmat(intmat A, list #)
1783"USAGE:  hilbert4ti2(A[,i]);
1784@*       A=intmat
1785@*       i=int
1786ASSUME:  - A is a matrix with integer entries which describes the lattice
1787@*         as ker(A), if second argument is not present,
1788@*         as the left image Im(A) = {zA : z \in ZZ^k}, if second argument is a positive integer
1789@*       - number of variables of basering equals number of columns of A
1790@*         (for ker(A)) resp. of rows of A (for Im(A))
1791CREATE:  temporary files sing4ti2.mat, sing4ti2.lat, sing4ti2.mar
1792@*       in the current directory (I/O files for communication with 4ti2)
1793NOTE:    input rules for 4ti2 also apply to input to this procedure
1794@*       hence ker(A)={x|Ax=0} and Im(A)={xA}
1795RETURN:  toric ideal specified by Hilbert basis thereof
1796EXAMPLE: example graver4ti2; shows an example
1797"
1798{
1799//--------------------------------------------------------------------------
1800// Initialization and Sanity Checks
1801//--------------------------------------------------------------------------
1802   int i,j;
1803   int nr=nrows(A);
1804   int nc=ncols(A);
1805   string fileending="mat";
1806   if (size(#)!=0)
1807   {
1808//--- default behaviour: use ker(A) as lattice
1809//--- if #[1]!=0 use Im(A) as lattice
1810      if(typeof(#[1])!="int")
1811      {
1812         ERROR("optional parameter needs to be integer value");
1813      }
1814      if(#[1]!=0)
1815      {
1816         fileending="lat";
1817      }
1818   }
1819//--- we should also be checking whether all entries are indeed integers
1820//--- or whether there are fractions, but in this case the error message
1821//--- of 4ti2 is printed directly
1822
1823//--------------------------------------------------------------------------
1824// preparing input file for 4ti2
1825//--------------------------------------------------------------------------
1826   link eing=":w sing4ti2."+fileending;
1827   string eingstring=string(nr)+" "+string(nc);
1828   write(eing,eingstring);
1829   for(i=1;i<=nr;i++)
1830   {
1831      kill eingstring;
1832      string eingstring;
1833      for(j=1;j<=nc;j++)
1834      {
1835        //          if(g(A[i,j])>0)||(char(basering)!=0)||(npars(basering)>0))
1836        //          {
1837        //             ERROR("Input to hilbert4ti2 needs to be a matrix with integer entries");
1838        //          }
1839        eingstring=eingstring+string(A[i,j])+" ";
1840      }
1841      write(eing, eingstring);
1842   }
1843   close(eing);
1844
1845//----------------------------------------------------------------------
1846// calling 4ti2 and converting output
1847// Singular's string is too clumsy for this, hence we first prepare
1848// using standard unix commands
1849//----------------------------------------------------------------------
1850   j=system("sh","hilbert -q -n sing4ti2"); ////////// be quiet + no loggin!!!
1851
1852   j=system("sh","awk \'BEGIN{ORS=\",\";}{print $0;}\' sing4ti2.hil | sed s/[\\\ \\\t\\\v\\\f]/,/g | sed s/,+/,/g |sed s/,,/,/g|sed s/,,/,/g > sing4ti2.converted");
1853   if( defined(keepfiles) <= 0)
1854   {
1855      j=system("sh",("rm -f sing4ti2.hil sing4ti2."+fileending));
1856   }
1857//----------------------------------------------------------------------
1858// reading output of 4ti2
1859//----------------------------------------------------------------------
1860   link ausg=":r sing4ti2.converted";
1861//--- last entry ideal(0) is used to tie the list to the basering
1862//--- it will not be processed any further
1863
1864   string s = read(ausg);
1865   string ergstr = "intvec erglist = " + s + "0;";
1866   execute(ergstr);
1867 
1868   //   print(erglist);
1869 
1870   int Rnc = erglist[1];
1871   int Rnr = erglist[2];
1872   
1873   intmat R[Rnr][Rnc];
1874
1875   int k = 3;
1876
1877   for(i=1;i<=Rnc;i++)
1878   {
1879     for(j=1;j<=Rnr;j++)
1880     {
1881       //       "i: ", i, ", j: ", j, ", v: ", erglist[k];
1882       R[j, i] = erglist[k];
1883       k = k + 1;
1884     }
1885   }
1886
1887   return (R);
1888//--- get rid of leading entry 0;
1889//   toric=toric[2..ncols(toric)];
1890//   return(toric);
1891}
1892// A nice example here is the 3x3 Magic Squares
1893example
1894{
1895  "EXAMPLE:"; echo=2;
1896
1897   ring r=0,(x1,x2,x3,x4,x5,x6,x7,x8,x9),dp;
1898   intmat M[7][9]=1,1,1,-1,-1,-1,0,0,0,1,1,1,0,0,0,-1,-1,-1,0,1,1,-1,0,0,-1,0,0,1,0,1,0,-1,0,0,-1,0,1,1,0,0,0,-1,0,0,-1,0,1,1,0,-1,0,0,0,-1,1,1,0,0,-1,0,-1,0,0;
1899   hilbert4ti2intmat(M);
1900   hermite(M);
1901}
1902
1903/////////////////////////////////////////////////////////////////////////////
1904static proc getMonomByExponent(intvec exp)
1905{
1906  int n = nvars(basering);
1907
1908  if( nrows(exp) < n )
1909  {
1910    n = nrows(exp);
1911  }
1912
1913  poly m = 1; int e;
1914
1915  for( int i = 1; i <= n; i++ )
1916  {
1917    e = exp[i];
1918    if( e < 0 )
1919    {
1920      ERROR("Negative exponent!!!");
1921    }
1922
1923    m = m * (var(i)^e);
1924  }
1925
1926  return (m);
1927
1928}
1929
1930/******************************************************/
1931proc mDegBasis(intvec d)
1932"
1933USAGE: multidegree d
1934ASSUME: current ring is multigraded, monomial ordering is global
1935PURPOSE: compute all monomials of multidegree d
1936"
1937{
1938  def R = basering;  // setring R;
1939
1940  intmat M = getVariableWeights(R);
1941
1942  //  print(M);
1943
1944  int nr = nrows(M);
1945  int nc = ncols(M);
1946
1947  intmat A[nr][nc+1];
1948  A[1..nr, 1..nc] = M[1..nr, 1..nc];
1949  //typeof(A[1..nr, nc+1]);
1950  if( nr==1)
1951  {
1952    A[1..nr, nc+1]=-d[1];
1953  }
1954  else
1955  {
1956    A[1..nr, nc+1] = -d;
1957  }
1958
1959  intmat T = getTorsion(R);
1960
1961  if( isFreeRepresented() )
1962  {
1963    intmat B = hilbert4ti2intmat(A);
1964
1965    //      matrix B = unitMatrix(nrows(T));
1966  }
1967  else
1968  {
1969    int n = ncols(T);
1970
1971    nc = ncols(A);
1972
1973    intmat AA[nr][nc + 2 * n];
1974    AA[1..nr, 1.. nc] = A[1..nr, 1.. nc];
1975    AA[1..nr, nc + (1.. n)] = T[1..nr, 1.. n];
1976    AA[1..nr, nc + n + (1.. n)] = -T[1..nr, 1.. n];
1977
1978
1979    //      print ( AA );
1980
1981    intmat K = leftKernelZ(( AA ) ); //
1982
1983    //      print(K);
1984
1985    intmat KK[nc][ncols(K)] = K[ 1.. nc, 1.. ncols(K) ];
1986
1987    //      print(KK);
1988    //      "!";
1989
1990    intmat B = hilbert4ti2intmat(transpose(KK), 1); 
1991
1992    //      "!";      print(B);
1993
1994  }
1995
1996
1997  //  print(A);
1998
1999
2000
2001  int i; 
2002  int nnr = nrows(B);
2003  int nnc = ncols(B);
2004  ideal I, J;
2005  if(nnc==0){
2006    I=0;
2007    return(I);
2008  }
2009  I[nnc] = 0;
2010  J[nnc] = 0;
2011
2012  for( i = 1; i <= nnc; i++ )
2013  {
2014    //      "i: ", i;    B[nnr, i];
2015
2016    if( B[nnr, i] == 1)
2017    {
2018      // intvec(B[1..nnr-1, i]);
2019      I[i] = getMonomByExponent(intvec(B[1..nnr-1, i]));
2020    }
2021    else
2022    {
2023      if( B[nnr, i] == 0)
2024      {
2025        // intvec(B[1..nnr-1, i]);
2026        J[i] = getMonomByExponent(intvec(B[1..nnr-1, i]));
2027      }
2028    }
2029    //      I[i];
2030  }
2031
2032  ideal Q = (ideal(basering));
2033
2034  if ( size(Q) > 0 )
2035  {
2036    I = NF( I, lead(Q) );
2037    J = NF( J, lead(Q) ); // Global ordering!!!
2038  }
2039
2040  I = simplify(I, 2); // d
2041  J = simplify(J, 2); // d
2042
2043  attrib(I, "ZeroPart", J);
2044
2045  return (I);
2046
2047  //  setring ;
2048}
2049example
2050{
2051  "EXAMPLE:"; echo=2;
2052
2053  ring R = 0, (x, y), dp;
2054
2055  intmat g1[2][2]=1,0,0,1;
2056  intmat t[2][1]=2,0;
2057  intmat g2[2][2]=1,1,1,1;
2058  intvec v1=4,0;
2059  intvec v2=4,4;
2060 
2061  intmat g3[1][2]=1,1;
2062  setBaseMultigrading(g3);
2063  intvec v3=4:1;
2064  v3;
2065  mDegBasis(v3);
2066 
2067  setBaseMultigrading(g1,t);
2068  mDegBasis(v1);
2069  setBaseMultigrading(g2);
2070  mDegBasis(v2);
2071 
2072  intmat M[2][2] = 1, -1, -1, 1;
2073  intvec d = -2, 2;
2074
2075  setBaseMultigrading(M);
2076
2077  mDegBasis(d);
2078  attrib(_, "ZeroPart");
2079
2080  kill R;
2081  ring R = 0, (x, y, z), dp;
2082
2083  intmat M[2][3] = 1, -2, 1,     1, 1, 0;
2084
2085  intmat T[2][1] = 0, 2;
2086
2087  intvec d = 4, 1;
2088
2089  setBaseMultigrading(M, T);
2090
2091  mDegBasis(d);
2092  attrib(_, "ZeroPart");
2093
2094
2095  kill R;
2096
2097  ring R = 0, (x, y, z), dp;
2098  qring Q = std(ideal( y^6+ x*y^3*z-x^2*z^2 ));
2099
2100
2101  intmat M[2][3] = 1, 1, 2,     2, 1, 1;
2102  //  intmat T[2][1] = 0, 2;
2103
2104  setBaseMultigrading(M);
2105
2106  intvec d = 6, 6;
2107  mDegBasis(d);
2108  attrib(_, "ZeroPart");
2109
2110
2111
2112  kill R;
2113  ring R = 0, (x, y, z), dp;
2114  qring Q = std(ideal( x*z^3 - y *z^6, x*y*z  - x^4*y^2 ));
2115
2116
2117  intmat M[2][3] = 1, -2, 1,     1, 1, 0;
2118  intmat T[2][1] = 0, 2;
2119
2120  intvec d = 4, 1;
2121
2122  setBaseMultigrading(M, T);
2123
2124  mDegBasis(d);
2125  attrib(_, "ZeroPart");
2126}
2127
2128
2129proc mDegSyzygy(def I)
2130"USAGE: mDegSyzygy(I); I is a poly/vector/ideal/module
2131PURPOSE: computes the multigraded syzygy of I
2132RETURNS: module, the syzygy of I
2133NOTE: generators of I must be multigraded homogeneous
2134"
2135{
2136  if( isHomogenous(I, "checkGens") == 0)
2137  {
2138    ERROR ("Sorry: inhomogenous input!");
2139  }
2140  module S = syz(I);
2141  S = setModuleGrading(S, mDeg(I));
2142  return (S);
2143}
2144example
2145{
2146  "EXAMPLE:"; echo=2;
2147 
2148
2149  ring r = 0,(x,y,z,w),dp;
2150
2151  intmat M[2][4]=
2152    1,1,1,1,
2153    0,1,3,4;
2154
2155  setBaseMultigrading(M);
2156
2157 
2158  module M = ideal(  xw-yz, x2z-y3, xz2-y2w, yw2-z3);
2159 
2160 
2161  intmat v[2][nrows(M)]=
2162    1,
2163    0;
2164 
2165  M = setModuleGrading(M, v);
2166
2167  isHomogenous(M);
2168  "Multidegrees: "; print(mDeg(M));
2169
2170  // Let's compute Syzygy!
2171  def S = mDegSyzygy(M); S;
2172  "Module Units Multigrading: "; print( getModuleGrading(S) );
2173  "Multidegrees: "; print(mDeg(S));
2174
2175  isHomogenous(S);
2176}
2177
2178
2179proc mDegGroebner(def I)
2180"USAGE: mDegGroebner(I); I is a poly/vector/ideal/module
2181PURPOSE: computes the multigraded standard/groebner basis of I
2182NOTE: I must be multigraded homogeneous
2183RETURNS: ideal/module, the computed basis
2184"
2185{
2186  if( isHomogenous(I) == 0)
2187  {
2188    ERROR ("Sorry: inhomogenous input!");
2189  }
2190
2191  def S = groebner(I);
2192 
2193  if( typeof(I) == "module" or typeof(I) == "vector" )
2194  {
2195    S = setModuleGrading(S, getModuleGrading(I));     
2196  }
2197
2198  return(S);
2199}
2200example
2201{
2202  "EXAMPLE:"; echo=2;
2203
2204  ring r = 0,(x,y,z,w),dp;
2205
2206  intmat M[2][4]=
2207    1,1,1,1,
2208    0,1,3,4;
2209
2210  setBaseMultigrading(M);
2211
2212 
2213  module M = ideal(  xw-yz, x2z-y3, xz2-y2w, yw2-z3);
2214 
2215 
2216  intmat v[2][nrows(M)]=
2217    1,
2218    0;
2219 
2220  M = setModuleGrading(M, v);
2221
2222
2223  /////////////////////////////////////////////////////////////////////////////
2224  // GB:
2225  M = mDegGroebner(M); M;
2226  "Module Units Multigrading: "; print( getModuleGrading(M) );
2227  "Multidegrees: "; print(mDeg(M));
2228
2229  isHomogenous(M);
2230
2231  /////////////////////////////////////////////////////////////////////////////
2232  // Let's compute Syzygy!
2233  def S = mDegSyzygy(M); S;
2234  "Module Units Multigrading: "; print( getModuleGrading(S) );
2235  "Multidegrees: "; print(mDeg(S));
2236
2237  isHomogenous(S);
2238
2239  /////////////////////////////////////////////////////////////////////////////
2240  // GB:
2241  S = mDegGroebner(S); S;
2242  "Module Units Multigrading: "; print( getModuleGrading(S) );
2243  "Multidegrees: "; print(mDeg(S));
2244
2245  isHomogenous(S);
2246}
2247
2248
2249/******************************************************/
2250proc mDegResolution(def I, int ll, list #)
2251"USAGE: mDegResolution(I,l,[f]); I is poly/vector/ideal/module; l,f are integers
2252PURPOSE: computes the multigraded resolution of I of the length l,
2253or the whole resolution if l is zero. Returns minimal resolution if an optional
2254argument 1 is supplied
2255NOTE: input must have multigraded-homogeneous generators.
2256The returned list is trunkated at the first zero entry.
2257RETURNS: list, the computed resolution
2258"
2259{
2260  if( isHomogenous(I, "checkGens") == 0)
2261  {
2262    ERROR ("Sorry: inhomogenous input!");
2263  }
2264
2265  def R = res(I, ll, #); list L = R; int l = size(L);
2266
2267  if( (typeof(I) == "module") or (typeof(I) == "vector") )
2268  {
2269    L[1] = setModuleGrading(L[1], getModuleGrading(I));     
2270  }
2271
2272  int i;
2273  for( i = 2; i <= l; i++ )
2274  {
2275    if( size(L[i]) > 0 )
2276    {
2277      L[i] = setModuleGrading( L[i], mDeg(L[i-1]) );
2278    } else
2279    {
2280      return (L[1..(i-1)]);
2281    }
2282  }
2283 
2284  return (L);
2285
2286 
2287}
2288example
2289{
2290  "EXAMPLE:"; echo=2;
2291 
2292  ring r = 0,(x,y,z,w),dp;
2293
2294  intmat M[2][4]=
2295    1,1,1,1,
2296    0,1,3,4;
2297
2298  setBaseMultigrading(M);
2299
2300 
2301  module m= ideal(  xw-yz, x2z-y3, xz2-y2w, yw2-z3);
2302 
2303  isHomogenous(ideal(  xw-yz, x2z-y3, xz2-y2w, yw2-z3), "checkGens");
2304 
2305  ideal A = xw-yz, x2z-y3, xz2-y2w, yw2-z3;
2306
2307  int j;
2308 
2309  for(j=1; j<=ncols(A); j++)
2310  {
2311    mDegPartition(A[j]);
2312  }
2313 
2314  intmat v[2][1]=
2315    1,
2316    0;
2317 
2318  m = setModuleGrading(m, v);
2319
2320  // Let's compute Syzygy!
2321  def S = mDegSyzygy(m); S;
2322  "Module Units Multigrading: "; print( getModuleGrading(S) );
2323  "Multidegrees: "; print(mDeg(S));
2324
2325  /////////////////////////////////////////////////////////////////////////////
2326
2327  S = mDegGroebner(S); S;
2328  "Module Units Multigrading: "; print( getModuleGrading(S) );
2329  "Multidegrees: "; print(mDeg(S));
2330
2331  /////////////////////////////////////////////////////////////////////////////
2332
2333  def L = mDegResolution(m, 0, 1);
2334
2335  for( j =1; j<=size(L); j++)
2336  {
2337    "----------------------------------- ", j, " -----------------------------";
2338    L[j];
2339    "Module Multigrading: "; print( getModuleGrading(L[j]) );
2340    "Multigrading: "; print(mDeg(L[j]));
2341  }
2342
2343  /////////////////////////////////////////////////////////////////////////////
2344 
2345  L = mDegResolution(maxideal(1), 0, 1);
2346
2347  for( j =1; j<=size(L); j++)
2348  {
2349    "----------------------------------- ", j, " -----------------------------";
2350    L[j];
2351    "Module Multigrading: "; print( getModuleGrading(L[j]) );
2352    "Multigrading: "; print(mDeg(L[j]));
2353  }
2354 
2355  kill v;
2356 
2357
2358  def h = hilbertSeries(m);
2359  setring h;
2360
2361  numerator1;
2362  factorize(numerator1);
2363 
2364  denominator1;
2365  factorize(denominator1);
2366
2367  numerator2;
2368  factorize(numerator2);
2369
2370  denominator2;
2371  factorize(denominator2);
2372}
2373
2374/******************************************************/
2375proc hilbertSeries(def I)
2376"USAGE: hilbertSeries(I); I is poly/vector/ideal/module
2377PURPOSE: computes the multigraded Hilbert Series of M
2378NOTE: input must have multigraded-homogeneous generators.
2379Multigrading should be positive.
2380RETURNS: a ring in variables t_(i), s_(i), with polynomials
2381numerator1 and denominator1 and muturally prime numerator2
2382and denominator2, quotients of which give the series.
2383"
2384{
2385   
2386  if( !isFreeRepresented() )
2387  {
2388    ERROR("SORRY: ONLY TORSION-FREE CASE (POSITIVE GRADING)");
2389  }
2390   
2391  int i, j, k, v;
2392
2393  intmat M = getVariableWeights();
2394 
2395  int cc = ncols(M);
2396  int n = nrows(M);
2397
2398  if( n == 0 )
2399  {
2400    ERROR("Error: wrong Variable Weights?");
2401  }
2402
2403  list RES = mDegResolution(I,0,1);
2404
2405  int l = size(RES);
2406 
2407  list L; L[l + 1] = 0;
2408
2409  if(typeof(I) == "ideal")
2410  {
2411    intmat zeros[n][1];
2412    L[1] = zeros;
2413  }
2414  else
2415  {
2416    L[1] = getModuleGrading(RES[1]);
2417  }
2418
2419  for( j = 1; j <= l; j++)
2420  {
2421    L[j + 1] = mDeg(RES[j]);
2422  }
2423 
2424  l++;
2425
2426  ring R = 0,(t_(1..n),s_(1..n)),dp;
2427 
2428  ideal units;
2429  for( i=n; i>=1; i--)
2430  {
2431    units[i] = (var(i) * var(n + i) - 1);
2432  }
2433 
2434  qring Q = std(units);
2435 
2436  // TODO: should not it be a quotient ring depending on Torsion???
2437  // I am not sure about what to do in the torsion case, but since
2438  // we want to evaluate the polynomial at certain points to get
2439  // a dimension we need uniqueness for this. I think we would lose
2440  // this uniqueness if switching to this torsion ring.
2441
2442  poly monom, summand, numerator;
2443  poly denominator = 1;
2444 
2445  for( i = 1; i <= cc; i++)
2446  {
2447    monom = 1;
2448
2449    for( k = 1; k <= n; k++)
2450    {
2451      v = M[k,i];
2452
2453      if(v >= 0)
2454      {
2455        monom = monom * (var(k)^(v));
2456      }
2457      else
2458      {
2459        monom = monom * (var(n+k)^(-v));
2460      }
2461    }
2462   
2463    if( monom == 1)
2464    {
2465      ERROR("Multigrading not positive.");
2466    }
2467
2468    denominator = denominator * (1 - monom);
2469  }
2470 
2471  for( j = 1; j<= l; j++)
2472  {
2473    summand = 0;
2474    M = L[j];
2475
2476    for( i = 1; i <= ncols(M); i++)
2477    {
2478      monom = 1;
2479      for( k = 1; k <= n; k++)
2480      {
2481        v = M[k,i];
2482        if( v > 0 )
2483        {
2484          monom = monom * (var(k)^v);
2485        }
2486        else
2487        {
2488          monom = monom * (var(n+k)^(-v));
2489        }
2490      }
2491      summand = summand + monom;
2492    }
2493    numerator = numerator - (-1)^j * summand;
2494  }
2495 
2496  if( denominator == 0 )
2497  {
2498    ERROR("Multigrading not positive.");
2499  }
2500 
2501  poly denominator1 = denominator;
2502  poly numerator1 = numerator;
2503
2504  export denominator1;
2505  export numerator1;
2506
2507  if( numerator != 0 )
2508  {
2509    poly d = gcd(denominator, numerator);
2510
2511    poly denominator2 = denominator/d;
2512    poly numerator2 = numerator/d;
2513
2514    if( gcd(denominator2, numerator2) != 1 )
2515    {
2516      ERROR("Sorry: gcd should be 1 (after dividing out gcd)! Something went wrong!");
2517    }
2518  }
2519  else
2520  {
2521    poly denominator2 = denominator;
2522    poly numerator2 = numerator;
2523  }
2524
2525
2526  export denominator2;
2527  export numerator2;
2528
2529  " ------------ ";
2530  "This proc returns a ring with polynomials called 'numerator1/2' and 'denominator1/2'!";
2531  "They represent the first and the second Hilbert Series.";
2532  "The s_(i)-variables are defined to be the inverse of the t_(i)-variables.";
2533  " ------------ ";
2534 
2535  return(Q);
2536}
2537example
2538{
2539  "EXAMPLE:"; echo=2;
2540 
2541  ring r = 0,(x,y,z,w),dp;
2542  intmat g[2][4]=
2543    1,1,1,1,
2544    0,1,3,4;
2545  setBaseMultigrading(g);
2546 
2547  module M = ideal(xw-yz, x2z-y3, xz2-y2w, yw2-z3);
2548  intmat V[2][1]=
2549    1,
2550    0;
2551
2552  M = setModuleGrading(M, V);
2553
2554  def h = hilbertSeries(M); setring h;
2555
2556  factorize(numerator2);
2557  factorize(denominator2);
2558 
2559  kill g, h; setring r;
2560
2561  intmat g[2][4]=
2562    1,2,3,4,
2563    0,0,5,8;
2564 
2565  setBaseMultigrading(g);
2566 
2567  ideal I = x^2, y, z^3;
2568  I = std(I);
2569  def L = mDegResolution(I, 0, 1);
2570
2571  for( int j = 1; j<=size(L); j++)
2572  {
2573    "----------------------------------- ", j, " -----------------------------";
2574    L[j];
2575    "Module Multigrading: "; print( getModuleGrading(L[j]) );
2576    "Multigrading: "; print(mDeg(L[j]));
2577  }
2578
2579  mDeg(I);
2580  def h = hilbertSeries(I); setring h;
2581 
2582  factorize(numerator2);
2583  factorize(denominator2);
2584
2585  kill r, h, g, V;
2586  ////////////////////////////////////////////////
2587  ////////////////////////////////////////////////
2588 
2589  ring R = 0,(x,y,z),dp;
2590  intmat W[2][3] =
2591     1,1, 1,
2592     0,0,-1;
2593  setBaseMultigrading(W);
2594  ideal I = x3y,yz2,y2z,z4;
2595 
2596  def h = hilbertSeries(I); setring h;
2597 
2598  factorize(numerator2);
2599  factorize(denominator2);
2600
2601  kill R, W, h;
2602  ////////////////////////////////////////////////
2603  ////////////////////////////////////////////////
2604
2605  ring R = 0,(x,y,z,a,b,c),dp;
2606  intmat W[2][6] =
2607     1,1, 1,1,1,1,
2608     0,0,-1,0,0,0;
2609  setBaseMultigrading(W);
2610  ideal I = x3y,yz2,y2z,z4;
2611 
2612  def h = hilbertSeries(I); setring h;
2613 
2614  factorize(numerator2);
2615  factorize(denominator2);
2616 
2617  kill R, W, h;
2618
2619  ////////////////////////////////////////////////
2620  ////////////////////////////////////////////////
2621  ////////////////////////////////////////////////
2622  // This is example 5.3.9. from Robbianos book.
2623 
2624  ring R = 0,(x,y,z,w),dp;
2625  intmat W[1][4] =
2626     1,1, 1,1;
2627  setBaseMultigrading(W);
2628  ideal I = z3,y3zw2,x2y4w2xyz2;
2629
2630  hilb(std(I));
2631 
2632  def h = hilbertSeries(I); setring h;
2633 
2634  numerator1;
2635  denominator1;
2636
2637  factorize(numerator2);
2638  factorize(denominator2);
2639 
2640
2641  kill h;
2642  ////////////////////////////////////////////////
2643  setring R;
2644
2645  ideal I2 = x2,y2,z2; I2;
2646
2647  hilb(std(I2));
2648 
2649  def h = hilbertSeries(I2); setring h;
2650
2651  numerator1;
2652  denominator1;
2653
2654
2655  kill h;
2656  ////////////////////////////////////////////////
2657  setring R;
2658 
2659  W = 2,2,2,2;
2660 
2661  setBaseMultigrading(W);
2662
2663  getVariableWeights();
2664
2665  intvec w = 2,2,2,2;
2666
2667  hilb(std(I2), 1, w);
2668
2669  kill w;
2670 
2671
2672  def h = hilbertSeries(I2); setring h;
2673
2674 
2675  numerator1; denominator1;
2676  kill h;
2677
2678 
2679  kill R, W;
2680
2681  ////////////////////////////////////////////////
2682  ////////////////////////////////////////////////
2683  ////////////////////////////////////////////////
2684
2685  ring R = 0,(x),dp;
2686  intmat W[1][1] =
2687     1;
2688  setBaseMultigrading(W);
2689
2690  ideal I;
2691
2692  I = 1; I;
2693
2694  hilb(std(I));
2695 
2696  def h = hilbertSeries(I); setring h;
2697
2698  numerator1; denominator1;
2699
2700  kill h;
2701  ////////////////////////////////////////////////
2702  setring R;
2703
2704  I = x; I;
2705
2706  hilb(std(I));
2707
2708  def h = hilbertSeries(I); setring h;
2709
2710  numerator1; denominator1;
2711 
2712  kill h; 
2713  ////////////////////////////////////////////////
2714  setring R;
2715
2716  I = x^5; I;
2717
2718  hilb(std(I));
2719  hilb(std(I), 1);
2720
2721  def h = hilbertSeries(I); setring h;
2722
2723  numerator1; denominator1;
2724 
2725 
2726  kill h; 
2727  ////////////////////////////////////////////////
2728  setring R;
2729
2730  I = x^10; I;
2731
2732  hilb(std(I));
2733
2734  def h = hilbertSeries(I); setring h;
2735
2736  numerator1; denominator1;
2737
2738  kill h;
2739  ////////////////////////////////////////////////
2740  setring R;
2741
2742  module M = 1;
2743
2744  M = setModuleGrading(M, W);
2745
2746 
2747  hilb(std(M));
2748
2749  def h = hilbertSeries(M); setring h;
2750
2751  numerator1; denominator1;
2752
2753  kill h;
2754  ////////////////////////////////////////////////
2755  setring R;
2756
2757  kill M; module M = x^5*gen(1);
2758
2759//  intmat V[1][3] = 0; // TODO: this would lead to a wrong result!!!?
2760
2761  intmat V[1][1] = 0; // all gen(i) of degree 0!
2762
2763  M = setModuleGrading(M, V);
2764
2765  hilb(std(M));
2766
2767  def h = hilbertSeries(M); setring h;
2768
2769  numerator1; denominator1;
2770
2771  kill h;
2772  ////////////////////////////////////////////////
2773  setring R;
2774
2775  module N = x^5*gen(3);
2776
2777  kill V;
2778 
2779  intmat V[1][3] = 0; // all gen(i) of degree 0!
2780
2781  N = setModuleGrading(N, V);
2782     
2783  hilb(std(N));
2784
2785  def h = hilbertSeries(N); setring h;
2786
2787  numerator1; denominator1;
2788
2789  kill h;
2790  ////////////////////////////////////////////////
2791  setring R;
2792
2793 
2794  module S = M + N;
2795 
2796  S = setModuleGrading(S, V);
2797
2798  hilb(std(S));
2799
2800  def h = hilbertSeries(S); setring h;
2801
2802  numerator1; denominator1;
2803
2804  kill h;
2805
2806  kill V;
2807  kill R, W;
2808
2809}
2810
2811
2812
2813///////////////////////////////////////////////////////////////////////////////
2814// testing for consistency of the library:
2815proc testMultigradingLib ()
2816{
2817  example setBaseMultigrading;
2818  example setModuleGrading;
2819
2820  example getVariableWeights;
2821  example getTorsion;
2822  example getModuleGrading;
2823
2824
2825  example mDeg;
2826  example mDegPartition;
2827
2828
2829  example hermite;
2830  example isHomogenous;
2831  example isTorsionFree;
2832  example pushForward;
2833  example defineHomogenous;
2834
2835  example equalMDeg;
2836  example isTorsionElement;
2837
2838  example mDegResolution;
2839  example hilbertSeries;
2840
2841
2842// example mDegBasis; // needs 4ti2!
2843}
Note: See TracBrowser for help on using the repository browser.