source: git/Singular/LIB/standard.lib @ 7708934

spielwiese
Last change on this file since 7708934 was 8bb77b, checked in by Gert-Martin Greuel <greuel@…>, 23 years ago
* GMG: Kosmetik git-svn-id: file:///usr/local/Singular/svn/trunk@4983 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 26.3 KB
Line 
1//////////////////////////////////////////////////////////////////////////////
2version="$Id: standard.lib,v 1.52 2000-12-22 14:52:32 greuel Exp $";
3category="Miscellaneous";
4info="
5LIBRARY: standard.lib   Procedures which are always loaded at Start-up
6
7PROCEDURES:
8 stdfglm(ideal[,ord])   standard basis of ideal via fglm [and ordering ord]
9 stdhilb(ideal[,h])     standard basis of ideal using the Hilbert function
10 groebner(ideal/module) standard basis using a heuristically chosen method
11 quot(any,any[,n])      quotient using heuristically chosen method
12 res(ideal/module,[i])  free resolution of ideal or module
13 sprintf(fmt,...)       returns fomatted string
14 fprintf(link,fmt,..)   writes formatted string to link
15 printf(fmt,...)        displays formatted string
16";
17
18//////////////////////////////////////////////////////////////////////////////
19
20proc stdfglm (ideal i, list #)
21"SYNTAX:  stdfglm(ideal_expression);
22         stdfglm(ideal_expression, string_expression);
23TYPE:    ideal
24PURPOSE: computes the standard basis of the ideal in the basering,
25         calculated via fglm from ordering given as the second argumnent
26         to the ordering of the basering.
27         If no second argumnt is given, \"dp\" is used.
28SEE ALSO: stdhilb, std, groebner
29KEYWORDS: fglm
30EXAMPLE: example stdfglm; shows an example"
31{
32   string os;
33   def dr= basering;
34   if( (size(#)==0) or (typeof(#[1]) != "string") )
35   {
36     os = "dp(" + string( nvars(dr) ) + ")";
37     if ( (find( ordstr(dr), os ) != 0) and (find( ordstr(dr), "a") == 0) )
38     {
39       os= "Dp";
40     }
41     else
42     {
43       os= "dp";
44     }
45   }
46   else { os = #[1]; }
47   execute("ring sr=("+charstr(dr)+"),("+varstr(dr)+"),"+os+";");
48   ideal i= fetch(dr,i);
49   intvec opt= option(get);
50   option(redSB);
51   i=std(i);
52   option(set,opt);
53   setring dr;
54   return (fglm(sr,i));
55}
56example
57{ "EXAMPLE:"; echo = 2;
58   ring r  = 0,(x,y,z),lp;
59   ideal i = y3+x2, x2y+x2, x3-x2, z4-x2-y;
60   ideal i1= stdfglm(i);         //uses fglm from "dp" to "lp"
61   i1;
62   ideal i2= stdfglm(i,"Dp");    //uses fglm from "Dp" to "lp"
63   i2;
64}
65/////////////////////////////////////////////////////////////////////////////
66
67proc stdhilb(ideal i,list #)
68"SYNTAX:  stdhilb(ideal_expression);
69         stdhilb(ideal_expression, intvec_expression);
70TYPE:    ideal
71PURPOSE: computes the standard basis of the homogeneous ideal in the basering,
72         calculated via a Hilbert driven standard basis computation.
73         An optional second argument will be used as the Hilbert function.
74SEE ALSO: stdfglm, std, groebner
75KEYWORDS: Hilbert function
76EXAMPLE: example stdhilb;  shows an example"
77{
78   def R=basering;
79
80   if((homog(i)==1)||(ordstr(basering)[1]=="d"))
81   {
82      if ((size(#)!=0)&&(homog(i)==1))
83      {
84         return(std(i,#[1]));
85      }
86      return(std(i));
87   }
88
89   execute("ring S = ("+charstr(R)+"),("+varstr(R)+",@t),dp;");
90   ideal i=homog(imap(R,i),@t);
91   intvec v=hilb(std(i),1);
92   execute("ring T = ("+charstr(R)+"),("+varstr(R)+",@t),("+ordstr(R)+");");
93   ideal i=fetch(S,i);
94   ideal a=std(i,v);
95   setring R;
96   map phi=T,maxideal(1),1;
97   ideal a=phi(a);
98
99   int k,j;
100   poly m;
101   int c=size(i);
102
103   for(j=1;j<c;j++)
104   {
105     if(deg(a[j])==0)
106     {
107       a=ideal(1);
108       attrib(a,"isSB",1);
109       return(a);
110     }
111     if(deg(a[j])>0)
112     {
113       m=lead(a[j]);
114       for(k=j+1;k<=c;k++)
115       {
116          if(size(lead(a[k])/m)>0)
117          {
118            a[k]=0;
119          }
120       }
121     }
122   }
123   a=simplify(a,2);
124   attrib(a,"isSB",1);
125   return(a);
126}
127example
128{ "EXAMPLE:"; echo = 2;
129   ring  r = 0,(x,y,z),lp;
130   ideal i = y3+x2, x2y+x2, x3-x2, z4-x2-y;
131   ideal i1= stdhilb(i); i1;
132   // is in this case equivalent to:
133   intvec v=1,0,0,-3,0,1,0,3,-1,-1;
134   ideal i2=stdhilb(i,v);
135}
136//////////////////////////////////////////////////////////////////////////
137
138proc groebner(def i, list #)
139"SYNTAX:  groebner(ideal_expression);
140         groebner(module_expression);
141         groebner(ideal_expression, int_expression);
142         groebner(module_expression, int_expression);
143TYPE:    type of the first argument
144PURPOSE: computes the standard basis of the first argument i
145         (ideal or module) by a heuristically chosen method:
146         If the ordering of the current ring is a local ordering, or
147         if it is a non-block ordering and the current ring has no
148         parameters, then std(i) is returned.
149         Otherwise, i is mapped into a ring with no parameters and
150         ordering dp, where its Hilbert series is computed. This is
151         followed by a Hilbert-series based std computation in the
152         original ring.
153NOTE: If a 2nd argument 'wait' is given, then the computation proceeds
154      at most 'wait' seconds. That is, if no result could be computed in
155      'wait' seconds, then the computation is interrupted, 0 is returned,
156      a warning message is displayed, and the global variable
157      'groebner_error' is defined.
158SEE ALSO: stdhilb, stdfglm, std
159KEYWORDS: time limit on computations; MP, groebner basis computations
160EXAMPLE: example groebner;  shows an example"
161{
162  def P=basering;
163
164  // we have two arguments -- try to use MPfork links
165  if (size(#) > 0)
166  {
167    if (system("with", "MP"))
168    {
169      if (typeof(#[1]) == "int")
170      {
171        int wait = #[1];
172        int j = 10;
173
174        string bs = nameof(basering);
175        link l_fork = "MPtcp:fork";
176        open(l_fork);
177        write(l_fork, quote(system("pid")));
178        int pid = read(l_fork);
179        write(l_fork, quote(groebner(eval(i))));
180
181        // sleep in small intervalls for appr. one second
182        if (wait > 0)
183        {
184          while(j < 1000000)
185          {
186            if (status(l_fork, "read", "ready", j)) {break;}
187            j = j + j;
188          }
189        }
190
191        // sleep in intervalls of one second from now on
192        j = 1;
193        while (j < wait)
194        {
195          if (status(l_fork, "read", "ready", 1000000)) {break;}
196          j = j + 1;
197        }
198
199        if (status(l_fork, "read", "ready"))
200        {
201          def result = read(l_fork);
202          if (bs != nameof(basering))
203          {
204            def PP = basering;
205            setring P;
206            def result = imap(PP, result);
207            kill PP;
208          }
209          if (defined(groebner_error))
210          {
211            kill(groebner_error);
212          }
213          kill (l_fork);
214        }
215        else
216        {
217          ideal result;
218          if (! defined(groebner_error))
219          {
220            int groebner_error = 1;
221            export groebner_error;
222          }
223          "// ** groebner did not finish";
224          j = system("sh", "kill " + string(pid));
225        }
226        return (result);
227      }
228      else
229      {
230        "// ** groebner needs int as 2nd arg";
231      }
232    }
233    else
234    {
235      "// ** groebner with two args is not supported in this configuration";
236    }
237  }
238
239  // we are still here -- do the actual computation
240  string ordstr_P = ordstr(P);
241  if (find(ordstr_P,"s") > 0)
242  {
243    //spaeter den lokalen fall ueber lp oder aehnlich behandeln
244    return(std(i));
245  }
246
247  int IsSimple_P;
248  if (system("nblocks") <= 2)
249  {
250    if (find(ordstr_P, "M") <= 0)
251    {
252      IsSimple_P = 1;
253    }
254  }
255  int npars_P = npars(P);
256
257  // return std if no parameters and (dp or wp)
258  if ((npars_P <= 1) && IsSimple_P)
259  {
260    if (find(ordstr_P, "d") > 0)
261    {
262      return (std(i));
263    }
264    if (find(ordstr_P,"w") > 0)
265    {
266      return (std(i));
267    }
268  }
269
270  // reset options
271  intvec opt=option(get);
272  int p_opt;
273  string s_opt = option();
274  option(none);
275  // turn on option(prot) and/or option(mem), if previously set
276  if (find(s_opt, "prot"))
277  {
278    option(prot);
279    p_opt = 1;
280  }
281  if (find(s_opt, "mem"))
282  {
283    option(mem);
284  }
285
286  // construct ring in which first std computation is done
287  string varstr_P = varstr(P);
288  string parstr_P = parstr(P);
289  int is_homog = (homog(i) && (npars_P <= 1));
290  int add_vars = 0;
291  string ri = "ring Phelp =";
292
293  // more than one parameters are converted to ring variables
294  if (npars_P > 1)
295  {
296    ri = ri + string(char(P)) + ",(" + varstr_P + "," + parstr_P;
297    add_vars = npars_P;
298  }
299  else
300  {
301    ri = ri + "(" + charstr(P) + "),(" + varstr_P;
302  }
303
304  // a homogenizing variable is added, if necessary
305  if (! is_homog)
306  {
307    ri = ri + ",@t";
308    add_vars = add_vars + 1;
309  }
310  // ordering is set to (dp, C)
311  ri = ri + "),(dp,C);";
312
313  // change the ring
314  execute(ri);
315
316  // get ideal from previous ring
317  if (is_homog)
318  {
319    ideal qh = imap(P, i);
320  }
321  else
322  {
323    // and homogenize
324    ideal qh=homog(imap(P,i),@t);
325  }
326
327  // compute std and hilbert series
328  if (p_opt)
329  {
330    "std in " + ri[13, size(ri) - 13];
331  }
332  intvec hi=hilb(std(qh),1);
333
334  if (add_vars == 0)
335  {
336    // no additional variables were introduced
337    setring P; // can immediately change to original ring
338    // simply compute std with hilbert series in original ring
339    if (p_opt)
340    {
341      "std with hilb in basering";
342    }
343    i = std(i, hi);
344  }
345  else
346  {
347    // additional variables were introduced
348    // need another intermediate ring
349    ri = "ring Phelp1 = (" + charstr(Phelp)
350      + "),(" + varstr(Phelp) + "),(" + ordstr_P;
351
352    // for lp wit at most one parameter, we do not need a block ordering
353    if ( ! (IsSimple_P && (add_vars <2) && find(ordstr_P, "l")))
354    {
355      // need block ordering
356      ri = ri + ", dp(" + string(add_vars) + ")";
357    }
358    ri = ri + ");";
359
360    // change to intermediate ring
361    execute(ri);
362    ideal qh = imap(Phelp, qh);
363    kill Phelp;
364    if (p_opt)
365    {
366      "std with hilb in " + ri[14,size(ri)-14];
367    }
368    // compute std with Hilbert series
369    qh = std(qh, hi);
370    // subst 1 for homogenizing var
371    if (!is_homog)
372    {
373      if (p_opt)
374      {
375        "dehomogenization";
376      }
377      qh = subst(qh, @t, 1);
378    }
379
380    // go back to original ring
381    setring P;
382    // get ideal, delete zeros and clean SB
383    if (p_opt)
384    {
385      "imap to original ring";
386    }
387    i = imap(Phelp1,qh);
388    if (p_opt)
389    {
390      "simplification";
391    }
392    i = simplify(i, 34);
393    kill Phelp1;
394  }
395
396  // clean-up time
397  option(set, opt);
398  if (find(s_opt, "redSB") > 0)
399  {
400    if (p_opt)
401    {
402      "interreduction";
403    }
404    i=interred(i);
405  }
406  attrib(i, "isSB", 1);
407  return (i);
408}
409example
410{ "EXAMPLE: "; echo = 2;
411  ring r = 0, (a,b,c,d), lp;
412  option(prot);
413  ideal i = a+b+c+d, ab+ad+bc+cd, abc+abd+acd+bcd, abcd-1; // cyclic 4
414  groebner(i);
415  ring rp = (0, a, b), (c,d), lp;
416  ideal i = imap(r, i);
417  ideal j = groebner(i);
418  option(noprot);
419  j; simplify(j, 1); std(i);
420  if (system("with", "MP")) {groebner(i, 0);}
421  defined(groebner_error);
422}
423//////////////////////////////////////////////////////////////////////////
424
425proc res(list #)
426"@c we do texinfo here:
427@cindex resolution, computation of
428@table @code
429@item @strong{Syntax:}
430@code{res (} ideal_expression@code{,} int_expression @code{[,} any_expression @code{])}
431@*@code{res (} module_expression@code{,} int_expression @code{[,} any_expression @code{])}
432@item @strong{Type:}
433resolution
434@item @strong{Purpose:}
435computes a (possibly minimal) free resolution of an ideal or module using
436a heuristically choosen method.
437@* The second (int) argument (say, @code{k}) specifies the length of
438the resolution. If @code{k <=0 } then k is assumed to be the number of
439variables of the basering.
440@* If a third argument is given, the returned resolution is minimized.
441
442Depending on the input, the returned resolution is computed using the
443following methods:
444@table @asis
445@item @strong{quotient rings:}
446@code{nres} (classical method using syzygies) , see @ref{nres}.
447
448@item @strong{homogenous ideals and k == 0:}
449@code{lres} (La'Scala's method), see @ref{lres}.
450
451@item @strong{not minimized resolution,  and, homogenous input with k != 0 or local rings:}
452@code{sres} (Schreyer's method), see @ref{sres}.
453
454@item @strong{all other inputs:}
455@code{mres} (classical method), see @ref{mres}.
456@end table
457@item @strong{Note:}
458Accessing single elements of a resolution may require that some partial computations have to be finished and may therefor take some time.
459@end table
460@c ref
461See also
462@ref{betti};
463@ref{ideal};
464@ref{minres};
465@ref{module};
466@ref{mres};
467@ref{nres};
468@ref{lres};
469@ref{hres};
470@ref{sres}.
471@ref{resolution}
472@c ref
473"
474{
475   def P=basering;
476   if (size(#) < 2)
477   {
478     ERROR("res: need at least two arguments: ideal/module, int");
479   }
480
481   def m=#[1]; //the ideal or module
482   int i=#[2]; //the length of the resolution
483   if (i< 0) { i=0;}
484
485   string varstr_P = varstr(P);
486
487   int p_opt;
488   string s_opt = option();
489   // set p_opt, if option(prot) is set
490   if (find(s_opt, "prot"))
491   {
492     p_opt = 1;
493   }
494
495   if(size(ideal(basering)) > 0)
496   {
497     // the quick hack for qrings - seems to fit most needs
498     // (lres is not implemented for qrings, sres is not so efficient)
499     if (p_opt) { "using nres";}
500     return(nres(m,i));
501   }
502
503   if(homog(m)==1)
504   {
505      resolution re;
506      if (((i==0) or (i>=nvars(basering))) && typeof(m) != "module")
507      {
508        //LaScala for the homogeneous case and i == 0
509        if (p_opt) { "using lres";}
510        re=lres(m,i);
511        if(size(#)>2)
512        {
513           re=minres(re);
514        }
515      }
516      else
517      {
518        if(size(#)>2)
519        {
520          if (p_opt) { "using mres";}
521          re=mres(m,i);
522        }
523        else
524        {
525          if (p_opt) { "using sres";}
526          re=sres(std(m),i);
527        }
528      }
529      return(re);
530   }
531
532   //mres for the global non homogeneous case
533   if(find(ordstr(P),"s")==0)
534   {
535      string ri= "ring Phelp ="
536                  +string(char(P))+",("+varstr_P+"),(dp,C);";
537      execute(ri);
538      def m=imap(P,m);
539      if (p_opt) { "using mres in another ring";}
540      list re=mres(m,i);
541      setring P;
542      resolution result=imap(Phelp,re);
543      if (size(#) > 2) {result = minres(result);}
544      return(result);
545   }
546
547   //sres for the local case and not minimal resolution
548   if(size(#)<=2)
549   {
550      string ri= "ring Phelp ="
551                  +string(char(P))+",("+varstr_P+"),(ls,c);";
552      execute(ri);
553      def m=imap(P,m);
554      m=std(m);
555      if (p_opt) { "using sres in another ring";}
556      list re=sres(m,i);
557      setring P;
558      resolution result=imap(Phelp,re);
559      return(result);
560   }
561
562   //mres for the local case and minimal resolution
563   string ri= "ring Phelp ="
564                  +string(char(P))+",("+varstr_P+"),(ls,C);";
565   execute(ri);
566   def m=imap(P,m);
567    if (p_opt) { "using mres in another ring";}
568   list re=mres(m,i);
569   setring P;
570   resolution result=imap(Phelp,re);
571   result = minres(result);
572   return(result);
573}
574example
575{"EXAMPLE:"; echo = 2;
576  ring r=0,(x,y,z),dp;
577  ideal i=xz,yz,x^3-y^3;
578  def l=res(i,0); // homogenous ideal: uses lres
579  l;              // resolution is not yet minimized
580  print(betti(l), "betti"); // input to betti may be of type resolution
581  l[2];         // element access may take some time
582  i=i, x+1;
583  l=res(i,0);   // inhomogenous ideal: uses mres
584  l;            // resolution is not yet minimized
585  ring rs=0,(x,y,z),ds;
586  ideal i = imap(r, i);
587  def l=res(i,0); // local ring not minimized: uses sres
588  l;              // resolution is minimized
589  res(i,0,0);     // local ring and minimized: uses mres
590}
591/////////////////////////////////////////////////////////////////////////
592
593proc quot (m1,m2,list #)
594"SYNTAX: quot(module_expression, module_expression);
595         @*quot(module_expression, module_expression, int_expression);
596         @*quot(ideal_expression, ideal_expression);
597         @*quot(ideal_expression, ideal_expression, int_expression);
598TYPE:    ideal
599SYNTAX:  quot(module_expression, ideal_expression);
600TYPE:    module
601PURPOSE: computes the quotient of the first and the 2nd argument
602         If a 3rd argumnt 'n' is given the n-th method is used
603         (1 <= n <= 5).
604SEE ALSO: quotient
605EXAMPLE: example quot; shows an example"
606{
607  if (((typeof(m1)!="ideal") and (typeof(m1)!="module"))
608     or ((typeof(m2)!="ideal") and (typeof(m2)!="module")))
609  {
610    "USAGE:   quot(m1, m2[, n]); m1, m2 two submodules of k^s,";
611    "         n (optional) integer (1<= n <=5)";
612    "RETURN:  the quotient of m1 and m2";
613    "EXAMPLE: example quot; shows an example";
614    return();
615  }
616  if (typeof(m1)!=typeof(m2))
617  {
618    return(quotient(m1,m2));
619  }
620  if (size(#)>0)
621  {
622    if (typeof(#[1])=="int" )
623    {
624      return(quot1(m1,m2,#[1]));
625    }
626  }
627  else
628  {
629    return(quot1(m1,m2,2));
630  }
631}
632example
633{ "EXAMPLE:"; echo = 2;
634  ring r=181,(x,y,z),(c,ls);
635  ideal id1=maxideal(4);
636  ideal id2=x2+xyz,y2-z3y,z3+y5xz;
637  option(prot);
638  ideal id6=quotient(id1,id2);
639  id6;
640  ideal id7=quot(id1,id2,1);
641  id7;
642  ideal id8=quot(id1,id2,2);
643  id8;
644}
645
646static proc quot1 (module m1, module m2,int n)
647"USAGE:   quot1(m1, m2, n); m1, m2 two submodules of k^s,
648         n integer (1<= n <=5)
649RETURN:  the quotient of m1 and m2
650EXAMPLE: example quot1; shows an example"
651{
652  if (n==1)
653  {
654    return(quotient1(m1,m2));
655  }
656  else
657  {
658    if (n==2)
659    {
660      return(quotient2(m1,m2));
661    }
662    else
663    {
664      if (n==3)
665      {
666        return(quotient3(m1,m2));
667      }
668      else
669      {
670        if (n==4)
671        {
672          return(quotient4(m1,m2));
673        }
674        else
675        {
676          if (n==5)
677          {
678            return(quotient5(m1,m2));
679          }
680          else
681          {
682            return(quotient(m1,m2));
683          }
684        }
685      }
686    }
687  }
688}
689example
690{ "EXAMPLE:"; echo = 2;
691  ring r=181,(x,y,z),(c,ls);
692  ideal id1=maxideal(4);
693  ideal id2=x2+xyz,y2-z3y,z3+y5xz;
694  option(prot);
695  ideal id6=quotient(id1,id2);
696  id6;
697  ideal id7=quot1(id1,id2,1);
698  id7;
699  ideal id8=quot1(id1,id2,2);
700  id8;
701}
702
703static proc quotient0(module a,module b)
704{
705  module mm=b+a;
706  resolution rs=lres(mm,0);
707  list I=list(rs);
708  matrix M=I[2];
709  matrix A[1][nrows(M)]=M[1..nrows(M),1];
710  ideal i=A;
711  return (i);
712}
713proc quotient1(module a,module b)  //17sec
714"USAGE:   quotient1(m1, m2); m1, m2 two submodules of k^s,
715RETURN:  the quotient of m1 and m2"
716{
717  int i;
718  a=std(a);
719  module dummy;
720  module B=NF(b,a)+dummy;
721  ideal re=quotient(a,module(B[1]));
722  for(i=2;i<=size(B);i++)
723  {
724     re=intersect1(re,quotient(a,module(B[i])));
725  }
726  return(re);
727}
728proc quotient2(module a,module b)    //13sec
729"USAGE:   quotient2(m1, m2); m1, m2 two submodules of k^s,
730RETURN:  the quotient of m1 and m2"
731{
732  a=std(a);
733  module dummy;
734  module bb=NF(b,a)+dummy;
735  int i=size(bb);
736  ideal re=quotient(a,module(bb[i]));
737  bb[i]=0;
738  module temp;
739  module temp1;
740  module bbb;
741  int mx;
742  i=i-1;
743  while (1)
744  {
745    if (i==0) break;
746    temp = a+bb*re;
747    temp1 = lead(interred(temp));
748    mx=ncols(a);
749    if (ncols(temp1)>ncols(a))
750    {
751      mx=ncols(temp1);
752    }
753    temp1 = matrix(temp1,1,mx)-matrix(lead(a),1,mx);
754    temp1 = dummy+temp1;
755    if (deg(temp1[1])<0) break;
756    re=intersect1(re,quotient(a,module(bb[i])));
757    bb[i]=0;
758    i = i-1;
759  }
760  return(re);
761}
762proc quotient3(module a,module b)   //89sec
763"USAGE:   quotient3(m1, m2); m1, m2 two submodules of k^s,
764         only for global rings
765RETURN:  the quotient of m1 and m2"
766{
767  string s="ring @newr=("+charstr(basering)+
768           "),("+varstr(basering)+",@t,@w),dp;";
769  def @newP=basering;
770  execute(s);
771  module b=imap(@newP,b);
772  module a=imap(@newP,a);
773  int i;
774  int j=size(b);
775  vector @b;
776  for(i=1;i<=j;i++)
777  {
778     @b=@b+@t^(i-1)*@w^(j-i+1)*b[i];
779  }
780  ideal re=quotient(a,module(@b));
781  setring @newP;
782  ideal re=imap(@newr,re);
783  return(re);
784}
785proc quotient5(module a,module b)   //89sec
786"USAGE:   quotient5(m1, m2); m1, m2 two submodules of k^s,
787         only for global rings
788RETURN:  the quotient of m1 and m2"
789{
790  string s="ring @newr=("+charstr(basering)+
791           "),("+varstr(basering)+",@t),dp;";
792  def @newP=basering;
793  execute(s);
794  module b=imap(@newP,b);
795  module a=imap(@newP,a);
796  int i;
797  int j=size(b);
798  vector @b;
799  for(i=1;i<=j;i++)
800  {
801     @b=@b+@t^(i-1)*b[i];
802  }
803  @b=homog(@b,@w);
804  ideal re=quotient(a,module(@b));
805  setring @newP;
806  ideal re=imap(@newr,re);
807  return(re);
808}
809proc quotient4(module a,module b)   //95sec
810"USAGE:   quotient4(m1, m2); m1, m2 two submodules of k^s,
811         only for global rings
812RETURN:  the quotient of m1 and m2"
813{
814  string s="ring @newr=("+charstr(basering)+
815           "),("+varstr(basering)+",@t),dp;";
816  def @newP=basering;
817  execute(s);
818  module b=imap(@newP,b);
819  module a=imap(@newP,a);
820  int i;
821  vector @b=b[1];
822  for(i=2;i<=size(b);i++)
823  {
824     @b=@b+@t^(i-1)*b[i];
825  }
826  matrix sy=modulo(@b,a);
827  ideal re=sy;
828  setring @newP;
829  ideal re=imap(@newr,re);
830  return(re);
831}
832static proc intersect1(ideal i,ideal j)
833{
834  def R=basering;
835  execute("ring gnir = ("+charstr(basering)+"),
836                       ("+varstr(basering)+",@t),(C,dp);");
837  ideal i=var(nvars(basering))*imap(R,i)+(var(nvars(basering))-1)*imap(R,j);
838  ideal j=eliminate(i,var(nvars(basering)));
839  setring R;
840  map phi=gnir,maxideal(1);
841  return(phi(j));
842}
843
844//////////////////////////////////////////////////////////////////
845///
846/// sprintf, fprintf printf
847///
848proc sprintf(string fmt, list #)
849"USAGE:    sprintf(fmt, ...) fmt string
850RETURN:   string
851PURPOSE:  sprintf performs output formatting. The first argument is a format
852          control string. Additional arguments may be required, depending on
853          the contents of the control string. A series of output characters is
854          generated as directed by the control string; these characters are
855          returned as a string. The control string is simply text to be copied,
856          except that the string may contain conversion specifications. Do
857          'help print:' for a listing of valid conversion specifications.
858          As an addition to the conversions of 'print', the '%n' and '%2'
859          conversion specification does not consume an additional argument,
860          but simply generates a newline character.
861NOTE:     If one of the additional arguments is a list, then it should be
862          enclosed once more into a list() command, since passing a list
863          as an argument flattens the list by one level.
864SEE ALSO: fprintf, printf, print, string
865EXAMPLE : example sprintf; shows an example
866"
867{
868  int sfmt = size(fmt);
869  if (sfmt  <= 1)
870  {
871    return (fmt);
872  }
873  int next, l, nnext;
874  string ret;
875  list formats = "%l", "%s", "%2l", "%2s", "%t", "%;", "%p", "%b", "%n", "%2";
876  while (1)
877  {
878    if (size(#) <= 0)
879    {
880      return (ret + fmt);
881    }
882    nnext = 0;
883    while (nnext < sfmt)
884    {
885      nnext = find(fmt, "%", nnext + 1);
886      if (nnext == 0)
887      {
888        next = 0;
889        break;
890      }
891      l = 1;
892      while (l <= size(formats))
893      {
894        next = find(fmt, formats[l], nnext);
895        if (next == nnext) break;
896        l++;
897      }
898      if (next == nnext) break;
899    }
900    if (next == 0)
901    {
902      return (ret + fmt);
903    }
904    if (formats[l] != "%2" && formats[l] != "%n")
905    {
906      ret = ret + fmt[1, next - 1] + print(#[1], formats[l]);
907      # = delete(#, 1);
908    }
909    else
910    {
911      ret = ret + fmt[1, next - 1] + print("", "%2s");
912    }
913    if (size(fmt) <= (next + size(formats[l]) - 1))
914    {
915      return (ret);
916    }
917    fmt = fmt[next + size(formats[l]), size(fmt)-next-size(formats[l]) + 1];
918  }
919}
920example
921{ "EXAMPLE:"; echo=2;
922  ring r=0,(x,y,z),dp;
923  module m=[1,y],[0,x+z];
924  intmat M=betti(mres(m,0));
925  list l = r, m, M;
926  string s = sprintf("s:%s,%n l:%l", 1, 2); s;
927  s = sprintf("s:%n%s", l); s;
928  s = sprintf("s:%2%s", list(l)); s;
929  s = sprintf("2l:%n%2l", list(l)); s;
930  s = sprintf("%p", list(l)); s;
931  s = sprintf("%;", list(l)); s;
932  s = sprintf("%b", M); s;
933}
934
935proc printf(string fmt, list #)
936"USAGE:    printf(fmt, ...) fmt string
937RETURN:   none
938PURPOSE:  printf performs output formatting. The first argument is a format
939          control string. Additional arguments may be required, depending on
940          the contents of the control string. A series of output characters is
941          generated as directed by the control string; these characters are
942          displayed (i.e. printed to standard out).
943          The control string is simply text to be copied, except that the
944          string may contain conversion specifications.
945          Do 'help print:' for a listing of valid conversion specifications.
946          As an addition to the conversions of 'print', the '%n' and '%2'
947          conversion specification does not consume an additional argument,
948          but simply generates a newline character.
949
950NOTE:     If one of the additional arguments is a list, then it should be
951          enclosed once more into a list() command, since passing a list
952          as an argument flattens the list by one level.
953SEE ALSO: sprintf, fprintf, print, string
954EXAMPLE : example printf; shows an example
955"
956{
957  write("", sprintf(fmt, #));
958}
959example
960{ "EXAMPLE:"; echo=2;
961  ring r=0,(x,y,z),dp;
962  module m=[1,y],[0,x+z];
963  intmat M=betti(mres(m,0));
964  list l = r, m, M;
965  printf("s:%s, l:%l", 1, 2);
966  printf("s:%s", l);
967  printf("s:%s", list(l));
968  printf("2l:%2l", list(l));
969  printf("%p", list(l));
970  printf("%;", list(l));
971  printf("%b", M);
972}
973
974
975proc fprintf(link l, string fmt, list #)
976"USAGE:    fprintf(l, fmt, ...) l link; fmt string
977RETURN:   none
978PURPOSE:  fprintf performs output formatting. The second argument is a format
979          control string. Additional arguments may be required, depending on
980          the contents of the control string. A series of output characters is
981          generated as directed by the control string; these characters are
982          written to the link l.
983          The control string is simply text to be copied, except that the
984          string may contain conversion specifications.
985          Do 'help print:' for a listing of valid conversion specifications.
986          As an addition to the conversions of 'print', the '%n' and '%2'
987          conversion specification does not consume an additional argument,
988          but simply generates a newline character.
989
990NOTE:     If one of the additional arguments is a list, then it should be
991          enclosed once more into a list() command, since passing a list
992          as an argument flattens the list by one level.
993SEE ALSO: sprintf, printf, print, string
994EXAMPLE : example fprintf; shows an example
995"
996{
997  write(l, sprintf(fmt, #));
998}
999example
1000{ "EXAMPLE:"; echo=2;
1001  ring r=0,(x,y,z),dp;
1002  module m=[1,y],[0,x+z];
1003  intmat M=betti(mres(m,0));
1004  list l = r, m, M;
1005  link li = ""; // link to stdout
1006  fprintf(li, "s:%s, l:%l", 1, 2);
1007  fprintf(li, "s:%s", l);
1008  fprintf(li, "s:%s", list(l));
1009  fprintf(li, "2l:%2l", list(l));
1010  fprintf(li, "%p", list(l));
1011  fprintf(li, "%;", list(l));
1012  fprintf(li, "%b", M);
1013}
1014
1015/*
1016proc minres(list #)
1017{
1018  if (size(#) == 2)
1019  {
1020    if (typeof(#[1]) == "ideal" || typeof(#[1]) == "module")
1021    {
1022      if (typeof(#[2] == "int"))
1023      {
1024        return (res(#[1],#[2],1));
1025      }
1026    }
1027  }
1028
1029  if (typeof(#[1]) == "resolution")
1030  {
1031    return minimizeres(#[1]);
1032  }
1033  else
1034  {
1035    return minimizeres(#);
1036  }
1037
1038}
1039*/
Note: See TracBrowser for help on using the repository browser.