close
Warning:
Can't synchronize with repository "(default)" (The repository directory has changed, you should resynchronize the repository with: trac-admin $ENV repository resync '(default)'). Look in the Trac log for more information.
Line | |
---|
1 | (in-package "POLYNOMIAL")
|
---|
2 |
|
---|
3 | (defun fast-add (p q order-fn add-fn &aux result result-last)
|
---|
4 | (flet ((add-to-result (x)
|
---|
5 | (assert (consp x))
|
---|
6 | (setf (cdr x) nil)
|
---|
7 | (if (endp result)
|
---|
8 | (setf result x
|
---|
9 | result-last x)
|
---|
10 | (setf (cdr result-last) x
|
---|
11 | result-last (cdr result-last)))))
|
---|
12 | (loop
|
---|
13 | (cond
|
---|
14 | ((endp p) (unless (endp q) (add-to-result q)) (return result))
|
---|
15 | ((endp q) (unless (endp p) (add-to-result p)) (return result))
|
---|
16 | (t
|
---|
17 | (multiple-value-bind
|
---|
18 | (greater-p equal-p)
|
---|
19 | (funcall order-fn (car p) (car q))
|
---|
20 | (cond
|
---|
21 | (greater-p ; (> (car p) (car q))
|
---|
22 | (let ((tmp (cdr p)))
|
---|
23 | (add-to-result p)
|
---|
24 | (setf p tmp)))
|
---|
25 | (equal-p ; (= (car p)) (car q))
|
---|
26 | (let ((s (funcall add-fn (lc p) (lc q))))
|
---|
27 | (cond
|
---|
28 | ((universal-zerop s)
|
---|
29 | ;; Terms cancel, discard both
|
---|
30 | (setf p (cdr p)
|
---|
31 | q (cdr q)))
|
---|
32 | (t
|
---|
33 | ;; Terms do not cancel, store the
|
---|
34 | ;; sum of coefficients in (lc p)
|
---|
35 | (setf (lc p) s)
|
---|
36 | (let ((tmp (cdr p)))
|
---|
37 | (add-to-result p)
|
---|
38 | (setf p tmp
|
---|
39 | q (cdr q)))))))
|
---|
40 | (t ;(< (car p) (car q))
|
---|
41 | (let ((tmp (cdr q)))
|
---|
42 | (add-to-result q)
|
---|
43 | (setf q tmp))
|
---|
44 | ))))))))
|
---|
Note:
See
TracBrowser
for help on using the repository browser.
Download in other formats: