Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defun p* (p1 p2)
- "Multiplies the two given polynomials together."
- (mapcan #'(lambda (x)
- (mapcan #'(lambda (y)
- (list (cons (* (car x) (car y)) (concatenate 'list (cdr x) (cdr y))))
- ) p1)
- ) p2)
- )
- (defun merge-variables (input &optional output)
- "Recursively collects terms given the original input"
- ;(print input)
- ;(print output)
- (if (null input)
- (return-from merge-variables output)) ; input is exhausted, so return output
- (merge-variables
- (mapcan #'(lambda (x)
- (if (not (equal (caar input) (car x)))
- (list x) )
- ) input) ; new input list will contain all variables not equal to the current variable.
- (check-for-zero-exponential (list (caar input)
- (apply #'+ (mapcan #'(lambda (x)
- (if (equal (caar input) (car x))
- (cdr x))
- ) input))) output) ; current variable (with accumulated exponents) will
- ; be appended to the output list (providing its exponent is non-zero).
- )
- )
- (defun check-for-zero-exponential (variable-with-expo output)
- "If exponent is non-zero, append to output. Otherwise, returns the 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 evaluate-polynomial (p)
- (mapcar #'(lambda (x)
- ;(print x)
- (cons (car x) (merge-variables (cdr x))) ; cons original coefficient onto simplified list of variables for that term
- ) p)
- )
- (equal '((4 (X 5) (X 2)) (4 (X 5) (Y 1))) (p* '((2 (X 2)) (2 (Y 1))) '((2 (X 5))))) ; evaluates to true
- (evaluate-polynomial (p* '((2 (X 2)) (2 (Y 1))) '((2 (X 5))))) ; evalutes to ((4 (X 7)) (4 (X 7) (Y 1))) (incorrect)
- (evaluate-polynomial '((4 (X 5) (X 2)) (4 (X 5) (Y 1)))) ; evaluates to ((4 (X 7)) (4 (X 5) (Y 1))) (correct)
Add Comment
Please, Sign In to add comment