Previous: Expressions and Operations in CL under MAXIMA Up: Helpful Hints to MAXIMA Programmers Next: Testing and Debugging

A Complete Example

MAXIMA already has a solve command for solving certain algebraic equations. But for our example we will forget that and build a quadratic equation solver for MAXIMA.


;;  File name:   quadsolve.lsp

;; maxima example ;; defining a new facility called quadsolve

(in-package "MAXIMA") ;; always use this line at the beginning

(defmfun $quadsolve(eqn var) ;; top-level function (prog(a b c) (cond ( (or (not (atom var)) (not (setq eqn (quadp eqn var))) ) (merror "incorrect input to quadsolve %")) ) (desetq (a b c) eqn) (return (quadformula a b c)) ))

(defun quadp(eqn var) ;; test for quad poly (prog (a b c) (setq c (mcall '$substitute 0 var eqn)) (setq eqn (mcall '$diff eqn var)) (setq b (mcall '$substitute 0 var eqn)) (setq eqn (mcall '$diff eqn var)) (setq a (div (mcall '$substitute 0 var eqn) 2)) (cond ( (and (not (mzerop a)) (zerop (mcall '$diff eqn var)) ) (return (list a b c)) ) ) ))

(defun quadformula(a b c) ;; apply quadratic formula (setq c (power (addn (list (power b 2) (muln (list -4 a c) t)) t) (div 1 2) ) ) (list '(mlist) (div (addn (list (neg b) c) t) (muln (list 2 a) t)) (div (sub (neg b) c) (muln (list 2 a) t)) ) )

(defun mzerop(ex) (and (numberp ex) (zerop ex)) )

farrell@mcs.kent.edu