source: git/Singular/LIB/ring.lib @ d85510

spielwiese
Last change on this file since d85510 was d85510, checked in by Jakob Kröker <kroeker@…>, 10 years ago
typo fix
  • Property mode set to 100644
File size: 32.6 KB
Line 
1/////////////////////////////////////////////////////////////////////////////
2version="version ring.lib 4.0.0.0 Jun_2013 "; // $Id$
3category="General purpose";
4info="
5LIBRARY:  ring.lib      Manipulating Rings and Maps
6
7PROCEDURES:
8 changechar(c[,r]); make a copy of basering [ring r] with new char c
9 changeord(o[,r]);  make a copy of basering [ring r] with new ord o
10 changevar(v[,r]);  make a copy of basering [ring r] with new vars v
11 defring(\"R\",c,n,v,o);  define a ring R in specified char c, n vars v, ord o
12 defrings(n[,p]);         define ring Sn in n vars, char 32003 [p], ord ds
13 defringp(n[,p]);         define ring Pn in n vars, char 32003 [p], ord dp
14 extendring(\"R\",n,v,o); extend given ring by n vars v, ord o and name it R
15 fetchall(R[,str]);       fetch all objects of ring R to basering
16 imapall(R[,str]);        imap all objects of ring R to basering
17 mapall(R,i[,str]);       map all objects of ring R via ideal i to basering
18 ord_test(R);             test wether ordering of R is global, local or mixed
19 ringtensor(s,t,..);      create ring, tensor product of rings s,t,...
20 ringweights(r);          intvec of weights of ring variables of ring r
21 preimageLoc(R,phi,Q)     computes preimage for non-global orderings
22 rootofUnity(n);          the minimal polynomial for the n-th primitive root of unity
23             (parameters in square brackets [] are optional)
24 optionIsSet(opt)         check if as a string given option is set or not.
25 hasFieldCoefficient      check if the coefficient ring is considered a field
26 hasNumericCoeffs(rng)    check for use of floating point numbers
27 hasCommutativeVars(rng)  non-commutive or commnuative polynomial ring
28 hasGlobalOrdering(rng)   global versus mixed/local monomial ordering
29 hasMixedOrdering()       mixed versus global/local ordering
30 isSubModule(I,J)         check if I is in J as submodule
31";
32
33LIB "inout.lib";
34LIB "general.lib";
35LIB "primdec.lib";
36
37///////////////////////////////////////////////////////////////////////////////
38proc optionIsSet(string optionName)
39"
40USAGE:       optionIsSet( optionName )
41PARAMETERS:  optionName: a name as string of an option of interest
42RETURN:      true, if the by optionName given option is active, false otherwise.
43EXAMPLE:     example optionIsSet;
44"
45{
46   def op = option(get);
47   //sanity check, if option is valid. will raise an error if not
48   option(optionName);    option("no" + optionName);
49   option(set,op);
50   // first entry is currently a comment "//options:", which is not an option.
51   int pos = find(option(), optionName, 11 );
52   return(pos>0);
53}
54example
55{
56    // check if the option "warn" is set.
57    optionIsSet("warn");
58    option("warn");
59    // now the option is set
60    optionIsSet("warn");
61
62    option("nowarn");
63    // now the option is unset
64    optionIsSet("warn");
65}
66
67
68static proc testOptionIsSet()
69{
70     option("warn");
71     ASSUME(0, optionIsSet("warn") );
72     option("nowarn");
73     ASSUME(0, 0 == optionIsSet("warn") );
74}
75
76///////////////////////////////////////////////////////////////////////////////
77
78proc changechar (list @L, list #)
79"USAGE:   changechar(c[,r]);  c=list, r=ring
80RETURN:  ring R, obtained from the ring r [default: r=basering], by changing
81         ringlist(r)[1] to c.
82EXAMPLE: example changechar; shows an example
83"
84{
85   def save_ring=basering;
86   if( size(#)==0 ) { def @r=basering; }
87   if(( size(#)==1 ) and (typeof(#[1])=="ring")) { def @r=#[1]; }
88   setring @r;
89   list rl=ringlist(@r);
90   if(defined(@L)!=voice) { def @L=fetch(save_ring,@L); }
91   if (size(@L)==1) { rl[1]=@L[1];} else { rl[1]=@L;}
92   def Rnew=ring(rl);
93   setring save_ring;
94   return(Rnew);
95}
96example
97{  "EXAMPLE:"; echo = 2;
98   ring rr=2,A,dp;
99   ring r=0,(x,y,u,v),(dp(2),ds);
100   def R=changechar(ringlist(rr)); R;"";
101   def R1=changechar(32003,R); setring R1; R1;
102   kill R,R1;
103}
104///////////////////////////////////////////////////////////////////////////////
105
106proc changeord (list @o, list #)
107"USAGE:   changeord(neword[,r]);  newordstr=list, r=ring/qring
108RETURN:  ring R, obtained from the ring r [default: r=basering], by changing
109         order(r) to neword.
110         If, say, neword=list(list(\"wp\",intvec(2,3)),list(list(\"dp\",1:(n-2))));
111         and if the ring r exists and has n variables, the ring R will be
112         equipped with the monomial ordering wp(2,3),dp.
113EXAMPLE: example changeord; shows an example
114"
115{
116   def save_ring=basering;
117   if( size(#)==0 ) { def @r=basering; }
118   if( size(#)==1 ) { def @r=#[1]; }
119   setring @r;
120   list rl=ringlist(@r);
121   rl[3]=@o;
122   def Rnew=ring(rl);
123   setring save_ring;
124   return(Rnew);
125}
126example
127{  "EXAMPLE:"; echo = 2;
128   ring r=0,(x,y,u,v),(dp(2),ds);
129   def R=changeord(list(list("wp",intvec(2,3)),list("dp",1:2))); R; "";
130   ideal i = x^2,y^2-u^3,v;
131   qring Q = std(i);
132   def Q'=changeord(list(list("lp",nvars(Q))),Q); setring Q'; Q';
133   kill R,Q,Q';
134}
135///////////////////////////////////////////////////////////////////////////////
136
137proc changevar (string vars, list #)
138"USAGE:   changevar(vars[,r]);  vars=string, r=ring/qring
139RETURN:  ring R, obtained from the ring r [default: r=basering], by changing
140         varstr(r) according to the value of vars.
141         If, say, vars = \"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.
145NOTE:    This procedure is useful in connection with the procedure ringtensor,
146         when a conflict between variable names must be avoided.
147         This proc uses 'execute' or calls a procedure using 'execute'.
148EXAMPLE: example changevar; shows an example
149"
150{
151   if( size(#)==0 ) { def @r=basering; }
152   if( size(#)==1 ) { def @r=#[1]; }
153   setring @r;
154   ideal i = ideal(@r); int @q = size(i);
155   if( @q!=0 )
156      { string @s = "Rnew1"; }
157   else
158      { string @s = "Rnew"; }
159   string @newring = @s+"=("+charstr(@r)+"),(";
160   if( vars[size(vars)-1]=="(" and vars[size(vars)]==")" )
161   {
162      @newring = @newring+vars[1,size(vars)-2]+"(1.."+string(nvars(@r))+")";
163   }
164   else { @newring = @newring+vars; }
165   @newring = @newring+"),("+ordstr(@r)+");";
166   execute("ring "+@newring);
167   if( @q!=0 )
168   {
169      map phi = @r,maxideal(1);
170      ideal i = phi(i);
171      attrib(i,"isSB",1);         //*** attrib funktioniert ?
172      qring Rnew=i;
173   }
174   return(Rnew);
175}
176example
177{  "EXAMPLE:"; echo = 2;
178   ring r=0,(x,y,u,v),(dp(2),ds);
179   ideal i = x^2,y^2-u^3,v;
180   qring Q = std(i);
181   setring(r);
182   def R=changevar("A()"); R; "";
183   def Q'=changevar("a,b,c,d",Q); setring Q'; Q';
184   kill R,Q,Q';
185}
186///////////////////////////////////////////////////////////////////////////////
187
188proc defring (string s2, int n, string s3, string s4)
189"USAGE:   defring(ch,n,va,or);  ch,va,or=strings, n=integer
190RETURN:  ring R with characteristic 'ch', ordering 'or' and n variables with
191         names derived from va.
192         If va is a single letter, say va=\"a\", and if n<=26 then a and the
193         following n-1 letters from the alphabet (cyclic order) are taken as
194         variables. If n>26 or if va is a single letter followed by a bracket,
195         say va=\"T(\", the variables are T(1),...,T(n).
196NOTE:    This proc is useful for defining a ring in a procedure.
197         This proc uses 'execute' or calls a procedure using 'execute'.
198EXAMPLE: example defring; shows an example
199"
200{
201   string @newring = "ring newring =("+s2+"),(";
202   if( n>26 or s3[2]=="(" ) { string @v = s3[1]+"(1.."+string(n)+")"; }
203   else { string @v = A_Z(s3,n); }
204   @newring=@newring+@v+"),("+s4+");";
205   execute(@newring);
206   return(newring);
207}
208example
209{ "EXAMPLE:"; echo = 2;
210   def r=defring("0",5,"u","ls"); r; setring r;"";
211   def R=defring("2,A",10,"x(","dp(3),ws(1,2,3),ds"); R; setring R;
212   kill R,r;
213}
214///////////////////////////////////////////////////////////////////////////////
215
216proc defrings (int n, list #)
217"USAGE:   defrings(n,[p]);  n,p integers
218RETURN:  ring R with characteristic p [default: p=32003], ordering ds and n
219         variables x,y,z,a,b,...if n<=26 (resp. x(1..n) if n>26)
220NOTE:    This proc uses 'execute' or calls a procedure using 'execute'.
221EXAMPLE: example defrings; shows an example
222"
223{
224   int p;
225   if (size(#)==0) { p=32003; }
226   else { p=#[1]; }
227   if (n >26)
228   {
229      string s="ring S ="+string(p)+",x(1.."+string(n)+"),ds;";
230   }
231   else
232   {
233      string s="ring S ="+string(p)+",("+A_Z("x",n)+"),ds;";
234   }
235   execute(s);
236   dbprint(printlevel-voice+2,"
237// 'defrings' created a ring. To see the ring, type (if the name R was
238// assigned to the return value):
239    show R;
240// To make the ring the active basering, type
241    setring R; ");
242   return(S);
243}
244example
245{ "EXAMPLE:"; echo = 2;
246   def S5=defrings(5,0); S5; "";
247   def S30=defrings(30); S30;
248   kill S5,S30;
249}
250///////////////////////////////////////////////////////////////////////////////
251
252proc defringp (int n,list #)
253"USAGE:   defringp(n,[p]);  n,p=integers
254RETURN:  ring R with characteristic p [default: p=32003], ordering dp and n
255         variables x,y,z,a,b,...if n<=26 (resp. x(1..n) if n>26)
256NOTE:    This proc uses 'execute' or calls a procedure using 'execute'.
257EXAMPLE: example defringp; shows an example
258"
259{
260   int p;
261   if (size(#)==0) { p=32003; }
262   else { p=#[1]; }
263   if (n >26)
264   {
265      string s="ring P="+string(p)+",x(1.."+string(n)+"),dp;";
266   }
267   else
268   {
269     string s="ring P="+string(p)+",("+A_Z("x",n)+"),dp;";
270   }
271   execute(s);
272   dbprint(printlevel-voice+2,"
273// 'defringp' created a ring. To see the ring, type (if the name R was
274// assigned to the return value):
275    show R;
276// To make the ring the active basering, type
277    setring R; ");
278   return(P);
279}
280example
281{ "EXAMPLE:"; echo = 2;
282   def P5=defringp(5,0); P5; "";
283   def P30=defringp(30); P30;
284   kill P5,P30;
285}
286///////////////////////////////////////////////////////////////////////////////
287
288proc extendring (int n, string va, string o, list #)
289"USAGE:   extendring(n,va,o[,iv,i,r]);  va,o=strings, n,i=integers, r=ring,
290          iv=intvec of positive integers or iv=0
291RETURN:  ring R, which extends the ring r by adding n new variables in front
292         of (resp. after, if i!=0) the old variables.
293         [default: (i,r)=(0,basering)].
294@*       -- The characteristic is the characteristic of r.
295@*       -- The new vars are derived from va. If va is a single letter, say
296            va=\"T\", and if n<=26 then T and the following n-1 letters from
297            T..Z..T (resp. T(1..n) if n>26) are taken as additional variables.
298            If va is a single letter followed by a bracket, say va=\"x(\",
299            the new variables are x(1),...,x(n).
300@*       -- The ordering is the product ordering of the ordering of r and of an
301            ordering derived from `o` [and iv].
302@*        -  If o contains a 'c' or a 'C' in front resp. at the end, this is
303            taken for the whole ordering in front, resp. at the end. If o does
304            not contain a 'c' or a 'C' the same rule applies to ordstr(r).
305@*        -  If no intvec iv is given, or if iv=0, o may be any allowed ordstr,
306            like \"ds\" or \"dp(2),wp(1,2,3),Ds(2)\" or \"ds(a),dp(b),ls\" if
307            a and b are globally (!) defined integers and if a+b+1<=n.
308            If, however, a and b are local to a proc calling extendring, the
309            intvec iv must be used to let extendring know the values of a and b
310@*        -  If a non-zero intvec iv is given, iv[1],iv[2],... are taken for the
311            1st, 2nd,... block of o, if o contains no substring \"w\" or \"W\"
312            i.e. no weighted ordering (in the above case o=\"ds,dp,ls\"
313            and iv=a,b).
314            If o contains a weighted ordering (only one (!) weighted block is
315            allowed) iv[1] is taken as size for the weight-vector, the next
316            iv[1] values of iv are taken as weights and the remaining values of
317            iv as block size for the remaining non-weighted blocks.
318            e.g. o=\"dp,ws,Dp,ds\", iv=3,2,3,4,2,5 creates the ordering
319            dp(2),ws(2,3,4),Dp(5),ds
320NOTE:    This proc is useful for adding deformation parameters.
321         This proc uses 'execute' or calls a procedure using 'execute'.
322         If you use it in your own proc, it may be advisable to let the local
323         names of your proc start with a @
324EXAMPLE: example extendring; shows an example
325"
326{
327//--------------- initialization and place c/C of ordering properly -----------
328   string @o1,@o2,@ro,@wstr,@v,@newring;
329   int @i,@w,@ii,@k;
330   intvec @iv,@iw;
331   if( find(o,"c")+find(o,"C") != 0)
332   {
333      @k=1;
334      if( o[1]=="c" or o[1]=="C" ) { @o1=o[1,2]; o=o[3..size(o)]; }
335      else                         { @o2=o[size(o)-1,2]; o=o[1..size(o)-2]; }
336   }
337   if( size(#)==0 ) { #[1]=0; }
338   if( typeof(#[1])!="intvec" )
339   {
340     if( size(#)==1 ) { @i=#[1]; def @r=basering; }
341     if( size(#)==2 ) { @i=#[1]; def @r=#[2]; }
342     if( o[size(o)]!=")" and find(o,",")==0 ) { o=o+"("+string(n)+")"; }
343   }
344   else
345   {
346     @iv=#[1];
347     if( size(#)==2 ) { @i=#[2]; def @r=basering; }
348     if( size(#)==3 ) { @i=#[2]; def @r=#[3]; }
349     if( @iv==0 && o[size(o)]!=")" && find(o,",")==0 ) {o=o+"("+string(n)+")";}
350   }
351   @ro=ordstr(@r);
352   if( @ro[1]=="c" or @ro[1]=="C" )
353      { @v=@ro[1,2]; @ro=@ro[3..size(@ro)]; }
354   else
355      { @wstr=@ro[size(@ro)-1,2]; @ro=@ro[1..size(@ro)-2]; }
356   if( @k==0) { @o1=@v; @o2=@wstr; }
357//----------------- prepare ordering if an intvec is given --------------------
358   if( typeof(#[1])=="intvec" and #[1]!=0 )
359   {
360      @k=n;                             //@k counts no of vars not yet ordered
361      @w=find(o,"w")+find(o,"W");o=o+" ";
362      if( @w!=0 )
363      {
364         @wstr=o[@w..@w+1];
365         o=o[1,@w-1]+"@"+o[@w+2,size(o)];
366         @iw=@iv[2..@iv[1]+1];
367         @wstr=@wstr+"("+string(@iw)+")";
368         @k=@k-@iv[1];
369         @iv=@iv[@iv[1]+2..size(@iv)];
370         @w=0;
371      }
372      for( @ii=1; @ii<=size(@iv); @ii=@ii+1 )
373      {
374         if( find(o,",",@w+1)!=0 )
375         {
376            @w=find(o,",",@w+1);
377            if( o[@w-1]!="@" )
378            {
379               o=o[1,@w-1]+"("+string(@iv[@ii])+")"+o[@w,size(o)];
380               @w=find(o,",",@w+1);
381               @k=@k-@iv[@ii];
382            }
383            else { @ii=@ii-1; }
384         }
385      }
386      @w=find(o,"@");
387      if( @w!=0 ) { o=o[1,@w-1] + @wstr + o[@w+1,size(o)]; }
388      if( @k>0 and o[size(o)]!=")" ) { o=o+"("+string(@k)+")"; }
389   }
390//------------------------ prepare string of new ring -------------------------
391   @newring = "ring na =("+charstr(@r)+"),(";
392   if( n>26 or va[2]=="(" ) { @v = va[1]+"(1.."+string(n)+")"; }
393   else                     { @v = A_Z(va,n); }
394   if( @i==0 )
395   {
396      @v=@v+","+varstr(@r);
397      o=@o1+o+","+@ro+@o2;
398   }
399   else
400   {
401      @v=varstr(@r)+","+@v;
402      o=@o1+@ro+","+o+@o2;
403   }
404   @newring=@newring+@v+"),("+o+");";
405//---------------------------- execute and export -----------------------------
406   execute(@newring);
407   dbprint(printlevel-voice+2,"
408// 'extendring' created a new ring.
409// To see the ring, type (if the name 'R' was assigned to the return value):
410     show(R);
411");
412
413   return(na);
414}
415example
416{ "EXAMPLE:"; echo = 2;
417   ring r=0,(x,y,z),ds;
418   show(r);"";
419   // blocksize is derived from no of vars:
420   int t=5;
421   def R1=extendring(t,"a","dp");         //t global: "dp" -> "dp(5)"
422   show(R1); setring R1; "";
423   def R2=extendring(4,"T(","c,dp",1,r);    //"dp" -> "c,..,dp(4)"
424   show(R2); setring R2; "";
425
426   // no intvec given, blocksize given: given blocksize is used:
427   def R3=extendring(4,"T(","dp(2)",0,r);   // "dp(2)" -> "dp(2)"
428   show(R3); setring R3; "";
429
430   // intvec given: weights and blocksize is derived from given intvec
431   // (no specification of a blocksize in the given ordstr is allowed!)
432   // if intvec does not cover all given blocks, the last block is used
433   // for the remaining variables, if intvec has too many components,
434   // the last ones are ignored
435   intvec v=3,2,3,4,1,3;
436   def R4=extendring(10,"A","ds,ws,Dp,dp",v,0,r);
437   // v covers 3 blocks: v[1] (=3) : no of components of ws
438   // next v[1] values (=v[2..4]) give weights
439   // remaining components of v are used for the remaining blocks
440   show(R4);
441   kill r,R1,R2,R3,R4;
442}
443///////////////////////////////////////////////////////////////////////////////
444
445proc fetchall (def R, list #)
446"USAGE:   fetchall(R[,s]);  R=ring/qring, s=string
447CREATE:  fetch all objects of ring R (of type poly/ideal/vector/module/number/matrix)
448         into the basering.
449         If no 2nd argument is present, the names are the same as in R. If,
450         say, f is a polynomial in R and the 2nd argument is the string \"R\", then f
451         is mapped to f_R etc.
452RETURN:  no return value
453NOTE:    As fetch, this procedure maps the 1st, 2nd, ... variable of R to the
454         1st, 2nd, ... variable of the basering.
455         The 2nd argument is useful in order to avoid conflicts of names, the
456         empty string is allowed
457CAUTION: fetchall does not work for locally defined names.
458         It does not work if R contains a map.
459SEE ALSO: imapall
460EXAMPLE: example fetchall; shows an example
461"
462{
463   list @L@=names(R);
464   int @ii@; string @s@;
465   if( size(#) > 0 ) { @s@=@s@+"_"+#[1]; }
466   for( @ii@=size(@L@); @ii@>0; @ii@-- )
467   {
468      execute("def "+@L@[@ii@]+@s@+"=fetch(R,`@L@[@ii@]`);");
469      execute("export "+@L@[@ii@]+@s@+";");
470   }
471   return();
472}
473example
474{  "EXAMPLE:"; echo=2;
475// The example is not shown since fetchall does not work in a procedure;
476// (and hence not in the example procedure). Try the following commands:
477//   ring R=0,(x,y,z),dp;
478//   ideal j=x,y2,z2;
479//   matrix M[2][3]=1,2,3,x,y,z;
480//   j; print(M);
481//   ring S=0,(a,b,c),ds;
482//   fetchall(R);       //map from R to S: x->a, y->b, z->c;
483//   names(S);
484//   j; print(M);
485//   fetchall(S,"1");   //identity map of S: copy objects, change names
486//   names(S);
487//   kill R,S;
488}
489///////////////////////////////////////////////////////////////////////////////
490
491proc imapall (def R, list #)
492"USAGE:   imapall(R[,s]);  R=ring/qring, s=string
493CREATE:  map all objects of ring R (of type poly/ideal/vector/module/number/matrix)
494         into the basering by applying imap to all objects of R.
495         If no 2nd argument is present, the names are the same as in R. If,
496         say, f is a polynomial in R and the 3rd argument is the string \"R\", then f
497         is mapped to f_R etc.
498RETURN:  no return value
499NOTE:    As imap, this procedure maps the variables of R to the variables with
500         the same name in the basering, the other variables are mapped to 0.
501         The 2nd argument is useful in order to avoid conflicts of names, the
502         empty string is allowed
503CAUTION: imapall does not work for locally defined names.
504         It does not work if R contains a map
505SEE ALSO: fetchall
506EXAMPLE: example imapall; shows an example
507"
508{
509   list @L@=names(R);
510   int @ii@; string @s@;
511   if( size(#) > 0 ) { @s@=@s@+"_"+#[1]; }
512   for( @ii@=size(@L@); @ii@>0; @ii@-- )
513   {
514         execute("def "+@L@[@ii@]+@s@+"=imap(R,`@L@[@ii@]`);");
515         execute("export "+@L@[@ii@]+@s@+";");
516   }
517   return();
518}
519example
520{  "EXAMPLE:"; echo = 2;
521// The example is not shown since imapall does not work in a procedure
522// (and hence not in the example procedure). Try the following commands:
523//   ring R=0,(x,y,z,u),dp;
524//   ideal j=x,y,z,u2+ux+z;
525//   matrix M[2][3]=1,2,3,x,y,uz;
526//   j; print(M);
527//   ring S=0,(a,b,c,x,z,y),ds;
528//   imapall(R);         //map from R to S: x->x, y->y, z->z, u->0
529//   names(S);
530//   j; print(M);
531//   imapall(S,"1");     //identity map of S: copy objects, change names
532//   names(S);
533//   kill R,S;
534}
535///////////////////////////////////////////////////////////////////////////////
536
537proc mapall (def R, ideal i, list #)
538"USAGE:   mapall(R,i[,s]);  R=ring/qring, i=ideal of basering, s=string
539CREATE:  map all objects of ring R (of type poly/ideal/vector/module/number/
540         matrix, map) into the basering by mapping the j-th variable of R to
541         the j-th generator of the ideal i. If no 3rd argument is present, the
542         names are the same as in R. If, say, f is a polynomial in R and the 3rd
543         argument is the string \"R\", then f is mapped to f_R etc.
544RETURN:  no return value.
545NOTE:    This procedure has the same effect as defining a map, say psi, by
546         map psi=R,i; and then applying psi to all objects of R. In particular,
547         maps from R to some ring S are composed with psi, creating thus a map
548         from the basering to S.
549         mapall may be combined with copyring to change vars for all objects.
550         The 3rd argument is useful in order to avoid conflicts of names, the
551         empty string is allowed.
552CAUTION: mapall does not work for locally defined names.
553EXAMPLE: example mapall; shows an example
554"
555{
556   list @L@=names(R); map @psi@ = R,i;
557   int @ii@; string @s@;
558   if( size(#) > 0 ) { @s@=@s@+"_"+#[1]; }
559   for( @ii@=size(@L@); @ii@>0; @ii@-- )
560   {
561      execute("def "+@L@[@ii@]+@s@+"=@psi@(`@L@[@ii@]`);");
562      execute("export "+@L@[@ii@]+@s@+";");
563   }
564   return();
565}
566example
567{  "EXAMPLE:"; echo = 2;
568// The example is not shown since mapall does not work in a procedure
569// (and hence not in the example procedure). Try the following commands:
570//   ring R=0,(x,y,z),dp;
571//   ideal j=x,y,z;
572//   matrix M[2][3]=1,2,3,x,y,z;
573//   map phi=R,x2,y2,z2;
574//   ring S=0,(a,b,c),ds;
575//   ideal i=c,a,b;
576//   mapall(R,i);         //map from R to S: x->c, y->a, z->b
577//   names(S);
578//   j; print(M); phi;    //phi maps R to S: x->c2, y->a2, z->b2
579//   ideal i1=a2,a+b,1;
580//   mapall(R,i1,\"\");     //map from R to S: x->a2, y->a+b, z->1
581//   names(S);
582//   j_; print(M_); phi_;
583//   changevar(\"T\",\"x()\",R);  //change vars in R and call result T
584//   mapall(R,maxideal(1));   //identity map from R to T
585//   names(T);
586//   j; print(M); phi;
587//   kill R,S,T;
588}
589///////////////////////////////////////////////////////////////////////////////
590
591proc ord_test (def r)
592"USAGE:   ord_test(r);  r ring/qring
593RETURN:  int 1 (resp. -1, resp. 0) if ordering of r is global (resp. local,
594         resp. mixed)
595SEE ALSO: attrib
596EXAMPLE: example ord_test; shows an example
597"
598{
599   if ((typeof(r) != "ring") and (typeof(r) != "qring"))
600   {
601      ERROR("ord_test requires a ring/qring as input");
602   }
603   if (attrib(r,"global")==1) { return(1);}
604   def BAS = basering;
605   setring r;
606   poly f;
607   int n,o,u = nvars(r),1,1;
608   int ii;
609   for ( ii=1; ii<=n; ii++ )
610   {
611      f = 1+var(ii);
612      o = o*(lead(f) == var(ii));
613      u = u*(lead(f) == 1);
614   }
615   setring BAS;
616   if ( o==1 ) { return(1); }
617   if ( u==1 ) { return(-1); }
618   else { return(0); }
619}
620example
621{ "EXAMPLE:"; echo = 2;
622   ring R = 0,(x,y),dp;
623   ring S = 0,(u,v),ls;
624   ord_test(R);
625   ord_test(S);
626   ord_test(R+S);
627}
628///////////////////////////////////////////////////////////////////////////////
629
630proc ringtensor (list #)
631"USAGE:   ringtensor(r1,r2,...);  r1,r2,...=rings
632RETURN:  ring R whose variables are the variables from all rings r1,r2,...
633         and whose monomial ordering is the block (product) ordering of the
634         respective monomial orderings of r1,r2,... . Hence, R
635         is the tensor product of the rings r1,r2,... with ordering matrix
636         equal to the direct sum of the ordering matrices of r1,r2,...
637NOTE:    The characteristic of the new ring will be p if one ring has
638         characteristic p. The names of variables in the rings r1,r2,...
639         must differ.
640         The procedure works also for quotient rings ri, if the characteristic
641         of ri is compatible with the characteristic of the result
642         (i.e. if imap from ri to the result is implemented)
643SEE ALSO: ring operations
644EXAMPLE: example ringtensor; shows an example
645"
646{
647   int @i;
648   int @n = size(#);
649   if (@n<=1) { ERROR("at least 2 rings required"); }
650   def @s=#[1]+#[2];
651   for (@i=3; @i<=@n;@i++)
652   {
653     def @ss=@s+#[@i];
654     kill @s;
655     def @s=@ss;
656     kill @ss;
657   }
658   dbprint(printlevel-voice+2,"
659// 'ringtensor' created a ring. To see the ring, type (if the name R was
660// assigned to the return value):
661    show(R);
662// To make the ring the active basering, type
663    setring R; ");
664   return(@s);
665}
666example
667{ "EXAMPLE:"; echo = 2;
668   ring r=32003,(x,y,u,v),dp;
669   ring s=0,(a,b,c),wp(1,2,3);
670   ring t=0,x(1..5),(c,ls);
671   def R=ringtensor(r,s,t);
672   type R;
673   setring s;
674   ideal i = a2+b3+c5;
675   def S=changevar("x,y,z");       //change vars of s
676   setring S;
677   qring qS =std(fetch(s,i));      //create qring of S mod i (mapped to S)
678   def T=changevar("d,e,f,g,h",t); //change vars of t
679   setring T;
680   qring qT=std(d2+e2-f3);         //create qring of T mod d2+e2-f3
681   def Q=ringtensor(s,qS,t,qT);
682   setring Q; type Q;
683   kill R,S,T,Q;
684}
685///////////////////////////////////////////////////////////////////////////////
686
687proc ringweights (def P)
688"USAGE:   ringweights(P); P=name of an existing ring (true name, not a string)
689RETURN:  intvec consisting of the weights of the variables of P, as they
690         appear when typing P;.
691NOTE:    This is useful when enlarging P but keeping the weights of the old
692         variables.
693EXAMPLE: example ringweights; shows an example
694"
695{
696   int i;
697   intvec rw;
698//------------------------- find weights  -------------------------
699   for(i=nvars(P);i>0;i--)
700   { rw[i]=ord(var(i)); }
701   return(rw);
702}
703example
704{"EXAMPLE:";  echo = 2;
705  ring r0 = 0,(x,y,z),dp;
706  ringweights(r0);
707  ring r1 = 0,x(1..5),(ds(3),wp(2,3));
708  ringweights(r1);"";
709  // an example for enlarging the ring, keeping the first weights:
710  intvec v = ringweights(r1),6,2,3,4,5;
711  ring R = 0,x(1..10),(a(v),dp);
712  ordstr(R);
713}
714///////////////////////////////////////////////////////////////////////////////
715proc preimageLoc(string R_name,string phi_name,string Q_name )
716"USAGE: preimageLoc ( ring_name, map_name, ideal_name );
717        all input parameters of type string
718RETURN:  ideal
719PURPOSE: compute the preimage of an ideal under a given map for non-global
720         orderings.
721         The 2nd argument has to be the name of a map from the basering to
722         the given ring (or the name of an ideal defining such a map), and
723         the ideal has to be an ideal in the given ring.
724SEE ALSO: preimage
725KEYWORDS: preimage under a map between local rings, map between local rings, map between local and global rings
726EXAMPLE: example preimageLoc ; shows an example
727"{
728  def S=basering;
729  int i;
730  string newRing,minpoly_string;
731  if(attrib(S,"global")!=1)
732  {
733    if(typeof(S)=="qring")
734    {
735      ideal I=ideal(S);
736      newRing="ring S0=("+charstr(S)+"),("+varstr(S)+"),dp;";
737      minpoly_string=string(minpoly);
738      execute(newRing);
739      execute("minpoly="+minpoly_string+";");
740      ideal I=imap(S,I);
741      list pr=primdecGTZ(I);
742      newRing="ring SL=("+charstr(S)+"),("+varstr(S)+"),("+ordstr(S)+");";
743      execute(newRing);
744      execute("minpoly="+minpoly_string+";");
745      list pr=imap(S0,pr);
746      ideal I0=std(pr[1][1]);
747      for(i=2;i<=size(pr);i++)
748      {
749         I0=intersect(I0,std(pr[i][1]));
750      }
751      setring S0;
752      ideal I0=imap(SL,I0);
753      qring S1=std(I0);
754    }
755    else
756    {
757      def S1=S;
758    }
759  }
760  else
761  {
762    def S1=S;
763  }
764  def @R=`R_name`;
765  setring @R;
766  def @phi=`phi_name`;
767  ideal phiId=ideal(@phi);
768  def Q=`Q_name`;
769  if(attrib(@R,"global")!=1)
770  {
771    if(typeof(@R)=="qring")
772    {
773      ideal J=ideal(@R);
774      newRing="ring R0=("+charstr(@R)+"),("+varstr(@R)+"),dp;";
775      minpoly_string=string(minpoly);
776      execute(newRing);
777      execute("minpoly="+minpoly_string+";");
778      ideal J=imap(@R,J);
779      list pr=primdecGTZ(J);
780      newRing="ring RL=("+charstr(@R)+"),("+varstr(@R)+"),("+ordstr(@R)+");";
781      execute(newRing);
782      execute("minpoly="+minpoly_string+";");
783      list pr=imap(R0,pr);
784      ideal J0=std(pr[1][1]);
785      for(i=2;i<=size(pr);i++)
786      {
787         J0=intersect(J0,std(pr[i][1]));
788      }
789      setring R0;
790      ideal J0=imap(RL,J0);
791      qring R1=std(J0);
792      ideal Q=imap(@R,Q);
793      map @phi=S1,imap(@R,phiId);
794    }
795    else
796    {
797      def R1=@R;
798    }
799  }
800  else
801  {
802    def R1=@R;
803  }
804  setring S1;
805  ideal preQ=preimage(R1,@phi,Q);
806  setring S;
807  ideal prQ=imap(S1,preQ);
808  return(prQ);
809}
810example
811{ "EXAMPLE:"; echo=2;
812  ring S =0,(x,y,z),dp;
813  ring R0=0,(x,y,z),ds;
814  qring R=std(x+x2);
815  map psi=S,x,y,z;
816  ideal null;
817  setring S;
818  ideal nu=preimageLoc("R","psi","null");
819  nu;
820}
821
822//////////////////////////////////////////////////////////////////////////////
823/* moved here from the nctools.lib */
824///////////////////////////////////////////////////////////////////////////////
825proc rootofUnity(int n)
826"USAGE:  rootofUnity(n); n an integer
827RETURN:  number
828PURPOSE: compute the minimal polynomial for the n-th primitive root of unity
829NOTE: works only in field extensions by one element
830EXAMPLE: example rootofUnity; shows examples
831"
832{
833  if ( npars(basering) !=1 )
834  {
835    "the procedure works only with one parameter";
836    return(0);
837  }
838  if (n<1) { return(0); }
839  number mp = par(1);
840  if (n==1) { return(mp-1); }
841  if (n==2) { return(mp+1); }
842  def OldRing = basering;
843  string CH = charstr(basering);
844  string MCH;
845  int j=1;
846  while ( (CH[j] !=",") && (j<=size(CH)))
847  {
848    MCH=MCH+CH[j]; j++;
849  }
850  string SR = "ring @@rR="+MCH+","+parstr(basering)+",dp;";
851  execute(SR);
852  poly @t=var(1)^n-1; // (x^2i-1)=(x^i-1)(x^i+1)
853  list l=factorize(@t);
854  ideal @l=l[1];
855  list @d;
856  int s=size(@l);
857  int d=deg(@l[s]);
858  int cnt=1;
859  poly res;
860  for (j=s-1; j>=1; j--)
861  {
862    if ( deg(@l[j]) > d) { d=deg(@l[j]); }
863  }
864  for (j=1; j<=s; j++)
865  {
866    if ( deg(@l[j]) == d) { @d[cnt]=@l[j]; cnt++; }
867  }
868  if ( size(@d)==1 )
869  {
870    res = poly(@d[1]);
871  }
872  else
873  {
874    j=1;
875    while  ( j <= size(@d) )
876    {
877      res = @d[j]-lead(@d[j]);
878      if ( leadcoef(res) >=0 ) { j++; }
879      else { break; }
880    }
881    res = @d[j];
882  }
883  setring OldRing;
884  poly I = imap(@@rR,res);
885  mp = leadcoef(I);
886  kill @@rR;
887  return(mp);
888}
889example
890{
891  "EXAMPLE:";echo=2;
892  ring r = (0,q),(x,y,z),dp;
893  rootofUnity(6);
894  rootofUnity(7);
895  minpoly = rootofUnity(8);
896  r;
897}
898
899
900
901
902proc isQuotientRing( rng )
903"USAGE: isQuotientRing ( rng );
904RETURN:  1 if rng is a quotient ring, 0 otherwise.
905PURPOSE: check if typeof a rng "qring"
906KEYWORDS: qring ring ideal 'factor ring'
907EXAMPLE: example isQuotientRing ; shows an example
908"
909{
910    return ( size(ideal(rng)) != 0 );
911}
912example
913{
914  ring rng = 0,x,dp;
915  isQuotientRing(rng); //no
916  // if a certain method does not support quotient rings,
917  // then a parameter test could be performed:
918   ASSUME( 0, 0==isQuotientRing(basering));
919
920  qring q= ideal(x);  // constructs rng/ideal(x)
921  isQuotientRing(q);  // yes
922}
923
924static proc testIsQuotientRing()
925{
926   ring rng = real,x,dp;
927   ASSUME(0, 0== isQuotientRing(rng) ) ;
928
929   qring qrng = 1;
930   ASSUME(0, isQuotientRing(qrng) ) ;
931
932   ring rng2 = integer,x,dp;
933   ASSUME(0, 0 == isQuotientRing(rng2) ) ;
934
935   qring qrng2=0;
936   ASSUME(0, not isQuotientRing(qrng2) ) ;
937
938   ring rng3 = 0,x,dp;
939   ASSUME(0, 0 == isQuotientRing(rng3) ) ;
940
941   qring qrng3=1;
942   ASSUME(0, isQuotientRing(qrng3) ) ;
943}
944
945proc hasFieldCoefficient( rng )
946"USAGE: hasFieldCoefficient ( rng );
947RETURN:  1 if the coeffcients form a field, 0 otherwise.
948KEYWORDS: ring coefficients
949EXAMPLE: example hasFieldCoefficient; shows an example
950"
951{
952  return (attrib(rng,"ring_cf")==0);
953}
954example
955{
956  ring rng = integer,x,dp;
957  hasFieldCoefficient(rng); //no
958  // if a certain method supports only rings with integer coefficients,
959  // then a parameter test could be performed:
960  ring rng2 = 0, x, dp;
961  hasFieldCoefficient(rng2);  // yes
962}
963
964proc hasGlobalOrdering (rng)
965"USAGE: hasGlobalOrdering ( rng );
966RETURN:  1 if rng has a global monomial ordering, 0 otherwise.
967KEYWORDS: monomial ordering
968EXAMPLE: example hasGlobalOrdering; shows an example
969"
970{
971  return (attrib(rng,"global")==1);
972}
973example
974{
975  ring rng = integer,x,dp;
976  hasGlobalOrdering(rng); //yes
977  ring rng2 = 0, x, ds;
978  hasGlobalOrdering(rng2);  // no
979}
980
981proc hasCommutativeVars (rng)
982"USAGE: hasCommutativeVars ( rng );
983RETURN:  1 if rng is a commutative polynomial ring, 0 otherwise.
984KEYWORDS: plural
985EXAMPLE: example hasCommutativeVars; shows an example
986"
987{
988  list rl=ringlist(rng);
989  return (size(rl)==4);
990}
991
992proc hasNumericCoeffs(rng)
993"USAGE: hasNumericCoeffs ( rng );
994RETURN:  1 if rng has inexact coeffcients, 0 otherwise.
995KEYWORDS: floating point
996EXAMPLE: example hasNumericCoeffs; shows an example
997"
998{
999  ERROR("not yet implemented");
1000}
1001
1002proc isSubModule(I,J)
1003"USAGE: isSubModule(I,J): I, J: ideal or module
1004RETURN: 1 if module(I) is in module(J), 0 otherwise
1005EXAMPLE: isSubModule; shows an example
1006{
1007  if (attrib(J,"isSB")==1)
1008  { return(size(reduce(I,J,1)==0)); }
1009  else
1010  { return(size(reduce(I,groebner(J),1)==0)); }
1011}
1012
1013proc hasMixedOrdering()
1014"USAGE:  hasMixedOrdering();
1015RETURN:  1 if ordering of basering is mixed, 0 else
1016EXAMPLE: example hasMixedOrdering(); shows an example
1017"
1018{
1019   int i,p,m;
1020   for(i = 1; i <= nvars(basering); i++)
1021   {
1022      if(var(i) > 1)
1023      {
1024         p++;
1025      }
1026      else
1027      {
1028         m++;
1029      }
1030   }
1031   if((p > 0) && (m > 0)) { return(1); }
1032   return(0);
1033}
1034example
1035{ "EXAMPLE:"; echo = 2;
1036   ring R1 = 0,(x,y,z),dp;
1037   hasMixedOrdering();
1038   ring R2 = 31,(x(1..4),y(1..3)),(ds(4),lp(3));
1039   hasMixedOrdering();
1040   ring R3 = 181,x(1..9),(dp(5),lp(4));
1041   hasMixedOrdering();
1042}
1043
Note: See TracBrowser for help on using the repository browser.