grinterf.red 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. module grinterf;% Interface of Groebner package to reduce.
  2. % Entry points to the main module and general interface support.
  3. flag('(groebrestriction gvarslast groebprotfile gltb glterms gmodule),'share);
  4. switch groebopt,trgroeb,gltbasis,gsugar;
  5. vdpsortmode!*:='lex;% Initial mode .
  6. gltb:='(list); % Initially empty .
  7. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  8. %
  9. % Interface functions .
  10. symbolic procedure groebnereval u;
  11. % Non factorizing Groebner calculation .
  12. begin scalar n,!*groebfac,!*groebrm,!*factor,!*exp;!*exp:=t;
  13. n:=length u;
  14. if n=1 then return cadr groebner1(reval car u,nil,nil)
  15. else if n neq 2
  16. then rerror(groebner,1,"groebner called with wrong number of arguments");
  17. u:=groebner1(reval car u,reval cadr u,nil);
  18. if !*gltbasis then gltb:=cadr gltb;
  19. return cadr u end;
  20. put('groebner,'psopfn,'groebnereval);
  21. symbolic procedure groebnerfeval u;
  22. % Non factorizing Groebner calculation .
  23. begin scalar n,!*groebfac,!*groebrm,!*factor,!*exp,!*ezgcd,
  24. s,r,q;!*exp:=t;
  25. !*groebrm:=!*groebfac:=t;
  26. groebrestriction!*:=reval groebrestriction;
  27. if null dmode!* then !*ezgcd:=t;
  28. n:=length u;
  29. r:=if n=1 then groebner1(reval car u,nil,nil)else
  30. if n=2 then groebner1(reval car u,reval cadr u,nil)else
  31. if n neq 3
  32. then rerror(groebner,2,
  33. "groebner called with wrong number of arguments")
  34. else groebner1(reval car u,reval cadr u,reval caddr u);
  35. q:=r;
  36. % Remove duplicates.
  37. while q do<<s:=car q;q:=cdr q;if member(s,q)then r:=delete(s,r)>>;
  38. return r end;
  39. put('groebnerf,'psopfn,'groebnerfeval);
  40. symbolic procedure idquotienteval u;
  41. begin scalar n,!*factor,!*exp;!*exp:=t;
  42. n:=length u;
  43. if n=2 then return groebidq(reval car u,reval cadr u,nil)
  44. else if n neq 3
  45. then rerror(groebner,3,"idquotient called with wrong number of arguments")
  46. else return groebidq(reval car u,reval cadr u,reval caddr u)end;
  47. put('idealquotient,'psopfn,'idquotienteval);
  48. symbolic procedure saturationeval u;
  49. begin scalar a,b,c,!*factor,!*exp;!*exp:=t;
  50. if length u=2 then go to aa;
  51. rerror(groebner,19,"saturation called with wrong number of arguments");
  52. aa:a:=reval car u;
  53. if car a='list then go to bb;
  54. rerror(groebner,20,"saturation, first parameter must be a list");
  55. bb:a:='list.for each aa in cdr a collect
  56. if eqexpr aa then reval !*eqn2a aa else aa;
  57. c:=reval cadr u;
  58. if car c='list then
  59. rerror(groebner,25,"saturation, second parameter must no be a list");
  60. if eqexpr c then c:=reval !*eqn2a c;
  61. while not(b=a)do<<if b then a:=b;b:=groebidq(a,c,nil)>>;return b end;
  62. put('saturation,'psopfn,'saturationeval);
  63. symbolic procedure groebner1(u,v,r);
  64. % Buchberger algorithm system driver.'u'is a list of expressions
  65. % and'v'a list of variables or nil in which case the variables in'u'
  66. % are used.
  67. begin scalar vars,w,np,oldorder,!*grmod!*;integer pcount!*;
  68. w:=for each j in groerevlist u
  69. collect if eqexpr j then !*eqn2a j else j;
  70. if null w then rerror(groebner,4,"empty list in groebner");
  71. vars:=groebnervars(w,v);
  72. if r then r:=groerevlist r;
  73. groedomainmode();
  74. if vars then go to notempty;
  75. u:=0;for each p in w do if p neq 0 then u:=1;
  76. return{'list,{'list,u}};
  77. notempty:if dmode!* eq'!:mod!: and null setdiff(gvarlis w,vars)
  78. and current!-modulus < largest!-small!-modulus
  79. then !*grmod!*:=t;
  80. oldorder:=vdpinit vars;
  81. % Cancel common denominators.
  82. w:=for each j in w collect reorder numr simp j;
  83. % Optimize variable sequence if desired.
  84. if !*groebopt and vdpsortmode!* memq'(lex gradlex revgradlex)
  85. then<<w:=vdpvordopt(w,vars);vars:=cdr w;w:=car w;vdpinit vars>>;
  86. w:=for each j in w collect f2vdp j;
  87. if not !*vdpinteger then
  88. <<np:=t;
  89. for each p in w do np:=if np then vdpcoeffcientsfromdomain!? p else nil;
  90. if not np then<<!*vdpmodular:= nil;!*vdpinteger:=t>>>>;
  91. if !*groebprot then groebprotfile:={'list};
  92. if r then r:=for each p in r collect vdpsimpcont f2vdp numr simp p;
  93. w:=groebner2(w,r);
  94. if cdr w then % Remove redundant partial bases.
  95. begin scalar !*gsugar;
  96. for each b in w do
  97. for each c in w do
  98. if b and b neq c then
  99. <<v:=t;for each p in c do v:=v and vdpzero!? groebnormalform(p,b,'list);
  100. if v then<<w:=delete(b,w);b:=nil>>>>end;
  101. if !*gltbasis then
  102. gltb:='list.for each base in w collect
  103. 'list.for each j in base collect vdp2a vdpfmon(a2vbc 1,vdpevlmon j);
  104. w:='list.for each base in w collect 'list.for each j in base collect vdp2a j;
  105. vdpcleanup();gvarslast:='list.vars;return w end;
  106. symbolic procedure groebnervars(w,v);
  107. begin scalar z,dv,gdv,vars;
  108. if v='(list)then v:=nil;
  109. v:=v or(gdv:=cdr global!-dipvars!*)and global!-dipvars!*;
  110. vars:=
  111. if null v then
  112. for each j in gvarlis w collect !*a2k j
  113. else % test, if vars are really used
  114. <<z:=gvarlis w;
  115. groebnerzerobc setdiff(z,v:=groerevlist v);
  116. for each j in v do if member(j,z) then dv:=!*a2k j.dv;
  117. dv:=reversip dv;
  118. if not(length v=length dv)and !*trgroeb then
  119. <<prin2 " Groebner: ";
  120. prin2(length v-length dv);
  121. prin2t " of the variables not used";terpri()>>;dv>>;
  122. return gdv or vars end;
  123. symbolic procedure groebnerzerobc u;
  124. %'u'is the list of parameters in a Groebner job. Extract the
  125. % corresponding rules from !*match and powlis!*.
  126. if u then
  127. begin scalar w,m,p;
  128. bczerodivl!*:=nil;m:=!*match;!*match:=nil;p:=powlis!*;powlis!*:=nil;
  129. for each r in m do if cadr r='(nil.t)then
  130. <<w:=(numr simp{'difference,'times.for each q in car r collect
  131. {'expt,car q,cdr q}
  132. ,caddr r});
  133. for each x in kernels w do if not member(x,u)then w:=nil;
  134. if w then bczerodivl!*:=w.bczerodivl!*>>;
  135. for each r in p do if member(car r,u)and caddr r='(nil.t)then
  136. <<w:=(numr simp{'difference,{'expt,car r,cadr r},cadddr r});
  137. bczerodivl!*:=w.bczerodivl!*>>;
  138. for each r in asymplis!* do if member(car r,u)then
  139. bczerodivl!*:=(r .* 1 .+ nil).bczerodivl!*;
  140. !*match:=m;powlis!*:=p end;
  141. % Hier
  142. symbolic procedure gvarlis u;
  143. % Finds variables(kernels)in the list of expressions u.
  144. sort(gvarlis1(u,nil),function ordop);
  145. symbolic procedure gvarlis1(u,v);
  146. if null u then v else union(gvar1(car u,v),gvarlis1(cdr u,v));
  147. symbolic procedure gvar1(u,v);
  148. if null u or numberp u or(u eq'i and !*complex)then v
  149. else if atom u then if u member v then v else u.v
  150. else if get(car u,'dname)then v
  151. else if car u memq'(plus times expt difference minus)
  152. then gvarlis1(cdr u,v)
  153. else if car u eq'quotient then gvar1(cadr u,v)
  154. else if u member v then v else u.v;
  155. symbolic procedure groebidq(u,f,v);
  156. % Ideal quotient.'u'is a list of expressions(gbasis),'f'a polynomial
  157. % and'v'a list of variables or nil.
  158. begin scalar vars,w,np,oldorder,!*factor,!*exp;integer pcount!*;
  159. !*exp:=t;
  160. w:=for each j in groerevlist u
  161. collect if eqexpr j then !*eqn2a j else j;
  162. if null w then rerror(groebner,5,"empty list in idealquotient");
  163. if eqexpr f then f:=!*eqn2a f;
  164. vars:=groebnervars(w,v);
  165. groedomainmode();
  166. if null vars then vdperr'idealquotient;
  167. oldorder:=vdpinit vars;
  168. % Cancel common denominators.
  169. w:=for each j in w collect numr simp j;
  170. f:=numr simp f;
  171. w:=for each j in w collect f2vdp j;
  172. f:=f2vdp f;% Now do the conversions.
  173. if not !*vdpinteger then
  174. <<np:=t;
  175. for each p in f.w do
  176. np:=if np then vdpcoeffcientsfromdomain!? p else nil;
  177. if not np then <<!*vdpmodular:= nil;!*vdpinteger:=t>> >>;
  178. w:=groebidq2(w,f);
  179. w:='list.for each j in w collect vdp2a j;
  180. setkorder oldorder;
  181. return w end;
  182. fluid'(!*backtrace);
  183. symbolic procedure vdperr name;
  184. % Case that no variables were found.
  185. <<prin2 "**** Groebner illegal parmeter in ";prin2 name;
  186. if !*backtrace then backtrace();
  187. rerror(groebner,6,",e.g. no relevant variables found")>>;
  188. symbolic procedure groeparams(u,nmin,nmax);
  189. %'u'is a list of psopfn-parameters;they are given to reval and
  190. % the number of parameters is controlled to be between nmin,nmax;
  191. % result is the list of evaluated parameters padded with nils.
  192. begin scalar n,w;n:=length u;
  193. if n < nmin or n > nmax then rerror(groebner,7,
  194. "illegal number of parameters in call to groebner package");
  195. u:=for each v in u collect
  196. <<w:=reval v;
  197. if eqcar(w,'list)then'list.groerevlist w else w>>;
  198. while length u < nmax do u:=append(u,'(nil));return u end;
  199. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  200. %
  201. % Initialization of the distributive polynomial arithmetic.
  202. %
  203. symbolic procedure vdpinit vars;
  204. begin scalar r,gm;
  205. % Eventually set up module basis.
  206. if eqcar(gmodule,'list)and cdr gmodule then
  207. gm:=for each y in cdr gmodule collect
  208. <<y:=reval y;if not member(y,vars)then vars:=append(vars,{y});y>>;
  209. r:=vdpinit2 vars;
  210. % Convert an eventual module basis.
  211. gmodule!*:=if gm then vdpevlmon a2vdp('times.gm);return r end;
  212. symbolic procedure groedomainmode();
  213. <<!*vdpinteger:=!*vdpmodular:=nil;
  214. if not flagp(dmode!*,'field)then !*vdpinteger:=t
  215. else if !*modular then !*vdpmodular:=t>>;
  216. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  217. %
  218. % Some lisp functions which are not member of standard lisp.
  219. %
  220. symbolic procedure groedeletip(a,b);
  221. begin scalar q;
  222. while b and a=car b do b:=cdr b;if null b then return nil;
  223. q:=b;
  224. while cdr b do if a=cadr b then cdr b:=cddr b else b:=cdr b;
  225. return q end;
  226. symbolic procedure groerevlist u;
  227. <<if idp u then u:=reval u;for each p in getrlist u collect reval p>>;
  228. endmodule;;end;