%% Name: *** %% Date: *** %% Tabs: *** Package derivative ============================================================== %% This package defines type Expression, and %% some functions on expressions, including %% %% taking the derivative of an expression %% %% simplifying an expression %% %% The simplification is 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. ============================================================== %: 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 Type Expression interface with ==, $ %Type ============================================================== %% derivativeX ============================================================== Exception derivativeX(Expression) "Unable to take the derivative of an expression" %Exception ============================================================== %% 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. ; derivative : 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. ; %Expect ======================================================== implementation ======================================================== ======================================================== %% Type 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. ======================================================== Type 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 with == %Type ======================================================== ======================================================== %% Expression Simplification ======================================================== ======================================================== %% simplifySum ======================================================== %% simplifySum(e) is a (possibly) simplified form %% of expression e, which is presumed to be 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. ======================================================== Define ---------------------------------------------------- %% constant arithmetic ---------------------------------------------------- case simplifySum(constant(m) + constant(n)) = constant(m+n) ---------------------------------------------------- %% 0 + a = a ---------------------------------------------------- case simplifySum(constant(0) + a) = a ---------------------------------------------------- %% a + 0 = a ---------------------------------------------------- case simplifySum(a + constant(0)) = a ---------------------------------------------------- %% 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 ======================================================== ======================================================== %% deriv ======================================================== %% deriv(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. ======================================================== Define ---------------------------------------------------- %% c' = 0. ---------------------------------------------------- case deriv(constant(?)) = constant(0) ---------------------------------------------------- %% xx' = 1. ---------------------------------------------------- case deriv(=xx) = constant(1) ---------------------------------------------------- %% (a+b)' = a' + b'. ---------------------------------------------------- case deriv(a + b) = deriv(a) + deriv(b) %Define ======================================================== %% derivative ======================================================= Define derivative(a) = simplify(deriv(a)). ======================================================== %% $ for Expressions ======================================================== %% Function $ is used to convert an expression to a string. %% It tries to make the expression look readable. ======================================================== Define case $(constant(r)) = $(r) case $(=xx) = "x" case $(a + b) = concat["(", $(a), "+", $(b), ")"] %Define %Package