|
|
@@ -1,24 +1,68 @@
|
|
|
1
|
+;; Add two polynomials together
|
|
1
|
2
|
(defun poly+ (poly1 poly2)
|
|
2
|
|
- (add-terms poly1 poly2))
|
|
|
3
|
+ (add-terms (append poly1 poly2) nil nil))
|
|
3
|
4
|
|
|
|
5
|
+;; Subtract one polynomial from another
|
|
|
6
|
+(defun poly- (poly1 poly2)
|
|
|
7
|
+ (add-terms (append poly1 (flip-multipliers poly2)) nil nil))
|
|
|
8
|
+
|
|
|
9
|
+;; Returns the variable component of a singular polynomial
|
|
4
|
10
|
(defun variable-symbol (single-poly)
|
|
5
|
|
- (car (car (car single-poly))))
|
|
|
11
|
+ (car (car single-poly)))
|
|
6
|
12
|
|
|
|
13
|
+;; Returns the exponent component of a singular polynomial
|
|
7
|
14
|
(defun exponent (single-poly)
|
|
8
|
|
- (cdr (car (car single-poly))))
|
|
|
15
|
+ (car (cdr (car single-poly))))
|
|
9
|
16
|
|
|
|
17
|
+;; Returns the multiplier component of a singular polynomial
|
|
10
|
18
|
(defun multiplier (single-poly)
|
|
11
|
|
- (cdr (car single-poly)))
|
|
|
19
|
+ (car (cdr single-poly)))
|
|
|
20
|
+
|
|
|
21
|
+;; Returns a polynomial with all the multipliers multiplied by -1
|
|
|
22
|
+(defun flip-multipliers (poly)
|
|
|
23
|
+ (map 'list #'(lambda (x)
|
|
|
24
|
+ (list (list (variable-symbol x) (exponent x))
|
|
|
25
|
+ (* (multiplier x) -1))) poly))
|
|
|
26
|
+
|
|
|
27
|
+;; Returns an added term if the addition is succesful, nil otherwise
|
|
|
28
|
+(defun term-addition (term1 term2)
|
|
|
29
|
+ (if (and (equal (variable-symbol term1) (variable-symbol term2))
|
|
|
30
|
+ (equal (exponent term1) (exponent term2)))
|
|
|
31
|
+ (list (list (variable-symbol term1) (exponent term1))
|
|
|
32
|
+ (+ (multiplier term1) (multiplier term2)))
|
|
|
33
|
+ nil))
|
|
|
34
|
+
|
|
|
35
|
+;; Returns the failed term if the addition fails, nil otherwise
|
|
|
36
|
+(defun term-failure (term1 term2)
|
|
|
37
|
+ (if (and (equal (variable-symbol term1) (variable-symbol term2))
|
|
|
38
|
+ (equal (exponent term1) (exponent term2)))
|
|
|
39
|
+ nil
|
|
|
40
|
+ term2))
|
|
|
41
|
+
|
|
|
42
|
+;; This function replaces a full list of nils with the replace-term, otherwise
|
|
|
43
|
+;; returns the list
|
|
|
44
|
+(defun useful-replace-nil (addition-seq replace-term)
|
|
|
45
|
+ (if (equal (remove nil addition-seq) nil)
|
|
|
46
|
+ (list replace-term)
|
|
|
47
|
+ addition-seq))
|
|
12
|
48
|
|
|
13
|
|
-(defun is-single-poly (var-poly)
|
|
14
|
|
- (if (equal (length var-poly) 1) T nil))
|
|
|
49
|
+;; Removes all zeroes from the polynomial
|
|
|
50
|
+(defun clear-zero (poly)
|
|
|
51
|
+ (remove nil (map 'list #'(lambda (x)
|
|
|
52
|
+ (if (equal (multiplier x) 0) nil x)) poly)))
|
|
15
|
53
|
|
|
16
|
|
-(defun add-terms (poly1 poly2)
|
|
17
|
|
- (if (equal (is-single-poly poly1) (is-single-poly poly2))
|
|
18
|
|
- (cons poly1 poly2)
|
|
19
|
|
- (progn
|
|
20
|
|
- )
|
|
21
|
|
- ))
|
|
|
54
|
+;; Recursively adds all the terms in the list
|
|
|
55
|
+(defun add-terms (poly orig_var curr_var)
|
|
|
56
|
+ (cond
|
|
|
57
|
+ ((and (equal orig_var curr_var) (not (equal orig_var nil))) (clear-zero poly))
|
|
|
58
|
+ (T (add-terms (remove nil (append (map 'list #'(lambda (x)
|
|
|
59
|
+ (term-failure (car poly) x))
|
|
|
60
|
+ (cdr poly))
|
|
|
61
|
+ (useful-replace-nil (map 'list #'(lambda (y)
|
|
|
62
|
+ (term-addition (car poly) y))
|
|
|
63
|
+ (cdr poly)) (car poly))))
|
|
|
64
|
+ (if (equal orig_var nil) (car (car poly)) orig_var)
|
|
|
65
|
+ (car (car (cdr poly)))))))
|
|
22
|
66
|
|
|
23
|
|
-(poly+ '(((x 2) 3)) '(((y 2) 4)))
|
|
|
67
|
+; (poly+ '(((x 2) 3)) '(((y 2) 4)))
|
|
24
|
68
|
; (poly+ '(((x 2) 3) ((y 2) 3)) '(((y 2) 4)))
|