# lisp-random

committed b2b53a1

# polynomial.lisp

` `
` (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)))`
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.