Package deriv ============================================================== %% This package defines type Expression, and %% some functions on expressions, including %% %% taking the derivative of an expression %% %% simplifying an expression %% %% The simplification is very rudimentary. ============================================================== ============================================================== export ============================================================== ============================================================== %% Expression ============================================================== %% The expression type is defined in the body of this package %% so that its representation is hidden from view. Here, %% we only advertise that the type exists. ============================================================== Species{--equal--} Expression interface %: An expression involves constants, an independent variable %: called xx and a collection of operations. The operations %: that are supported are %: %: xx The independent variable %: constant(c) A constant expression having value c %: A+B The sum of expressions A and B %Species ============================================================== %% Functions ============================================================== %% Here are the exported functions for expressions. Only %% these are public. ============================================================== Expect constant : Real -> Expression %: constant(r) is an expression that is the %: real number r. For example, constant(2.5) %: is the real number 2.5, as an expression. ; xx : Expression %: xx is the independent variable. ; + : (Expression, Expression) -> Expression %: a + b is the expression that is the sum of %: expressions a and b. ; deriv : Expression -> Expression %: deriv(e) is the derivative of e with %: respect to variable xx. ; simplify : Expression -> Expression %: simplify(e) is a (possibly) simplified %: form of expression e. ; substitute : Expression -> Expression -> Expression %: (substitute p e) produces the result of substituting %: p for each occurrence of xx in expression e, and %: then simplifying the result. ; $ : Expression -> String %Expect ======================================================== implementation ======================================================== ======================================================== %% Species Expression ======================================================== %% Here, type Expression type and its constructors %% and unconstructors are defined. We do not want the %% default definition of $, and instead define our own. ======================================================== Species{--equal,noDollar--} Expression = -------------------------------------------------- %% An expression can be a real constant. -------------------------------------------------- | constant(Real) -------------------------------------------------- %% An expression can be the independent variable xx. -------------------------------------------------- | xx -------------------------------------------------- %% An expression can be the sum of two expressions. -------------------------------------------------- | Expression + Expression %Species ======================================================== ======================================================== %% Expression Simplification ======================================================== ======================================================== %% simplifySum ======================================================== Define %: simplifySum(e) is a (possibly) simplified form %: of expression e. It is presumed that e is a sum. %: The following simplifications are employed. %: %: 0 + a = a a + 0 = a %: %: computation with constants. We replace %: constant(m) + constant(n) by constant(m+n). %: %: simplifySum only looks at e to see if it %: is exactly one of these forms. It does not %: look inside e for subexpressions that have these %: forms. ---------------------------------------------------- %% 0 + a = a ---------------------------------------------------- case simplifySum(constant(0) + ?a) = a ---------------------------------------------------- %% a + 0 = a ---------------------------------------------------- case simplifySum(?a + constant(0)) = a ---------------------------------------------------- %% constant arithmetic ---------------------------------------------------- case simplifySum(constant(?m) + constant(?n)) = constant(m+n) ---------------------------------------------------- %% Otherwise, don't simplify. Return a as is. ---------------------------------------------------- else simplifySum(?a) = a %Define ======================================================== %% simplify ======================================================== %% Function simplify does a full simplification, going into %% the structure of an expression looking for subexpressions %% that can be simplified. ======================================================== Define case simplify(constant(?r)) = constant(r) case simplify(xx) = xx case simplify(?a + ?b) = simplifySum(simplify(a) + simplify(b)) %Define ======================================================== %% Computing Derivatives ======================================================== ======================================================== %% rawDeriv ======================================================== Define %: rawDeriv(e) is the derivative of expression e with %: respect to variable xx, but it does not do any %: simplifications. It employs the rules for %: derivatives directly. ---------------------------------------------------- %% c' = 0. ---------------------------------------------------- case rawDeriv(constant(?)) = constant(0) ---------------------------------------------------- %% xx' = 1. ---------------------------------------------------- case rawDeriv(xx) = constant(1) ---------------------------------------------------- %% (a+b)' = a' + b'. ---------------------------------------------------- case rawDeriv(?a + ?b) = rawDeriv(a) + rawDeriv(b) %Define ======================================================== %% deriv ======================================================= %% Deriv takes the derivative with simplification. ======================================================= Define deriv(?a) = simplify(rawDeriv(a)). ======================================================== %% Substitution ======================================================== %% Subst performs substitution without simplification. %% Substitute does the simplification. ======================================================== Define case subst ? (constant(?c)) = constant(c) case subst ?p (xx) = p case subst ?p (?a + ?b) = subst p a + subst p b %Define Define substitute ?p ?e = simplify(subst p e). ======================================================== %% $ for Expressions ======================================================== %% Function $ is used to convert an expression to a string. %% It tries to make the expression look readable. ======================================================== Let case $(constant(?r)) = $r case $(xx) = "x" case $(?a + ?b) = concat["(", $a, "+", $b, ")"] %Let %Package