source: git/Singular/LIB/freegb.lib @ 285d21

spielwiese
Last change on this file since 285d21 was 285d21, checked in by Viktor Levandovskyy <levandov@…>, 16 years ago
*levandov: fixes, new procs, cleanup git-svn-id: file:///usr/local/Singular/svn/trunk@10608 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 26.5 KB
Line 
1//////////////////////////////////////////////////////////////////////////////
2version="$Id: freegb.lib,v 1.7 2008-02-26 23:36:12 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 ex2()
868{
869  option(prot);
870  LIB "freegb.lib";
871  ring r = 0,(x,y),dp;
872  module M = [-1,x,y],[3,x,x]; // 3x^2 - xy
873  def U = freegb(M,7);
874  lst2str(U);
875}
876
877proc ex_nonhomog()
878{
879  option(prot);
880  LIB "freegb.lib";
881  ring r = 0,(x,y,h),dp;
882  list L;
883  module M;
884  M = [-1,y,y],[1,x,x,x];  // x3-y2
885  L[1] = M;
886  M = [1,x,h],[-1,h,x];  // xh-hx
887  L[2] = M;
888  M = [1,y,h],[-1,h,y];  // yh-hy
889  L[3] = M;
890  def U = freegb(L,4);
891  lst2str(U);
892  // strange elements in the basis
893}
894
895proc ex_nonhomog_comm()
896{
897  option(prot);
898  LIB "freegb.lib";
899  ring r = 0,(x,y),dp;
900  module M = [-1,y,y],[1,x,x,x];
901  def U = freegb(M,5);
902  lst2str(U);
903}
904
905proc ex_nonhomog_h()
906{
907  option(prot);
908  LIB "freegb.lib";
909  ring r = 0,(x,y,h),(a(1,1),dp);
910  module M = [-1,y,y,h],[1,x,x,x]; // x3 - y2h
911  def U = freegb(M,6);
912  lst2str(U);
913}
914
915proc ex_nonhomog_h2()
916{
917  option(prot);
918  LIB "freegb.lib";
919  ring r = 0,(x,y,h),(dp);
920  list L;
921  module M;
922  M = [-1,y,y,h],[1,x,x,x]; // x3 - y2h
923  L[1] = M;
924  M = [1,x,h],[-1,h,x]; // xh - hx
925  L[2] = M;
926  M = [1,y,h],[-1,h,y]; // yh - hy
927  L[3] = M;
928  def U = freegbasis(L,3);
929  lst2str(U);
930  // strange answer CHECK
931}
932
933
934proc ex_nonhomog_3()
935{
936  option(prot);
937  LIB "./freegb.lib";
938  ring r = 0,(x,y,z),(dp);
939  list L;
940  module M;
941  M = [1,z,y],[-1,x]; // zy - x
942  L[1] = M;
943  M = [1,z,x],[-1,y]; // zx - y
944  L[2] = M;
945  M = [1,y,x],[-1,z]; // yx - z
946  L[3] = M;
947  lst2str(L);
948  list U = freegb(L,4);
949  lst2str(U);
950  // strange answer CHECK
951}
952
953proc ex_densep_2()
954{
955  option(prot);
956  LIB "freegb.lib";
957  ring r = (0,a,b,c),(x,y),(Dp); // deglex
958  module M = [1,x,x], [a,x,y], [b,y,x], [c,y,y];
959  lst2str(M);
960  list U = freegb(M,5);
961  lst2str(U);
962  // a=b is important -> finite basis!!!
963  module M = [1,x,x], [a,x,y], [a,y,x], [c,y,y];
964  lst2str(M);
965  list U = freegb(M,5);
966  lst2str(U);
967}
968
969
970// 1. form a new ring
971// 2. produce shifted generators
972// 3. compute GB
973// 4. skip shifted elts
974// 5. go back to orig vars, produce strings/modules
975// 6. return the result
976
977proc freegbold(list LM, int d)
978"USAGE:  freegbold(L, d);  L a list of modules, d an integer
979RETURN:  ring
980PURPOSE: compute the two-sided Groebner basis of an ideal, encoded by L in
981the free associative algebra, up to degree d
982EXAMPLE: example freegbold; shows examples
983"
984{
985  // d = up to degree, will be shifted to d+1
986  if (d<1) {"bad d"; return(0);}
987
988  int ppl = printlevel-voice+2;
989  string err = "";
990
991  int i,j,s;
992  def save = basering;
993  // determine max no of places in the input
994  int slm = size(LM); // numbers of polys in the ideal
995  int sm;
996  intvec iv;
997  module M;
998  for (i=1; i<=slm; i++)
999  {
1000    // modules, e.g. free polynomials
1001    M  = LM[i];
1002    sm = ncols(M);
1003    for (j=1; j<=sm; j++)
1004    {
1005      //vectors, e.g. free monomials
1006      iv = iv, size(M[j])-1; // 1 place is reserved by the coeff
1007    }
1008  }
1009  int D = Max(iv); // max size of input words
1010  if (d<D) {"bad d"; return(LM);}
1011  D = D + d-1;
1012  //  D = d;
1013  list LR  = ringlist(save);
1014  list L, tmp;
1015  L[1] = LR[1]; // ground field
1016  L[4] = LR[4]; // quotient ideal
1017  tmp  = LR[2]; // varnames
1018  s = size(LR[2]);
1019  for (i=1; i<=D; i++)
1020  {
1021    for (j=1; j<=s; j++)
1022    {
1023      tmp[i*s+j] = string(tmp[j])+"("+string(i+1)+")";
1024    }
1025  }
1026  for (i=1; i<=s; i++)
1027  {
1028    tmp[i] = string(tmp[i])+"("+string(1)+")";
1029  }
1030  L[2] = tmp;
1031  list OrigNames = LR[2];
1032  // ordering: d blocks of the ord on r
1033  // try to get whether the ord on r is blockord itself
1034  // TODO: make L(2) ordering! exponent is maximally 2
1035  s = size(LR[3]);
1036  if (s==2)
1037  {
1038    // not a blockord, 1 block + module ord
1039    tmp = LR[3][s]; // module ord
1040    for (i=1; i<=D; i++)
1041    {
1042      LR[3][s-1+i] = LR[3][1];
1043    }
1044    LR[3][s+D] = tmp;
1045  }
1046  if (s>2)
1047  {
1048    // there are s-1 blocks
1049    int nb = s-1;
1050    tmp = LR[3][s]; // module ord
1051    for (i=1; i<=D; i++)
1052    {
1053      for (j=1; j<=nb; j++)
1054      {
1055        LR[3][i*nb+j] = LR[3][j];
1056      }
1057    }
1058    //    size(LR[3]);
1059    LR[3][nb*(D+1)+1] = tmp;
1060  }
1061  L[3] = LR[3];
1062  def @R = ring(L);
1063  setring @R;
1064  ideal I;
1065  poly @p;
1066  s = size(OrigNames);
1067  //  "s:";s;
1068  // convert LM to canonical vectors (no powers)
1069  setring save;
1070  kill M; // M was defined earlier
1071  module M;
1072  slm = size(LM); // numbers of polys in the ideal
1073  int sv,k,l;
1074  vector v;
1075  //  poly p;
1076  string sp;
1077  setring @R;
1078  poly @@p=0;
1079  setring save;
1080  for (l=1; l<=slm; l++)
1081  {
1082    // modules, e.g. free polynomials
1083    M  = LM[l];
1084    sm = ncols(M); // in intvec iv the sizes are stored
1085    for (i=0; i<=d-iv[l]; i++)
1086    {
1087      // modules, e.g. free polynomials
1088      for (j=1; j<=sm; j++)
1089      {
1090        //vectors, e.g. free monomials
1091        v  = M[j];
1092        sv = size(v);
1093        //      "sv:";sv;
1094        sp = "@@p = @@p + ";
1095        for (k=2; k<=sv; k++)
1096        {
1097          sp = sp + string(v[k])+"("+string(k-1+i)+")*";
1098        }
1099        sp = sp + string(v[1])+";"; // coef;
1100        setring @R;
1101        execute(sp);
1102        setring save;
1103      }
1104      setring @R;
1105      //      "@@p:"; @@p;
1106      I = I,@@p;
1107      @@p = 0;
1108      setring save;
1109    }
1110  }
1111  kill sp;
1112  // 3. compute GB
1113  setring @R;
1114  dbprint(ppl,"computing GB");
1115  //  ideal J = groebner(I);
1116  ideal J = slimgb(I);
1117  dbprint(ppl,J);
1118  // 4. skip shifted elts
1119  ideal K = select1(J,1,s); // s = size(OrigNames)
1120  dbprint(ppl,K);
1121  dbprint(ppl, "done with GB");
1122  // K contains vars x(1),...z(1) = images of originals
1123  // 5. go back to orig vars, produce strings/modules
1124  if (K[1] == 0)
1125  {
1126    "no reasonable output, GB gives 0";
1127    return(0);
1128  }
1129  int sk = size(K);
1130  int sp, sx, a, b;
1131  intvec x;
1132  poly p,q;
1133  poly pn;
1134  // vars in 'save'
1135  setring save;
1136  module N;
1137  list LN;
1138  vector V;
1139  poly pn;
1140  // test and skip exponents >=2
1141  setring @R;
1142  for(i=1; i<=sk; i++)
1143  {
1144    p  = K[i];
1145    while (p!=0)
1146    {
1147      q  = lead(p);
1148      //      "processing q:";q;
1149      x  = leadexp(q);
1150      sx = size(x);
1151      for(k=1; k<=sx; k++)
1152      {
1153        if ( x[k] >= 2 )
1154        {
1155          err = "skip: the value x[k] is " + string(x[k]);
1156          dbprint(ppl,err);
1157          //        return(0);
1158          K[i] = 0;
1159          p    = 0;
1160          q    = 0;
1161          break;
1162        }
1163      }
1164      p  = p - q;
1165    }
1166  }
1167  K  = simplify(K,2);
1168  sk = size(K);
1169  for(i=1; i<=sk; i++)
1170  {
1171    //    setring save;
1172    //    V  = 0;
1173    setring @R;
1174    p  = K[i];
1175    while (p!=0)
1176    {
1177      q  = lead(p);
1178      err =  "processing q:" + string(q);
1179      dbprint(ppl,err);
1180      x  = leadexp(q);
1181      sx = size(x);
1182      pn = leadcoef(q);
1183      setring save;
1184      pn = imap(@R,pn);
1185      V  = V + leadcoef(pn)*gen(1);
1186      for(k=1; k<=sx; k++)
1187      {
1188        if (x[k] ==1)
1189        {
1190          a = k / s; // block number=a+1, a!=0
1191          b = k % s; // remainder
1192          //      printf("a: %s, b: %s",a,b);
1193          if (b == 0)
1194          {
1195            // that is it's the last var in the block
1196            b = s;
1197            a = a-1;
1198          }
1199          V = V + var(b)*gen(a+2);
1200        }
1201//      else
1202//      {
1203//        printf("error: the value x[k] is %s", x[k]);
1204//        return(0);
1205//      }
1206      }
1207      err = "V: " + string(V);
1208      dbprint(ppl,err);
1209      //      printf("V: %s", string(V));
1210      N = N,V;
1211      V  = 0;
1212      setring @R;
1213      p  = p - q;
1214      pn = 0;
1215    }
1216    setring save;
1217    LN[i] = simplify(N,2);
1218    N     = 0;
1219  }
1220  setring save;
1221  return(LN);
1222}
1223example
1224{
1225  "EXAMPLE:"; echo = 2;
1226  ring r = 0,(x,y,z),(dp(1),dp(2));
1227  module M = [-1,x,y],[-7,y,y],[3,x,x];
1228  module N = [1,x,y,x],[-1,y,x,y];
1229  list L; L[1] = M; L[2] = N;
1230  lst2str(L);
1231  def U = freegbold(L,5);
1232  lst2str(U);
1233}
1234
1235proc sgb(ideal I, int d)
1236{
1237  // new code
1238  // map x_i to x_i(1) via map()
1239  //LIB "freegb.lib";
1240  def save = basering;
1241  //int d =7;// degree
1242  int nv = nvars(save);
1243  def R = freegbRing(d);
1244  setring R;
1245  int i;
1246  ideal Imap;
1247  for (i=1; i<=nv; i++)
1248  {
1249    Imap[i] = var(i);
1250  }
1251  //ideal I = x(1)*y(2), y(1)*x(2)+z(1)*z(2);
1252  ideal I = x(1)*x(2),x(1)*y(2) + z(1)*x(2);
1253  option(prot);
1254  //option(teach);
1255  ideal J = system("freegb",I,d,nv);
1256}
1257
1258
1259
1260static proc checkCeq()
1261{
1262  ring r = 0,(x,y),Dp;
1263  def A = freegbRing(4);
1264  setring A;
1265  A;
1266  // I = x2-xy
1267  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);
1268  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);
1269  ideal K = I,C;
1270  groebner(K);
1271}
1272
1273
1274proc exHom1()
1275{
1276  // we start with
1277  // z*y - x, z*x - y, y*x - z
1278  LIB "freegb.lib";
1279  LIB "elim.lib";
1280  ring r = 0,(x,y,z,h),dp;
1281  list L;
1282  module M;
1283  M = [1,z,y],[-1,x,h]; // zy - xh
1284  L[1] = M;
1285  M = [1,z,x],[-1,y,h]; // zx - yh
1286  L[2] = M;
1287  M = [1,y,x],[-1,z,h]; // yx - zh
1288  L[3] = M;
1289  lst2str(L);
1290  def U = crs(L,4);
1291  setring U;
1292  I = I,
1293    y(2)*h(3)+z(2)*x(3),     y(3)*h(4)+z(3)*x(4),
1294    y(2)*x(3)-z(2)*h(3),     y(3)*x(4)-z(3)*h(4);
1295  I = simplify(I,2);
1296  ring r2 = 0,(x(0..4),y(0..4),z(0..4),h(0..4)),dp;
1297  ideal J = imap(U,I);
1298  //  ideal K = homog(J,h);
1299  option(redSB);
1300  option(redTail);
1301  ideal L = groebner(J); //(K);
1302  ideal LL = sat(L,ideal(h))[1];
1303  ideal M = subst(LL,h,1);
1304  M = simplify(M,2);
1305  setring U;
1306  ideal M = imap(r2,M);
1307  lst2str(U);
1308}
1309
1310static proc test1()
1311{
1312  LIB "freegb.lib";
1313  ring r = 0,(x,y),Dp;
1314  int d = 10; // degree
1315  def R = freegbRing(d);
1316  setring R;
1317  ideal I = x(1)*x(2) - y(1)*y(2);
1318  option(prot);
1319  option(teach);
1320  ideal J = system("freegb",I,d,2);
1321  J;
1322}
1323
1324static proc test2()
1325{
1326  LIB "freegb.lib";
1327  ring r = 0,(x,y),Dp;
1328  int d = 10; // degree
1329  def R = freegbRing(d);
1330  setring R;
1331  ideal I = x(1)*x(2) - x(1)*y(2);
1332  option(prot);
1333  option(teach);
1334  ideal J = system("freegb",I,d,2);
1335  J;
1336}
1337
1338static proc test3()
1339{
1340  LIB "freegb.lib";
1341  ring r = 0,(x,y,z),dp;
1342  int d =5; // degree
1343  def R = freegbRing(d);
1344  setring R;
1345  ideal I = x(1)*y(2), y(1)*x(2)+z(1)*z(2);
1346  option(prot);
1347  option(teach);
1348  ideal J = system("freegb",I,d,3);
1349}
1350
1351proc schur2-3()
1352{
1353  // nonhomog:
1354  //  h^4-10*h^2+9,f*e-e*f+h, h*2-e*h-2*e,h*f-f*h+2*f
1355  // homogenized with t
1356  //  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,
1357  // t*h - h*t, t*f - f*t, t*e - e*t
1358}
Note: See TracBrowser for help on using the repository browser.