source: git/Singular/LIB/standard.lib @ 94e2bf

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