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 f-add (p q order-fn add-fn)
|
---|
4 | "Add two polynomials, P and Q, represented as lists of terms.
|
---|
5 | The operation is destructive to both polynomials, as the terms
|
---|
6 | of both lists are combined into the result. The operation does not
|
---|
7 | create any new instance of TERM."
|
---|
8 | (do (r)
|
---|
9 | ((or (endp p) (endp q))
|
---|
10 | ;; NOTE: R contains the result in reverse order. Can it
|
---|
11 | ;; be more efficient to produce the terms in correct order?
|
---|
12 | (unless (endp q)
|
---|
13 | (setf r (nreconc r q)))
|
---|
14 | (unless (endp p)
|
---|
15 | (setf r (nreconc r p)))
|
---|
16 | r)
|
---|
17 | (multiple-value-bind
|
---|
18 | (greater-p equal-p)
|
---|
19 | (funcall order-fn (car p) (car q))
|
---|
20 | (cond
|
---|
21 | (greater-p
|
---|
22 | (rotatef (cdr p) r p)
|
---|
23 | )
|
---|
24 | (equal-p
|
---|
25 | (let ((s (funcall add-fn (lc p) (lc q))))
|
---|
26 | (cond
|
---|
27 | ((universal-zerop s)
|
---|
28 | (setf p (cdr p))
|
---|
29 | )
|
---|
30 | (t
|
---|
31 | (setf (lc p) s)
|
---|
32 | (rotatef (cdr p) r p))))
|
---|
33 | (setf q (cdr q))
|
---|
34 | )
|
---|
35 | (t
|
---|
36 | (rotatef (cdr q) r q))))))
|
---|
37 |
|
---|
38 | (defun s-add (p q order-fn add-fn &aux result)
|
---|
39 | "Non-recursive version of SLOW-ADD."
|
---|
40 | (loop
|
---|
41 | (cond
|
---|
42 | ((endp p) (return-from s-add (nreconc result q)))
|
---|
43 | ((endp q) (return-from s-add (nreconc result p)))
|
---|
44 | (t
|
---|
45 | (multiple-value-bind
|
---|
46 | (greater-p equal-p)
|
---|
47 | (funcall order-fn (car p) (car q))
|
---|
48 | (cond
|
---|
49 | (greater-p ; (> (car p) (car q))
|
---|
50 | (push (pop p) result)
|
---|
51 | )
|
---|
52 | (equal-p ; (= (car p)) (car q))
|
---|
53 | (let ((s (funcall add-fn (lc p) (lc q))))
|
---|
54 | (cond
|
---|
55 | ((universal-zerop s)
|
---|
56 | (pop p))
|
---|
57 | (t
|
---|
58 | ;; Adjust the lc of p
|
---|
59 | (setf (lc p) s)
|
---|
60 | (push (pop p) result)
|
---|
61 | )
|
---|
62 | ))
|
---|
63 | (pop q)
|
---|
64 | )
|
---|
65 | (t ;(< (car p) (car q))
|
---|
66 | (push (pop q) result)
|
---|
67 | )
|
---|
68 | ))))))
|
---|
Note:
See
TracBrowser
for help on using the repository browser.
Download in other formats: