home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / sci / math / symbolic / 3045 < prev    next >
Encoding:
Internet Message Format  |  1992-11-19  |  10.3 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!ames!network.ucsd.edu!sdcc12!oba!mstankus
  2. From: mstankus@oba.ucsd.edu (Mark Stankus)
  3. Newsgroups: sci.math.symbolic
  4. Subject: Re: Mathematica Package for system of ineqaulities?
  5. Keywords: :Mathematica, system of ineqaulities
  6. Message-ID: <41270@sdcc12.ucsd.edu>
  7. Date: 19 Nov 92 20:02:02 GMT
  8. References: <1992Nov19.034155.10743@cs.cornell.edu>
  9. Sender: news@sdcc12.ucsd.edu
  10. Organization: Mathematics @ UCSD
  11. Lines: 303
  12. Nntp-Posting-Host: oba.ucsd.edu
  13.  
  14. In article <1992Nov19.034155.10743@cs.cornell.edu> ho@cs.cornell.edu (Pei-Hsin Ho) writes:
  15. >Hello! Is there a package in Mathematica that simplifies and/or solves system ofinequalities? If anyone knows anything about this, please let me know.
  16. >
  17. >Thanks a lot!
  18. >
  19. >..Pei-Hsin
  20.  
  21. I have written the following. It only works for >= and <= and
  22. it is smart enough to deduce m+n >= 2 if you tell it that 
  23. m>= 1 and n>= 1. It is not smart enough to deduce as much 
  24. as I would hope. 
  25.  
  26. The call to Convert2 converts an expr==0 into a corrsponding 
  27. rule. The code for Convert2 is tied up with another project 
  28. I am working on so I am not supplying it with this post. 
  29. BadCall[str,more stuff] gives an error message 
  30. and Abort's.
  31.  
  32. If you want the code which I am ommiting, please send me e-mail. 
  33.  
  34. Mark Stankus
  35.  
  36. P.S. This writing of the following code was partially supported
  37.      by the National Science Foundation.
  38.  
  39.      My attitude toward code development is GNUish. 
  40.      All comments/bug reports/code enhancements are 
  41.      welcome and appreciated.
  42.  
  43.  
  44. (* :Title:     Inequalites // Mathematica 1.2 and 2.0 *)
  45.  
  46. (* :Author:     Mark Stankus (mstankus). *)
  47.  
  48. (* :Context:     Inequalities` *)
  49.  
  50. (* :Summary:
  51. *)
  52.  
  53. (* :Alias:
  54. *)
  55.  
  56. (* :Warnings: 
  57. *)
  58.  
  59. (* :History: 
  60. *)
  61. BeginPackage["Inequalities`",
  62.        "Convert2`","Errors`"];
  63.  
  64. Clear[SetInequalityFactBase];
  65.  
  66. SetInequalityFactBase::usage = 
  67.      "SetInequalityFactBase[aList] allows for InequalityFactQ, \
  68.       BoundedQ,LowerBound,and UpperBound to take one parameter \
  69.       and aList is put in for the second parameter.";
  70.  
  71. Clear[InequalityFactQ];
  72.  
  73. InequalityFactQ::usage = 
  74.      "InequalityFactQ[eqn,aListOfFacts] gives True if the equation \
  75.       eqn can easily be deduced from the list of assumptions \
  76.       aListOfFacts eqn is a True inequality, False if it is False \
  77.       and unknown if the program algorithm cannot decide if it is \
  78.       True of False. Right now the code only works for GreaterEqual \
  79.       equations.";
  80.  
  81. Clear[InequalitySearch];
  82.  
  83. InequalitySearch::usage = 
  84.      "InequalitySearch[expr,aHead,aListOfFacts] searches \
  85.       through the list aListOfFacts to find all expressions \
  86.       gamma such that aHead[expr,gamma]===True. For example, \
  87.       InequalitySearch[a-b,GreaterEqual, \
  88.       {a>=2, a<= 7, b<=9, b>= -10,b<=10}] \
  89.       would give {-7,-8} (since 2-9 == -7 and 2-10 == -8). \
  90.       list. See also NumericalLeafs.";
  91.  
  92. Clear[InequalityToStandardForm];
  93.  
  94. InequalityToStandardForm::usage = 
  95.      "InequalityToStandardForm[eqn] takes an inequality eqn \
  96.       and changes it to a standardform. Here, standard form \
  97.       is defined in terms of the Convert2 algorithm. For \
  98.       example, InequalityToStandardForm[x-y <=0] is y>=x \
  99.       (becuase Convert2[x-y==0] is y->x and so the inequality \
  100.       is rearranged so that the left hand side is y).";
  101.  
  102. Clear[NumericalLeafs];
  103.  
  104. NumericalLeafs::usage = 
  105.      "NumericalLeafs[expr,aHead,aListOfFacts] evaluates \
  106.       NumericalLeafs[expr,aHead,aListOfFacts,40]; \
  107.       NumericalLeafs[expr,>=,aListOfFacts,n] tries to find \
  108.       all expressions >= 0. Since >= is transitive, this \
  109.       may require many calls to InequalitySearch. n gives \
  110.       the maximum number of times NumercalLeafs calls the \
  111.       InequalitySearch module.";
  112.  
  113. Clear[BoundedQ];
  114.  
  115. BoundedQ::usage = 
  116.      "BoundedQ[expr,aListOfFacts] returns True if the program \
  117.       can easily deduce from the list of assumptions given in \
  118.       aListOfFacts two inequalities m<= expr and expr<=n where m and \
  119.       n are numbers.";
  120.  
  121. Clear[LowerBound];
  122.  
  123. LowerBound::usage = 
  124.      "LowerBound[expr,aListOfFacts] returns the maximum number n such \
  125.       that InequalitySearch can deduce expr>=n. If no such \
  126.       inequality is generated by InequalitySearch, \
  127.       LowerBound[expr,aListOfFacts] is -Infinity.";
  128.  
  129. Clear[UpperBound];
  130.  
  131. UpperBound::usage = 
  132.      "UpperBound[expr,aListOfFacts] returns the minimum number n such \
  133.       that InequalitySearch can deduce n>=expr. If no such \
  134.       inequality is generated by InequalitySearch, \
  135.       UpperBound[expr,aListOfFacts] is Infinity.";
  136.  
  137. Begin["`Private`"];
  138.  
  139. InequalitiesDefault = {};
  140.  
  141. SetInequalityFactBase[aList_List] := InequalitiesDefault = Flatten[aList];
  142.  
  143. InequalityFactQ[x_] := InequalityFactQ[x,InequalitiesDefault];
  144. LowerBound[x_] := LowerBound[x,InequalitiesDefault];
  145. UpperBound[x_] := UpperBound[x,InequalitiesDefault];
  146. BoundedQ[x_] := BoundedQ[x,InequalitiesDefault];
  147.  
  148. InequalityFactQ[True,aListOfFacts_List] := True;
  149.  
  150. InequalityFactQ[False,aListOfFacts_List] := False;
  151.  
  152. InequalityFactQ[x_Equal,aListOfFacts_List] := InequalityFactQ[x,aListOfFacts] = 
  153. Module[{first,second,result},
  154.      first = x[[1]];
  155.      second = x[[2]];
  156.      result = And[InequalityFactQAux[first >= second,aListOfFacts],
  157.                   InequalityFactQAux[first <= second,aListOfFacts]
  158.                  ];
  159.      Return[result]
  160. ];
  161.  
  162. InequalityFactQ[x_,aListOfFacts_List] := 
  163.      InequalityFactQAux[x,aListOfFacts];
  164.  
  165. InequalityFactQ[x___] := BadCall["InequalityFactQ in Inequalities`",x];
  166.  
  167. InequalityFactQAux[anInequality_,aListOfFacts_List] := 
  168. Module[{theInequality,difference,list,temp,result},
  169.     theInequality = InequalityToStandardForm[anInequality];
  170.     difference = theInequality[[1]] - theInequality[[2]];
  171.     list = InequalitySearch[difference,Head[theInequality],aListOfFacts];
  172.     temp = Map[(InequalityFactQ[Head[theInequality][#,0]])&,list];
  173.     If[MemberQ[temp,True], result = True
  174.                          , result = unknown
  175.                          , result = unknown
  176.     ]; 
  177.     Return[result]
  178. ];
  179.  
  180. InequalityFactQAux[x___] := 
  181.       BadCall["InequalityFactQAux in Inequalities.m",x];
  182.  
  183. InequalitySearch[aSymbol_Symbol,aHead_,aListOfFacts_List] := 
  184.     Map[#[[2]]&,Select[aListOfFacts,(Head[#]==aHead && #[[1]] == aSymbol)&]];
  185.  
  186. InequalitySearch[x_ + y_,aHead_,aListOfFacts_List] := 
  187. Module[{temp,temp2,len1,len2,j,k,result},
  188.      temp = InequalitySearch[x,aHead,aListOfFacts];
  189.      temp2 = InequalitySearch[y,aHead,aListOfFacts];
  190.      len1 = Length[temp];
  191.      len2 = Length[temp2];
  192.      result = Table[ temp[[j]] + temp2[[k]]
  193.                     ,{j,1,len1},{k,1,len2}];
  194.      result = Union[Flatten[result]];
  195.      Return[result]
  196. ];
  197.  
  198. InequalitySearch[c_?NumberQ x_Symbol,aHead_,aListOfFacts_List] := 
  199. Module[{temp,result},
  200.      If[c > 0, temp = InequalitySearch[x,aHead,aListOfFacts]
  201.              , temp = InequalitySearch[x,Alternate[aHead],aListOfFacts]
  202.      ];
  203.      result = c temp;
  204.      result = Union[Flatten[result]];
  205.      Return[result]
  206. ]; 
  207.  
  208. Alternate[GreaterEqual] := LessEqual;
  209. Alternate[LessEqual] := GreaterEqual;
  210. Alternate[Less] := Greater;
  211. Alternate[Greater] := Less;
  212. Alternate[Equal] := Equal;
  213. Alternate[_] := "nothing";
  214. Alternate[x___] := BadCall["Alternate",x];
  215.  
  216. InequalitySearch[c_?NumberQ,GreaterEqual,aListOfFacts_List] := {c};
  217. InequalitySearch[c_?NumberQ,LessEqual,aListOfFacts_List] := {c};
  218. InequalitySearch[c_?NumberQ,Equal,aListOfFacts_List] := {c};
  219. InequalitySearch[c_?NumberQ,Less,aListOfFacts_List] := {Less};
  220. InequalitySearch[c_?NumberQ,Greater,aListOfFacts_List] := {Greater};
  221. InequalitySearch[Infinity,_,_List] := {};
  222. InequalitySearch[-Infinity,_,_List] := {};
  223.  
  224. InequalitySearch[x___] := BadCall["InequalitySearch",x];
  225.  
  226. InequalityToStandardForm[x_List] := Map[InequalityToStandardForm,x]; 
  227.  
  228. InequalityToStandardForm[True] := True;
  229.  
  230. InequalityToStandardForm[False] := False;
  231.  
  232. InequalityToStandardForm[x_] := 
  233. Module[{result,head,expr,ru,top,rest,leadcoeff},
  234.    result = {};
  235.    head = Head[x];
  236.    expr = x[[1]] - x[[2]];
  237.    ru =  Convert2[expr==0];
  238.    top = ru[[1]];
  239.    rest = expr/.{top->0};
  240.    leadcoeff = Expand[(expr - rest)/top];
  241. (* 
  242.      Now expr ==something top + rest
  243. *)   
  244.    If[Not[FreeQ[leadcoeff,top]], Abort[]];
  245.    If[Positive[leadcoeff], result = Apply[head,{ru[[1]],ru[[2]]}]];
  246.    If[Negative[leadcoeff], result = Apply[Alternate[head],
  247.                                           {ru[[1]],ru[[2]]}
  248.                                          ]
  249.    ];
  250.    If[result == {},result = Apply[head,{leadcoeff top, -rest}]];
  251.    Return[result]
  252. ];  
  253.  
  254. InequalityToStandardForm[x___] := BadCall["InequalityToStandardForm",x];
  255.  
  256. NumericalLeafs[expr_,aHead_,aListOfFacts_List] :=
  257.              NumericalLeafs[expr,aHead,aListOfFacts,40];
  258.  
  259. NumericalLeafs[expr_,aHead_,
  260.                aListOfFacts_List,NumberOfIterations_Integer?Positive] :=
  261. Module[{temp,j},
  262.      temp = Union[Flatten[{expr}]];
  263.      For[j=1,j<=NumberOfIterations && Not[ListOfNumbersQ[temp]],j++,
  264.          temp = Map[InequalitySearch[#,aHead,aListOfFacts]&,temp];
  265.          temp = Union[Flatten[temp]];
  266.      ];
  267.      Return[temp]
  268. ];
  269.  
  270. ListOfNumbersQ[{___?NumberQ}] := True;
  271.  
  272. ListOfNumbersQ[_] := False;
  273.  
  274. ListOfNumbersQ[x___] := BadCall["ListOfNumbersQ in Inequalities`",x];
  275.  
  276. BoundedQ[expr_,aListOfFacts_List] :=  
  277.             TrueQ[And[LowerBound[expr,aListOfFacts] =!= -Infinity,
  278.                       UpperBound[expr,aListOfFacts] =!=  Infinity
  279.                      ]
  280.                  ];
  281.  
  282. LowerBound[expr_,aListOfFacts_List] := LowerBound[expr,aListOfFacts] =
  283. Module[{lowerlist,aListOfNumbers,result},
  284.     lowerlist = NumericalLeafs[expr,GreaterEqual,aListOfFacts];
  285.     If[Not[ListOfNumbersQ[lowerlist]], 
  286.            lowerlist = Sort[lowerlist];
  287.            Print["LowerBound has encountered symbols :-( ", lowerlist]
  288.     ];
  289.     aListOfNumbers = Select[lowerlist,NumberQ];
  290.     If[aListOfNumbers ==={}, result = -Infinity
  291.                            , result = Apply[Max,aListOfNumbers]
  292.     ];
  293.     Return[result]
  294. ];
  295.  
  296. UpperBound[expr_,aListOfFacts_List] := UpperBound[expr,aListOfFacts] = 
  297. Module[{upperlist,aListOfNumbers,result},
  298.     upperlist = NumericalLeafs[expr,LessEqual,aListOfFacts];
  299.     If[Not[ListOfNumbersQ[upperlist]], 
  300.            upperlist = Sort[upperlist];
  301.            Print["UpperBound has encountered symbols :-( ", upperlist]
  302.     ];
  303.     aListOfNumbers = Select[upperlist,NumberQ];
  304.     If[aListOfNumbers ==={}, result = Infinity
  305.                            , result = Apply[Min,aListOfNumbers]
  306.     ];
  307.     result = Apply[Min,aListOfNumbers];
  308.     Return[result]
  309. ];
  310.  
  311.  
  312. End[];
  313. EndPackage[]
  314. -- 
  315. mstankus
  316. mstankus@oba 
  317.