source: git/Singular/LIB/mrrcount.lib @ d3b3ab

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