Просмотр исходного кода

Add multiplication functionality

Matt Coles лет назад: 10
Родитель
Сommit
650203993f
1 измененных файлов с 48 добавлено и 2 удалено
  1. 48 2
      poly.cl

+ 48 - 2
poly.cl

6
 (defun poly- (poly1 poly2)
6
 (defun poly- (poly1 poly2)
7
   (add-terms (append poly1 (flip-multipliers poly2))))
7
   (add-terms (append poly1 (flip-multipliers poly2))))
8
 
8
 
9
+;; Multiply polynomials together
10
+(defun poly* (poly1 poly2)
11
+  (clear-zero (add-terms (multiply-terms poly1 poly2))))
12
+
9
 ;; Returns the variable component of a singular polynomial
13
 ;; Returns the variable component of a singular polynomial
10
 (defun variable-symbol (single-poly)
14
 (defun variable-symbol (single-poly)
11
   (car (car single-poly)))
15
   (car (car single-poly)))
42
 ;; Returns true if a polynomial contains only unique terms
46
 ;; Returns true if a polynomial contains only unique terms
43
 (defun poly-unique-terms? (poly)
47
 (defun poly-unique-terms? (poly)
44
   (cond ((equal poly nil) T)
48
   (cond ((equal poly nil) T)
45
-  (T (if (some #'(lambda (x) (equal (car x) (car (car poly)))) (cdr poly))
49
+  (T (if (some #'(lambda (x) (and (equal (flatten 
50
+                                                  (variable-symbol x))
51
+                                                (flatten 
52
+                                                  (variable-symbol 
53
+                                                    (car poly))))
54
+                              (equal (flatten (exponent x))
55
+                                     (flatten (exponent (car poly))))))
56
+               (cdr poly))
46
     nil
57
     nil
47
     (poly-unique-terms? (cdr poly))))))
58
     (poly-unique-terms? (cdr poly))))))
48
 
59
 
60
+;; Functional flatten from Rosetta Code: 
61
+;; http://rosettacode.org/wiki/Flatten_a_list#Common_Lisp
62
+(defun flatten (x &optional stack out)
63
+  (cond ((consp x) (flatten (rest x) (cons (first x) stack) out))
64
+        (x         (flatten (first stack) (rest stack) (cons x out)))
65
+        (stack     (flatten (first stack) (rest stack) out))
66
+        (t out)))
67
+
49
 ;; This function replaces a full list of nils with the replace-term, otherwise
68
 ;; This function replaces a full list of nils with the replace-term, otherwise
50
 ;; returns the list
69
 ;; returns the list
51
 (defun useful-replace-nil (addition-seq replace-term)
70
 (defun useful-replace-nil (addition-seq replace-term)
56
 ;; Removes all zeroes from the polynomial
75
 ;; Removes all zeroes from the polynomial
57
 (defun clear-zero (poly)
76
 (defun clear-zero (poly)
58
   (remove nil (map 'list #'(lambda (x)
77
   (remove nil (map 'list #'(lambda (x)
59
-                 (if (equal (multiplier x) 0) nil x)) poly)))
78
+                 (if (or (equal (multiplier x) 0) (equal (exponent x) 0)) nil x)) 
79
+                   poly)))
60
 
80
 
61
 ;; Recursively adds all the terms in the list
81
 ;; Recursively adds all the terms in the list
62
 (defun add-terms (poly)
82
 (defun add-terms (poly)
69
                        (term-addition (car poly) y))
89
                        (term-addition (car poly) y))
70
              (cdr poly)) (car poly))))))))
90
              (cdr poly)) (car poly))))))))
71
 
91
 
92
+(defun map-onto-poly (single-poly poly)
93
+  (map 'list #'(lambda (x) (multiply-singles single-poly x)) poly))
94
+
95
+(defun multiply-singles (single-poly1 single-poly2)
96
+  (cond
97
+    ((equal (variable-symbol single-poly1) (variable-symbol single-poly2))
98
+     (list (list (variable-symbol single-poly1) 
99
+                 (if (typep (exponent single-poly1) 'list)
100
+                   (map 'list #'+ (exponent single-poly1) 
101
+                        (exponent single-poly2))
102
+                   (+ (exponent single-poly1) (exponent single-poly2))))
103
+           (* (multiplier single-poly1) (multiplier single-poly2)))
104
+    )
105
+    (T
106
+      (list (list (list (variable-symbol single-poly1)
107
+                        (variable-symbol single-poly2))
108
+                  (list (exponent single-poly1)
109
+                        (exponent single-poly2)))
110
+            (* (multiplier single-poly1) (multiplier single-poly2))))))
111
+
112
+(defun multiply-terms (map-poly poly)
113
+  (cond
114
+    ((equal map-poly nil) nil)
115
+    (T (remove nil (append (map-onto-poly (car map-poly) poly) 
116
+               (multiply-terms (cdr map-poly) poly))))))
117
+
72
 ; (poly+ '(((x 2) 3)) '(((y 2) 4)))
118
 ; (poly+ '(((x 2) 3)) '(((y 2) 4)))
73
 ; (poly+ '(((x 2) 3) ((y 2) 3)) '(((y 2) 4)))
119
 ; (poly+ '(((x 2) 3) ((y 2) 3)) '(((y 2) 4)))