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.

source: branches/f4grobner/.junk/fast-add.lisp@ 4541

Last change on this file since 4541 was 4471, checked in by Marek Rychlik, 9 years ago
File size: 2.0 KB
Line 
1(in-package "POLYNOMIAL")
2
3(defun fast-and-risky-add (p q order-fn add-fn &aux result result-last)
4 (when (and p q (eq p q)) (warn "FAST-AND-RISKY-ADD: ~S is EQ to ~S" p q))
5 (flet ((add-to-result (x)
6 (assert (consp x))
7 (setf (cdr x) nil)
8 (if (endp result)
9 (setf result x
10 result-last x)
11 (setf (cdr result-last) x
12 result-last (cdr result-last)))))
13 (loop
14 ;;TODO: Eliminate this debugging code
15 #+nil
16 (progn
17 (format t "~V@{~C~:*~}~%" 80 #\-)
18 (format t "RESULT: ~S~%" (mapcar #'->list result))
19 (format t "RESULT-LAST: ~S~%" (mapcar #'->list result-last))
20 (format t "P: ~S~%" (mapcar #'->list p))
21 (format t "Q: ~S~%" (mapcar #'->list q)))
22 (cond
23 ((endp p) (unless (endp q) (add-to-result q)) (return result))
24 ((endp q) (unless (endp p) (add-to-result p)) (return result))
25 (t
26 (multiple-value-bind
27 (greater-p equal-p)
28 (funcall order-fn (car p) (car q))
29 (cond
30 (greater-p ; (> (car p) (car q))
31 (let ((tmp (cdr p)))
32 (add-to-result p)
33 (setf p tmp)))
34 (equal-p ; (= (car p)) (car q))
35 (let ((s (funcall add-fn (lc p) (lc q))))
36 (cond
37 ((universal-zerop s)
38 ;; Terms cancel, discard both
39 (setf p (cdr p)
40 q (cdr q)))
41 (t
42 ;; Terms do not cancel, store the
43 ;; sum of coefficients in (lc p)
44 (setf (lc p) s)
45 (let ((tmp (cdr p)))
46 (add-to-result p)
47 (setf p tmp
48 q (cdr q)))))))
49 (t ;(< (car p) (car q))
50 (let ((tmp (cdr q)))
51 (add-to-result q)
52 (setf q tmp))
53 ))))))))
54
55(defun fast-add (p q order-fn add-fn)
56 (let ((r1 (slow-add p q order-fn add-fn))
57 (r2 (fast-and-risky-add p q order-fn add-fn)))
58 (unless (every #'universal-equalp r1 r2)
59 (format t "~&Error in addition of ~%~A~% and~%~A~% should be:~%" (mapcar #'->list p) (mapcar #'->list q))
60 (princ (mapcar #'->list r1))
61 (format t "~&Actually is:~%")
62 (princ (mapcar #'->list r2))
63 )
64 r2))
65
66
67
Note: See TracBrowser for help on using the repository browser.