source: git/Singular/LIB/ring.lib @ 6d37e8

spielwiese
Last change on this file since 6d37e8 was b9b906, checked in by Hans Schönemann <hannes@…>, 23 years ago
*hannes: lib format revisited git-svn-id: file:///usr/local/Singular/svn/trunk@5078 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 29.0 KB
Line 
1//(GMG, last modified 03.11.95)
2///////////////////////////////////////////////////////////////////////////////
3version="$Id: ring.lib,v 1.17 2001-01-16 13:48:41 Singular Exp $";
4category="General purpose";
5info="
6LIBRARY:  ring.lib      Manipulating Rings and Maps
7
8PROCEDURES:
9 changechar(\"R\",c[,r]); make a copy R of basering [ring r] with new char c
10 changeord(\"R\",o[,r]);  make a copy R of basering [ring r] with new ord o
11 changevar(\"R\",v[,r]);  make a copy R of basering [ring r] with new vars v
12 defring(\"R\",c,n,v,o);  define a ring R in specified char c, n vars v, ord o
13 defrings(n[,p]);         define ring Sn in n vars, char 32003 [p], ord ds
14 defringp(n[,p]);         define ring Pn in n vars, char 32003 [p], ord dp
15 extendring(\"R\",n,v,o); extend given ring by n vars v, ord o and name it R
16 fetchall(R[,str]);       fetch all objects of ring R to basering
17 imapall(R[,str]);        imap all objects of ring R to basering
18 mapall(R,i[,str]);       map all objects of ring R via ideal i to basering
19 ord_test(R);             test wether ordering of R is global, local or mixed
20 ringtensor(\"R\",s,t,..);create ring R, tensor product of rings s,t,...
21 ringweights(r);          intvec of weights of ring variables of ring r
22             (parameters in square brackets [] are optional)
23";
24
25LIB "inout.lib";
26LIB "general.lib";
27///////////////////////////////////////////////////////////////////////////////
28
29proc changechar (string newr, string c, list #)
30"USAGE:   changechar(newr,c[,r]);  newr,c=strings, r=ring
31CREATE:  create a new ring with name `newr` and make it the basering if r is
32         an existing ring [default: r=basering].
33         The new ring differs from the old ring only in the characteristic.
34         If, say, (newr,c) = (\"R\",\"0,A\") and the ring r exists, the new
35         basering will have name R, characteristic 0 and one parameter A.
36RETURN:  No return value
37NOTE:    Works for qrings if map from old_char to new_char is implemented
38         This proc uses 'execute' or calls a procedure using 'execute'.
39         If you use it in your own proc, let the local names of your proc
40         start with @.
41EXAMPLE: example changechar; shows an example
42"
43{
44   if( size(#)==0 ) { def @r=basering; }
45   if( size(#)==1 ) { def @r=#[1]; }
46   setring @r;
47   ideal i = ideal(@r); int @q = size(i);
48   if( @q!=0 )
49      { string @s = "@newr1"; }
50   else
51      { string @s = newr; }
52   string @newring = @s+"=("+c+"),("+varstr(@r)+"),("+ordstr(@r)+");";
53   execute("ring "+@newring);
54   if( @q!=0 )
55   {
56      map phi = @r,maxideal(1);
57      ideal i = phi(i);
58      attrib(i,"isSB",1);         //*** attrib funktioniert ?
59      execute("qring "+newr+"=i;");
60   }
61   export(`newr`);
62   keepring(`newr`);
63   if (voice==2) { "// basering is now",newr; }
64   return();
65}
66example
67{  "EXAMPLE:"; echo = 2;
68   ring r=0,(x,y,u,v),(dp(2),ds);
69   changechar("R","2,A"); R;"";
70   changechar("R1","32003",R); R1;
71   kill R,R1;
72   if(system("with","Namespaces")) {
73      if( nameof(Current) == "Ring" ) {
74        kill Top::R,Top::R1;
75      } else {
76        kill Ring::R,Ring::R1;
77      }
78   }
79}
80///////////////////////////////////////////////////////////////////////////////
81
82proc changeord (string newr, string o, list #)
83"USAGE:   changeord(newr,o[,r]);  newr,o=strings, r=ring/qring
84CREATE:  create a new ring with name `newr` and make it the basering if r is
85         an existing ring/qring [default: r=basering].
86         The new ring differs from the old ring only in the ordering. If, say,
87         (newr,o) = (\"R\",\"wp(2,3),dp\") and the ring r exists and has >=3
88         variables, the new basering will have name R and ordering wp(2,3),dp.
89RETURN:  No return value
90NOTE:    This proc uses 'execute' or calls a procedure using 'execute'.
91         If you use it in your own proc, let the local names of your proc
92         start with @.
93EXAMPLE: example changeord; shows an example
94"
95{
96   if( size(#)==0 ) { def @r=basering; }
97   if( size(#)==1 ) { def @r=#[1]; }
98   setring @r;
99   ideal i = ideal(@r); int @q = size(i);
100   if( @q!=0 )
101      { string @s = "@newr1"; }
102   else
103      { string @s = newr; }
104   string @newring = @s+"=("+charstr(@r)+"),("+varstr(@r)+"),("+o+");";
105   execute("ring "+@newring);
106   if( @q!=0 )
107   {
108      map phi = @r,maxideal(1);
109      ideal i = phi(i);
110      attrib(i,"isSB",1);         //*** attrib funktioniert ?
111      execute("qring "+newr+"=i;");
112   }
113   export(`newr`);
114   keepring(`newr`);
115   if (voice==2) { "// basering is now",newr; }
116   return();
117}
118example
119{  "EXAMPLE:"; echo = 2;
120   ring r=0,(x,y,u,v),(dp(2),ds);
121   changeord("R","wp(2,3),dp"); R; "";
122   ideal i = x^2,y^2-u^3,v;
123   qring Q = std(i);
124   changeord("Q'","lp",Q); Q';
125   kill R,Q,Q';
126   if(system("with","Namespaces")) {
127      if( nameof(Current) == "Ring" ) {
128        kill Top::R,Top::Q';
129      } else {
130        kill Ring::R,Ring::Q';
131      }
132   }
133}
134///////////////////////////////////////////////////////////////////////////////
135
136proc changevar (string newr, string vars, list #)
137"USAGE:   changevar(newr,vars[,r]);  newr,vars=strings, r=ring/qring
138CREATE:  creates a new ring with name `newr` and makes it the basering if r
139         is an existing ring/qring [default: r=basering].
140         The new ring differs from the old ring only in the variables. If,
141         say, (newr,vars) = (\"R\",\"t()\") and the ring r exists and has n
142         variables, the new basering will have name R and variables
143         t(1),...,t(n).
144         If vars = \"a,b,c,d\", the new ring will have the variables a,b,c,d.
145RETURN:  No return value
146NOTE:    This procedure is useful in connection with the procedure ringtensor,
147         when a conflict between variable names must be avoided.
148         This proc uses 'execute' or calls a procedure using 'execute'.
149         If you use it in your own proc, let the local names of your proc
150         start with @.
151EXAMPLE: example changevar; shows an example
152"
153{
154   if( size(#)==0 ) { def @r=basering; }
155   if( size(#)==1 ) { def @r=#[1]; }
156   setring @r;
157   ideal i = ideal(@r); int @q = size(i);
158   if( @q!=0 )
159      { string @s = "@newr1"; }
160   else
161      { string @s = newr; }
162   string @newring = @s+"=("+charstr(@r)+"),(";
163   if( vars[size(vars)-1]=="(" and vars[size(vars)]==")" )
164   {
165      @newring = @newring+vars[1,size(vars)-2]+"(1.."+string(nvars(@r))+")";
166   }
167   else { @newring = @newring+vars; }
168   @newring = @newring+"),("+ordstr(@r)+");";
169   execute("ring "+@newring);
170   if( @q!=0 )
171   {
172      map phi = @r,maxideal(1);
173      ideal i = phi(i);
174      attrib(i,"isSB",1);         //*** attrib funktioniert ?
175      execute("qring "+newr+"=i;");
176   }
177   export(`newr`);
178   keepring(`newr`);
179   if (voice==2) { "// basering is now",newr; }
180   return();
181}
182example
183{  "EXAMPLE:"; echo = 2;
184   ring r=0,(x,y,u,v),(dp(2),ds);
185   ideal i = x^2,y^2-u^3,v;
186   qring Q = std(i);
187   setring(r);
188   changevar("R","A()"); R; "";
189   changevar("Q'","a,b,c,d",Q); Q';
190   kill R,Q,Q';
191   if(system("with","Namespaces")) {
192      if( nameof(Current) == "Ring" ) {
193        kill Top::R,Top::Q';
194      } else {
195        kill Ring::R,Ring::Q';
196      }
197   }
198}
199///////////////////////////////////////////////////////////////////////////////
200
201proc defring (string s1, string s2, int n, string s3, string s4)
202"USAGE:   defring(s1,s2,n,s3,s4);  s1..s4=strings, n=integer
203CREATE:  Define a ring with name 's1', characteristic 's2', ordering 's4' and
204         n variables with names derived from s3 and make it the basering.
205         If s3 is a single letter, say s3=\"a\", and if n<=26 then a and the
206         following n-1 letters from the alphabeth (cyclic order) are taken as
207         variables. If n>26 or if s3 is a single letter followed by (, say
208         s3=\"T(\", the variables are T(1),...,T(n).
209RETURN:  No return value
210NOTE:    This proc is useful for defining a ring in a procedure.
211         This proc uses 'execute' or calls a procedure using 'execute'.
212         If you use it in your own proc, let the local names of your proc
213         start with @.
214EXAMPLE: example defring; shows an example
215"
216{
217   string @newring = "ring "+s1+"=("+s2+"),(";
218   if( n>26 or s3[2]=="(" ) { string @v = s3[1]+"(1.."+string(n)+")"; }
219   else { string @v = A_Z(s3,n); }
220   @newring=@newring+@v+"),("+s4+");";
221   execute(@newring);
222   export(basering);
223   keepring(`s1`);
224   if (voice==2) { "// basering is now:",s1; }
225   return();
226}
227example
228{ "EXAMPLE:"; echo = 2;
229   defring("r","0",5,"u","ls"); r; "";
230   defring("R","2,A",10,"x(","dp(3),ws(1,2,3),ds"); R;
231   kill r,R;
232   if(system("with","Namespaces")) {
233      if( nameof(Current) == "Ring" ) {
234        kill Top::r,Top::R;
235      } else {
236        kill Ring::r,Ring::R;
237      }
238   }
239}
240///////////////////////////////////////////////////////////////////////////////
241
242proc defrings (int n, list #)
243"USAGE:   defrings(n,[p]);  n,p integers
244CREATE:  Defines a ring with name Sn, characteristic p, ordering ds and n
245         variables x,y,z,a,b,...if n<=26 (resp. x(1..n) if n>26) and makes it
246         the basering (default: p=32003)
247RETURN:  No return value
248EXAMPLE: example defrings; shows an example
249"
250{
251   int p;
252   if (size(#)==0) { p=32003; }
253   else { p=#[1]; }
254   if (n >26)
255   {
256      string s="ring S"+string(n)+"="+string(p)+",x(1.."+string(n)+"),ds;";
257   }
258   else
259   {
260      string s="ring S"+string(n)+"="+string(p)+",("+A_Z("x",n)+"),ds;";
261   }
262   execute(s);
263   export basering;
264   execute("keepring S"+string(n)+";");
265   if (voice==2) { "// basering is now:",s; }
266}
267example
268{ "EXAMPLE:"; echo = 2;
269   defrings(5,0); S5; "";
270   defrings(30); S30;
271   kill S5, S30;
272   if(system("with","Namespaces")) {
273      if( nameof(Current) == "Ring" ) {
274        kill Top::S5,Top::S30;
275      } else {
276        kill Ring::S5,Ring::S30;
277      }
278   }
279}
280///////////////////////////////////////////////////////////////////////////////
281
282proc defringp (int n,list #)
283"USAGE:   defringp(n,[p]);  n,p=integers
284CREATE:  defines a ring with name Pn, characteristic p, ordering dp and n
285         variables x,y,z,a,b,...if n<=26 (resp. x(1..n) if n>26) and makes it
286         the basering (default: p=32003)
287RETURN:  No return value
288EXAMPLE: example defringp; shows an example
289"
290{
291   int p;
292   if (size(#)==0) { p=32003; }
293   else { p=#[1]; }
294   if (n >26)
295   {
296      string s="ring P"+string(n)+"="+string(p)+",x(1.."+string(n)+"),dp;";
297   }
298   else
299   {
300     string s="ring P"+string(n)+"="+string(p)+",("+A_Z("x",n)+"),dp;";
301   }
302   execute(s);
303   export basering;
304   execute("keepring P"+string(n)+";");
305   //the next comment is only shown if defringp is not called by another proc
306   if (voice==2) { "// basering is now:",s; }
307}
308example
309{ "EXAMPLE:"; echo = 2;
310   defringp(5,0); P5; "";
311   defringp(30); P30;
312   kill P5, P30;
313   if(system("with","Namespaces")) {
314      if( nameof(Current) == "Ring" ) {
315        kill Top::P5,Top::P30;
316      } else {
317        kill Ring::P5,Ring::P30;
318      }
319   }
320}
321///////////////////////////////////////////////////////////////////////////////
322
323proc extendring (string na, int n, string va, string o, list #)
324"USAGE:   extendring(na,n,va,o[iv,i,r]);  na,va,o=strings,
325         n,i=integers, r=ring, iv=intvec of positive integers or iv=0
326CREATE:  Define a ring with name `na` which extends the ring r by adding n new
327         variables in front of [after, if i!=0] the old variables and make it
328         the basering [default: (i,r)=(0,basering)].
329@*       -- The characteristic is the characteristic of r.
330@*       -- The new vars are derived from va. If va is a single letter, say
331            va=\"T\", and if n<=26 then T and the following n-1 letters from
332            T..Z..T (resp. T(1..n) if n>26) are taken as additional variables.
333            If va is a single letter followed by (, say va=\"x(\", the new
334            variables are x(1),...,x(n).
335@*       -- The ordering is the product ordering between the ordering of r and
336            an ordering derived from `o` [and iv].
337@*        -  If o contains a 'c' or a 'C' in front resp. at the end this is
338            taken for the whole ordering in front resp. at the end. If o does
339            not contain a 'c' or a 'C' the same rule applies to ordstr(r).
340@*        -  If no intvec iv is given, or if iv=0, o may be any allowed ordstr,
341            like \"ds\" or \"dp(2),wp(1,2,3),Ds(2)\" or \"ds(a),dp(b),ls\" if
342            a and b are globally (!) defined integers and if a+b+1<=n.
343            If, however, a and b are local to a proc calling extendring, the
344            intvec iv must be used to let extendring know the values of a and b
345@*        -  If an intvec iv !=0 is given, iv[1],iv[2],... is taken for the
346            1st, 2nd,... block of o, if o contains no substring \"w\" or \"W\"
347            i.e. no weighted ordering (in the above case o=\"ds,dp,ls\"
348            and iv=a,b).
349            If o contains a weighted ordering (only one (!) weighted block is
350            allowed) iv[1] is taken as size for the weight-vector, the next
351            iv[1] values of iv are taken as weights and the remaining values of
352            iv as block-size for the remaining non-weighted blocks.
353            e.g. o=\"dp,ws,Dp,ds\", iv=3,2,3,4,2,5 creates the ordering
354            dp(2),ws(2,3,4),Dp(5),ds
355RETURN:  No return value
356NOTE:    This proc is useful for adding deformation parameters.
357         This proc uses 'execute' or calls a procedure using 'execute'.
358         If you use it in your own proc, let the local names of your proc
359         start with @ (see the file HelpForProc)
360EXAMPLE: example extendring; shows an example
361"
362{
363//--------------- initialization and place c/C of ordering properly -----------
364   string @o1,@o2,@ro,@wstr,@v,@newring;
365   int @i,@w,@ii,@k;
366   intvec @iv,@iw;
367   if( find(o,"c")+find(o,"C") != 0)
368   {
369      @k=1;
370      if( o[1]=="c" or o[1]=="C" ) { @o1=o[1,2]; o=o[3..size(o)]; }
371      else                         { @o2=o[size(o)-1,2]; o=o[1..size(o)-2]; }
372   }
373   if( size(#)==0 ) { #[1]=0; }
374   if( typeof(#[1])!="intvec" )
375   {
376     if( size(#)==1 ) { @i=#[1]; def @r=basering; }
377     if( size(#)==2 ) { @i=#[1]; def @r=#[2]; }
378     if( o[size(o)]!=")" and find(o,",")==0 ) { o=o+"("+string(n)+")"; }
379   }
380   else
381   {
382     @iv=#[1];
383     if( size(#)==2 ) { @i=#[2]; def @r=basering; }
384     if( size(#)==3 ) { @i=#[2]; def @r=#[3]; }
385     if( @iv==0 && o[size(o)]!=")" && find(o,",")==0 ) {o=o+"("+string(n)+")";}
386   }
387   @ro=ordstr(@r);
388   if( @ro[1]=="c" or @ro[1]=="C" )
389      { @v=@ro[1,2]; @ro=@ro[3..size(@ro)]; }
390   else
391      { @wstr=@ro[size(@ro)-1,2]; @ro=@ro[1..size(@ro)-2]; }
392   if( @k==0) { @o1=@v; @o2=@wstr; }
393//----------------- prepare ordering if an intvec is given --------------------
394   if( typeof(#[1])=="intvec" and #[1]!=0 )
395   {
396      @k=n;                             //@k counts no of vars not yet ordered
397      @w=find(o,"w")+find(o,"W");o=o+" ";
398      if( @w!=0 )
399      {
400         @wstr=o[@w..@w+1];
401         o=o[1,@w-1]+"@"+o[@w+2,size(o)];
402         @iw=@iv[2..@iv[1]+1];
403         @wstr=@wstr+"("+string(@iw)+")";
404         @k=@k-@iv[1];
405         @iv=@iv[@iv[1]+2..size(@iv)];
406         @w=0;
407      }
408      for( @ii=1; @ii<=size(@iv); @ii=@ii+1 )
409      {
410         if( find(o,",",@w+1)!=0 )
411         {
412            @w=find(o,",",@w+1);
413            if( o[@w-1]!="@" )
414            {
415               o=o[1,@w-1]+"("+string(@iv[@ii])+")"+o[@w,size(o)];
416               @w=find(o,",",@w+1);
417               @k=@k-@iv[@ii];
418            }
419            else { @ii=@ii-1; }
420         }
421      }
422      @w=find(o,"@");
423      if( @w!=0 ) { o=o[1,@w-1] + @wstr + o[@w+1,size(o)]; }
424      if( @k>0 and o[size(o)]!=")" ) { o=o+"("+string(@k)+")"; }
425   }
426//------------------------ prepare string of new ring -------------------------
427   @newring = "ring "+na+"=("+charstr(@r)+"),(";
428   if( n>26 or va[2]=="(" ) { @v = va[1]+"(1.."+string(n)+")"; }
429   else                     { @v = A_Z(va,n); }
430   if( @i==0 )
431   {
432      @v=@v+","+varstr(@r);
433      o=@o1+o+","+@ro+@o2;
434   }
435   else
436   {
437      @v=varstr(@r)+","+@v;
438      o=@o1+@ro+","+o+@o2;
439   }
440   @newring=@newring+@v+"),("+o+");";
441//---------------------------- execute and export -----------------------------
442   execute(@newring);
443   export(basering);
444   keepring(`na`);
445   if (voice==2) { "// basering is now",na; }
446   return();
447}
448example
449{ "EXAMPLE:"; echo = 2;
450   ring r=0,(x,y,z),ds;
451   show(r);"";
452   //blocksize is derived from no of vars:
453   int t=5;
454   extendring("R1",t,"a","dp");         //t global: "dp" -> "dp(5)"
455   show(R1); "";
456   extendring("R2",4,"T(","c,dp",1,r);    //"dp" -> "c,..,dp(4)"
457   show(R2);"";
458
459   //no intvec given, blocksize given: given blocksize is used:
460   extendring("R3",4,"T(","dp(2)",0,r);   // "dp(2)" -> "dp(2)"
461   show(R3);"";
462
463   //intvec given: weights and blocksize is derived from given intvec
464   //(no specification of a blocksize in the given ordstr is allowed!)
465   //if intvec does not cover all given blocks, the last block is used
466   //for the remaining variables, if intvec has too many components,
467   //the last ones are ignored
468   intvec v=3,2,3,4,1,3;
469   extendring("R4",10,"A","ds,ws,Dp,dp",v,0,r);
470         //v covers 3 blocks: v[1] (=3) : no of components of ws
471         //next v[1] values (=v[2..4]) give weights
472         //remaining components of v are used for the remaining blocks
473   show(R4);
474   kill r,R1,R2,R3,R4;
475   if(system("with","Namespaces")) {
476      if( nameof(Current) == "Ring" ) {
477        kill Top::R1,Top::R2,Top::R3,Top::R4;
478      } else {
479        kill Ring::R1,Ring::R2,Ring::R3,Ring::R4;
480      }
481   }
482}
483///////////////////////////////////////////////////////////////////////////////
484
485proc fetchall (R, list #)
486"USAGE:   fetchall(R[,s]);  R=ring/qring, s=string
487CREATE:  fetch all objects of ring R (of type poly/ideal/vector/module/number/
488         matrix) into the basering.
489         If no 3rd argument is present, the names are the same as in R. If,
490         say, f is a poly in R and the 3rd argument is the string \"R\", then f
491         is maped to f_R etc.
492RETURN:  no return value
493NOTE:    As fetch, this procedure maps the 1st, 2nd, ... variable of R to the
494         1st, 2nd, ... variable of the basering.
495         The 3rd argument is useful in order to avoid conflicts of names, the
496         empty string is allowed
497CAUTION: fetchall does not work inside a procedure.
498         It does not work if R contains a map.
499EXAMPLE: example fetchall; shows an example
500"
501{
502   list @L@=names(R);
503   int @ii@; string @s@;
504   if( size(#) > 0 ) { @s@=@s@+"_"+#[1]; }
505   for( @ii@=size(@L@); @ii@>0; @ii@=@ii@-1 )
506   {
507      execute("def "+@L@[@ii@]+@s@+"=fetch(R,`@L@[@ii@]`);");
508      execute("export "+@L@[@ii@]+@s@+";");
509   }
510   return();
511}
512example
513{  "EXAMPLE:"; echo=2;
514// The example is not shown since fetchall does not work in a procedure;
515// (and hence not in the example procedure). Try the following commands:
516//   ring R=0,(x,y,z),dp;
517//   ideal j=x,y2,z2;
518//   matrix M[2][3]=1,2,3,x,y,z;
519//   j; print(M);
520//   ring S=0,(a,b,c),ds;
521//   fetchall(R);       //map from R to S: x->a, y->b, z->c;
522//   names(S);
523//   j; print(M);
524//   fetchall(S,"1");   //identity map of S: copy objects, change names
525//   names(S);
526//   kill R,S;
527}
528///////////////////////////////////////////////////////////////////////////////
529
530proc imapall (R, list #)
531"USAGE:   imapall(R[,s]);  R=ring/qring, s=string
532CREATE:  map all objects of ring R (of type poly/ideal/vector/module/number/
533         matrix) into the basering, by applying imap to all objects of R.
534         If no 3rd argument is present, the names are the same as in R. If,
535         say, f is a poly in R and the 3rd argument is the string \"R\", then f
536         is maped to f_R etc.
537RETURN:  no return value
538NOTE:    As imap, this procedure maps the variables of R to the variables with
539         the same name in the basering, the other variables are maped to 0.
540         The 3rd argument is useful in order to avoid conflicts of names, the
541         empty string is allowed
542CAUTION: imapall does not work inside a procedure
543         It does not work if R contains a map
544EXAMPLE: example imapall; shows an example
545"
546{
547   list @L@=names(R);
548   int @ii@; string @s@;
549   if( size(#) > 0 ) { @s@=@s@+"_"+#[1]; }
550   for( @ii@=size(@L@); @ii@>0; @ii@=@ii@-1 )
551   {
552         execute("def "+@L@[@ii@]+@s@+"=imap(R,`@L@[@ii@]`);");
553         execute("export "+@L@[@ii@]+@s@+";");
554   }
555   return();
556}
557example
558{  "EXAMPLE:"; echo = 2;
559// The example is not shown since imapall does not work in a procedure
560// (and hence not in the example procedure). Try the following commands:
561//   ring R=0,(x,y,z,u),dp;
562//   ideal j=x,y,z,u2+ux+z;
563//   matrix M[2][3]=1,2,3,x,y,uz;
564//   j; print(M);
565//   ring S=0,(a,b,c,x,z,y),ds;
566//   imapall(R);         //map from R to S: x->x, y->y, z->z, u->0
567//   names(S);
568//   j; print(M);
569//   imapall(S,"1");     //identity map of S: copy objects, change names
570//   names(S);
571//   kill R,S;
572}
573///////////////////////////////////////////////////////////////////////////////
574
575proc mapall (R, ideal i, list #)
576"USAGE:   mapall(R,i[,s]);  R=ring/qring, i=ideal of basering, s=string
577CREATE:  map all objects of ring R (of type poly/ideal/vector/module/number/
578         matrix, map) into the basering, by mapping the jth variable of R to
579         the jth generator of the ideal i. If no 3rd argument is present, the
580         names are the same as in R. If, say, f is a poly in R and the 3rd
581         argument is the string \"R\", then f is maped to f_R etc.
582RETURN:  no return value.
583NOTE:    This procedure has the same effect as defining a map, say psi, by
584         map psi=R,i; and then applying psi to all objects of R. In particular,
585         maps from R to some ring S are composed with psi, creating thus a map
586         from the basering to S.
587         mapall may be combined with copyring to change vars for all objects.
588         The 3rd argument is useful in order to avoid conflicts of names, the
589         empty string is allowed.
590CAUTION: mapall does not work inside a procedure.
591EXAMPLE: example mapall; shows an example
592"
593{
594   list @L@=names(R); map @psi@ = R,i;
595   int @ii@; string @s@;
596   if( size(#) > 0 ) { @s@=@s@+"_"+#[1]; }
597   for( @ii@=size(@L@); @ii@>0; @ii@=@ii@-1 )
598   {
599      execute("def "+@L@[@ii@]+@s@+"=@psi@(`@L@[@ii@]`);");
600      execute("export "+@L@[@ii@]+@s@+";");
601   }
602   return();
603}
604example
605{  "EXAMPLE:"; echo = 2;
606// The example is not shown since mapall does not work in a procedure
607// (and hence not in the example procedure). Try the following commands:
608//   ring R=0,(x,y,z),dp;
609//   ideal j=x,y,z;
610//   matrix M[2][3]=1,2,3,x,y,z;
611//   map phi=R,x2,y2,z2;
612//   ring S=0,(a,b,c),ds;
613//   ideal i=c,a,b;
614//   mapall(R,i);         //map from R to S: x->c, y->a, z->b
615//   names(S);
616//   j; print(M); phi;    //phi maps R to S: x->c2, y->a2, z->b2
617//   ideal i1=a2,a+b,1;
618//   mapall(R,i1,\"\");   //map from R to S: x->a2, y->a+b, z->1
619//   names(S);
620//   j_; print(M_); phi_;
621//   changevar(\"T\",\"x()\",R);  //change vars in R and call result T
622//   mapall(R,maxideal(1));       //identity map from R to T
623//   names(T);
624//   j; print(M); phi;
625//   kill R,S,T;
626}
627///////////////////////////////////////////////////////////////////////////////
628
629proc ord_test (r)
630"USAGE:   ord_test(r);  r ring
631RETURN:  int 1 (resp. -1, resp. 0) if ordering of r is global (resp. local,
632         resp. mixed)
633EXAMPLE: example ord_test; shows an example
634"
635{
636   if (typeof(r) != "ring")
637   {
638      "// ord_test requires a ring as input";
639      return();
640   }
641   def BAS = basering;
642   setring r;
643   poly f;
644   int n,o,u = nvars(r),1,1;
645   int ii;
646   for ( ii=1; ii<=n; ii++ )
647   {
648      f = 1+var(ii);
649      o = o*(lead(f) == var(ii));
650      u = u*(lead(f) == 1);
651   }
652   setring BAS;
653   if ( o==1 ) { return(1); }
654   if ( u==1 ) { return(-1); }
655   else { return(0); }
656}
657example
658{ "EXAMPLE:"; echo = 2;
659   ring R = 0,(x,y),dp;
660   ring S = 0,(u,v),ls;
661   ord_test(R);
662   ord_test(S);
663   ord_test(R+S);
664}
665///////////////////////////////////////////////////////////////////////////////
666
667proc ringtensor (string s, list #)
668"USAGE:   ringtensor(s,r1,r2,...); s=string, r1,r2,...=rings
669CREATE:  A new base ring with name `s` if r1,r2,... are existing rings.
670         If, say, s = \"R\" and the rings r1,r2,... exist, the new ring will
671         have name R, variables from all rings r1,r2,... and as monomial
672         ordering the block (product) ordering of r1,r2,... . Hence, R
673         is the tensor product of the rings r1,r2,... with ordering matrix
674         equal to the direct sum of the ordering matrices of r1,r2,...
675RETURN:  no return value
676NOTE:    The characteristic of the new ring will be that of r1. The names of
677         variables in the rings r1,r2,... should differ (if a name, say x,
678         occurs in r1 and r2, then, in the new ring r, x always refers to the
679         variable with name x in r1, there is no access to x in r2).
680         The procedure works also for quotient rings ri, if the characteristic
681         of ri is compatible with the characteristic of r1 (i.e. if imap from
682         ri to r1 is implemented)
683         This proc uses 'execute' or calls a procedure using 'execute'.
684         If you use it in your own proc, let the local names of your proc
685         start with @ (see the file HelpForProc)
686EXAMPLE: example ringtensor; shows an example
687"
688{
689   int @ii,@q;
690   int @n = size(#);
691   string @vars,@order,@oi,@s1;
692//---- gather variables, orderings and ideals (of qrings) from given rings ----
693   for(@ii=1; @ii<=@n; @ii=@ii+1)
694   {
695      if( ordstr(#[@ii])[1]=="C" or ordstr(#[@ii])[1]=="c" )
696           { @oi=ordstr(#[@ii])[3,size(ordstr(#[@ii]))-2]; }
697      else { @oi=ordstr(#[@ii])[1,size(ordstr(#[@ii]))-2]; }
698      @vars = @vars+varstr(#[@ii])+",";
699      @order= @order+@oi+",";
700      def @r(@ii)=#[@ii];
701      setring @r(@ii);
702      ideal i(@ii)=ideal(@r(@ii));
703      int @q(@ii)=size(i(@ii));
704      @q=@q+@q(@ii);
705   }
706   if( @q!=0 ) { @s1 = "@newr"; }   // @q=0 iff none of the rings ri is a qring
707   else {  @s1 = s; }
708//------------------------------- create new ring -----------------------------
709   string @newring ="=("+charstr(#[1])+"),("+@vars[1,size(@vars)-1]+"),("
710                  +@order[1,size(@order)-1]+");";
711   execute("ring "+@s1+@newring);
712//------ create ideal for new ring if one of the given rings is a qring -------
713   if( @q!=0 )
714   {
715      ideal i;
716      for(@ii=1; @ii<=@n; @ii=@ii+1)
717      {
718         if( @q(@ii)!=0 )
719         {
720            i=i+imap(@r(@ii),i(@ii));
721         }
722      }
723      i=std(i);
724      execute("qring "+s+"=i;");
725   }
726//----------------------- export and keep created ring ------------------------
727   export(`s`);
728   keepring(`s`);
729   if (voice==2) { "// basering is now",s; }
730   return();
731}
732example
733{ "EXAMPLE:"; echo = 2;
734   ring r=32003,(x,y,u,v),dp;
735   ring s=0,(a,b,c),wp(1,2,3);
736   ring t=0,x(1..5),(c,ls);
737   ringtensor("R",r,s,t);
738   type R;
739   setring s;
740   ideal i = a2+b3+c5;
741   changevar("S","x,y,z");       //change vars of sand make S the basering
742   qring qS =std(fetch(s,i));    //create qring of S mod i (maped to S)
743   changevar("T","d,e,f,g,h",t); //change vars of t and make T the basering
744   qring qT=std(d2+e2-f3);       //create qring of T mod d2+e2-f3
745   ringtensor("Q",s,qS,t,qT);
746   type Q;
747   kill R,Q,S,T;
748   if(system("with","Namespaces")) {
749      if( nameof(Current) == "Ring" ) {
750        kill Top::R,Top::Q,Top::S,Top::T;
751      } else {
752        kill Ring::R,Ring::Q,Ring::S,Ring::T;
753      }
754   }
755}
756///////////////////////////////////////////////////////////////////////////////
757
758proc ringweights (list # )
759"USAGE:   ringweights(P); P=name of an existing ring (true name, not a string)
760RETURN:  intvec consisting of the weights of the variables of P, as they
761         appear when typing P;.
762NOTE:    This is useful when enlarging P but keeping the weights of the old
763         variables.
764EXAMPLE: example ringweights; shows an example
765"
766{
767   int ii,q,fi,fo,fia;
768   intvec rw,nw;
769   string os;
770   def P = #[1];
771   string osP = ordstr(P);
772   fo  = 1;
773//------------------------- find weights in ordstr(P) -------------------------
774   fi  = find(osP,"(",fo);
775   fia = find(osP,"a",fo)+find(osP,"w",fo)+find(osP,"W",fo);
776   while ( fia )
777   {
778      os = osP[fi+1,find(osP,")",fi)-fi-1];
779      if( find(os,",") )
780      {
781         execute("nw = "+os+";");
782         if( size(nw) > ii )
783         {
784             rw = rw,nw[ii+1..size(nw)];
785         }
786         else  {  ii = ii - size(nw); }
787
788         if( find(osP[1,fi],"a",fo) ) { ii = size(nw); }
789      }
790      else
791      {
792         execute("q = "+os+";");
793         if( q > ii )
794         {
795            nw = 0; nw[q-ii] = 0;
796            nw = nw + 1;          //creates an intvec 1,...,1 of length q-ii
797            rw = rw,nw;
798         }
799         else { ii = ii - q; }
800      }
801      fo  = fi+1;
802      fi  = find(osP,"(",fo);
803      fia = find(osP,"a",fo)+find(osP,"w",fo)+find(osP,"W",fo);
804   }
805//-------------- adjust weight vector to length = nvars(P)  -------------------
806   if( fo > 1 )
807   {                                            // case when weights were found
808      rw = rw[2..size(rw)];
809      if( size(rw) > nvars(P) )
810      {
811         rw = rw[1..nvars(P)];
812      }
813      if( size(rw) < nvars(P) )
814      {
815         nw=0; nw[nvars(P)-size(rw)]=0; nw=nw+1; rw=rw,nw;
816      }
817   }
818   else
819   {                                         // case when no weights were found
820      rw[nvars(P)]= 0; rw=rw+1;
821   }
822   return(rw);
823}
824example
825{"EXAMPLE:";  echo = 2;
826  ring r0 = 0,(x,y,z),dp;
827  ringweights(r0);
828  ring r1 = 0,x(1..5),(ds(3),wp(2,3));
829  ringweights(r1);"";
830  // an example for enlarging the ring, keeping the first weights:
831  intvec v = ringweights(r1),6,2,3,4,5;
832  ring R = 0,x(1..10),(a(v),dp);
833  ordstr(R);
834}
835///////////////////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.