The calculation of derivatives has many uses. However, the calculation of derivatives can often be tedious. To make this faster, I've written the following program to make it faster.
Derivatives are commonly used to find the slope of the line tangent to a point on a curve. The definition of a derivative, as found using the limit process is as follows:
\[\frac{d}{dx}f(x) = \lim_{h \to 0} \frac{f(x + h) - f(x)}{h}\].
There are, however, a series of rules that allow one to calculate derivatives of functions much more quickly and easily. These rules are (definitions found with expansion definitions):
The Power Rule
The Chain Rule ($\ac{d}{dx}f(g(x))=f^′(g(x))⋅ g^′(x)$)
The Xerox Rule
The Multiplication Rule
The Quotient Rule
This program works in terms of expansion functions, and application tests. That is to say, there is a test to see if the expansion is valid for the given expression.
To be able to apply an expansion, you need to determine eligibility. To do this, you need an expression that matches on two things, function name and arity. To generate this, it takes an operation name and the arity. Based on the arity type ($= $> $\q$)it will construct a simple boolean statement in the format of $(nction = operator) ∧ (argument-count == arity)$,here $= is one of the above arity types.
(defun generate-match-expression (on arity &optional (type '=)) (check-type on symbol) (check-type type (member = > >=)) (check-type arity (integer 0)) (case type (= `(and (eq function ',on) (= arg-count ,arity))) (> `(and (eq function ',on) (> arg-count ,arity))) (>= `(and (eq function ',on) (>= arg-count ,arity)))))
To define an expansion requires just a bit of syntactic sugar in the form of the defexpansion
macro. This macro does 3 things, generate a test function, generate an expansion function and pushes the name of the expansion, the test function and the expansion function on to the rules list.
To generate the test function, it uses the match-expression generator and wraps it into a function taking two arguments, a function and a list of arguments to the function. The test is then made, acting as predicate function for whether or not the expansion is applicable.
To generate the expansion function, a series of expressions is used as the body of the function, with the function destructured to form the arguments.
(defmacro defexpansion (name (on arity &optional (type '=)) (&rest arguments) &body expansion) (let ((match-expression (generate-match-expression on arity type)) (test-name (symbolicate name '-test)) (expansion-name (symbolicate name '-expansion))) `(progn (defun ,test-name (function &rest arguments &aux (arg-count (length arguments))) ,match-expression) (defun ,expansion-name (,@arguments) ,@expansion) (setf (aget *rules* ',name) (make-rule :name ',name :test-function #',test-name :expansion-function #',expansion-name)) ',name)))
To allow for the use of expansions, you must be able to retrieve the correct one from the expansions list.
To do so, you need the second element of the list that is the (name test expansion)
for the rule. This is found by removing the expansions for which the test returns false for the given expression.
(defun get-expansion (expression) (rule-expansion-function (rest (first (remove-if-not #'(lambda (nte) (let ((test (rule-test-function (rest nte)))) (apply test expression))) ,*rules*)))))
One of the more important parts of the program is a way to store expansions. This is however, quite boring. It's just a global variable (*rules*
), containing a list of lists having the form of (name test-lambda expander-lambda)
.
(defstruct (rule (:type list)) name test-function expansion-function) (defvar *rules* '())
There are many rules for derivation of equations. These rules allow one to derive equations quickly and easily by matching equations up with relevant rules and applying those rules.
The derivatives of multiplication follows two rules, the Constant Multiple rule:
\[ \frac{d}{dx} cf(x) = c \cdot f^\prime(x) ,\]
which is a specialized version of the more generalized Product Rule:
\[ \frac{d}{dx} f(x) \cdot g(x) = f(x) \cdot g^\prime(x) + g(x) \cdot f^\prime(x) .\]
There are two forms of the Product Rule as implemented, both matching on the *
function, but taking a different number of arguments. The first takes 2 arguments, and is the main driver for derivation, following the two above rules. The second takes 3 or more, and modifies the arguments slightly so as to make it a derivative of two different equations.
(defexpansion mult/2 (* 2) (first second) (cond ((numberp first) `(* ,first ,(derive (if (listp second) second (list second))))) ((numberp second) `(* ,second ,(derive (if (listp first) first (list second))))) (t `(+ (* ,first ,(derive (if (listp second) second (list second)))) (* ,second ,(derive (if (listp first) first (list first)))))))) (defexpansion mult/3+ (* 3 >=) (first &rest rest) (derive `(* ,first ,(cons '* rest))))
Division follows the Quotient Rule, which is as follows:
\[ \frac{d}{dx} \frac{f(x)}{g(x)} = \frac{f^\prime(x) \cdot g(x) - g^\prime(x) \cdot f(x)}{(g(x))^2} .\]
The rule matches on the /
function, and takes 2 arguments, a numerator and a denominator, its expansion is as above.
(defexpansion div/2 (/ 2) (numerator denominator) `(/ (- (* ,numerator ,(derive (if (listp denominator) denominator (list denominator)))) (* ,denominator ,(derive (if (listp numerator) numerator (list numerator))))) (expt ,denominator 2)))
Addition and subtraction of functions in derivatives is simple, simply add or subtract the derivatives of the functions, as shown here:
\[ \frac{d}{dx} f_1(x) + f_2(x) + \cdots + f_n(x) = f_1^\prime(x) + f_2^\prime(x) + \cdots + f_n^\prime(x) \]
and here:
\[ \frac{d}{dx} f_1(x) - f_2(x) - \cdots - f_n(x) = f_1^\prime(x) - f_2^\prime(x) - \cdots - f_n^\prime(x) .\]
This is accomplished by matching on either +
or -
, and taking 2 or more arguments, deriving all of the passed in equations and applying the respective operation.
(defexpansion plus/2+ (+ 2 >=) (&rest clauses) `(+ ,@(map 'list #'(lambda (clause) (if (listp clause) (derive clause) (derive (list clause)))) clauses))) (defexpansion minus/2+ (- 2 >=) (&rest clauses) `(- ,@(map 'list #'(lambda (clause) (if (listp clause) (derive clause) (derive (list clause)))) clauses)))
The derivatives of exponential and logarithmic functions follow several rules. For $e$ $a$,he "Xerox" rule is used:
\[ \frac{d}{dx} e^x = e^x ,\]
and
\[ \frac{d}{dx} a^x = a^x \cdot \ln x .\]
Logarithmic functions follow the forms as shown:
\[ \frac{d}{dx} \ln x = \frac{x^\prime}{x} ,\]
and
\[ \frac{d}{dx} \log_b x = \frac{x^\prime}{\ln b \cdot x} .\]
However, equations of the form $x$ llow this form (The Power Rule):
\[ \frac{d}{dx} x^n = x^\prime \cdot n \cdot x^{n-1} .\]
The following rules match based on the appropriate Lisp functions and the number of arguments taken based on whether or not you are performing natural or unnatural operations.
(defexpansion exp/1 (exp 1) (expression) (if (listp expression) `(* (exp ,expression) ,(derive expression)) (if (numberp expression) 0 `(exp ,expression)))) (defexpansion expt/2 (expt 2) (base exponent) (if (numberp exponent) (if (listp base) `(* ,exponent (expt ,base ,(1- exponent)) ,(derive base)) `(* ,exponent (expt ,base ,(1- exponent)))) `(* (expt ,base ,exponent) (log ,base)))) (defexpansion log/1 (log 1) (expression) `(/ ,(derive (if (listp expression) expression (list expression))) ,expression)) (defexpansion log/2 (log 2) (number base) (declare (ignorable number base)) `(/ ,(derive (cons 'log number)) (* (log ,base) ,number)))
The derivation of trigonometric functions is simply the application of the chain rule. As such, each of the trig functions has a different derivative, as shown here:
\[ \frac{d}{dx} \sin x = x^\prime \cdot \cos x ,\]
\[ \frac{d}{dx} \cos x = x^\prime \cdot -\sin x ,\]
\[ \frac{d}{dx} \tan x = x^\prime \cdot \sec^2 x ,\]
\[ \frac{d}{dx} \csc x = x^\prime \cdot -\csc x \cdot \cot x ,\]
\[ \frac{d}{dx} \sec x = x^\prime \cdot \sec x \cdot \tan x ,\]
and
\[ \frac{d}{dx} \cot x = x^\prime \cdot -\csc^2 x .\]
These rules all match on their respective trig function and substitute as appropriate.
(defexpansion sin/1 (sin 1) (arg) `(* (cos ,arg) ,(derive (if (listp arg) arg (list arg))))) (defexpansion cos/1 (cos 1) (arg) `(* (- (sin ,arg)) ,(derive (if (listp arg) arg (list arg))))) (defexpansion tan/1 (tan 1) (arg) `(* (expt (sec ,arg) 2) ,(derive (if (listp arg) arg (list arg))))) (defexpansion csc/1 (csc 1) (arg) `(* (- (csc ,arg)) (cot ,arg) ,(derive (if (listp arg) arg (list arg))))) (defexpansion sec/1 (sec 1) (arg) `(* (sec ,arg) (tan ,arg) ,(derive (if (listp arg) arg (list arg))))) (defexpansion cot/1 (cot 1) (arg) `(* (- (expt (csc ,arg) 2)) ,(derive (if (listp arg) arg (list arg)))))
This function is probably the most important user-facing function in the package.
Derive takes a list, and based on the first element in the list, and the length of the list, it will do one of the following things:
Return 0, the derivative of a number is 0, except in certain cases listed above.
This is a variable. Return 1, $\ac{d}{dx}x=1$.
There is an expansion rule, use this to derive the equation.
Signal an error, equation was likely malformed.
(defun derive (function) (check-type function cons) (let ((op (first function))) (cond ((numberp op) 0) ((and (symbolp op) (= 1 (length function))) 1) (t (let ((expansion-function (get-expansion function))) (if (functionp expansion-function) (apply expansion-function (rest function)) (error "Undefined expansion: ~a" op)))))))
As Common Lisp does not have cosecant or secant functions, and they appear in the definitions of the derivatives of some trigonometric functions, I define them here as follows:
\[ \csc x = \frac{1}{\sin x} \]
\[ \sec x = \frac{1}{\cos x} \]
I also take the liberty of defining two macros, a define-equation-functions
macro and take-derivative
. The first defines two functions, one that is the original equation, and the second being the derivative of the original equation. The take-derivative
macro does simply that, but allows you to write the equation without having to quote it, providing a little bit of syntactic sugar.
(defun csc (x) "csc -- (csc x) Calculate the cosecant of x" (/ (sin x))) (defun sec (x) "sec -- (sec x) Calculate the secant of x" (/ (cos x))) (defmacro define-equation-functions (name variable equation) (let ((derivative-name (symbolicate 'd/d- variable '- name)) (derivative (derive equation))) `(progn (defun ,name (,variable) ,equation) (defun ,derivative-name (,variable) ,derivative)))) (defmacro take-derivative (equation) (let ((derivative (derive equation))) `',derivative))
Now that the functions, macros and rules are defined, it's time to put them together into a package. This package has only one dependency, Common Lisp itself, and exports the following five symbols: derive
, csc
, sec
, define-equation-functions
and take-derivative
.
;;;; derive.lisp ;;;; ;;;; Copyright (c) 2015 Samuel W. Flint <swflint@flintfam.org> (defpackage #:derive (:use #:cl #:com.informatimago.common-lisp.cesarum.list) (:import-from #:alexandria #:symbolicate) (:export :derive :csc :sec :define-equation-functions :take-derivative)) (in-package #:derive) ;;; "derive" goes here. <<expansion-storage>> <<expansion-retrieval>> <<match-expressions>> <<expansion-definition>> <<derivative-driver>> <<multiplication>> <<division>> <<addition-subtraction>> <<exponentials-logarithms>> <<trigonometrics>> <<misc-functions>> ;;; End derive