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

spielwiese
Last change on this file since a1c745 was a1c745, checked in by Hans Schönemann <hannes@…>, 17 years ago
*hannes: format git-svn-id: file:///usr/local/Singular/svn/trunk@10133 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 12.7 KB
Line 
1//////////////////////////////////////////////////////////////////////////////
2version="$Id: freegb.lib,v 1.2 2007-06-20 15:39:45 Singular Exp $";
3category="Noncommutative";
4info="
5LIBRARY: ratgb.lib  Twosided Noncommutative Groebner bases in Free Algebras
6AUTHOR: Viktor Levandovskyy,     levandov@math.rwth-aachen.de
7
8PROCEDURES:
9freegb(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// given the element -7xy^2x, it is represented as [-7,x,y^2,x] or as [-7,x,y,y,x]
281// use the orig ord on (x,y,z) and expand it blockwise to (x(i),y(i),z(i))
282
283// the correspondences:
284// monomial in K<x,y,z>    <<--->> vector in R
285// polynomial in K<x,y,z>  <<--->> list of vectors (matrix/module) in R
286// ideal in K<x,y,z>       <<--->> list of matrices/modules in R
287
288
289// 1. form a new ring
290// 2. produce shifted generators
291// 3. compute GB
292// 4. skip shifted elts
293// 5. go back to orig vars, produce strings/modules
294// 6. return the result
295
296proc freegb(list LM, int d)
297"USAGE:  freegb(L, d);  L a list of modules, d an integer
298RETURN:  ring
299PURPOSE: compute the two-sided Groebner basis of an ideal, encoded by L in
300the free associative algebra, up to degree d
301EXAMPLE: example freegb; shows examples
302"
303{
304  // d = up to degree, will be shifted to d+1
305  if (d<1) {"bad d"; return(0);}
306
307  int ppl = printlevel-voice+2;
308  string err = "";
309
310  int i,j,s;
311  def save = basering;
312  // determine max no of places in the input
313  int slm = size(LM); // numbers of polys in the ideal
314  int sm;
315  intvec iv;
316  module M;
317  for (i=1; i<=slm; i++)
318  {
319    // modules, e.g. free polynomials
320    M  = LM[i];
321    sm = ncols(M);
322    for (j=1; j<=sm; j++)
323    {
324      //vectors, e.g. free monomials
325      iv = iv, size(M[j])-1; // 1 place is reserved by the coeff
326    }
327  }
328  int D = Max(iv); // max size of input words
329  if (d<D) {"bad d"; return(LM);}
330  D = D + d-1;
331  //  D = d;
332  list LR  = ringlist(save);
333  list L, tmp;
334  L[1] = LR[1]; // ground field
335  L[4] = LR[4]; // quotient ideal
336  tmp  = LR[2]; // varnames
337  s = size(LR[2]);
338  for (i=1; i<=D; i++)
339  {
340    for (j=1; j<=s; j++)
341    {
342      tmp[i*s+j] = string(tmp[j])+"("+string(i+1)+")";
343    }
344  }
345  for (i=1; i<=s; i++)
346  {
347    tmp[i] = string(tmp[i])+"("+string(1)+")";
348  }
349  L[2] = tmp;
350  list OrigNames = LR[2];
351  // ordering: d blocks of the ord on r
352  // try to get whether the ord on r is blockord itself
353  s = size(LR[3]);
354  if (s==2)
355  {
356    // not a blockord, 1 block + module ord
357    tmp = LR[3][s]; // module ord
358    for (i=1; i<=D; i++)
359    {
360      LR[3][s-1+i] = LR[3][1];
361    }
362    LR[3][s+D] = tmp;
363  }
364  if (s>2)
365  {
366    // there are s-1 blocks
367    int nb = s-1;
368    tmp = LR[3][s]; // module ord
369    for (i=1; i<=D; i++)
370    {
371      for (j=1; j<=nb; j++)
372      {
373        LR[3][i*nb+j] = LR[3][j];
374      }
375    }
376    //    size(LR[3]);
377    LR[3][nb*(D+1)+1] = tmp;
378  }
379  L[3] = LR[3];
380  def @R = ring(L);
381  setring @R;
382  ideal I;
383  poly @p;
384  s = size(OrigNames);
385  //  "s:";s;
386  // convert LM to canonical vectors (no powers)
387  setring save;
388  kill M; // M was defined earlier
389  module M;
390  slm = size(LM); // numbers of polys in the ideal
391  int sv,k,l;
392  vector v;
393  //  poly p;
394  string sp;
395  setring @R;
396  poly @@p=0;
397  setring save;
398  for (l=1; l<=slm; l++)
399  {
400    // modules, e.g. free polynomials
401    M  = LM[l];
402    sm = ncols(M); // in intvec iv the sizes are stored
403    for (i=0; i<=d-iv[l]; i++)
404    {
405      // modules, e.g. free polynomials
406      for (j=1; j<=sm; j++)
407      {
408        //vectors, e.g. free monomials
409        v  = M[j];
410        sv = size(v);
411        //      "sv:";sv;
412        sp = "@@p = @@p + ";
413        for (k=2; k<=sv; k++)
414        {
415          sp = sp + string(v[k])+"("+string(k-1+i)+")*";
416        }
417        sp = sp + string(v[1])+";"; // coef;
418        setring @R;
419        execute(sp);
420        setring save;
421      }
422      setring @R;
423      //      "@@p:"; @@p;
424      I = I,@@p;
425      @@p = 0;
426      setring save;
427    }
428  }
429  kill sp;
430  // 3. compute GB
431  setring @R;
432  dbprint(ppl,"computing GB");
433  //  ideal J = groebner(I);
434  ideal J = slimgb(I);
435  dbprint(ppl,J);
436  // 4. skip shifted elts
437  ideal K = select1(J,1,s); // s = size(OrigNames)
438  dbprint(ppl,K);
439  dbprint(ppl, "done with GB");
440  // K contains vars x(1),...z(1) = images of originals
441  // 5. go back to orig vars, produce strings/modules
442  if (K[1] == 0)
443  {
444    "no reasonable output, GB gives 0";
445    return(0);
446  }
447  int sk = size(K);
448  int sp, sx, a, b;
449  intvec x;
450  poly p,q;
451  poly pn;
452  // vars in 'save'
453  setring save;
454  module N;
455  list LN;
456  vector V;
457  poly pn;
458  // test and skip exponents >=2
459  setring @R;
460  for(i=1; i<=sk; i++)
461  {
462    p  = K[i];
463    while (p!=0)
464    {
465      q  = lead(p);
466      //      "processing q:";q;
467      x  = leadexp(q);
468      sx = size(x);
469      for(k=1; k<=sx; k++)
470      {
471        if ( x[k] >= 2 )
472        {
473          err = "skip: the value x[k] is " + string(x[k]);
474          dbprint(ppl,err);
475          //        return(0);
476          K[i] = 0;
477          p    = 0;
478          q    = 0;
479          break;
480        }
481      }
482      p  = p - q;
483    }
484  }
485  K  = simplify(K,2);
486  sk = size(K);
487  for(i=1; i<=sk; i++)
488  {
489    //    setring save;
490    //    V  = 0;
491    setring @R;
492    p  = K[i];
493    while (p!=0)
494    {
495      q  = lead(p);
496      err =  "processing q:" + string(q);
497      dbprint(ppl,err);
498      x  = leadexp(q);
499      sx = size(x);
500      pn = leadcoef(q);
501      setring save;
502      pn = imap(@R,pn);
503      V  = V + leadcoef(pn)*gen(1);
504      for(k=1; k<=sx; k++)
505      {
506        if (x[k] ==1)
507        {
508          a = k / s; // block number=a+1, a!=0
509          b = k % s; // remainder
510          //      printf("a: %s, b: %s",a,b);
511          if (b == 0)
512          {
513            // that is it's the last var in the block
514            b = s;
515            a = a-1;
516          }
517          V = V + var(b)*gen(a+2);
518        }
519//      else
520//      {
521//        printf("error: the value x[k] is %s", x[k]);
522//        return(0);
523//      }
524      }
525      err = "V: " + string(V);
526      dbprint(ppl,err);
527      //      printf("V: %s", string(V));
528      N = N,V;
529      V  = 0;
530      setring @R;
531      p  = p - q;
532      pn = 0;
533    }
534    setring save;
535    LN[i] = simplify(N,2);
536    N     = 0;
537  }
538  setring save;
539  return(LN);
540}
541example
542{
543  "EXAMPLE:"; echo = 2;
544  ring r = 0,(x,y,z),(dp(1),dp(2));
545  module M = [-1,x,y],[-7,y,y],[3,x,x];
546  module N = [1,x,y,x],[-1,y,x,y];
547  list L; L[1] = M; L[2] = N;
548  lst2str(L);
549  def U = freegb(L,5);
550  lst2str(U);
551}
552
553proc ex2()
554{
555  option(prot);
556  LIB "freegb.lib";
557  ring r = 0,(x,y),dp;
558  module M = [-1,x,y],[3,x,x]; // 3x^2 - xy
559  def U = freegb(M,7);
560  lst2str(U);
561}
562
563proc ex_nonhomog()
564{
565  option(prot);
566  LIB "freegb.lib";
567  ring r = 0,(x,y,h),dp;
568  list L;
569  module M;
570  M = [-1,y,y],[1,x,x,x];  // x3-y2
571  L[1] = M;
572  M = [1,x,h],[-1,h,x];  // xh-hx
573  L[2] = M;
574  M = [1,y,h],[-1,h,y];  // yh-hy
575  L[3] = M;
576  def U = freegb(L,4);
577  lst2str(U);
578  // strange elements in the basis
579}
580
581proc ex_nonhomog_comm()
582{
583  option(prot);
584  LIB "freegb.lib";
585  ring r = 0,(x,y),dp;
586  module M = [-1,y,y],[1,x,x,x];
587  def U = freegb(M,5);
588  lst2str(U);
589}
590
591proc ex_nonhomog_h()
592{
593  option(prot);
594  LIB "freegb.lib";
595  ring r = 0,(x,y,h),(a(1,1),dp);
596  module M = [-1,y,y,h],[1,x,x,x]; // x3 - y2h
597  def U = freegb(M,6);
598  lst2str(U);
599}
600
601proc ex_nonhomog_h2()
602{
603  option(prot);
604  LIB "freegb.lib";
605  ring r = 0,(x,y,h),(dp);
606  list L;
607  module M;
608  M = [-1,y,y,h],[1,x,x,x]; // x3 - y2h
609  L[1] = M;
610  M = [1,x,h],[-1,h,x]; // xh - hx
611  L[2] = M;
612  M = [1,y,h],[-1,h,y]; // yh - hy
613  L[3] = M;
614  def U = freegb(L,3);
615  lst2str(U);
616  // strange answer CHECK
617}
618
619
620proc ex_nonhomog_3()
621{
622  option(prot);
623  LIB "./freegb.lib";
624  ring r = 0,(x,y,z),(dp);
625  list L;
626  module M;
627  M = [1,z,y],[-1,x]; // zy - x
628  L[1] = M;
629  M = [1,z,x],[-1,y]; // zx - y
630  L[2] = M;
631  M = [1,y,x],[-1,z]; // yx - z
632  L[3] = M;
633  lst2str(L);
634  list U = freegb(L,4);
635  lst2str(U);
636  // strange answer CHECK
637}
638
639
640
641proc ex_densep_2()
642{
643  option(prot);
644  LIB "freegb.lib";
645  ring r = (0,a,b,c),(x,y),(Dp); // deglex
646  module M = [1,x,x], [a,x,y], [b,y,x], [c,y,y];
647  lst2str(M);
648  list U = freegb(M,5);
649  lst2str(U);
650  // a=b is important -> finite basis!!!
651  module M = [1,x,x], [a,x,y], [a,y,x], [c,y,y];
652  lst2str(M);
653  list U = freegb(M,5);
654  lst2str(U);
655}
Note: See TracBrowser for help on using the repository browser.