groebner.tst 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. % Examples of use of Groebner code.
  2. % In the Examples 1 - 3 the polynomial ring for the ideal operations
  3. % (variable sequence, term order mode) is defined globally in advance.
  4. % Example 1, Linz 85.
  5. torder ({q1,q2,q3,q4,q5,q6},lex)$
  6. groebner {q1,
  7. q2**2 + q3**2 + q4**2,
  8. q4*q3*q2,
  9. q3**2*q2**2 + q4**2*q2**2 + q4**2*q3**2,
  10. q6**2 + 1/3*q5**2,
  11. q6**3 - q5**2*q6,
  12. 2*q2**2*q6 - q3**2*q6 - q4**2*q6 + q3**2*q5 - q4**2*q5,
  13. 2*q2**2*q6**2 - q3**2*q6**2 - q4**2*q6**2 - 2*q3**2*q5*q6
  14. + 2*q4**2*q5*q6 - 2/3*q2**2*q5**2 + 1/3*q3**2*q5**2
  15. + 1/3*q4**2*q5**2,
  16. - q3**2*q2**2*q6 - q4**2*q2**2*q6 + 2*q4**2*q3**2*q6 -
  17. q3**2*q2**2*q5 + q4**2*q2**2*q5,
  18. - q3**2*q2**2*q6**2 - q4**2*q2**2*q6**2 + 2*q4**2*q3**2*q6**2
  19. + 2*q3**2*q2**2*q5*q6 - 2*q4**2*q2**2*q5*q6 + 1/3*q3**2*q2**2
  20. *q5**2 + 1/3*q4**2*q2**2*q5**2 - 2/3*q4**2*q3**2*q5**2,
  21. - 3*q3**2*q2**4*q5*q6**2 + 3*q4**2*q2**4*q5*q6**2
  22. + 3*q3**4*q2**2*q5*q6**2 - 3*q4**4*q2**2*q5*q6**2
  23. - 3*q4**2*q3**4*q5*q6**2 + 3*q4**4*q3**2*q5*q6**2
  24. + 1/3*q3**2*q2**4*q5**3 - 1/3*q4**2*q2**4*q5**3
  25. - 1/3*q3**4*q2**2*q5**3 + 1/3*q4**4*q2**2*q5**3 + 1/3*q4**2
  26. *q3**4*q5**3 - 1/3*q4**4*q3**2*q5**3};
  27. % Example 2. (Little) Trinks problem with 7 polynomials in 6 variables.
  28. trinkspolys:={45*p + 35*s - 165*b - 36,
  29. 35*p + 40*z + 25*t - 27*s,
  30. 15*w + 25*p*s + 30*z - 18*t - 165*b**2,
  31. - 9*w + 15*p*t + 20*z*s,
  32. w*p + 2*z*t - 11*b**3,
  33. 99*w - 11*s*b + 3*b**2,
  34. b**2 + 33/50*b + 2673/10000}$
  35. trinksvars := {w,p,z,t,s,b}$
  36. torder(trinksvars,lex)$
  37. switch varopt; off varopt;
  38. groebner trinkspolys;
  39. groesolve ws;
  40. % Example 3. Hairer, Runge-Kutta 1, 6 polynomials 8 variables.
  41. torder({c2,c3,b3,b2,b1,a21,a32,a31},lex);
  42. groebnerf{c2 - a21,
  43. c3 - a31 - a32,
  44. b1 + b2 + b3 - 1,
  45. b2*c2 + b3*c3 - 1/2,
  46. b2*c2**2 + b3*c3**2 - 1/3,
  47. b3*a32*c2 - 1/6};
  48. % The examples 4 and 5 use automatic variable extraction.
  49. % Example 4.
  50. torder gradlex$
  51. g4:=
  52. groebner{b + e + f - 1,
  53. c + d + 2*e - 3,
  54. b + d + 2*f - 1,
  55. a - b - c - d - e - f,
  56. d*e*a**2 - 1569/31250*b*c**3,
  57. c*f - 587/15625*b*d};
  58. hilbertpolynomial g4;
  59. glexconvert(g4,gvarslast,newvars={e},maxdeg=8);
  60. % Example 5.
  61. off varopt;
  62. torder({u0,u2,u3,u1},lex)$
  63. groesolve({u0**2 - u0 + 2*u1**2 + 2*u2**2 + 2*u3**2,
  64. 2*u0*u1 + 2*u1*u2 + 2*u2*u3 - u1,
  65. 2*u0*u2 + u1**2 + 2*u1*u3 - u2,
  66. u0 + 2*u1 + 2*u2 + 2*u3 - 1},
  67. {u0,u2,u3,u1});
  68. % Example 6. (Big) Trinks problem with 6 polynomials in 6 variables.
  69. torder(trinksvars,lex)$
  70. btbas:=
  71. groebner{45*p + 35*s - 165*b - 36,
  72. 35*p + 40*z + 25*t - 27*s,
  73. 15*w + 25*p*s + 30*z - 18*t - 165*b**2,
  74. -9*w + 15*p*t + 20*z*s,
  75. w*p + 2*z*t - 11*b**3,
  76. 99*w - 11*b*s + 3*b**2};
  77. % The above system has dimension zero. Therefore its Hilbert polynomial
  78. % is a constant which is the number of zero points (including complex
  79. % zeros and multipliticities);
  80. hilbertpolynomial ws;
  81. % Example of Groebner with numerical postprocessing.
  82. on rounded;off varopt;
  83. groesolve(trinkspolys,trinksvars);
  84. off rounded;
  85. % Additional groebner operators.
  86. % Reduce one polynomial wrt the basis of big Trinks. The result 0
  87. % is a proof for the ideal membership of the polynomial.
  88. torder(trinksvars,lex)$
  89. preduce(45*p + 35*s - 165*b - 36,btbas);
  90. % The following examples show how to work with the distributive
  91. % form of polynomials.
  92. torder({u0,u1,u2,u3},gradlex)$
  93. gsplit(2*u0*u2 + u1**2 + 2*u1*u3 - u2,{u0,u1,u2,u3});
  94. torder(trinksvars,lex)$
  95. gsort trinkspolys;
  96. gspoly(first trinkspolys,second trinkspolys);
  97. gvars trinkspolys;
  98. % Tagged basis and reduction trace. A tagged basis is a basis where
  99. % each polynomial is equated to a linear combination of the input
  100. % set. A tagged reduction shows how the result is computed by using
  101. % the basis polynomials.
  102. % First example for tagged polynomials: show how a polynomial is
  103. % represented as linear combination of the basis polynomials.
  104. % First I set up an environment for the computation.
  105. torder(trinksvars,lex)$
  106. % Then I compute an ordinary Groebner basis.
  107. bas:=groebner trinkspolys$
  108. % Next I assign a tag to each basis polynomial.
  109. taggedbas:=for i:=1:length bas collect mkid(p,i)=part(bas,i);
  110. % And finally I reduce a (tagged) polynomial wrt the tagged basis.
  111. preducet(new=w*p + 2*z*t - 11*b**3,taggedbas);
  112. % Second example for tagged polynomials: representing a Groebner basis
  113. % as a combination of the input polynomials, here in a simple geometric
  114. % problem.
  115. torder({x,y},lex)$
  116. groebnert {circle=x**2 + y**2 - r**2,line=a*x + b*y};
  117. % In the third example I enter two polynomials that have no common zero.
  118. % Consequently the basis is {1}. The tagged computation gives me a proof
  119. % for the inconsistency of the system which is independent of the
  120. % Groebner formalism.
  121. groebnert {circle1=x**2 + y**2 - 10,circle2=x**2 + y**2 - 2};
  122. % Solve a special elimination task by using a blockwise elimination
  123. % order defined by a matrix. The equation set goes back to A.M.H.
  124. % Levelt (Nijmegen). The question is whether there is a member in the
  125. % ideal which depends only on two variables. Here we select x4 and y1.
  126. % The existence of such a polynomial proves that the system has exactly
  127. % one degree of freedom.
  128. % The first two rows of the term order matrix define the groupwise
  129. % elimination. The remaining lines define a secondary local
  130. % lexicographical behavior which is needed to construct an admissible
  131. % ordering.
  132. f1:=y1^2 + z1^2 -1;
  133. f2:=x2^2 + y2^2 + z2^2 -1;
  134. f3:=x3^2 + y3^2 + z3^2 -1;
  135. f4:=x4^2 + z4^2 -1;
  136. f5:=y1*y2 + z1*z2;
  137. f6:=x2*x3 + y2*y3 + z2*z3;
  138. f7:=x3*x4 + z3*z4;
  139. f8:=x2 + x3 + x4 + 1;
  140. f9:=y1 + y2 + y3 - 1;
  141. f10:=z1 + z2 + z3 + z4;
  142. eqns:={f1,f2,f3,f4,f5,f6,f7,f8,f9,f10}$
  143. vars:={x2,x3,y2,y3,z1,z2,z3,z4,x4,y1}$
  144. torder(vars,matrix,
  145. mat((1,1,1,1,1,1,1,1,0,0),
  146. (0,0,0,0,0,0,0,0,1,1),
  147. (1,0,0,0,0,0,0,0,0,0),
  148. (0,1,0,0,0,0,0,0,0,0),
  149. (0,0,1,0,0,0,0,0,0,0),
  150. (0,0,0,1,0,0,0,0,0,0),
  151. (0,0,0,0,1,0,0,0,0,0),
  152. (0,0,0,0,0,1,0,0,0,0),
  153. (0,0,0,0,0,0,1,0,0,0),
  154. (0,0,0,0,0,0,0,0,1,0)));
  155. first reverse groebner(eqns,vars);
  156. % For a faster execution we convert the matrix into a
  157. % proper machine code routine.
  158. on comp;
  159. torder_compile(levelt,mat(
  160. (1,1,1,1,1,1,1,1,0,0),
  161. (0,0,0,0,0,0,0,0,1,1),
  162. (1,0,0,0,0,0,0,0,0,0),
  163. (0,1,0,0,0,0,0,0,0,0),
  164. (0,0,1,0,0,0,0,0,0,0),
  165. (0,0,0,1,0,0,0,0,0,0),
  166. (0,0,0,0,1,0,0,0,0,0),
  167. (0,0,0,0,0,1,0,0,0,0),
  168. (0,0,0,0,0,0,1,0,0,0),
  169. (0,0,0,0,0,0,0,0,1,0)));
  170. torder(vars,levelt)$
  171. first reverse groebner(eqns,vars);
  172. % For a homogeneous polynomial set we compute a graded Groebner
  173. % basis with grade limits. We use the graded term order with lex
  174. % as following order. As the grade vector has no zeros, this ordering
  175. % is functionally equivalent to a weighted ordering.
  176. torder({x,y,z},graded,{1,1,2},lex);
  177. dd_groebner(0,10,{x^10*y + y*z^5, x*y^12 + y*z^6});
  178. dd_groebner(0,50,{x^10*y + y*z^5, x*y^12 + y*z^6});
  179. dd_groebner(0,infinity,{x^10*y + y*z^5, x*y^12 + y*z^6});
  180. % Test groebner_walk
  181. trinkspolys := {45*p + 35*s - 165*b - 36,
  182. 35*p + 40*z + 25*t - 27*s,
  183. 15*w + 25*p*s + 30*z - 18*t - 165*b**2,
  184. - 9*w + 15*p*t + 20*z*s,
  185. w*p + 2*z*t - 11*b**3,
  186. 99*w - 11*s*b + 3*b**2,
  187. b**2 + 33/50*b + 2673/10000}$
  188. trinksvars := {w,p,z,t,s,b}$
  189. torder(trinksvars,gradlex)$
  190. gg:=groebner trinkspolys$
  191. g:=groebner_walk gg$
  192. on div$
  193. g;
  194. on varopt;
  195. g1:=solve({first g},{b});
  196. g0:=sub({first g1},g);
  197. solve({ second g0},{w});
  198. solve({third g0},{p});
  199. solve({part(g0,4)},{z});
  200. solve({part(g0,5)},{t});
  201. solve({part(g0,6)},{s});
  202. g0:=sub({second g1},g);
  203. solve({second g0},{w});
  204. solve({third g0},{p});
  205. solve({part(g0,4)},{z});
  206. solve({part(g0,5)},{t});
  207. solve({part(g0,6)},{s});
  208. % Example after the book "David Cox, John Little, Donal O'Shea:
  209. % "Ideals, Varieties and Algorithms", chapter 2, paragraph 8, example 3.
  210. % This example was given by Shigetoshi Katsura (Japan).
  211. off groebopt;torder({x,y,z,l},lex);
  212. g:=groebner{3*x^2+2*y*z-2*x*l,2*x*z-2*y*l,2*x*y-2*z-2*z*l,x^2+y^2+z^2-1}$
  213. gdimension g;
  214. gindependent_sets g;
  215. clear g, gg, trinkspolys, trinksvars$
  216. end;