ineq.red 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. module ineq; % Inequalities and linear optimization.
  2. % Author: Herbert Melenk <melenk@zib-berlin.dbp.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. % Common algebraic interface:
  9. %
  10. % ineq_solve(<ineq/ineqlist> [,<variable/variablelist>])
  11. create!-package('(ineq linineq liqsimp1 liqsimp2 polineq),'(solve));
  12. load!-package 'solve; % Some routines from solve are needed.
  13. fluid '(solvemethods!*);
  14. if not memq('ineqseval,solvemethods!*) then
  15. solvemethods!* := 'ineqseval!*!* . solvemethods!*;
  16. if not get('geq,'simpfn) then
  17. <<mkop 'leq; mkop 'geq; mkop 'lessp; mkop 'greaterp>>;
  18. if not get('!*interval!*,'simpfn) then
  19. <<mkop '!*interval!*;
  20. infix !*interval!*;
  21. put('!*interval!*,'prtch," .. ")
  22. >>;
  23. symbolic procedure ineqseval!*!* u;
  24. % Interface to solve.
  25. (if null w then nil
  26. else if w='(failed) then if smemql('(leq geq lessp greaterp),u)
  27. then w else nil
  28. else w)
  29. where w=ineqseval u;
  30. symbolic procedure ineqseval!* u;
  31. % Interface to ineq_solve.
  32. (if null w or w='(failed) then car u else w)
  33. where w=ineqseval u;
  34. put('ineq_solve,'psopfn,'ineqseval!*);
  35. symbolic procedure ineqseval(u);
  36. begin scalar s,s1,v,v1,l,w1,w2,err,ineqp,str;
  37. integer n;
  38. s:=reval car u;
  39. s:=if eqcar(s,'list) then cdr s else {s};
  40. if cdr u then
  41. <<v:= reval cadr u;
  42. v:=if eqcar(v,'list) then cdr v else {v};
  43. >>;
  44. % test for linearity, collect variables.
  45. l:=t;
  46. s1:=for each q in s join if not err then
  47. <<if atom q or not memq(car q,'(leq geq lessp greaterp equal))
  48. then err:=t else
  49. << if not(car q eq 'equal) then ineqp := t;
  50. n:=n#+1;
  51. str := str or memq(car q,'(lessp greaterp));
  52. w1:=simp cadr q; w2:=simp caddr q;
  53. v1:=union(v1,solvevars{w1,w2});
  54. if not domainp denr w1 or not domainp denr w2 then l:=nil;
  55. {numr w1,denr w1,numr w2,denr w2}
  56. >> >>;
  57. if err or not ineqp then return nil;
  58. if null v then v:=v1;
  59. l := l and not nonlnrsys(s1,v);
  60. if length v1 > length v or not subsetp(v,v1) or not l and cdr v1 then
  61. return '(failed); % Too many indeterminates in inequality system;
  62. % if not l and n#>1 then
  63. % return '(failed); % Nonlinear system not implemented.
  64. if l and str then
  65. return '(failed); % No strict linear system.
  66. return if l then linineqseval u else polineqeval u;
  67. end;
  68. endmodule;
  69. end;