Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defun d/dx (expr)
- "Derives expr with respect to x"
- (cond ((consp expr)
- (destructuring-bind (key &rest args) expr
- (case key
- (+ (cons '+ (loop for i in args collect (d/dx i))))
- (* (independent-factors args))
- (expt
- (destructuring-bind (base expt &aux (base-x-p (eql base :x)) (expt-x-p (eql expt :x))) args
- (cond ((and base-x-p expt-x-p) '(* (expt :x :x) (1+ (log :x))))
- ((and base-x-p (not expt-x-p)) (power-law args))
- ((and (not base-x-p) expt-x-p) (rule-of-exponents args))
- (t (cons 'expt args))))))))
- ((eql expr :x) 1)
- (t 0)))
- (defun power-law (list)
- "Derives (x^n) into (nx^(n − 1))"
- (destructuring-bind (base expt) list
- (cond ((equal expt 2) `(* 2 ,base))
- ((equal expt 1) 1)
- ((numberp expt) `(* ,expt (expt ,base ,(1- expt))))
- (t `(* ,expt (expt ,base (1- ,expt)))))))
- (defun rule-of-exponents (list)
- "Derives (n^x) into (n^x ln n)"
- (destructuring-bind (base expt) list
- (if (eql base :e)
- (cons 'expt list)
- `(* (expt ,base ,expt) (log ,base)))))
- (defun constant-rule (number)
- (declare (ignore number))
- "All constants become zero."
- 0)
- (defun independent-factors (list-of-multiplicands)
- "All constant multipliers can be left alone."
- (cons '* (loop for i in list-of-multiplicands
- collect (cond ((numberp i) i)
- ((and (listp i)
- (eql (car i) 'expt)) (d/dx i))
- (t 1)))))
Advertisement
Add Comment
Please, Sign In to add comment