source: git/Singular/LIB/standard.lib @ 803c5a1

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