daily pastebin goal
20%
SHARE
TWEET

Untitled

a guest May 16th, 2018 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defun p+ (p1 p2)
  2.     (evaluate-polynomial (concatenate 'list p1 p2)))  
  3. (defun p- (p1 p2)
  4.   "Negates the second given polynomial from the first."
  5.     (p+ p1 (mapcar #'(lambda (x) (cons (- (simplify-coefficient (cons 1 x))) (remove-coefficients x))) p2)))            
  6. (defun p* (p1 p2 &optional output)
  7.   (if (null p1)
  8.     (return-from p* (evaluate-polynomial output)))
  9.   (return-from p* (p* (cdr p1) p2 (concatenate 'list output (mapcar #'(lambda (x)
  10.        (cons (* (simplify-coefficient (cons 1 x)) (simplify-coefficient (cons 1 (car p1))))
  11.               ; cons the product of the two polynomial term coefficients and...
  12.               (concatenate 'list (remove-coefficients x) (remove-coefficients (car p1))))                                                            
  13.    ) p2)))))
  14. (defun p= (p1 p2)
  15.   (if (not (eq (list-length (evaluate-polynomial p1)) (list-length (evaluate-polynomial p2))))
  16.     (return-from p= NIL)) ; if evaluated polynomial's lengths are not equal, they themselves cannot be equal
  17.   (check-intersection (evaluate-polynomial p1) (evaluate-polynomial p2)))
  18. (defun check-intersection (p1 p2)
  19.   (if (null p1)
  20.     (return-from check-intersection t))
  21.   (mapcar #'(lambda (x)
  22.      (if (and (eq (list-length x) (list-length (car p1)))
  23.               (eq (list-length x) (list-length (intersection (car p1) x :test 'equal))))
  24.         (return-from check-intersection (check-intersection (cdr p1) p2)))
  25.     ) p2)
  26.   (return-from check-intersection NIL))
  27. (defun merge-variables (input &optional output)
  28.   (if (null input)
  29.     (return-from merge-variables output)) ; input is exhausted, so return output
  30.   (merge-variables
  31.         (remove-nils (mapcar #'(lambda (x)
  32.            (if (not (eq (caar input) (car x)))
  33.              (list x))) input))
  34.         (check-for-zero-exponential (list (caar input)
  35.           (add-function (remove-nils (mapcar #'(lambda (x)                                  
  36.             (if (eq (caar input) (car x))
  37.              (cdr x))) input)))) output)))
  38. (defun remove-nils (input &optional output)
  39.   (if (null input)
  40.     (return-from remove-nils (reverse output)))
  41.   (if (not (null (car input)))
  42.     (return-from remove-nils (remove-nils (cdr input) (concatenate 'list (car input) output)))
  43.     (return-from remove-nils (remove-nils (cdr input) output)))
  44.   )
  45.  
  46. (defun add-function (p &optional result)
  47.   (if (null p)
  48.     (return-from add-function result)) ; input term exhausted, return result
  49.   (if (null result)
  50.     (add-function (cdr p) (+ (car p) 0))
  51.     (add-function (cdr p) (+ (car p) result))))
  52.  
  53. (defun check-for-zero-exponential (variable-with-expo output)
  54.   (if (eq 0 (cadr variable-with-expo))
  55.     (return-from check-for-zero-exponential output)
  56.     (return-from check-for-zero-exponential (concatenate 'list output (list variable-with-expo)))))
  57. (defun group-terms (input &optional output)
  58.   (if (null input)
  59.     (return-from group-terms output)) ; input exhausted, so return output
  60.   (group-terms
  61.     (remove-nils (mapcar #'(lambda (x)  
  62.       (if (not (and (eq (list-length (cdar input)) (list-length (cdr x)))
  63.                     (eq (list-length (cdar input)) (list-length (intersection (cdar input) (cdr x) :test 'equal)))))
  64.         (list x))    
  65.     ) input))
  66.     (check-for-zero-coefficient
  67.       (cons (add-function (remove-nils (mapcar #'(lambda (y)
  68.       (if (and (eq (list-length (cdar input)) (list-length (cdr y)))
  69.                (eq (list-length (cdar input)) (list-length (intersection (cdar input) (cdr y) :test 'equal))))
  70.         (list (car y)))    
  71.       ) input))) (cdar input)) output)
  72.    ))
  73. (defun check-for-zero-coefficient (term output)
  74.   (if (eq 0 (car term))
  75.     (return-from check-for-zero-coefficient output)
  76.     (return-from check-for-zero-coefficient (concatenate 'list output (list term))))
  77.   )
  78. (defun simplify-coefficient (term)
  79.   (apply #'* (remove-nils (mapcar #'(lambda (x)
  80.         (if (numberp x) ; if only a number, must be a coefficient and not a variable
  81.           (list x))) term))))
  82. (defun remove-coefficients (term)
  83.   (remove-nils (mapcar #'(lambda (x)
  84.         (if (listp x)
  85.           (list x))) term)))
  86. (defun evaluate-polynomial (p)
  87.   (group-terms (mapcar #'(lambda (x)
  88.        (cons (simplify-coefficient (cons 1 x)) (merge-variables (remove-coefficients x)))
  89.     ) p)))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top