1 | #|
|
---|
2 | $Id: poly.lisp,v 1.6 2009/01/22 04:05:52 marek Exp $
|
---|
3 | *--------------------------------------------------------------------------*
|
---|
4 | | Copyright (C) 1994, Marek Rychlik (e-mail: rychlik@math.arizona.edu) |
|
---|
5 | | Department of Mathematics, University of Arizona, Tucson, AZ 85721 |
|
---|
6 | | |
|
---|
7 | | Everyone is permitted to copy, distribute and modify the code in this |
|
---|
8 | | directory, as long as this copyright note is preserved verbatim. |
|
---|
9 | *--------------------------------------------------------------------------*
|
---|
10 | |#
|
---|
11 | (defpackage "POLY"
|
---|
12 | (:export monom-times-poly scalar-times-poly term-times-poly
|
---|
13 | minus-poly poly+ poly- poly* poly-expt
|
---|
14 | sort-poly poly-op poly-constant-p poly-extend
|
---|
15 | poly-mexpt poly-extend-end
|
---|
16 | lt lm lc poly-zerop)
|
---|
17 | (:use "ORDER" "MONOM" "TERM" "COEFFICIENT-RING" "COMMON-LISP"))
|
---|
18 | (in-package "POLY")
|
---|
19 |
|
---|
20 | #+debug(proclaim '(optimize (speed 0) (debug 3)))
|
---|
21 | #-debug(proclaim '(optimize (speed 3) (debug 0)))
|
---|
22 |
|
---|
23 | ;;----------------------------------------------------------------
|
---|
24 | ;; BASIC OPERATIONS ON POLYNOMIALS of many variables
|
---|
25 | ;; Hybrid operations involving polynomials and monomials or terms
|
---|
26 | ;;----------------------------------------------------------------
|
---|
27 | ;; The representations are as follows:
|
---|
28 | ;;
|
---|
29 | ;; monom: (n1 n2 ... nk) where ni are non-negative integers
|
---|
30 | ;; term: (monom . coefficient)
|
---|
31 | ;; polynomial: (term1 term2 ... termk)
|
---|
32 | ;;
|
---|
33 | ;; The terms in a polynomial are assumed to be sorted in descending order by
|
---|
34 | ;; some admissible well-ordering, typically defaulting to the lexicographic
|
---|
35 | ;; order from the "order" package. Thus, the polynomials are represented
|
---|
36 | ;; non-recursively as alists.
|
---|
37 | ;;----------------------------------------------------------------
|
---|
38 | ;; EXAMPLES: Suppose that variables are x and y. Then
|
---|
39 | ;; Monom x*y^2 ---> (1 2)
|
---|
40 | ;; Term 3*x*y^2 ---> ((1 2) . 3)
|
---|
41 | ;; Polynomial x^2+x*y ---> (((2 0) . 1) ((1 1) . 1))
|
---|
42 | ;;----------------------------------------------------------------
|
---|
43 |
|
---|
44 |
|
---|
45 | (defun scalar-times-poly (c p &optional (ring *coefficient-ring*))
|
---|
46 | "Return product of a scalar C by a polynomial P with coefficient ring RING."
|
---|
47 | (unless (funcall (ring-zerop ring) c)
|
---|
48 | (mapcar
|
---|
49 | #'(lambda (term) (cons (car term) (funcall (ring-* ring) c (cdr term))))
|
---|
50 | p)))
|
---|
51 |
|
---|
52 | (defun term-times-poly (term f &optional (ring *coefficient-ring*))
|
---|
53 | "Return product of a term TERM by a polynomial F with coefficient ring RING."
|
---|
54 | (mapcar #'(lambda (x) (term* term x ring)) f))
|
---|
55 |
|
---|
56 | (defun monom-times-poly (m f)
|
---|
57 | "Return product of a monomial M by a polynomial F with coefficient ring RING."
|
---|
58 | (mapcar #'(lambda (x) (monom-times-term m x)) f))
|
---|
59 |
|
---|
60 | (defun minus-poly (f &optional (ring *coefficient-ring*))
|
---|
61 | "Changes the sign of a polynomial F with coefficients in coefficient ring
|
---|
62 | RING, and returns the result."
|
---|
63 | (mapcar #'(lambda (x) (cons (car x) (funcall (ring-- ring) (cdr x)))) f))
|
---|
64 |
|
---|
65 | (defun sort-poly (poly
|
---|
66 | &optional
|
---|
67 | (pred #'lex>)
|
---|
68 | (start 0)
|
---|
69 | (end (unless (null poly) ;zero
|
---|
70 | (length (caar poly)))))
|
---|
71 | "Destructively Sorts a polynomial POLY by predicate PRED; the predicate is assumed
|
---|
72 | to take arguments START and END in addition to the pair of
|
---|
73 | monomials, as the functions in the ORDER package do."
|
---|
74 | (sort poly #'(lambda (p q) (funcall pred p q :start start :end end))
|
---|
75 | :key #'first))
|
---|
76 |
|
---|
77 | (defun poly+ (p q &optional (pred #'lex>) (ring *coefficient-ring*))
|
---|
78 | "Returns the sum of two polynomials P and Q with coefficients in
|
---|
79 | ring RING, with terms ordered according to monomial order PRED."
|
---|
80 | (do (r)
|
---|
81 | (nil)
|
---|
82 | (cond
|
---|
83 | ((endp p) (return (nreconc r q)))
|
---|
84 | ((endp q) (return (nreconc r p)))
|
---|
85 | (t
|
---|
86 | (multiple-value-bind
|
---|
87 | (mgreater mequal)
|
---|
88 | (funcall pred (caar p) (caar q))
|
---|
89 | (cond
|
---|
90 | (mequal
|
---|
91 | (let ((s (funcall (ring-+ ring) (cdar p) (cdar q))))
|
---|
92 | (unless (funcall (ring-zerop ring) s) ;check for cancellation
|
---|
93 | (setf r (cons (cons (caar p) s) r)))
|
---|
94 | (setf p (cdr p) q (cdr q))))
|
---|
95 | (mgreater
|
---|
96 | (setf r (cons (car p) r)
|
---|
97 | p (cdr p)))
|
---|
98 | (t (setf r (cons (car q) r)
|
---|
99 | q (cdr q)))))))))
|
---|
100 |
|
---|
101 | (defun poly- (p q &optional (pred #'lex>) (ring *coefficient-ring*))
|
---|
102 | "Returns the difference of two polynomials P and Q with coefficients
|
---|
103 | in ring RING, with terms ordered according to monomial order PRED."
|
---|
104 | (do (r done)
|
---|
105 | (done r)
|
---|
106 | (cond
|
---|
107 | ((endp p) (return (revappend r (minus-poly q ring))))
|
---|
108 | ((endp q) (return (revappend r p)))
|
---|
109 | (t
|
---|
110 | (multiple-value-bind
|
---|
111 | (mgreater mequal)
|
---|
112 | (funcall pred (caar p) (caar q))
|
---|
113 | (cond
|
---|
114 | (mequal
|
---|
115 | (let ((s (funcall (ring-- ring) (cdar p) (cdar q))))
|
---|
116 | (unless (zerop s) ;check for cancellation
|
---|
117 | (setf r (cons (cons (caar p) s) r)))
|
---|
118 | (setf p (cdr p) q (cdr q))))
|
---|
119 | (mgreater
|
---|
120 | (setf r (cons (car p) r)
|
---|
121 | p (cdr p)))
|
---|
122 | (t (setf r (cons (cons (caar q) (funcall (ring-- ring) (cdar q))) r)
|
---|
123 | q (cdr q)))))))))
|
---|
124 |
|
---|
125 | ;; Multiplication of polynomials
|
---|
126 | ;; Non-destructive version
|
---|
127 | (defun poly* (p q &optional (pred #'lex>) (ring *coefficient-ring*))
|
---|
128 | "Returns the product of two polynomials P and Q with coefficients in
|
---|
129 | ring RING, with terms ordered according to monomial order PRED."
|
---|
130 | (cond ((or (endp p) (endp q)) nil) ;p or q is 0 (represented by NIL)
|
---|
131 | ;; If p=p0+p1 and q=q0+q1 then pq=p0q0+p0q1+p1q
|
---|
132 | (t (cons (cons (monom* (caar p) (caar q))
|
---|
133 | (funcall (ring-* ring) (cdar p) (cdar q)))
|
---|
134 | (poly+ (term-times-poly (car p) (cdr q) ring)
|
---|
135 | (poly* (cdr p) q pred ring)
|
---|
136 | pred ring)))))
|
---|
137 |
|
---|
138 | (defun poly-op (f m g pred ring)
|
---|
139 | "Returns F-M*G, where F and G are polynomials with coefficients in
|
---|
140 | ring RING, ordered according to monomial order PRED and M is a
|
---|
141 | monomial."
|
---|
142 | (poly- f (term-times-poly m g ring) pred ring))
|
---|
143 |
|
---|
144 | (defun poly-expt (poly n &optional (pred #'lex>) (ring *coefficient-ring*))
|
---|
145 | "Exponentiate a polynomial POLY to power N. The terms of the
|
---|
146 | polynomial are assumed to be ordered by monomial order PRED and with
|
---|
147 | coefficients in ring RING. Use the Chinese algorithm; assume N>=0 and
|
---|
148 | POLY is non-zero (not NIL)."
|
---|
149 | (labels ((poly-one ()
|
---|
150 | (list
|
---|
151 | (cons (make-list (length (caar poly)) :initial-element 0) (ring-unit ring)))))
|
---|
152 | (cond
|
---|
153 | ((minusp n) (error "Negative exponent."))
|
---|
154 | ((endp poly) (if (zerop n) (poly-one) nil))
|
---|
155 | (t
|
---|
156 | (do ((k 1 (ash k 1))
|
---|
157 | (q poly (poly* q q pred ring)) ;keep squaring
|
---|
158 | (p (poly-one) (if (not (zerop (logand k n))) (poly* p q pred ring) p)))
|
---|
159 | ((> k n) p))))))
|
---|
160 |
|
---|
161 | (defun poly-mexpt (plist monom &optional (pred #'lex>) (ring *coefficient-ring*))
|
---|
162 | "Raise a polynomial vector represented ad a list of polynomials
|
---|
163 | PLIST to power MULTIINDEX. Every polynomial has its terms ordered by
|
---|
164 | predicate PRED and coefficients in the ring RING."
|
---|
165 | (reduce
|
---|
166 | #'(lambda (u v) (poly* u v pred ring))
|
---|
167 | (mapcan #'(lambda (y i)
|
---|
168 | (cond ((endp y) (if (zerop i) nil (list nil)))
|
---|
169 | (t (list (poly-expt y i pred ring)))))
|
---|
170 | plist monom)))
|
---|
171 |
|
---|
172 | (defun poly-constant-p (p)
|
---|
173 | "Returns T if P is a constant polynomial."
|
---|
174 | (and (= (length p) 1)
|
---|
175 | (every #'zerop (caar p))))
|
---|
176 |
|
---|
177 |
|
---|
178 | (defun poly-extend (p &optional (m (list 0)))
|
---|
179 | "Given a polynomial P in k[x[r+1],...,xn], it returns the same
|
---|
180 | polynomial as an element of k[x1,...,xn], optionally multiplying it by
|
---|
181 | a monomial x1^m1*x2^m2*...*xr^mr, where m=(m1,m2,...,mr) is a
|
---|
182 | multiindex."
|
---|
183 | (mapcar #'(lambda (term) (cons (append m (car term)) (cdr term))) p))
|
---|
184 |
|
---|
185 | (defun poly-extend-end (p &optional (m (list 0)))
|
---|
186 | "Similar to POLY-EXTEND, but it adds new variables at the end."
|
---|
187 | (mapcar #'(lambda (term) (cons (append (car term) m) (cdr term))) p))
|
---|
188 |
|
---|
189 | (defun poly-zerop (p)
|
---|
190 | "Returns T if P is a zero polynomial."
|
---|
191 | (null p))
|
---|
192 |
|
---|
193 | (defun lt (p)
|
---|
194 | "Returns the leading term of a polynomial P."
|
---|
195 | #+debugging(assert (consp p))
|
---|
196 | (first p))
|
---|
197 |
|
---|
198 | (defun lm (p)
|
---|
199 | "Returns the leading monomial of a polynomial P."
|
---|
200 | #+debugging(assert (consp (lt p)))
|
---|
201 | (car (lt p)))
|
---|
202 |
|
---|
203 | (defun lc (p)
|
---|
204 | "Returns the leading coefficient of a polynomial P."
|
---|
205 | #+debugging(assert (consp (lt p)))
|
---|
206 | (cdr (lt p)))
|
---|
207 |
|
---|