source: git/Singular/LIB/fastsolv.lib @ 63be42

spielwiese
Last change on this file since 63be42 was 3939bc, checked in by Hans Schönemann <hannes@…>, 26 years ago
* hannes: changed res->nres, resu->res git-svn-id: file:///usr/local/Singular/svn/trunk@2009 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 9.8 KB
Line 
1//
2version="$Id: fastsolv.lib,v 1.5 1998-05-29 15:01:56 Singular Exp $";
3info="";
4
5// ====================================================================
6//  library fast.lib
7//
8// ====================================================================
9
10proc Tsimplify
11"USAGE:    simplify(id,n);  id ideal, n integer
12RETURNS:  list of two ideals the first containing the simplified
13          elements, the second the variables which already have
14          been eliminated
15ASSUME:   basering has ordering dp, the elimination of the variables
16          n+1,... is not supported
17EXAMPLE:  example simplify; shows an example
18"
19{
20  def bsr=basering;
21  int @n, @m, @k;
22  option (redSB);
23  ideal @i = interred(#[1]);
24  ideal @j, @im, @va;
25  for (@n=1;@n<=size(@i);@n=@n+1)
26  {
27    if (deg(@i[@n])==1)
28    {
29      @k=0;
30      for (@m=#[2]+1;@m<=nvars(basering);@m=@m+1)
31      {
32        if (lead(@i[@n])/var(@m)!=0)
33        {
34          @k=1;
35          break;
36        }
37      }
38      if (@k==0)
39      {
40        @va=@va+lead(@i[@n]);
41        @i[@n]=0;
42      }
43    }
44  }
45  @i=@i+@j;
46  map @phi;
47  for (@m=1;@m<=#[2];@m=@m+1)
48  {
49    for (@n=size(@i);@n>=1;@n=@n-1)
50    {
51      if (deg(@i[@n]/var(@m))==0)
52      {
53        @im = maxideal(1);
54        @im[@m]=(-1/coeffs(@i[@n],var(@m))[2,1])*(@i[@n]-coeffs(@i[@n],var(@m))[2,1]*var(@m));
55        @phi = basering,@im;
56        @i=@phi(@i);
57        @i=@i+@j;
58        @va=@va+var(@m);
59        break;// hier muesste evt. sorgfaeltiger ausgewaehlt werden
60      }
61    }
62  }
63  @i=interred(@i);
64  option(noredSB);
65  return(@i,@va);
66}
67example
68{
69   ring s=181,(x,y,z,t,u,v,w,a,b,c,d,f,e),dp;
70   ideal j=
71            w2+f2-1,
72            x2+t2+a2-1,
73            y2+u2+b2-1,
74            z2+v2+c2-1,
75            d2+e2-1,
76            f4+2u,
77            wa+tf,
78            xy+tu+ab,
79            yz+uv+bc,
80            cd+ze,
81            x+y+z+e+1,
82            t+u+v+f-1,
83            w+a+b+c+d;
84    list l=Tsimplify(j,10);
85    l;
86}
87
88
89proc simplifyInBadRings
90"USAGE:    simplify(id,n);  id ideal, n product of the variables
91          to be eliminated later
92RETURNS:  list of two ideals the first containing the simplified
93          elements, the second the variables which already have
94          been eliminated
95EXAMPLE:  example simplify; shows an example
96"
97{
98  int @i,@j;
99  string @es=",(";
100  string @ns="),dp;";
101  for (@i=1;@i<=nvars(basering);@i=@i+1)
102  {
103    if (deg(#[2]/var(@i))>=0)
104    {
105      @es=@es+varstr(@i)+",";
106      @j=@j+1;
107    }
108    else
109    {
110      @ns=","+varstr(@i)+@ns;
111    }
112  }
113  string @s=@es[1,size(@es)-1]+@ns;
114  @ns=nameof(basering);
115  ideal i=#[1];
116  execute "ring @gnir ="+charstr(basering)+@s;
117  ideal @laedi=imap(`@ns`,i);
118  list @l=Tsimplify(@laedi,@j);
119  ideal @l1, @l2=@l[1], @l[2];
120  execute "setring "+@ns+";";
121  return(imap(@gnir,@l1),imap(@gnir,@l2));
122}
123example
124{
125   ring s=0,(e,f,x,y,z,t,u,v,w,a,b,c,d),dp;
126   ideal j=
127            w2+f2-1,
128            x2+t2+a2-1,
129            y2+u2+b2-1,
130            z2+v2+c2-1,
131            d2+e2-1,
132            f4+2u,
133            wa+tf,
134            xy+tu+ab,
135            yz+uv+bc,
136            cd+ze,
137            x+y+z+e+1,
138            t+u+v+f-1,
139            w+a+b+c+d;
140    list l=simplifyInBadRings(j,xyztuvwabc);
141    l;
142}
143
144
145proc fastElim
146"USAGE:    fastelim(id,v);  id ideal, v Product of variables to
147          be eliminated.
148RETURNS:  list of two ideals the first containing the simplified
149          elements, the second the variables which already have
150          been eliminated
151EXAMPLE:  example fastElim; shows an example.
152"{
153   string @oldgnir = nameof(basering);  // Namen des alten Baserings merken
154   string @varnames;
155   int @i;
156   poly @testpoly=1;      // Variablen, die schon eliminiert sind (nach Simplify)
157   poly @elimvarspoly=#[2]; // Variablen, die noch eliminiert werden sollen
158
159   // das Ideal vereinfachen. Beachte: l[1] erzeugt nicht mehr id!
160   list @l=simplifyInBadRings(#[1],#[2]);
161   ideal @a, @b = @l[1], @l[2];
162
163   // ------------------------------------------------------------
164   // in einen Ring abbilden, der die in (2) angegebenen Variablen
165   // nicht mehr enthaelt.
166   // ------------------------------------------------------------
167
168
169   // Aus dem von simplifyInBadRings zurueckgegebenen Ideal(2), das die
170   // Variablen anthealt, die eliminiert sind, ein Polynom bauen:
171   for (@i=1; @i<=size(@b); @i=@i+1)
172   {
173     @testpoly=@testpoly*@b[@i];
174     @elimvarspoly = subst(@elimvarspoly, @b[@i], 1);
175   }
176
177   //Wenn keine Variablen mehr uebrig sind, sind wir fertig:
178   if (@elimvarspoly==1)
179   {
180     // Falls das 4. Argument 1 ist, eine minimale Basis berechnen
181     if (#[4]==1)
182     {
183       @a=mres(@a,1)[1];
184     }
185     return(@a);
186   }
187
188   // Anhand dieses Polynoms ueberpruefen, welche Variablen unser
189   // neuer Ring braucht: @varnames
190   for (@i=1; @i<=nvars(basering); @i=@i+1)
191   {
192     if (@testpoly/var(@i)==0)
193     {
194       @varnames=@varnames + nameof(var(@i)) + ",";
195     }
196   }
197   @varnames=@varnames[1, size(@varnames)-1];
198
199   // Den neuen Ring erzeugen und das Ideal a darin abbilden:
200   execute("ring @r1=" + charstr(basering) + ",(" + @varnames + ")," + "dp;" );
201   ideal @a=imap(`@oldgnir`, @a);
202   list #=imap(`@oldgnir`,#);
203
204   // Nun die beste Reihenfolge fuer die Variablen berechnen:
205   string @orderedvars=string(maxideal(1));
206   if (#[3]==1)
207   {
208      poly @elimvarspoly=imap(`@oldgnir`,@elimvarspoly);
209      intvec @v= valvars(@a,#[5], @elimvarspoly,#[6]);
210      @v;
211      @orderedvars=string(sort1(maxideal(1),@v));
212   }
213
214   // In einen Ring wechseln, der diese Reihenfolge beruecksichtig:
215   execute("ring @r2=" + charstr(basering) + ",(" + @orderedvars + ",@homo),dp;" );
216   ideal @a= imap(@r1, @a);
217   kill @r1;
218
219   //das ideal homogenisieren
220   @a=homog(@a,@homo);
221
222   // Die Variablen holen, die wirklich noch zu eliminieren sind, und
223   // danach eliminate aufrufen.
224   poly @elimvarspoly= imap(`@oldgnir`, @elimvarspoly);
225   ideal @b=eliminate(@a, @elimvarspoly);
226
227   // Falls das 4. Argument 1 ist, eine minimale Basis berechnen
228   if (#[4]==1)
229   {
230     @b=mres(@b,1)[1];
231   }
232
233   // dehomogenisieren (@homo=1) und zurueck in den alten Ring
234   @b=subst(@b,@homo,1);
235   execute "setring "+@oldgnir+";";
236   return(imap(@r2,@b));
237}
238example
239{
240   ring s=31991,(e,f,x,y,z,t,u,v,w,a,b,c,d),dp;
241   ideal j=
242            w2+f2-1,
243            x2+t2+a2-1,
244            y2+u2+b2-1,
245            z2+v2+c2-1,
246            d2+e2-1,
247            f4+2u,
248            wa+tf,
249            xy+tu+ab,
250            yz+uv+bc,
251            cd+ze,
252            x+y+z+e+1,
253            t+u+v+f-1,
254            w+a+b+c+d;
255   fastElim(j,xyztuvwabcd,1,0,0,0);
256}
257
258
259proc sort1
260{
261  intvec @i=#[2];
262  int @ii=size(@i);
263  int @j;
264  string @s=string(typeof(#[1]))+" @m;";
265  execute(@s);
266  for (@j=1;@j<=@ii;@j=@j+1)
267  {
268    @m[@j] = #[1][@i[@j]];
269  }
270  return(@m);
271}
272
273
274proc valvars
275"
276USAGE:    valvars(id, [n1, [m, [n2]]]); id (poly|ideal|vector|module);
277                                        m monom; n1,n2 int
278RETURNS:  an intvec describing the permutation such that the permuted
279          ringvariables are ordered with respect to inreasing complexity
280          [resp. decreasing complexity if n1 is present and non-zero].
281          If m is present, the variables occuring in m are sorted in one
282          block before all variables not occuring in m. n2 controls the
283          order of this block as n1 does for the other one.
284          A variable x is more complex than y (with respect to id) if, in the
285          input, either the maximal occuring power of x is bigger than that
286          of y or both are equal and the coefficient of x to this power
287          contains more monomials than that of y.
288EXAMPLE:  example valvars; shows an example
289"
290{
291  int     @order1, @order2,     // order of vars not to elim resp. to elim
292          @i1,                  // runs through all variables
293          @i2,                  // # of vars to elim already found
294          @N, @M,               // N = total # of vars, M = # of vars to elim
295          @i, @j, @k, @size, @degree;
296  intvec  @varvec, @degvec, @sizevec;
297  poly    @m;
298  ideal   @id;
299  matrix  @C;
300
301  @id = matrix( #[1] ); @m = 0;
302  @N = nvars( basering ); @M = 0;
303  @order1 = 1; @order2 = 1;
304  if ( size(#) >= 4 )
305    { @order2 = not( #[4] ) * 2 - 1; }
306  if ( size(#) >= 3 )
307    { @m = #[3]; @M = deg( #[3] ); }
308  if ( size(#) >= 2 )
309    { @order1 = not( #[2] ) * 2 - 1; }
310
311  @i2 = 0;
312  for ( @i1 = 1; @i1 <= @N; @i1++ )
313  {
314    // get valuation of current variable.
315    @C = coeffs( @id, var( @i1 ));
316    @degree = nrows( @C );
317    @size = 0;
318    for ( @j = ncols( @C ); @j >= 1; @j-- )
319      { @size = @size + size( @C[ @degree, @j ] ); }
320
321    // mind order and calculate limits for search
322    if ( @M and size( @m / var( @i1 )))
323    {
324      @degree = @degree * @order2; @size = @size * @order2;
325      @i2++;
326      @i = @i2; @j = 1;
327    }
328    else
329    {
330      @degree = @degree * @order1; @size = @size * @order1;
331      @i = @i1 - @i2 + @M; @j = @M+1;
332    }
333
334    // look where we have to insert the variable
335    // "start = " + string( @j ) + ", end = " + string( @i );
336    @degvec[ @i ] = @degree + 1; @sizevec[ @i ] = @size + 1;
337    while ( @degvec[ @j ] < @degree )
338      { @j++; }
339    while ( @degvec[ @j ] == @degree and @sizevec[ @j ] < @size )
340      { @j++; }
341
342    // now shift the variables behind the current one back
343    // "inserting at " + string( @j );
344    for ( @k = @i-1; @k >= @j; @k-- )
345    {
346      @varvec[ @k+1 ] = @varvec[ @k ];
347      @degvec[ @k+1 ] = @degvec[ @k ];
348      @sizevec[ @k+1 ] = @sizevec[ @k ];
349    }
350    @varvec[ @j ] = @i1;             // print( @varvec );
351    @degvec[ @j ] = @degree;         // print( @degvec );
352    @sizevec[ @j ] = @size;          // print( @sizevec );
353  }
354
355  return( @varvec );
356}
357example
358{
359  echo = 1; ring @r=0,(a,b,c,x,y,z),lp;
360  poly @f=a3+b4+xyz2+xyz+yz+1;
361  valvars( @f );
362  valvars( @f, 1 );
363  valvars( @f, 0, abc );
364  valvars( @f, 0, xyz );
365  valvars( @f, 0, abc, 1 );
366  valvars( @f, 1, abc, 1 );
367  ideal @i=@f,z5,y5,-y5;
368  valvars( @i );
369  valvars( @i, 0, abcxyz );
370  kill @r; echo = 0;
371}
Note: See TracBrowser for help on using the repository browser.