Commits

Robert Smith committed b2b53a1

add a TERM data structure, COPY-POLY, POLY-ADD-TERM

Comments (0)

Files changed (1)

 
 (defvar +zero+ #(0))
 
+(defstruct (term (:constructor term (coefficient exponent))
+                 (:predicate termp))
+  (coefficient 0 :type real)
+  (exponent 0 :type integer))
+
 (defun normalize-poly (p)
   "Normalize the polynomial P."
   (let ((last (position-if-not #'zerop p :from-end t))
       ((= (1- length) last) p)
       (t (subseq p 0 (1+ last))))))
 
+(defun copy-poly (p)
+  (copy-seq p))
+
 (defun degree (p)
   "Compute the degree of a polynomial P."
   (let ((p (normalize-poly p)))
   "Negate a polynomial P."
   (map 'vector (lambda (x) (- x)) p))
 
+(defun poly-add-term (p term)
+  (let ((coef (term-coefficient term))
+        (exponent (term-exponent term)))
+    (if (< exponent
+           (length p)) ; This is an optimization for non-normalized P
+        (let ((sum (copy-poly p)))
+          (setf (aref sum exponent)
+                (+ (aref sum exponent)
+                   coef))
+          sum)
+        (let ((sum (make-array (1+ exponent) :initial-element 0)))
+          (replace sum p)
+          (setf (aref sum exponent)
+                (+ (aref sum exponent)
+                   coef))
+          sum))))
+
 (defun poly-add (p q)
   (let ((l1 (length p))
         (l2 (length q)))