Simple Symbolic Differentiation

; Representation: formula is (op lhs rhs)
(defun op  (form) (car form))   ; operator
(defun lhs (form) (cadr form))  ; left-hand side
(defun rhs (form) (caddr form)) ; right-hand side

; Derivative of FORM with respect to VAR
; Example: (deriv '(* 5 x) 'x)
(defun deriv (form var)
  (cond ((atom form)
          (if (eq form var) 1 0))
        ((eq (op form) '+) (deriv+ form var))
        ((eq (op form) '*) (deriv* form var))
        (t (error "No deriv fn for ~S" form))) )

; Derivative of a sum, e.g. (+ u v)
; d/dx(u + v) = d/dx(u) + d/dx(v)
(defun deriv+ (form var)
  (s+ (deriv (lhs form) var)
      (deriv (rhs form) var)) )

; Symbolic + (trivial version)
(defun s+ (x y) (list '+ x y))

Contents    Page-10    Prev    Next    Page+10    Index