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

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!usc!news.service.uci.edu!network.ucsd.edu!sdcc12!oba!mstankus
  2. From: mstankus@oba.ucsd.edu (Mark Stankus)
  3. Newsgroups: sci.math.symbolic
  4. Subject: Inequalites posting: Inequalities.m
  5. Message-ID: <41346@sdcc12.ucsd.edu>
  6. Date: 20 Nov 92 19:29:54 GMT
  7. References: <41345@sdcc12.ucsd.edu>
  8. Sender: news@sdcc12.ucsd.edu
  9. Organization: Mathematics @ UCSD
  10. Lines: 273
  11. Nntp-Posting-Host: oba.ucsd.edu
  12.  
  13. (* :Title:     Inequalites // Mathematica 1.2 and 2.0 *)
  14.  
  15. (* :Author:     Mark Stankus (mstankus). *)
  16.  
  17. (* :Context:     Inequalities` *)
  18.  
  19. (* :Summary:
  20. *)
  21.  
  22. (* :Alias:
  23. *)
  24.  
  25. (* :Warnings: 
  26. *)
  27.  
  28. (* :History: 
  29. *)
  30. BeginPackage["Inequalities`",
  31.        "Convert1`","Errors`"];
  32.  
  33. Clear[SetInequalityFactBase];
  34.  
  35. SetInequalityFactBase::usage = 
  36.      "SetInequalityFactBase[aList] allows for InequalityFactQ, \
  37.       BoundedQ,LowerBound,and UpperBound to take one parameter \
  38.       and aList is put in for the second parameter.";
  39.  
  40. Clear[InequalityFactQ];
  41.  
  42. InequalityFactQ::usage = 
  43.      "InequalityFactQ[eqn,aListOfFacts] gives True if the equation \
  44.       eqn can easily be deduced from the list of assumptions \
  45.       aListOfFacts eqn is a True inequality, False if it is False \
  46.       and unknown if the program algorithm cannot decide if it is \
  47.       True of False. Right now the code only works for GreaterEqual \
  48.       equations.";
  49.  
  50. Clear[InequalitySearch];
  51.  
  52. InequalitySearch::usage = 
  53.      "InequalitySearch[expr,aHead,aListOfFacts] searches \
  54.       through the list aListOfFacts to find all expressions \
  55.       gamma such that aHead[expr,gamma]===True. For example, \
  56.       InequalitySearch[a-b,GreaterEqual, \
  57.       {a>=2, a<= 7, b<=9, b>= -10,b<=10}] \
  58.       would give {-7,-8} (since 2-9 == -7 and 2-10 == -8). \
  59.       list. See also NumericalLeafs.";
  60.  
  61. Clear[InequalityToStandardForm];
  62.  
  63. InequalityToStandardForm::usage = 
  64.      "InequalityToStandardForm[eqn] takes an inequality eqn \
  65.       and changes it to a standardform. Here, standard form \
  66.       is defined in terms of the Convert1 algorithm. For \
  67.       example, InequalityToStandardForm[x-y <=0] is y>=x \
  68.       (becuase Convert1[x-y==0] is y->x and so the inequality \
  69.       is rearranged so that the left hand side is y).";
  70.  
  71. Clear[NumericalLeafs];
  72.  
  73. NumericalLeafs::usage = 
  74.      "NumericalLeafs[expr,aHead,aListOfFacts] evaluates \
  75.       NumericalLeafs[expr,aHead,aListOfFacts,40]; \
  76.       NumericalLeafs[expr,>=,aListOfFacts,n] tries to find \
  77.       all expressions >= 0. Since >= is transitive, this \
  78.       may require many calls to InequalitySearch. n gives \
  79.       the maximum number of times NumercalLeafs calls the \
  80.       InequalitySearch module.";
  81.  
  82. Clear[BoundedQ];
  83.  
  84. BoundedQ::usage = 
  85.      "BoundedQ[expr,aListOfFacts] returns True if the program \
  86.       can easily deduce from the list of assumptions given in \
  87.       aListOfFacts two inequalities m<= expr and expr<=n where m and \
  88.       n are numbers.";
  89.  
  90. Clear[LowerBound];
  91.  
  92. LowerBound::usage = 
  93.      "LowerBound[expr,aListOfFacts] returns the maximum number n such \
  94.       that InequalitySearch can deduce expr>=n. If no such \
  95.       inequality is generated by InequalitySearch, \
  96.       LowerBound[expr,aListOfFacts] is -Infinity.";
  97.  
  98. Clear[UpperBound];
  99.  
  100. UpperBound::usage = 
  101.      "UpperBound[expr,aListOfFacts] returns the minimum number n such \
  102.       that InequalitySearch can deduce n>=expr. If no such \
  103.       inequality is generated by InequalitySearch, \
  104.       UpperBound[expr,aListOfFacts] is Infinity.";
  105.  
  106. Begin["`Private`"];
  107.  
  108. InequalitiesDefault = {};
  109.  
  110. SetInequalityFactBase[aList_List] := InequalitiesDefault = Flatten[aList];
  111.  
  112. InequalityFactQ[x_] := InequalityFactQ[x,InequalitiesDefault];
  113. LowerBound[x_] := LowerBound[x,InequalitiesDefault];
  114. UpperBound[x_] := UpperBound[x,InequalitiesDefault];
  115. BoundedQ[x_] := BoundedQ[x,InequalitiesDefault];
  116.  
  117. InequalityFactQ[True,aListOfFacts_List] := True;
  118.  
  119. InequalityFactQ[False,aListOfFacts_List] := False;
  120.  
  121. InequalityFactQ[x_Equal,aListOfFacts_List] := InequalityFactQ[x,aListOfFacts] = 
  122. Module[{first,second,result},
  123.      first = x[[1]];
  124.      second = x[[2]];
  125.      result = And[InequalityFactQAux[first >= second,aListOfFacts],
  126.                   InequalityFactQAux[first <= second,aListOfFacts]
  127.                  ];
  128.      Return[result]
  129. ];
  130.  
  131. InequalityFactQ[x_,aListOfFacts_List] := 
  132.      InequalityFactQAux[x,aListOfFacts];
  133.  
  134. InequalityFactQ[x___] := BadCall["InequalityFactQ in Inequalities`",x];
  135.  
  136. InequalityFactQAux[anInequality_,aListOfFacts_List] := 
  137. Module[{theInequality,difference,list,temp,result},
  138.     theInequality = InequalityToStandardForm[anInequality];
  139.     difference = theInequality[[1]] - theInequality[[2]];
  140.     list = InequalitySearch[difference,Head[theInequality],aListOfFacts];
  141.     temp = Map[(InequalityFactQ[Head[theInequality][#,0]])&,list];
  142.     If[MemberQ[temp,True], result = True
  143.                          , result = unknown
  144.                          , result = unknown
  145.     ]; 
  146.     Return[result]
  147. ];
  148.  
  149. InequalityFactQAux[x___] := 
  150.       BadCall["InequalityFactQAux in Inequalities.m",x];
  151.  
  152. InequalitySearch[aSymbol_Symbol,aHead_,aListOfFacts_List] := 
  153.     Map[#[[2]]&,Select[aListOfFacts,(Head[#]==aHead && #[[1]] == aSymbol)&]];
  154.  
  155. InequalitySearch[x_ + y_,aHead_,aListOfFacts_List] := 
  156. Module[{temp,temp2,len1,len2,j,k,result},
  157.      temp = InequalitySearch[x,aHead,aListOfFacts];
  158.      temp2 = InequalitySearch[y,aHead,aListOfFacts];
  159.      len1 = Length[temp];
  160.      len2 = Length[temp2];
  161.      result = Table[ temp[[j]] + temp2[[k]]
  162.                     ,{j,1,len1},{k,1,len2}];
  163.      result = Union[Flatten[result]];
  164.      Return[result]
  165. ];
  166.  
  167. InequalitySearch[c_?NumberQ x_Symbol,aHead_,aListOfFacts_List] := 
  168. Module[{temp,result},
  169.      If[c > 0, temp = InequalitySearch[x,aHead,aListOfFacts]
  170.              , temp = InequalitySearch[x,Alternate[aHead],aListOfFacts]
  171.      ];
  172.      result = c temp;
  173.      result = Union[Flatten[result]];
  174.      Return[result]
  175. ]; 
  176.  
  177. Alternate[GreaterEqual] := LessEqual;
  178. Alternate[LessEqual] := GreaterEqual;
  179. Alternate[Less] := Greater;
  180. Alternate[Greater] := Less;
  181. Alternate[Equal] := Equal;
  182. Alternate[_] := "nothing";
  183. Alternate[x___] := BadCall["Alternate",x];
  184.  
  185. InequalitySearch[c_?NumberQ,GreaterEqual,aListOfFacts_List] := {c};
  186. InequalitySearch[c_?NumberQ,LessEqual,aListOfFacts_List] := {c};
  187. InequalitySearch[c_?NumberQ,Equal,aListOfFacts_List] := {c};
  188. InequalitySearch[c_?NumberQ,Less,aListOfFacts_List] := {Less};
  189. InequalitySearch[c_?NumberQ,Greater,aListOfFacts_List] := {Greater};
  190. InequalitySearch[Infinity,_,_List] := {};
  191. InequalitySearch[-Infinity,_,_List] := {};
  192.  
  193. InequalitySearch[x___] := BadCall["InequalitySearch",x];
  194.  
  195. InequalityToStandardForm[x_List] := Map[InequalityToStandardForm,x]; 
  196.  
  197. InequalityToStandardForm[True] := True;
  198.  
  199. InequalityToStandardForm[False] := False;
  200.  
  201. InequalityToStandardForm[x_] := 
  202. Module[{result,head,expr,ru,top,rest,leadcoeff},
  203.    result = {};
  204.    head = Head[x];
  205.    expr = x[[1]] - x[[2]];
  206.    ru =  Convert1[expr==0];
  207.    top = ru[[1]];
  208.    rest = expr/.{top->0};
  209.    leadcoeff = Expand[(expr - rest)/top];
  210. (* 
  211.      Now expr ==something top + rest
  212. *)   
  213.    If[Not[FreeQ[leadcoeff,top]], Abort[]];
  214.    If[Positive[leadcoeff], result = Apply[head,{ru[[1]],ru[[2]]}]];
  215.    If[Negative[leadcoeff], result = Apply[Alternate[head],
  216.                                           {ru[[1]],ru[[2]]}
  217.                                          ]
  218.    ];
  219.    If[result == {},result = Apply[head,{leadcoeff top, -rest}]];
  220.    Return[result]
  221. ];  
  222.  
  223. InequalityToStandardForm[x___] := BadCall["InequalityToStandardForm",x];
  224.  
  225. NumericalLeafs[expr_,aHead_,aListOfFacts_List] :=
  226.              NumericalLeafs[expr,aHead,aListOfFacts,40];
  227.  
  228. NumericalLeafs[expr_,aHead_,
  229.                aListOfFacts_List,NumberOfIterations_Integer?Positive] :=
  230. Module[{temp,j},
  231.      temp = Union[Flatten[{expr}]];
  232.      For[j=1,j<=NumberOfIterations && Not[ListOfNumbersQ[temp]],j++,
  233.          temp = Map[InequalitySearch[#,aHead,aListOfFacts]&,temp];
  234.          temp = Union[Flatten[temp]];
  235.      ];
  236.      Return[temp]
  237. ];
  238.  
  239. ListOfNumbersQ[{___?NumberQ}] := True;
  240.  
  241. ListOfNumbersQ[_] := False;
  242.  
  243. ListOfNumbersQ[x___] := BadCall["ListOfNumbersQ in Inequalities`",x];
  244.  
  245. BoundedQ[expr_,aListOfFacts_List] :=  
  246.             TrueQ[And[LowerBound[expr,aListOfFacts] =!= -Infinity,
  247.                       UpperBound[expr,aListOfFacts] =!=  Infinity
  248.                      ]
  249.                  ];
  250.  
  251. LowerBound[expr_,aListOfFacts_List] := LowerBound[expr,aListOfFacts] =
  252. Module[{lowerlist,aListOfNumbers,result},
  253.     lowerlist = NumericalLeafs[expr,GreaterEqual,aListOfFacts];
  254.     If[Not[ListOfNumbersQ[lowerlist]], 
  255.            lowerlist = Sort[lowerlist];
  256.            Print["LowerBound has encountered symbols :-( ", lowerlist]
  257.     ];
  258.     aListOfNumbers = Select[lowerlist,NumberQ];
  259.     If[aListOfNumbers ==={}, result = -Infinity
  260.                            , result = Apply[Max,aListOfNumbers]
  261.     ];
  262.     Return[result]
  263. ];
  264.  
  265. UpperBound[expr_,aListOfFacts_List] := UpperBound[expr,aListOfFacts] = 
  266. Module[{upperlist,aListOfNumbers,result},
  267.     upperlist = NumericalLeafs[expr,LessEqual,aListOfFacts];
  268.     If[Not[ListOfNumbersQ[upperlist]], 
  269.            upperlist = Sort[upperlist];
  270.            Print["UpperBound has encountered symbols :-( ", upperlist]
  271.     ];
  272.     aListOfNumbers = Select[upperlist,NumberQ];
  273.     If[aListOfNumbers ==={}, result = Infinity
  274.                            , result = Apply[Min,aListOfNumbers]
  275.     ];
  276.     result = Apply[Min,aListOfNumbers];
  277.     Return[result]
  278. ];
  279.  
  280.  
  281. End[];
  282. EndPackage[]
  283. -- 
  284. mstankus
  285. mstankus@oba 
  286.