source: git/Singular/LIB/freegb.lib @ ba49d9f

spielwiese
Last change on this file since ba49d9f was ba49d9f, checked in by Viktor Levandovskyy <levandov@…>, 16 years ago
*levandov: some shift GB fixes and auxiliary procs git-svn-id: file:///usr/local/Singular/svn/trunk@10617 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 27.2 KB
Line 
1//////////////////////////////////////////////////////////////////////////////
2version="$Id: freegb.lib,v 1.8 2008-03-13 19:26:16 levandov Exp $";
3category="Noncommutative";
4info="
5LIBRARY: ratgb.lib  Twosided Noncommutative Groebner bases in Free Algebras
6AUTHOR: Viktor Levandovskyy,     levandov@math.rwth-aachen.de
7
8PROCEDURES:
9freegbasis(list L, int n);   compute two-sided Groebner basis of ideal, encoded via L, up to degree n
10lst2str(list L);         convert a list (of modules) into polynomials in free algebra
11mod2str(module M);       convert a module into a polynomial in free algebra
12"
13
14// this library computes two-sided GB of an ideal
15// in a free associative algebra
16
17// a monomial is encoded via a vector V
18// where V[1] = coefficient
19// V[1+i] = the corresponding symbol
20
21LIB "qhmoduli.lib"; // for Max
22
23proc vct2mono(vector v)
24{
25  // produces a monomial in new vars
26  // need a ring with new vars!!
27}
28
29proc lshift(module M, int s, string varing, def lpring)
30{
31  // FINALLY IMPLEMENTED AS A PART OT THE CODE
32  // shifts a poly from the ring @R to s positions
33  // M lives in varing, the result in lpring
34  // to be run from varing
35  int i, j, k, sm, sv;
36  vector v;
37  //  execute("setring "+lpring);
38  setring lpring;
39  poly @@p;
40  ideal I;
41  execute("setring "+varing);
42  sm = ncols(M);
43  for (i=1; i<=s; i++)
44  {
45    // modules, e.g. free polynomials
46    for (j=1; j<=sm; j++)
47    {
48      //vectors, e.g. free monomials
49      v  = M[j];
50      sv = size(v);
51      sp = "@@p = @@p + ";
52      for (k=2; k<=sv; k++)
53      {
54        sp = sp + string(v[k])+"("+string(k-1+s)+")*";
55      }
56      sp = sp + string(v[1])+";"; // coef;
57      setring lpring;
58      //      execute("setring "+lpring);
59      execute(sp);
60      execute("setring "+varing);
61    }
62    setring lpring;
63    //    execute("setring "+lpring);
64    I = I,@@p;
65    @@p = 0;
66  }
67  setring lpring;
68  //execute("setring "+lpring);
69  export(I);
70  //  setring varing;
71  execute("setring "+varing);
72}
73
74proc skip0(vector v)
75{
76  // skips zeros in vector
77  int sv = nrows(v);
78  int sw = size(v);
79  if (sv == sw)
80  {
81    return(v);
82  }
83  int i;
84  int j=1;
85  vector w;
86  for (i=1; i<=sv; i++)
87  {
88    if (v[i] != 0)
89    {
90      w = w + v[i]*gen(j);
91      j++;
92    }
93  }
94  return(w);
95}
96
97proc lst2str(list L)
98"USAGE:  lst2str(L);  L a list of modules
99RETURN:  list (of strings)
100PURPOSE: convert a list (of modules) into polynomials in free algebra
101EXAMPLE: example lst2str; shows examples
102"
103{
104  // returns a list of strings
105  // being sentences in words built from L
106  int i;
107  int s    = size(L);
108  list N;
109  for(i=1; i<=s; i++)
110  {
111    if ((typeof(L[i]) == "module") || (typeof(L[i]) == "matrix") )
112    {
113      N[i] = mod2str(L[i]);
114    }
115    else
116    {
117      "module or matrix expected in the list";
118      return(N);
119    }
120  }
121  return(N);
122}
123example
124{
125  "EXAMPLE:"; echo = 2;
126  ring r = 0,(x,y,z),(dp(1),dp(2));
127  module M = [-1,x,y],[-7,y,y],[3,x,x];
128  module N = [1,x,y,x,y],[-2,y,x,y,x],[6,x,y,y,x,y];
129  list L; L[1] = M; L[2] = N;
130  lst2str(L);
131}
132
133
134proc mod2str(module M)
135"USAGE:  mod2str(M);  M a module
136RETURN:  string
137PURPOSE: convert a modules into a polynomial in free algebra
138EXAMPLE: example mod2str; shows examples
139"
140{
141  // returns a string
142  // a sentence in words built from M
143  int i;
144  int s    = ncols(M);
145  string t;
146  string mp;
147  for(i=1; i<=s; i++)
148  {
149    mp = vct2str(M[i]);
150    if (mp[1] == "-")
151    {
152      t = t + mp;
153    }
154    else
155    {
156      t = t + "+" + mp;
157    }
158  }
159  if (t[1]=="+")
160  {
161    t = t[2..size(t)]; // remove first "+"
162  }
163  return(t);
164}
165example
166{
167  "EXAMPLE:"; echo = 2;
168  ring r = 0,(x,y,z),(dp);
169  module M = [1,x,y,x,y],[-2,y,x,y,x],[6,x,y,y,x,y];
170  mod2str(M);
171}
172
173proc vct2str(vector v)
174{
175  int ppl = printlevel-voice+2;
176  // for a word, encoded by v
177  // produces a string for it
178  v = skip0(v);
179  number cf = leadcoef(v[1]);
180  int s = size(v);
181  string vs,vv,vp,err;
182  int i,j,p,q;
183  for (i=1; i<=s-1; i++)
184  {
185    p     = IsVar(v[i+1]);
186    if (p==0)
187    {
188      err = "Error: monomial expected at" + string(i+1);
189      dbprint(ppl,err);
190      return("_");
191    }
192    if (p==1)
193    {
194      vs = vs + string(v[i+1]);
195    }
196    else //power
197    {
198      vv = string(v[i+1]);
199      q = find(vv,"^");
200      if (q==0)
201      {
202        q = find(vv,string(p));
203        if (q==0)
204        {
205          err = "error in find for string "+vv;
206          dbprint(ppl,err);
207          return("_");
208        }
209      }
210      // q>0
211      vp = vv[1..q-1];
212      for(j=1;j<=p;j++)
213      {
214        vs = vs + vp;
215      }
216    }
217  }
218  string scf;
219  if (cf == -1)
220  {
221    scf = "-";
222  }
223  else
224  {
225    scf = string(cf);
226    if (cf == 1)
227    {
228      scf = "";
229    }
230  }
231  vs = scf + vs;
232  return(vs);
233}
234example
235{
236  ring r = (0,a),(x,y3,z(1)),dp;
237  vector v = [-7,x,y3^4,x2,z(1)^3];
238  vct2str(v);
239  vector w = [-7a^5+6a,x,y3,y3,x,z(1),z(1)];
240  vct2str(w);
241}
242
243proc IsVar(poly p)
244{
245  // checks whether p is a variable indeed
246  // if it's a power of a variable, returns the power
247  if (p==0) {  return(0); } //"p=0";
248  poly q   = leadmonom(p);
249  if ( (p-lead(p)) !=0 ) { return(0); } // "p-lm(p)>0";
250  intvec v = leadexp(p);
251  int s = size(v);
252  int i=1;
253  int cnt = 0;
254  int pwr = 0;
255  for (i=1; i<=s; i++)
256  {
257    if (v[i] != 0)
258    {
259      cnt++;
260      pwr = v[i];
261    }
262  }
263  //  "cnt:";  cnt;
264  if (cnt==1) { return(pwr); }
265  else { return(0); }
266}
267example
268{
269  ring r = 0,(x,y),dp;
270  poly f = xy+1;
271  IsVar(f);
272  poly g = xy;
273  IsVar(g);
274  poly h = y^3;
275  IsVar(h);
276  poly i = 1;
277  IsVar(i);
278}
279
280// new conversion routines
281
282proc id2words(ideal I, int d)
283{
284  // input: ideal I of polys in letter-place notation
285  // in the ring with d real vars
286  // output: the list of strings: associative words
287  // extract names of vars
288  int i,m,n; string s; string place = "(1)";
289  list lv;
290  for(i=1; i<=d; i++)
291  {
292    s = string(var(i));
293    // get rid of place
294    n = find(s, place);
295    if (n>0)
296    {
297      s = s[1..n-1];
298    }
299    lv[i] = s;
300  }
301  poly p,q;
302  for (i=1; i<=ncols(I); i++)
303  {
304    if (I[i] != 0)
305    {
306      p = I[i];
307      while (p!=0)
308      {
309         q = leadmonom(p);
310         
311      }
312    }
313  }
314
315  return(lv);
316}
317example
318{
319  "EXAMPLE:"; echo = 2;
320  ring r = 0,(x(1),y(1),z(1)),dp;
321  ideal I = x(1)*y(2) -z(1)*x(2);
322  id2words(I,3);
323}
324
325
326
327proc mono2word(poly p, int d)
328{
329 
330}
331
332// given the element -7xy^2x, it is represented as [-7,x,y^2,x] or as [-7,x,y,y,x]
333// use the orig ord on (x,y,z) and expand it blockwise to (x(i),y(i),z(i))
334
335// the correspondences:
336// monomial in K<x,y,z>    <<--->> vector in R
337// polynomial in K<x,y,z>  <<--->> list of vectors (matrix/module) in R
338// ideal in K<x,y,z>       <<--->> list of matrices/modules in R
339
340
341// 1. form a new ring
342// 2. NOP
343// 3. compute GB -> with the kernel stuff
344// 4. skip shifted elts (check that no such exist?)
345// 5. go back to orig vars, produce strings/modules
346// 6. return the result
347
348proc freegbasis(list LM, int d)
349"USAGE:  freegbasis(L, d);  L a list of modules, d an integer
350RETURN:  ring
351PURPOSE: compute the two-sided Groebner basis of an ideal, encoded by L in
352the free associative algebra, up to degree d
353EXAMPLE: example freegbasis; shows examples
354"
355{
356  // d = up to degree, will be shifted to d+1
357  if (d<1) {"bad d"; return(0);}
358
359  int ppl = printlevel-voice+2;
360  string err = "";
361
362  int i,j,s;
363  def save = basering;
364  // determine max no of places in the input
365  int slm = size(LM); // numbers of polys in the ideal
366  int sm;
367  intvec iv;
368  module M;
369  for (i=1; i<=slm; i++)
370  {
371    // modules, e.g. free polynomials
372    M  = LM[i];
373    sm = ncols(M);
374    for (j=1; j<=sm; j++)
375    {
376      //vectors, e.g. free monomials
377      iv = iv, size(M[j])-1; // 1 place is reserved by the coeff
378    }
379  }
380  int D = Max(iv); // max size of input words
381  if (d<D) {"bad d"; return(LM);}
382  D = D + d-1;
383  //  D = d;
384  list LR  = ringlist(save);
385  list L, tmp;
386  L[1] = LR[1]; // ground field
387  L[4] = LR[4]; // quotient ideal
388  tmp  = LR[2]; // varnames
389  s = size(LR[2]);
390  for (i=1; i<=D; i++)
391  {
392    for (j=1; j<=s; j++)
393    {
394      tmp[i*s+j] = string(tmp[j])+"("+string(i+1)+")";
395    }
396  }
397  for (i=1; i<=s; i++)
398  {
399    tmp[i] = string(tmp[i])+"("+string(1)+")";
400  }
401  L[2] = tmp;
402  list OrigNames = LR[2];
403  // ordering: d blocks of the ord on r
404  // try to get whether the ord on r is blockord itself
405  s = size(LR[3]);
406  if (s==2)
407  {
408    // not a blockord, 1 block + module ord
409    tmp = LR[3][s]; // module ord
410    for (i=1; i<=D; i++)
411    {
412      LR[3][s-1+i] = LR[3][1];
413    }
414    LR[3][s+D] = tmp;
415  }
416  if (s>2)
417  {
418    // there are s-1 blocks
419    int nb = s-1;
420    tmp = LR[3][s]; // module ord
421    for (i=1; i<=D; i++)
422    {
423      for (j=1; j<=nb; j++)
424      {
425        LR[3][i*nb+j] = LR[3][j];
426      }
427    }
428    //    size(LR[3]);
429    LR[3][nb*(D+1)+1] = tmp;
430  }
431  L[3] = LR[3];
432  def @R = ring(L);
433  setring @R;
434  ideal I;
435  poly @p;
436  s = size(OrigNames);
437  //  "s:";s;
438  // convert LM to canonical vectors (no powers)
439  setring save;
440  kill M; // M was defined earlier
441  module M;
442  slm = size(LM); // numbers of polys in the ideal
443  int sv,k,l;
444  vector v;
445  //  poly p;
446  string sp;
447  setring @R;
448  poly @@p=0;
449  setring save;
450  for (l=1; l<=slm; l++)
451  {
452    // modules, e.g. free polynomials
453    M  = LM[l];
454    sm = ncols(M); // in intvec iv the sizes are stored
455    // modules, e.g. free polynomials
456    for (j=1; j<=sm; j++)
457    {
458      //vectors, e.g. free monomials
459      v  = M[j];
460      sv = size(v);
461      //        "sv:";sv;
462      sp = "@@p = @@p + ";
463      for (k=2; k<=sv; k++)
464      {
465        sp = sp + string(v[k])+"("+string(k-1)+")*";
466      }
467      sp = sp + string(v[1])+";"; // coef;
468      setring @R;
469      execute(sp);
470      setring save;
471    }
472    setring @R;
473    //      "@@p:"; @@p;
474    I = I,@@p;
475    @@p = 0;
476    setring save;
477  }
478  kill sp;
479  // 3. compute GB
480  setring @R;
481  dbprint(ppl,"computing GB");
482  ideal J = system("freegb",I,d,nvars(save));
483  //  ideal J = slimgb(I);
484  dbprint(ppl,J);
485  // 4. skip shifted elts
486  ideal K = select1(J,1,s); // s = size(OrigNames)
487  dbprint(ppl,K);
488  dbprint(ppl, "done with GB");
489  // K contains vars x(1),...z(1) = images of originals
490  // 5. go back to orig vars, produce strings/modules
491  if (K[1] == 0)
492  {
493    "no reasonable output, GB gives 0";
494    return(0);
495  }
496  int sk = size(K);
497  int sp, sx, a, b;
498  intvec x;
499  poly p,q;
500  poly pn;
501  // vars in 'save'
502  setring save;
503  module N;
504  list LN;
505  vector V;
506  poly pn;
507  // test and skip exponents >=2
508  setring @R;
509  for(i=1; i<=sk; i++)
510  {
511    p  = K[i];
512    while (p!=0)
513    {
514      q  = lead(p);
515      //      "processing q:";q;
516      x  = leadexp(q);
517      sx = size(x);
518      for(k=1; k<=sx; k++)
519      {
520        if ( x[k] >= 2 )
521        {
522          err = "skip: the value x[k] is " + string(x[k]);
523          dbprint(ppl,err);
524          //        return(0);
525          K[i] = 0;
526          p    = 0;
527          q    = 0;
528          break;
529        }
530      }
531      p  = p - q;
532    }
533  }
534  K  = simplify(K,2);
535  sk = size(K);
536  for(i=1; i<=sk; i++)
537  {
538    //    setring save;
539    //    V  = 0;
540    setring @R;
541    p  = K[i];
542    while (p!=0)
543    {
544      q  = lead(p);
545      err =  "processing q:" + string(q);
546      dbprint(ppl,err);
547      x  = leadexp(q);
548      sx = size(x);
549      pn = leadcoef(q);
550      setring save;
551      pn = imap(@R,pn);
552      V  = V + leadcoef(pn)*gen(1);
553      for(k=1; k<=sx; k++)
554      {
555        if (x[k] ==1)
556        {
557          a = k / s; // block number=a+1, a!=0
558          b = k % s; // remainder
559          //      printf("a: %s, b: %s",a,b);
560          if (b == 0)
561          {
562            // that is it's the last var in the block
563            b = s;
564            a = a-1;
565          }
566          V = V + var(b)*gen(a+2);
567        }
568//      else
569//      {
570//        printf("error: the value x[k] is %s", x[k]);
571//        return(0);
572//      }
573      }
574      err = "V: " + string(V);
575      dbprint(ppl,err);
576      //      printf("V: %s", string(V));
577      N = N,V;
578      V  = 0;
579      setring @R;
580      p  = p - q;
581      pn = 0;
582    }
583    setring save;
584    LN[i] = simplify(N,2);
585    N     = 0;
586  }
587  setring save;
588  return(LN);
589}
590example
591{
592  "EXAMPLE:"; echo = 2;
593  ring r = 0,(x,y,z),(dp(1),dp(2));
594  module M = [-1,x,y],[-7,y,y],[3,x,x];
595  module N = [1,x,y,x],[-1,y,x,y];
596  list L; L[1] = M; L[2] = N;
597  lst2str(L);
598  def U = freegbasis(L,5);
599  lst2str(U);
600}
601
602proc crs(list LM, int d)
603"USAGE:  crs(L, d);  L a list of modules, d an integer
604RETURN:  ring
605PURPOSE: create a ring and shift the ideal
606EXAMPLE: example crs; shows examples
607"
608{
609  // d = up to degree, will be shifted to d+1
610  if (d<1) {"bad d"; return(0);}
611
612  int ppl = printlevel-voice+2;
613  string err = "";
614
615  int i,j,s;
616  def save = basering;
617  // determine max no of places in the input
618  int slm = size(LM); // numbers of polys in the ideal
619  int sm;
620  intvec iv;
621  module M;
622  for (i=1; i<=slm; i++)
623  {
624    // modules, e.g. free polynomials
625    M  = LM[i];
626    sm = ncols(M);
627    for (j=1; j<=sm; j++)
628    {
629      //vectors, e.g. free monomials
630      iv = iv, size(M[j])-1; // 1 place is reserved by the coeff
631    }
632  }
633  int D = Max(iv); // max size of input words
634  if (d<D) {"bad d"; return(LM);}
635  D = D + d-1;
636  //  D = d;
637  list LR  = ringlist(save);
638  list L, tmp;
639  L[1] = LR[1]; // ground field
640  L[4] = LR[4]; // quotient ideal
641  tmp  = LR[2]; // varnames
642  s = size(LR[2]);
643  for (i=1; i<=D; i++)
644  {
645    for (j=1; j<=s; j++)
646    {
647      tmp[i*s+j] = string(tmp[j])+"("+string(i)+")";
648    }
649  }
650  for (i=1; i<=s; i++)
651  {
652    tmp[i] = string(tmp[i])+"("+string(0)+")";
653  }
654  L[2] = tmp;
655  list OrigNames = LR[2];
656  // ordering: d blocks of the ord on r
657  // try to get whether the ord on r is blockord itself
658  s = size(LR[3]);
659  if (s==2)
660  {
661    // not a blockord, 1 block + module ord
662    tmp = LR[3][s]; // module ord
663    for (i=1; i<=D; i++)
664    {
665      LR[3][s-1+i] = LR[3][1];
666    }
667    LR[3][s+D] = tmp;
668  }
669  if (s>2)
670  {
671    // there are s-1 blocks
672    int nb = s-1;
673    tmp = LR[3][s]; // module ord
674    for (i=1; i<=D; i++)
675    {
676      for (j=1; j<=nb; j++)
677      {
678        LR[3][i*nb+j] = LR[3][j];
679      }
680    }
681    //    size(LR[3]);
682    LR[3][nb*(D+1)+1] = tmp;
683  }
684  L[3] = LR[3];
685  def @R = ring(L);
686  setring @R;
687  ideal I;
688  poly @p;
689  s = size(OrigNames);
690  //  "s:";s;
691  // convert LM to canonical vectors (no powers)
692  setring save;
693  kill M; // M was defined earlier
694  module M;
695  slm = size(LM); // numbers of polys in the ideal
696  int sv,k,l;
697  vector v;
698  //  poly p;
699  string sp;
700  setring @R;
701  poly @@p=0;
702  setring save;
703  for (l=1; l<=slm; l++)
704  {
705    // modules, e.g. free polynomials
706    M  = LM[l];
707    sm = ncols(M); // in intvec iv the sizes are stored
708    for (i=0; i<=d-iv[l]; i++)
709    {
710      // modules, e.g. free polynomials
711      for (j=1; j<=sm; j++)
712      {
713        //vectors, e.g. free monomials
714        v  = M[j];
715        sv = size(v);
716        //      "sv:";sv;
717        sp = "@@p = @@p + ";
718        for (k=2; k<=sv; k++)
719        {
720          sp = sp + string(v[k])+"("+string(k-2+i)+")*";
721        }
722        sp = sp + string(v[1])+";"; // coef;
723        setring @R;
724        execute(sp);
725        setring save;
726      }
727      setring @R;
728      //      "@@p:"; @@p;
729      I = I,@@p;
730      @@p = 0;
731      setring save;
732    }
733  }
734  setring @R;
735  export I;
736  return(@R);
737}
738example
739{
740  "EXAMPLE:"; echo = 2;
741  ring r = 0,(x,y,z),(dp(1),dp(2));
742  module M = [-1,x,y],[-7,y,y],[3,x,x];
743  module N = [1,x,y,x],[-1,y,x,y];
744  list L; L[1] = M; L[2] = N;
745  lst2str(L);
746  def U = crs(L,5);
747  setring U; U;
748  I;
749}
750
751proc polylen(ideal I)
752{
753  // returns the ideal of length of polys
754  int i;
755  intvec J;
756  number s = 0;
757  for(i=1;i<=ncols(I);i++)
758  {
759    J[i] = size(I[i]);
760    s = s + J[i];
761  }
762  printf("the sum of length %s",s);
763  //  print(s);
764  return(J);
765}
766
767proc freegbRing(int d)
768"USAGE:  freegbRing(d); d an integer
769RETURN:  ring
770PURPOSE: creates a ring with d blocks of shifted original variables
771EXAMPLE: example freegbRing; shows examples
772"
773{
774  // d = up to degree, will be shifted to d+1
775  if (d<1) {"bad d"; return(0);}
776
777  int ppl = printlevel-voice+2;
778  string err = "";
779
780  int i,j,s;
781  def save = basering;
782  int D = d-1;
783  list LR  = ringlist(save);
784  list L, tmp;
785  L[1] = LR[1]; // ground field
786  L[4] = LR[4]; // quotient ideal
787  tmp  = LR[2]; // varnames
788  s = size(LR[2]);
789  for (i=1; i<=D; i++)
790  {
791    for (j=1; j<=s; j++)
792    {
793      tmp[i*s+j] = string(tmp[j])+"("+string(i+1)+")";
794    }
795  }
796  for (i=1; i<=s; i++)
797  {
798    tmp[i] = string(tmp[i])+"("+string(1)+")";
799  }
800  L[2] = tmp;
801  list OrigNames = LR[2];
802  // ordering: d blocks of the ord on r
803  // try to get whether the ord on r is blockord itself
804  // TODO: make L(2) ordering! exponent is maximally 2
805  s = size(LR[3]);
806  if (s==2)
807  {
808    // not a blockord, 1 block + module ord
809    tmp = LR[3][s]; // module ord
810    for (i=1; i<=D; i++)
811    {
812      LR[3][s-1+i] = LR[3][1];
813    }
814    LR[3][s+D] = tmp;
815  }
816  if (s>2)
817  {
818    // there are s-1 blocks
819    int nb = s-1;
820    tmp = LR[3][s]; // module ord
821    for (i=1; i<=D; i++)
822    {
823      for (j=1; j<=nb; j++)
824      {
825        LR[3][i*nb+j] = LR[3][j];
826      }
827    }
828    //    size(LR[3]);
829    LR[3][nb*(D+1)+1] = tmp;
830  }
831  L[3] = LR[3];
832  def @R = ring(L);
833  //  setring @R;
834  return (@R);
835}
836example
837{
838  "EXAMPLE:"; echo = 2;
839  ring r = 0,(x,y,z),(dp(1),dp(2));
840  def A = freegbRing(2);
841  setring A;
842  A;
843}
844
845
846proc ex_shift()
847{
848  LIB "freegb.lib";
849  ring r = 0,(x,y,z),(dp(1),dp(2));
850  module M = [-1,x,y],[-7,y,y],[3,x,x];
851  module N = [1,x,y,x],[-1,y,x,y];
852  list L; L[1] = M; L[2] = N;
853  lst2str(L);
854  def U = crs(L,5);
855  setring U; U;
856  I;
857  poly p = I[2]; // I[8];
858  p;
859  system("stest",p,7,7,3); // error -> the world is ok
860  poly q1 = system("stest",p,1,7,3); //ok
861  poly q6 = system("stest",p,6,7,3); //ok
862  system("btest",p,3); //ok
863  system("btest",q1,3); //ok
864  system("btest",q6,3); //ok
865}
866
867proc test_shrink()
868{
869  LIB "freegb.lib";
870  ring r =0,(x,y,z),dp;
871  int d = 5;
872  def R = freegbRing(d);
873  setring R;
874  poly p1 = x(1)*y(2)*z(3);
875  poly p2 = x(1)*y(4)*z(5);
876  poly p3 = x(1)*y(1)*z(3);
877  poly p4 = x(1)*y(2)*z(2);
878  poly p5 = x(3)*z(5);
879  poly p6 = x(1)*y(1)*x(3)*z(5);
880  poly p7 = x(1)*y(2)*x(3)*y(4)*z(5);
881  poly p8 = p1+p2+p3+p4+p5 + p6 + p7;
882  p1; system("shrinktest",p1,3);
883  p2; system("shrinktest",p2,3);
884  p3; system("shrinktest",p3,3);
885  p4; system("shrinktest",p4,3);
886  p5; system("shrinktest",p5,3);
887  p6; system("shrinktest",p6,3);
888  p7; system("shrinktest",p7,3);
889  p8; system("shrinktest",p8,3);
890  poly p9 = p1 + 2*p2 + 5*p5 + 7*p7;
891  p9; system("shrinktest",p9,3);
892}
893
894proc ex2()
895{
896  option(prot);
897  LIB "freegb.lib";
898  ring r = 0,(x,y),dp;
899  module M = [-1,x,y],[3,x,x]; // 3x^2 - xy
900  def U = freegb(M,7);
901  lst2str(U);
902}
903
904proc ex_nonhomog()
905{
906  option(prot);
907  LIB "freegb.lib";
908  ring r = 0,(x,y,h),dp;
909  list L;
910  module M;
911  M = [-1,y,y],[1,x,x,x];  // x3-y2
912  L[1] = M;
913  M = [1,x,h],[-1,h,x];  // xh-hx
914  L[2] = M;
915  M = [1,y,h],[-1,h,y];  // yh-hy
916  L[3] = M;
917  def U = freegb(L,4);
918  lst2str(U);
919  // strange elements in the basis
920}
921
922proc ex_nonhomog_comm()
923{
924  option(prot);
925  LIB "freegb.lib";
926  ring r = 0,(x,y),dp;
927  module M = [-1,y,y],[1,x,x,x];
928  def U = freegb(M,5);
929  lst2str(U);
930}
931
932proc ex_nonhomog_h()
933{
934  option(prot);
935  LIB "freegb.lib";
936  ring r = 0,(x,y,h),(a(1,1),dp);
937  module M = [-1,y,y,h],[1,x,x,x]; // x3 - y2h
938  def U = freegb(M,6);
939  lst2str(U);
940}
941
942proc ex_nonhomog_h2()
943{
944  option(prot);
945  LIB "freegb.lib";
946  ring r = 0,(x,y,h),(dp);
947  list L;
948  module M;
949  M = [-1,y,y,h],[1,x,x,x]; // x3 - y2h
950  L[1] = M;
951  M = [1,x,h],[-1,h,x]; // xh - hx
952  L[2] = M;
953  M = [1,y,h],[-1,h,y]; // yh - hy
954  L[3] = M;
955  def U = freegbasis(L,3);
956  lst2str(U);
957  // strange answer CHECK
958}
959
960
961proc ex_nonhomog_3()
962{
963  option(prot);
964  LIB "./freegb.lib";
965  ring r = 0,(x,y,z),(dp);
966  list L;
967  module M;
968  M = [1,z,y],[-1,x]; // zy - x
969  L[1] = M;
970  M = [1,z,x],[-1,y]; // zx - y
971  L[2] = M;
972  M = [1,y,x],[-1,z]; // yx - z
973  L[3] = M;
974  lst2str(L);
975  list U = freegb(L,4);
976  lst2str(U);
977  // strange answer CHECK
978}
979
980proc ex_densep_2()
981{
982  option(prot);
983  LIB "freegb.lib";
984  ring r = (0,a,b,c),(x,y),(Dp); // deglex
985  module M = [1,x,x], [a,x,y], [b,y,x], [c,y,y];
986  lst2str(M);
987  list U = freegb(M,5);
988  lst2str(U);
989  // a=b is important -> finite basis!!!
990  module M = [1,x,x], [a,x,y], [a,y,x], [c,y,y];
991  lst2str(M);
992  list U = freegb(M,5);
993  lst2str(U);
994}
995
996
997// 1. form a new ring
998// 2. produce shifted generators
999// 3. compute GB
1000// 4. skip shifted elts
1001// 5. go back to orig vars, produce strings/modules
1002// 6. return the result
1003
1004proc freegbold(list LM, int d)
1005"USAGE:  freegbold(L, d);  L a list of modules, d an integer
1006RETURN:  ring
1007PURPOSE: compute the two-sided Groebner basis of an ideal, encoded by L in
1008the free associative algebra, up to degree d
1009EXAMPLE: example freegbold; shows examples
1010"
1011{
1012  // d = up to degree, will be shifted to d+1
1013  if (d<1) {"bad d"; return(0);}
1014
1015  int ppl = printlevel-voice+2;
1016  string err = "";
1017
1018  int i,j,s;
1019  def save = basering;
1020  // determine max no of places in the input
1021  int slm = size(LM); // numbers of polys in the ideal
1022  int sm;
1023  intvec iv;
1024  module M;
1025  for (i=1; i<=slm; i++)
1026  {
1027    // modules, e.g. free polynomials
1028    M  = LM[i];
1029    sm = ncols(M);
1030    for (j=1; j<=sm; j++)
1031    {
1032      //vectors, e.g. free monomials
1033      iv = iv, size(M[j])-1; // 1 place is reserved by the coeff
1034    }
1035  }
1036  int D = Max(iv); // max size of input words
1037  if (d<D) {"bad d"; return(LM);}
1038  D = D + d-1;
1039  //  D = d;
1040  list LR  = ringlist(save);
1041  list L, tmp;
1042  L[1] = LR[1]; // ground field
1043  L[4] = LR[4]; // quotient ideal
1044  tmp  = LR[2]; // varnames
1045  s = size(LR[2]);
1046  for (i=1; i<=D; i++)
1047  {
1048    for (j=1; j<=s; j++)
1049    {
1050      tmp[i*s+j] = string(tmp[j])+"("+string(i+1)+")";
1051    }
1052  }
1053  for (i=1; i<=s; i++)
1054  {
1055    tmp[i] = string(tmp[i])+"("+string(1)+")";
1056  }
1057  L[2] = tmp;
1058  list OrigNames = LR[2];
1059  // ordering: d blocks of the ord on r
1060  // try to get whether the ord on r is blockord itself
1061  // TODO: make L(2) ordering! exponent is maximally 2
1062  s = size(LR[3]);
1063  if (s==2)
1064  {
1065    // not a blockord, 1 block + module ord
1066    tmp = LR[3][s]; // module ord
1067    for (i=1; i<=D; i++)
1068    {
1069      LR[3][s-1+i] = LR[3][1];
1070    }
1071    LR[3][s+D] = tmp;
1072  }
1073  if (s>2)
1074  {
1075    // there are s-1 blocks
1076    int nb = s-1;
1077    tmp = LR[3][s]; // module ord
1078    for (i=1; i<=D; i++)
1079    {
1080      for (j=1; j<=nb; j++)
1081      {
1082        LR[3][i*nb+j] = LR[3][j];
1083      }
1084    }
1085    //    size(LR[3]);
1086    LR[3][nb*(D+1)+1] = tmp;
1087  }
1088  L[3] = LR[3];
1089  def @R = ring(L);
1090  setring @R;
1091  ideal I;
1092  poly @p;
1093  s = size(OrigNames);
1094  //  "s:";s;
1095  // convert LM to canonical vectors (no powers)
1096  setring save;
1097  kill M; // M was defined earlier
1098  module M;
1099  slm = size(LM); // numbers of polys in the ideal
1100  int sv,k,l;
1101  vector v;
1102  //  poly p;
1103  string sp;
1104  setring @R;
1105  poly @@p=0;
1106  setring save;
1107  for (l=1; l<=slm; l++)
1108  {
1109    // modules, e.g. free polynomials
1110    M  = LM[l];
1111    sm = ncols(M); // in intvec iv the sizes are stored
1112    for (i=0; i<=d-iv[l]; i++)
1113    {
1114      // modules, e.g. free polynomials
1115      for (j=1; j<=sm; j++)
1116      {
1117        //vectors, e.g. free monomials
1118        v  = M[j];
1119        sv = size(v);
1120        //      "sv:";sv;
1121        sp = "@@p = @@p + ";
1122        for (k=2; k<=sv; k++)
1123        {
1124          sp = sp + string(v[k])+"("+string(k-1+i)+")*";
1125        }
1126        sp = sp + string(v[1])+";"; // coef;
1127        setring @R;
1128        execute(sp);
1129        setring save;
1130      }
1131      setring @R;
1132      //      "@@p:"; @@p;
1133      I = I,@@p;
1134      @@p = 0;
1135      setring save;
1136    }
1137  }
1138  kill sp;
1139  // 3. compute GB
1140  setring @R;
1141  dbprint(ppl,"computing GB");
1142  //  ideal J = groebner(I);
1143  ideal J = slimgb(I);
1144  dbprint(ppl,J);
1145  // 4. skip shifted elts
1146  ideal K = select1(J,1,s); // s = size(OrigNames)
1147  dbprint(ppl,K);
1148  dbprint(ppl, "done with GB");
1149  // K contains vars x(1),...z(1) = images of originals
1150  // 5. go back to orig vars, produce strings/modules
1151  if (K[1] == 0)
1152  {
1153    "no reasonable output, GB gives 0";
1154    return(0);
1155  }
1156  int sk = size(K);
1157  int sp, sx, a, b;
1158  intvec x;
1159  poly p,q;
1160  poly pn;
1161  // vars in 'save'
1162  setring save;
1163  module N;
1164  list LN;
1165  vector V;
1166  poly pn;
1167  // test and skip exponents >=2
1168  setring @R;
1169  for(i=1; i<=sk; i++)
1170  {
1171    p  = K[i];
1172    while (p!=0)
1173    {
1174      q  = lead(p);
1175      //      "processing q:";q;
1176      x  = leadexp(q);
1177      sx = size(x);
1178      for(k=1; k<=sx; k++)
1179      {
1180        if ( x[k] >= 2 )
1181        {
1182          err = "skip: the value x[k] is " + string(x[k]);
1183          dbprint(ppl,err);
1184          //        return(0);
1185          K[i] = 0;
1186          p    = 0;
1187          q    = 0;
1188          break;
1189        }
1190      }
1191      p  = p - q;
1192    }
1193  }
1194  K  = simplify(K,2);
1195  sk = size(K);
1196  for(i=1; i<=sk; i++)
1197  {
1198    //    setring save;
1199    //    V  = 0;
1200    setring @R;
1201    p  = K[i];
1202    while (p!=0)
1203    {
1204      q  = lead(p);
1205      err =  "processing q:" + string(q);
1206      dbprint(ppl,err);
1207      x  = leadexp(q);
1208      sx = size(x);
1209      pn = leadcoef(q);
1210      setring save;
1211      pn = imap(@R,pn);
1212      V  = V + leadcoef(pn)*gen(1);
1213      for(k=1; k<=sx; k++)
1214      {
1215        if (x[k] ==1)
1216        {
1217          a = k / s; // block number=a+1, a!=0
1218          b = k % s; // remainder
1219          //      printf("a: %s, b: %s",a,b);
1220          if (b == 0)
1221          {
1222            // that is it's the last var in the block
1223            b = s;
1224            a = a-1;
1225          }
1226          V = V + var(b)*gen(a+2);
1227        }
1228//      else
1229//      {
1230//        printf("error: the value x[k] is %s", x[k]);
1231//        return(0);
1232//      }
1233      }
1234      err = "V: " + string(V);
1235      dbprint(ppl,err);
1236      //      printf("V: %s", string(V));
1237      N = N,V;
1238      V  = 0;
1239      setring @R;
1240      p  = p - q;
1241      pn = 0;
1242    }
1243    setring save;
1244    LN[i] = simplify(N,2);
1245    N     = 0;
1246  }
1247  setring save;
1248  return(LN);
1249}
1250example
1251{
1252  "EXAMPLE:"; echo = 2;
1253  ring r = 0,(x,y,z),(dp(1),dp(2));
1254  module M = [-1,x,y],[-7,y,y],[3,x,x];
1255  module N = [1,x,y,x],[-1,y,x,y];
1256  list L; L[1] = M; L[2] = N;
1257  lst2str(L);
1258  def U = freegbold(L,5);
1259  lst2str(U);
1260}
1261
1262proc sgb(ideal I, int d)
1263{
1264  // new code
1265  // map x_i to x_i(1) via map()
1266  //LIB "freegb.lib";
1267  def save = basering;
1268  //int d =7;// degree
1269  int nv = nvars(save);
1270  def R = freegbRing(d);
1271  setring R;
1272  int i;
1273  ideal Imap;
1274  for (i=1; i<=nv; i++)
1275  {
1276    Imap[i] = var(i);
1277  }
1278  //ideal I = x(1)*y(2), y(1)*x(2)+z(1)*z(2);
1279  ideal I = x(1)*x(2),x(1)*y(2) + z(1)*x(2);
1280  option(prot);
1281  //option(teach);
1282  ideal J = system("freegb",I,d,nv);
1283}
1284
1285
1286
1287static proc checkCeq()
1288{
1289  ring r = 0,(x,y),Dp;
1290  def A = freegbRing(4);
1291  setring A;
1292  A;
1293  // I = x2-xy
1294  ideal I = x(1)*x(2) - x(1)*y(2), x(2)*x(3) - x(2)*y(3), x(3)*x(4) - x(3)*y(4);
1295  ideal C = x(2)-x(1),x(3)-x(2),x(4)-x(3),y(2)-y(1),y(3)-y(2),y(4)-y(3);
1296  ideal K = I,C;
1297  groebner(K);
1298}
1299
1300
1301proc exHom1()
1302{
1303  // we start with
1304  // z*y - x, z*x - y, y*x - z
1305  LIB "freegb.lib";
1306  LIB "elim.lib";
1307  ring r = 0,(x,y,z,h),dp;
1308  list L;
1309  module M;
1310  M = [1,z,y],[-1,x,h]; // zy - xh
1311  L[1] = M;
1312  M = [1,z,x],[-1,y,h]; // zx - yh
1313  L[2] = M;
1314  M = [1,y,x],[-1,z,h]; // yx - zh
1315  L[3] = M;
1316  lst2str(L);
1317  def U = crs(L,4);
1318  setring U;
1319  I = I,
1320    y(2)*h(3)+z(2)*x(3),     y(3)*h(4)+z(3)*x(4),
1321    y(2)*x(3)-z(2)*h(3),     y(3)*x(4)-z(3)*h(4);
1322  I = simplify(I,2);
1323  ring r2 = 0,(x(0..4),y(0..4),z(0..4),h(0..4)),dp;
1324  ideal J = imap(U,I);
1325  //  ideal K = homog(J,h);
1326  option(redSB);
1327  option(redTail);
1328  ideal L = groebner(J); //(K);
1329  ideal LL = sat(L,ideal(h))[1];
1330  ideal M = subst(LL,h,1);
1331  M = simplify(M,2);
1332  setring U;
1333  ideal M = imap(r2,M);
1334  lst2str(U);
1335}
1336
1337static proc test1()
1338{
1339  LIB "freegb.lib";
1340  ring r = 0,(x,y),Dp;
1341  int d = 10; // degree
1342  def R = freegbRing(d);
1343  setring R;
1344  ideal I = x(1)*x(2) - y(1)*y(2);
1345  option(prot);
1346  option(teach);
1347  ideal J = system("freegb",I,d,2);
1348  J;
1349}
1350
1351static proc test2()
1352{
1353  LIB "freegb.lib";
1354  ring r = 0,(x,y),Dp;
1355  int d = 10; // degree
1356  def R = freegbRing(d);
1357  setring R;
1358  ideal I = x(1)*x(2) - x(1)*y(2);
1359  option(prot);
1360  option(teach);
1361  ideal J = system("freegb",I,d,2);
1362  J;
1363}
1364
1365static proc test3()
1366{
1367  LIB "freegb.lib";
1368  ring r = 0,(x,y,z),dp;
1369  int d =5; // degree
1370  def R = freegbRing(d);
1371  setring R;
1372  ideal I = x(1)*y(2), y(1)*x(2)+z(1)*z(2);
1373  option(prot);
1374  option(teach);
1375  ideal J = system("freegb",I,d,3);
1376}
1377
1378proc schur2-3()
1379{
1380  // nonhomog:
1381  //  h^4-10*h^2+9,f*e-e*f+h, h*2-e*h-2*e,h*f-f*h+2*f
1382  // homogenized with t
1383  //  h^4-10*h^2*t^2+9*t^4,f*e-e*f+h*t, h*2-e*h-2*e*t,h*f-f*h+2*f*t,
1384  // t*h - h*t, t*f - f*t, t*e - e*t
1385}
Note: See TracBrowser for help on using the repository browser.