Bladeren bron

Add multiplication functionality

Matt Coles 10 jaren geleden
bovenliggende
commit
650203993f
1 gewijzigde bestanden met toevoegingen van 48 en 2 verwijderingen
  1. 48 2
      poly.cl

+ 48 - 2
poly.cl

@@ -6,6 +6,10 @@
6 6
 (defun poly- (poly1 poly2)
7 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 13
 ;; Returns the variable component of a singular polynomial
10 14
 (defun variable-symbol (single-poly)
11 15
   (car (car single-poly)))
@@ -42,10 +46,25 @@
42 46
 ;; Returns true if a polynomial contains only unique terms
43 47
 (defun poly-unique-terms? (poly)
44 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 57
     nil
47 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 68
 ;; This function replaces a full list of nils with the replace-term, otherwise
50 69
 ;; returns the list
51 70
 (defun useful-replace-nil (addition-seq replace-term)
@@ -56,7 +75,8 @@
56 75
 ;; Removes all zeroes from the polynomial
57 76
 (defun clear-zero (poly)
58 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 81
 ;; Recursively adds all the terms in the list
62 82
 (defun add-terms (poly)
@@ -69,5 +89,31 @@
69 89
                        (term-addition (car poly) y))
70 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 118
 ; (poly+ '(((x 2) 3)) '(((y 2) 4)))
73 119
 ; (poly+ '(((x 2) 3) ((y 2) 3)) '(((y 2) 4)))