source: git/Singular/LIB/mrrcount.lib @ 62a35c

spielwiese
Last change on this file since 62a35c was e182c8, checked in by Hans Schönemann <hannes@…>, 19 years ago
*lossen/greuel/hannes: new libs git-svn-id: file:///usr/local/Singular/svn/trunk@7847 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 14.2 KB
Line 
1// E. Tobis  12.Nov.2004
2// last change 16. Apr. 2005 (G.-M. Greuel)
3///////////////////////////////////////////////////////////////////////////////
4category="Symbolic-numerical solving"
5info="
6LIBRARY: mrrcount.lib Counting number of real roots of polynomial systems
7AUTHOR:               Enrique A. Tobis, etobis@dc.uba.ar
8
9OVERVIEW:  Routines for counting the number of real roots of a multivariate
10           polynomial system.
11           References:
12
13PROCEDURES:
14 symsignature(m)   Signature of the symmetric matrix m
15 sturmquery(h,B,I) Sturm query of h on V(I)
16 matbil(h,B,I)     Matrix of the bilinear form on R/I associated to h
17 matmult(f,B,I)    Matrix of multiplication by f (m_f) on R/I in the basis B
18 tracemult(f,B,I)  Trace of m_f (B is an ordered basis of R/I)
19 coords(f,B,I)     Coordinates of f in the ordered basis B
20 randcharpoly(B,I) Pseudorandom charpoly of univ. projection
21 verify(p,B,i)     Verifies the result of randcharpoly
22 randlinpoly()     Pseudorandom linear polynomial
23 powersums(f,B,I)  Powersums of the roots of a char polynomial
24 symmfunc(S)       Symmetric functions from the powersums S
25 univarpoly(l)     Polynomial with coefficients from l
26 qbase(i)          Like kbase, but the monomials are ordered
27
28KEYWORDS: real roots, univariate projection
29";
30///////////////////////////////////////////////////////////////////
31LIB "linalg.lib";   // We use charpoly
32LIB "urrcount.lib"; // We use varsigns
33
34proc symsignature(matrix m)
35"USAGE:     symsignature(m); m matrix. m must be symmetric.
36RETURN:    number: the signature of m
37SEE ALSO:  matbil,sturmquery
38EXAMPLE:   example symsignature; shows an example"
39{
40  int positive, negative, i, j;
41  list l;
42  poly variable;
43
44  if (isparam(m)) {
45    ERROR("This procedure cannot operate with parametric arguments");
46  }
47
48  if (!isSquare(m)) {
49    ERROR ("m must be a square matrix");
50  }
51
52  // We check whether m is symmetric
53  for (i = 1;i <= nrows(m);i++) {
54    for (j = i;j <= nrows(m);j++) {
55      if (m[i,j] != m[j,i]) {
56        ERROR ("m must be a symmetric matrix");
57      }
58    }
59  }
60
61  poly f = charpoly(m); // Uses the last variable of the ring
62
63  for (i = size(f);i >= 1;i--) {
64    l[i] = leadcoef(f[i]);
65  }
66  positive = varsigns(l);
67
68  variable = var(nvars(basering)); // charpoly uses the last variable
69  f = subst(f,variable,-variable);
70
71  for (i = size(f);i >= 1;i--) {
72    l[i] = leadcoef(f[i]);
73  }
74
75  negative = varsigns(l);
76  return (positive - negative);
77}
78example
79{
80  echo = 2;
81  ring r = 0,(x,y),dp;
82  ideal i = x4-y2x,y2-13;
83  i = groebner(i);
84  ideal b = qbase(i);
85
86  matrix m = matbil(1,b,i);
87  symsignature(m);
88}
89///////////////////////////////////////////////////////////////////////////////
90
91proc sturmquery(poly h,ideal B,ideal I)
92"USAGE:     sturmquery(h,b,i); h poly, b,i ideal
93RETURN:    number: the Sturm query of h in V(i)
94ASSUME:    b is an ordered monomial basis of r/i, r = basering
95SEE ALSO:  symsignature,matbil
96EXAMPLE:   example sturmquery; shows an example"
97{
98  if (isparam(h) || isparam(B) || isparam(I)) {
99    ERROR("This procedure cannot operate with parametric arguments");
100  }
101
102  return (mysymmsig(matbil(h,B,I)));
103}
104example
105{
106  echo = 2;
107  ring r = 0,(x,y),dp;
108  ideal i = x4-y2x,y2-13;
109  i = groebner(i);
110  ideal b = qbase(i);
111
112  sturmquery(1,b,i);
113}
114
115static proc mysymmsig(matrix m)
116// returns the signature of a square symmetric matrix m
117{
118  int positive, negative, i;
119  list l;
120  poly variable;
121
122  poly f = charpoly(m); // Uses the last variable of the ring
123
124  for (i = size(f);i >= 1;i--) {
125    l[i] = leadcoef(f[i]);
126  }
127  positive = varsigns(l);
128
129  variable = var(nvars(basering)); // charpoly uses the last variable
130  f = subst(f,variable,-variable);
131
132  for (i = size(f);i >= 1;i--) {
133    l[i] = leadcoef(f[i]);
134  }
135
136  negative = varsigns(l);
137  return (positive - negative);
138}
139///////////////////////////////////////////////////////////////////////////////
140
141proc matbil(poly h,ideal B,ideal I)
142"USAGE:    matbil(h,b,i); h poly, b,i ideal
143RETURN:    matrix: the matrix of the bilinear form (f,g) |-> trace(m_fhg),
144           m_fhg = multiplication with fhg on r/i
145ASSUME:    i is a Groebner basis and b is an ordered monomial basis of r/i,
146           r = basering
147SEE ALSO:  matmult,tracemult
148EXAMPLE:   example matbil; shows an example"
149{
150  matrix m[size(B)][size(B)];
151  poly f;
152  int k,l;
153
154  f = reduce(h,I);
155  for (k = 1;k <= size(B);k++) {
156    for (l = 1;l <= k;l++) {
157      m[k,l] = tracemult((reduce(f*B[k]*B[l],I),B,I));
158      m[l,k] = m[k,l]; // The matrix we are trying to compute is symmetric
159    }
160  }
161  return(m);
162}
163example
164{
165  echo = 2;
166  ring r = 0,(x,y),dp;
167  ideal i = x4-y2x,y2-13;
168  i = groebner(i);
169  ideal b = qbase(i);
170  poly f = x3-xy+y-13+x4-y2x;
171
172  matrix m = matbil(f,b,i);
173  print(m);
174
175}
176///////////////////////////////////////////////////////////////////////////////
177
178proc tracemult(poly f,ideal B,ideal I)
179"USAGE:     tracemult(f,b,i);f poly, b,i ideal
180RETURN:    number: the trace of the multiplication by f (m_f) on r/i,
181           written in the monomial basis b of r/i, r = basering
182           (faster than matmult + trace)
183ASSUME:    i is a Groebner basis and b is an ordered monomial basis of r/i
184SEE ALSO:  matmult,trace
185EXAMPLE:   example tracemult; shows an example"
186{
187  poly g;
188  int k; // Iterates over the basis monomials
189  int l; // Iterates over the rows of the matrix
190  list coordinates;
191  number m;
192
193  g = reduce(f,I);
194 
195  m = 0;
196  for (k = 1;k <= size(B);k++) {
197    coordinates = coords(g*(B[k]),B,I); // f*x_k written on the basis B
198    m = m + coordinates[k];
199  }
200  return (m);
201}
202example
203{
204  echo = 2;
205  ring r = 0,(x,y),dp;
206  ideal i = x4-y2x,y2-13;
207  i = groebner(i);
208  ideal b = qbase(i);
209
210  poly f = x3-xy+y-13+x4-y2x;
211  matrix m = matmult(f,b,i);
212  print(m);
213
214  tracemult(f,b,i);
215}
216///////////////////////////////////////////////////////////////////////////////
217
218proc matmult(poly f, ideal B, ideal I)
219"USAGE:     matmult(f,b,i); f poly, b,i ideal
220RETURN:    matrix: the matrix of the multiplication map by f (m_f) on r/i
221           wrt to the monomial basis b of r/i (r = basering)
222ASSUME:    i is a Groebner basis and b is an ordered monomial basis of r/i
223SEE ALSO:  coords,matbil
224EXAMPLE:   example matmult; shows an example"
225{
226  poly g;
227  int k; // Iterates over the basis monomials
228  int l; // Iterates over the rows of the matrix
229  list coordinates;
230  matrix m[size(B)][size(B)];
231
232  g = reduce(f,I);
233 
234  for (k = 1;k <= size(B);k++) {
235    coordinates = coords(g*(B[k]),B,I); // f*x_k written on the basis B
236    for (l = 1;l <= size(B);l++) {
237      m[l,k] = coordinates[l];
238    }
239  }
240  return (m);
241}
242example
243{
244  echo = 2;
245  ring r = 0,(x,y),dp;
246  ideal i = x4-y2x,y2-13;
247  i = groebner(i);
248  ideal b = qbase(i);
249
250  poly f = x3-xy+y-13+x4-y2x;
251
252  matrix m = matmult(f,b,i);
253  print(m);
254}
255///////////////////////////////////////////////////////////////////////////////
256
257proc coords(poly f,ideal B,ideal I)
258"USAGE:     coords(f,b,i), f poly, b,i ideal
259RETURN:    list: the coordinates of the class of f in the monomial basis b
260ASSUME:    i is a Groebner basis and b is an ordered monomial basis of r/i,
261           r = basering
262SEE ALSO:  matmult,matbil
263KEYWORDS:  coordinates
264EXAMPLE:   example coords; shows an example"
265{
266  // We assume the basis is sorted according to the ring order
267  poly g;
268  int k,l=1,1;
269  list coordinates;
270  int N = size(B);
271
272  // We first compute the normal form of f wrt I
273  g = reduce(f,I);
274
275  coordinates[N] = 0; // We resize the list of coordinates
276  while (k <= N) {
277    if (l <= size(g) && leadmonom(g[l]) == B[k]) {
278      coordinates[k] = leadcoef(g[l]);
279      l++;
280    } else {
281      coordinates[k] = 0;
282    }
283    k++;
284  }
285  return (coordinates);
286}
287example
288{
289  echo = 2;
290  ring r = 0,(x,y),dp;
291  ideal i = x4-y2x,y2-13;
292  i = groebner(i);
293  ideal b = qbase(i);
294 
295  coords(x3-xy+y-13+x4-y2x,b,i);
296  b;
297}
298///////////////////////////////////////////////////////////////////////////////
299
300static proc isSquare(matrix m)
301// returns 1 iff m is a square matrix
302{
303  return (nrows(m)==ncols(m));
304}
305///////////////////////////////////////////////////////////////////////////////
306
307proc randcharpoly(ideal B,ideal I)
308"USAGE:     randcharpoly(b,i); b,i ideal
309RETURN:    poly: the characteristic polynomial of a pseudorandom
310           rational univariate projection having one zero per zero of i
311ASSUME:    i is a Groebner basis and b is an ordered monomial basis of r/i,
312           r = basering
313KEYWORDS:  rational univariate projection
314EXAMPLE:   example randcharpoly; shows an example"
315{
316  poly p;
317  poly generic;
318  list l;
319  matrix m;
320  poly q;
321
322  generic = randlinpoly();
323
324  p = reduce(generic,I);
325  m = matmult(p,B,I);
326  q = charpoly(m);
327
328  print("*********************************************************************");
329  print("* WARNING: This polynomial was obtained using  pseudorandom numbers.*");
330  print("* If you want to verify the result, please use the command          *");
331  print("*                                                                   *");
332  print("* verify(p,b,i)                                                     *");
333  print("*                                                                   *");
334  print("* where p is the polynomial, b is the monomial basis used, and i    *");
335  print("* the Groebner basis of the ideal                                   *");
336  print("*********************************************************************");
337
338  return(q);
339}
340example
341{
342  echo = 2;
343  ring r = 0,(x,y,z),dp;
344  ideal i = (x-1)*(x-2),(y-1),(z-1)*(z-2)*(z-3)^2;
345  i = groebner(i);
346  ideal b = qbase(i);
347  poly p = randcharpoly(b,i);
348  p;
349  nrroots(p); // See nrroots in urrcount.lib
350}
351///////////////////////////////////////////////////////////////////////////////
352
353proc verify(poly p,ideal b,ideal i)
354"USAGE:     verify(p,b,i);p poly, b,i,ideal
355RETURN:    integer: 1 iff the polynomial p splits the points of V(i).
356           It's used to check the result of randcharpoly
357ASSUME:    i is a Groebner basis and b is an ordered monomial basis of r/i,
358           r = basering
359SEE ALSO:  randcharpoly
360EXAMPLE:   example verify; shows an example"
361{
362  poly sqrfree;
363  int correct;
364  poly variable;
365
366  if (isparam(p) || isparam(b) || isparam(i)) {
367    ERROR("This procedure cannot operate with parametric arguments");
368  }
369
370  variable = isuni(p);
371  sqrfree = p/gcd(p,diff(p,variable));
372  correct = (mat_rk(matbil(1,b,i)) == deg(sqrfree));
373
374  if (correct) {
375    print("Verification successful");
376  } else {
377    print("The choice of random numbers was not useful");
378  }
379  return (correct);
380}
381example
382{
383  echo = 2;
384  ring r = 0,(x,y),dp;
385  poly f = x3-xy+y-13+x4-y2x;
386  ideal i = x4-y2x,y2-13;
387  i = groebner(i);
388  ideal b = qbase(i);
389  poly p = randcharpoly(b,i);
390  verify(p,b,i);
391}
392///////////////////////////////////////////////////////////////////////////////
393
394proc randlinpoly()
395"USAGE:     randlinpoly();
396RETURN:    poly: a polynomial linear in each variable of the ring, with
397           pseudorandom coefficients
398SEE ALSO:  randcharpoly;
399EXAMPLE:   example randlinpoly; shows an example"
400{
401  int n,i;
402  poly p = 0;
403 
404  n = nvars(basering);
405  for (i = 1;i <= n;i++) {
406    p = p + var(i)*random(1,1000000);
407  }
408  return (p);
409}
410example
411{
412  echo = 2;
413  ring r = 0,(x,y,z,w),dp;
414  poly p = randlinpoly();
415  p;
416}
417///////////////////////////////////////////////////////////////////////////////
418
419proc powersums(poly f,ideal B,ideal I)
420"USAGE:     powersums(f,b,i); f poly; b,i ideal, b a sorted monomial basis for
421           the quotient between the basering and i.
422RETURN:    list: the powersums of the results of evaluating f at the zeros of I
423SEE ALSO:  symmfunc
424EXAMPLE:   example symmfunc; shows an example"
425{
426  int N,k;
427  list sums;
428
429  N = size(B);
430  for (k = 1;k <= N;k++) {
431    sums = sums + list(leadcoef(trace(matmult(f^k,B,I))));
432  }
433  return (sums);
434}
435example
436{
437  echo = 2;
438  ring r = 0,(x,y,z),dp;
439
440  ideal i = (x-1)*(x-2),(y-1),(z+5); // V(I) = {(1,1,-5),(2,1,-5)
441  i = groebner(i);
442
443  ideal b = qbase(i);
444  poly f = x+y+z;
445  list psums = list(-2-3,4+9); // f evaluated at V(I) gives {-3,-2}
446  list l = powersums(f,b,i);
447  psums;
448  l;
449}
450///////////////////////////////////////////////////////////////////////////////
451
452proc symmfunc(list S)
453// Takes the list of power sums and returns the symmetric functions
454"USAGE:     symmfunc(s); s list
455RETURN:    list: the symmetric functions of the roots of a polynomial, given
456                 the power sums of those roots.
457SEE ALSO:  powersums
458EXAMPLE:   example symmfunc; shows an example"
459{
460  list a;
461  int j,l,N;
462  number sum;
463
464  N = size(S);
465  a[N+1] = 1; // We set the length of the list and initialize its last element.
466
467  for (l = N - 1;l >= 0;l--) {
468    sum = 0;
469    for (j = l + 1;j <= N;j++) {
470      sum = sum + ((a[j+1])*(S[j-l]));
471    }
472    sum = -sum;
473    a[l+1] = sum/(N-l);
474  }
475
476  a = reverse(a);
477  return (a);
478}
479example
480{
481  echo = 2;
482  ring r = 0,x,dp;
483  poly p = (x-1)*(x-2)*(x-3);
484  list psums = list(1+2+3,1+4+9,1+8+27);
485  list l = symmfunc(psums);
486  l;
487  p; // Compare p with the elements of l
488}
489///////////////////////////////////////////////////////////////////////////////
490
491proc univarpoly(list l)
492"USAGE:     univarpoly(l); l list
493RETURN:    poly: a polynomial p on the first variable of basering, say x,
494           with p = l[1] + l[2]*x + l[3]*x^2 + ...
495EXAMPLE:  example univarpoly; shows an example"
496{
497  poly p;
498  int i,n;
499 
500  n = size(l);
501  for (i = 1;i <= n;i++) {
502    p = p + l[i]*var(1)^(n-i);
503  }
504  return (p);
505}
506example
507{
508  echo = 2;
509  ring r = 0,x,dp;
510  list l = list(1,2,3,4,5);
511  poly p = univarpoly(l);
512  p;
513}
514///////////////////////////////////////////////////////////////////////////////
515
516proc qbase(ideal i)
517"USAGE:    qbase(I); I zero-dimensional ideal
518RETURN:   ideal: A monomial basis of the quotient between the basering and the
519          ideal I, sorted according to the basering order.
520SEE ALSO: kbase
521KEYWORDS: zero-dimensional
522EXAMPLE:  example qbase; shows an example"
523{
524  ideal b;
525
526  b = kbase(i);
527  b = reverseideal(sort(b)[1]); // sort sorts in ascending order
528  return (b);
529}
530example
531{
532  echo = 2;
533  ring r = 0,(x,y,z),dp;
534
535  ideal i = 2x2,-y2,z3;
536  i = groebner(i);
537  ideal b = qbase(i);
538  b;
539  b = kbase(i);
540  b; // Compare this with the result of qbase
541}
542///////////////////////////////////////////////////////////////////////////////
543
544static proc reverseideal(ideal b) // Returns b reversed
545{
546  int i;
547  ideal result;
548
549  result = b[1];
550  for (i = 2;i <= size(b);i++) {
551    result = b[i], result;
552  }
553  return (result);
554}
555///////////////////////////////////////////////////////////////////////////////
556
Note: See TracBrowser for help on using the repository browser.