ineq.red 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. module ineq; % Inequalities and linear optimization.
  2. % Author: Herbert Melenk <melenk@zib.de>
  3. % Driver for solving inequalities and inequality systems.
  4. % Implemented methods:
  5. %
  6. % - linear multivariate system
  7. % - polynomial/rational univariate inequality and system
  8. % version 2: Jul 2003 Adaptation of the actual REDUCE language stand.
  9. % Return an isolated equation if only one inequality is
  10. % entered.
  11. % Common algebraic interface:
  12. %
  13. % ineq_solve(<ineq/ineqlist> [,<variable/variablelist>])
  14. create!-package('(ineq linineq liqsimp1 liqsimp2 polineq),'(solve));
  15. load!-package'solve; % Some routines from solve are needed.
  16. fluid'(solvemethods!*);
  17. if not memq('ineqseval,solvemethods!*) then
  18. solvemethods!*:='ineqseval!*!*.SOlvemethods!*;
  19. if not get('geq,'simpfn) then
  20. <<mkop'leq; mkop'geq; mkop'lessp; mkop'greaterp>>;
  21. if not get('!*interval!*,'simpfn) then
  22. <<mkop'!*interval!*;infix !*interval!*;
  23. put('!*interval!*,'prtch," .. ")>>;
  24. symbolic procedure ineqseval!*!* u;
  25. % Interface to solve.
  26. (if null w then nil
  27. else if w='(failed) then if smemql('(leq geq lessp greaterp),u)
  28. then w else nil else w)where w=ineqseval u;
  29. symbolic procedure ineqseval!* u;
  30. % Interface to ineq_solve.
  31. (if null w or w='(failed) then car u else w)where w=ineqseval u;
  32. put('ineq_solve,'psopfn,'ineqseval!*);
  33. symbolic procedure ineqseval u;
  34. begin scalar s,s1,v,v1,l,w1,w2,err,ineqp,str;
  35. integer n;
  36. s:=reval car u;
  37. s:=if eqcar(s,'list) then cdr s else {s};
  38. if cdr u then
  39. <<v:=reval cadr u;v:=if eqcar(v,'list) then cdr v else {v}>>else
  40. u:=append(u,{ggvars s});
  41. % test for linearity, collect variables.
  42. l:=t;
  43. s1:=for each q in s join if not err then
  44. <<if atom q or not memq(car q,'(leq geq lessp greaterp equal))
  45. then err:=t else
  46. <<if not(car q eq'equal) then ineqp:=t;
  47. n:=n#+1;
  48. str:=str or memq(car q,'(lessp greaterp));
  49. w1:=simp cadr q; w2:=simp caddr q;
  50. v1:=union(v1,solvevars{w1,w2});
  51. if not domainp denr w1 or not domainp denr w2 then l:=nil;
  52. {numr w1,denr w1,numr w2,denr w2}>>>>;
  53. if err or not ineqp then return nil;
  54. if null v then v:=v1;
  55. l:=l and not nonlnrsys(s1,v);
  56. if length v1 > length v or not subsetp(v,v1) or not l and cdr v1 then
  57. return'(failed); % Too many indeterminates in inequality system;
  58. if l and str then
  59. return'(failed); % No strict linear system.
  60. u:=if l then linineqseval u else polineqeval u;
  61. if null cdr u then u:={'list} else if null cddr u then u:=cadr u;
  62. return u end;
  63. symbolic procedure ggvars s;
  64. begin scalar v;
  65. for each u in s do v:=ggvars1(u,v);
  66. if v then(v:=if null cdr v then car v else 'list.v);
  67. return v end;
  68. symbolic procedure ggvars1(u,v);
  69. if not atom u and car u member '(leq geq lessp greaterp equal)
  70. then ggvars2(cadr u,ggvars2(caddr u,v))
  71. else nil;
  72. symbolic procedure ggvars2(u,v);
  73. if null u or numberp u or(u eq'i and !*complex)then v
  74. else if atom u then if u member v then v else u.v
  75. else if car u memq'(plus times expt difference minus quotient)
  76. then ggvars3(cdr u,v)
  77. else if u member v then v else u.v;
  78. symbolic procedure ggvars3(u,v);
  79. if null u then v else ggvars3(cdr u,ggvars2(car u,v));
  80. endmodule;
  81. end;