(* :Copyright: © 1993 by Stephan Kaufmann Permission is hereby granted to make copies of this file for any purpose other than direct profit, or as part of a commercial product, provided this copyright notice is left intact. Sale is prohibited. *) (* :Name: SKPackages`NonNegativeQ` *) (* :Author: Stephan Kaufmann Institute of Mechanics Swiss Federal Institute of Technology ETH Zentrum, HG F38.4 CH-8092 Zuerich Switzerland kaufmann@ifm.ethz.ch *) (* :Package Version: 2.2 *) (* :Mathematica Version: 2.2 *) (* :History: Changes in Version 2.2 (August 93): Completely rewritten in a more efficient way *) (* :Keywords: Positivity, Positive, Negative, NonNegative, NonNegativeQ *) (* :Requirements: none *) (* :Limitations: NonNegativeQ is not in any sense general. *) (* :Discussion: The Mathematica functions Positive, Negative and NonNegative evaluate for numbers only. They can be used to define properties of symbols but for combinations of such symbols, the properties are not evaluated any further. The function NonNegativeQ (defined in this package) tries to find out if the result can not be negative. In such cases, it returns True, otherwise False. *) (* :Summary: The function NonNegativeQ tries to find out if expressions can not be negative. *) If[$VersionNumber < 2.0, Print[ "This package requires Mathematica version 2.0 or higher."]] BeginPackage["SKPackages`NonNegativeQ`"]; Off[General::spell1]; NonNegativeQ::usage = "NonNegativeQ[expr] returns True if expr is obviously not negative and else False. Properties of symbols can be defined with the functions Positive, NonNegative and Negative (e.g.: x /: Positive[x] = True). Real symbols can be declared by definitions like a /: Im[a] = 0"; On[General::spell1]; Begin["`Private`"]; RealTest[x_?AtomQ] := True /; NonNegativeQ[x] RealTest[x_] := True /; Im[x] == 0 || Positive[x] || NonNegative[x] || Negative[x] RealTest[x_ + y_] := RealTest[x] && RealTest[y] RealTest[x_ y_] := RealTest[x] && RealTest[y] RealTest[x_^_Integer] := RealTest[x] NonNegativeQ[x_] := True /; Positive[x] || NonNegative[x] NonNegativeQ[x_?(!AtomQ[#]&)] := NNQ[Factor[x]] NonNegativeQ[x_] := False NNQ[x_ y_] := (NNQ[x] && NNQ[y]) || (NNQ[-x] && NNQ[-y]) /; x =!= -1 && y =!= -1 NNQ[x_ + y_] := NNQ[x] && NNQ[y] NNQ[x_^_?OddQ] := NNQ[x] /; RealTest[x] NNQ[-x_^_?OddQ] := NNQ[-x] /; RealTest[x] NNQ[x_^_?EvenQ] := True /; RealTest[x] NNQ[Abs[x_]] := True NNQ[Sqrt[x_]] := True /; RealTest[x] NNQ[Exp[x_]] := True /; RealTest[x] NNQ[-x_] := True /; Positive[-x] || NonNegative[-x] || Negative[x] NNQ[x_] := True /; Positive[x] || NonNegative[x] || Negative[-x] NNQ[x_?AtomQ] := NonNegativeQ[x] NNQ[x_] := False End[]; EndPackage[];