Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defun p+ (p1 p2)
- (evaluate-polynomial (concatenate 'list p1 p2)))
- (defun p- (p1 p2)
- "Negates the second given polynomial from the first."
- (p+ p1 (mapcar #'(lambda (x) (cons (- (simplify-coefficient (cons 1 x))) (remove-coefficients x))) p2)))
- (defun p* (p1 p2 &optional output)
- (if (null p1)
- (return-from p* (evaluate-polynomial output)))
- (return-from p* (p* (cdr p1) p2 (concatenate 'list output (mapcar #'(lambda (x)
- (cons (* (simplify-coefficient (cons 1 x)) (simplify-coefficient (cons 1 (car p1))))
- ; cons the product of the two polynomial term coefficients and...
- (concatenate 'list (remove-coefficients x) (remove-coefficients (car p1))))
- ) p2)))))
- (defun p= (p1 p2)
- (if (not (eq (list-length (evaluate-polynomial p1)) (list-length (evaluate-polynomial p2))))
- (return-from p= NIL)) ; if evaluated polynomial's lengths are not equal, they themselves cannot be equal
- (check-intersection (evaluate-polynomial p1) (evaluate-polynomial p2)))
- (defun check-intersection (p1 p2)
- (if (null p1)
- (return-from check-intersection t))
- (mapcar #'(lambda (x)
- (if (and (eq (list-length x) (list-length (car p1)))
- (eq (list-length x) (list-length (intersection (car p1) x :test 'equal))))
- (return-from check-intersection (check-intersection (cdr p1) p2)))
- ) p2)
- (return-from check-intersection NIL))
- (defun merge-variables (input &optional output)
- (if (null input)
- (return-from merge-variables output)) ; input is exhausted, so return output
- (merge-variables
- (remove-nils (mapcar #'(lambda (x)
- (if (not (eq (caar input) (car x)))
- (list x))) input))
- (check-for-zero-exponential (list (caar input)
- (add-function (remove-nils (mapcar #'(lambda (x)
- (if (eq (caar input) (car x))
- (cdr x))) input)))) output)))
- (defun remove-nils (input &optional output)
- (if (null input)
- (return-from remove-nils (reverse output)))
- (if (not (null (car input)))
- (return-from remove-nils (remove-nils (cdr input) (concatenate 'list (car input) output)))
- (return-from remove-nils (remove-nils (cdr input) output)))
- )
- (defun add-function (p &optional result)
- (if (null p)
- (return-from add-function result)) ; input term exhausted, return result
- (if (null result)
- (add-function (cdr p) (+ (car p) 0))
- (add-function (cdr p) (+ (car p) result))))
- (defun check-for-zero-exponential (variable-with-expo output)
- (if (eq 0 (cadr variable-with-expo))
- (return-from check-for-zero-exponential output)
- (return-from check-for-zero-exponential (concatenate 'list output (list variable-with-expo)))))
- (defun group-terms (input &optional output)
- (if (null input)
- (return-from group-terms output)) ; input exhausted, so return output
- (group-terms
- (remove-nils (mapcar #'(lambda (x)
- (if (not (and (eq (list-length (cdar input)) (list-length (cdr x)))
- (eq (list-length (cdar input)) (list-length (intersection (cdar input) (cdr x) :test 'equal)))))
- (list x))
- ) input))
- (check-for-zero-coefficient
- (cons (add-function (remove-nils (mapcar #'(lambda (y)
- (if (and (eq (list-length (cdar input)) (list-length (cdr y)))
- (eq (list-length (cdar input)) (list-length (intersection (cdar input) (cdr y) :test 'equal))))
- (list (car y)))
- ) input))) (cdar input)) output)
- ))
- (defun check-for-zero-coefficient (term output)
- (if (eq 0 (car term))
- (return-from check-for-zero-coefficient output)
- (return-from check-for-zero-coefficient (concatenate 'list output (list term))))
- )
- (defun simplify-coefficient (term)
- (apply #'* (remove-nils (mapcar #'(lambda (x)
- (if (numberp x) ; if only a number, must be a coefficient and not a variable
- (list x))) term))))
- (defun remove-coefficients (term)
- (remove-nils (mapcar #'(lambda (x)
- (if (listp x)
- (list x))) term)))
- (defun evaluate-polynomial (p)
- (group-terms (mapcar #'(lambda (x)
- (cons (simplify-coefficient (cons 1 x)) (merge-variables (remove-coefficients x)))
- ) p)))
Add Comment
Please, Sign In to add comment