瀏覽代碼

Fully comment poly.cl and add new tests.cl with some testing

Matt Coles 10 年之前
父節點
當前提交
2c7b699d1b
共有 2 個文件被更改,包括 28 次插入3 次删除
  1. 4 3
      poly.cl
  2. 24 0
      tests.cl

+ 4 - 3
poly.cl

89
                        (term-addition (car poly) y))
89
                        (term-addition (car poly) y))
90
              (cdr poly)) (car poly))))))))
90
              (cdr poly)) (car poly))))))))
91
 
91
 
92
+;; Map a single polynomial onto each term of another polynomial
92
 (defun map-onto-poly (single-poly poly)
93
 (defun map-onto-poly (single-poly poly)
93
   (map 'list #'(lambda (x) (multiply-singles single-poly x)) poly))
94
   (map 'list #'(lambda (x) (multiply-singles single-poly x)) poly))
94
 
95
 
96
+;; Multiply two singular polynomials together
95
 (defun multiply-singles (single-poly1 single-poly2)
97
 (defun multiply-singles (single-poly1 single-poly2)
96
   (cond
98
   (cond
97
     ((equal (variable-symbol single-poly1) (variable-symbol single-poly2))
99
     ((equal (variable-symbol single-poly1) (variable-symbol single-poly2))
109
                         (exponent single-poly2)))
111
                         (exponent single-poly2)))
110
             (* (multiplier single-poly1) (multiplier single-poly2))))))
112
             (* (multiplier single-poly1) (multiplier single-poly2))))))
111
 
113
 
114
+;; Multiply two polynomials together, see the map-poly will be recursed
115
+;; and the poly will remain constant
112
 (defun multiply-terms (map-poly poly)
116
 (defun multiply-terms (map-poly poly)
113
   (cond
117
   (cond
114
     ((equal map-poly nil) nil)
118
     ((equal map-poly nil) nil)
115
     (T (remove nil (append (map-onto-poly (car map-poly) poly) 
119
     (T (remove nil (append (map-onto-poly (car map-poly) poly) 
116
                (multiply-terms (cdr map-poly) poly))))))
120
                (multiply-terms (cdr map-poly) poly))))))
117
-
118
-; (poly+ '(((x 2) 3)) '(((y 2) 4)))
119
-; (poly+ '(((x 2) 3) ((y 2) 3)) '(((y 2) 4)))

+ 24 - 0
tests.cl

1
+(require "poly.cl")
2
+
3
+(defun run-tests ()
4
+  (if (equal 
5
+        (poly+ '(((x 2) 5)) '(((x 2) 3)))
6
+        '(((x 2) 8)))
7
+    (format t "5x^2 + 3x^2 ... passed - simple addition~%")
8
+    (format t "5x^2 + 3x^2 ... failed - simple addition~%"))
9
+  (if (equal
10
+        (poly+ '(((x 2) 5) ((y 2) 6)) '(((x 2) 3) ((y 2) 3) ((z 3) 4)))
11
+        '(((z 3) 4) ((x 2) 8) ((y 2) 9)))
12
+    (format t "5x^2+6y^2 + 3x^2+3y^2+4z^3 ... passed - complex addition~%")
13
+    (format t "5x^2+6y^2 + 3x^2+3y^2+4z^3 ... failed - complex addition~%"))
14
+  (if (equal 
15
+        (poly- '(((x 2) 5)) '(((x 2) 3)))
16
+        '(((x 2) 2)))
17
+    (format t "5x^2 - 3x^2 ... passed - simple subtraction~%")
18
+    (format t "5x^2 - 3x^2 ... failed - simple subtraction~%"))
19
+  (if (equal
20
+        (poly- '(((x 2) 5) ((y 2) 6)) '(((x 2) 3) ((y 2) 3) ((z 3) 4)))
21
+        '(((z 3) -4) ((x 2) 2) ((y 2) 3)))
22
+    (format t "5x^2+6y^2 - 3x^2+3y^2+4z^3 ... passed - complex subtraction~%")
23
+    (format t "5x^2+6y^2 - 3x^2+3y^2+4z^3 ... failed - complex subtraction~%"))
24
+  )