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/polynomial.lisp@ 3282

Last change on this file since 3282 was 3282, checked in by Marek Rychlik, 9 years ago

* empty log message *

File size: 15.3 KB
Line 
1;;; -*- Mode: Lisp -*-
2;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3;;;
4;;; Copyright (C) 1999, 2002, 2009, 2015 Marek Rychlik <rychlik@u.arizona.edu>
5;;;
6;;; This program is free software; you can redistribute it and/or modify
7;;; it under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 2 of the License, or
9;;; (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19;;;
20;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21
22(defpackage "POLYNOMIAL"
23 (:use :cl :utils :ring :monom :order :term)
24 (:export "POLY"
25 "POLY-DIMENSION"
26 "POLY-TERMLIST"
27 "POLY-TERM-ORDER"
28 "CHANGE-TERM-ORDER"
29 "STANDARD-EXTENSION"
30 "STANDARD-EXTENSION-1"
31 "STANDARD-SUM"
32 "SATURATION-EXTENSION"
33 "ALIST->POLY")
34 (:documentation "Implements polynomials."))
35
36(in-package :polynomial)
37
38(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
39
40(defclass poly ()
41 ((dimension :initform nil
42 :initarg :dimension
43 :accessor poly-dimension
44 :documentation "Shared dimension of all terms, the number of variables")
45 (termlist :initform nil :initarg :termlist :accessor poly-termlist
46 :documentation "List of terms.")
47 (order :initform #'lex> :initarg :order :accessor poly-term-order
48 :documentation "Monomial/term order."))
49 (:default-initargs :dimension nil :termlist nil :order #'lex>)
50 (:documentation "A polynomial with a list of terms TERMLIST, ordered
51according to term order ORDER, which defaults to LEX>."))
52
53(defmethod print-object ((self poly) stream)
54 (print-unreadable-object (self stream :type t :identity t)
55 (with-accessors ((dimension poly-dimension)
56 (termlist poly-termlist)
57 (order poly-term-order))
58 self
59 (format stream "DIMENSION=~A TERMLIST=~A ORDER=~A"
60 dimension termlist order))))
61
62(defgeneric change-term-order (self other)
63 (:documentation "Change term order of SELF to the term order of OTHER.")
64 (:method ((self poly) (other poly))
65 (unless (eq (poly-term-order self) (poly-term-order other))
66 (setf (poly-termlist self) (sort (poly-termlist self) (poly-term-order other))
67 (poly-term-order self) (poly-term-order other)))
68 self))
69
70(defmethod update-instance-for-different-class ((old term) (new poly)
71 &key
72 &allow-other-keys)
73 (setf (poly-termlist new) (list (old))
74 (poly-dimension new) (monom-dimension old))
75 new)
76
77(defun alist->poly (alist &aux (poly (make-instance 'poly)))
78 "It reads polynomial from an alist formatted as ( ... (exponents . coeff) ...).
79It can be used to enter simple polynomials by hand, e.g the polynomial
80in two variables, X and Y, given in standard notation as:
81
82 3*X^2*Y^3+2*Y+7
83
84can be entered as
85(ALIST->POLY '(((2 3) . 3) ((0 1) . 2) ((0 0) . 7))).
86
87NOTE: The primary use is for low-level debugging of the package."
88 (dolist (x alist poly)
89 (insert-item poly (make-instance 'term :exponents (car x) :coeff (cdr x)))))
90
91
92(defmethod r-equalp ((self poly) (other poly))
93 "POLY instances are R-EQUALP if they have the same
94order and if all terms are R-EQUALP."
95 (and (every #'r-equalp (poly-termlist self) (poly-termlist other))
96 (eq (poly-term-order self) (poly-term-order other))))
97
98(defmethod insert-item ((self poly) (item term))
99 (cond ((null (poly-dimension self))
100 (setf (poly-dimension self) (monom-dimension item)))
101 (t (assert (= (poly-dimension self) (monom-dimension item)))))
102 (push item (poly-termlist self))
103 self)
104
105(defmethod append-item ((self poly) (item term))
106 (cond ((null (poly-dimension self))
107 (setf (poly-dimension self) (monom-dimension item)))
108 (t (assert (= (poly-dimension self) (monom-dimension item)))))
109 (setf (cdr (last (poly-termlist self))) (list item))
110 self)
111
112;; Leading term
113(defgeneric leading-term (object)
114 (:method ((self poly))
115 (car (poly-termlist self)))
116 (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
117
118;; Second term
119(defgeneric second-leading-term (object)
120 (:method ((self poly))
121 (cadar (poly-termlist self)))
122 (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
123
124;; Leading coefficient
125(defgeneric leading-coefficient (object)
126 (:method ((self poly))
127 (scalar-coeff (leading-term self)))
128 (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
129
130;; Second coefficient
131(defgeneric second-leading-coefficient (object)
132 (:method ((self poly))
133 (scalar-coeff (second-leading-term self)))
134 (:documentation "The second leading coefficient of a polynomial. It
135 signals error for a polynomial with at most one term."))
136
137;; Testing for a zero polynomial
138(defmethod r-zerop ((self poly))
139 (null (poly-termlist self)))
140
141;; The number of terms
142(defmethod r-length ((self poly))
143 (length (poly-termlist self)))
144
145(defmethod multiply-by ((self poly) (other monom))
146 (mapc #'(lambda (term) (multiply-by term other))
147 (poly-termlist self))
148 self)
149
150(defmethod multiply-by ((self poly) (other term))
151 (mapc #'(lambda (term) (multiply-by term other))
152 (poly-termlist self))
153 self)
154
155(defmethod multiply-by ((self poly) (other scalar))
156 (mapc #'(lambda (term) (multiply-by term other))
157 (poly-termlist self))
158 self)
159
160
161(defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
162 "Return an expression which will efficiently adds/subtracts two
163polynomials, P and Q. The addition/subtraction of coefficients is
164performed by calling ADD/SUBTRACT-METHOD-NAME. If UMINUS-METHOD-NAME
165is supplied, it is used to negate the coefficients of Q which do not
166have a corresponding coefficient in P. The code implements an
167efficient algorithm to add two polynomials represented as sorted lists
168of terms. The code destroys both arguments, reusing the terms to build
169the result."
170 `(macrolet ((lc (x) `(scalar-coeff (car ,x))))
171 (do ((p ,p)
172 (q ,q)
173 r)
174 ((or (endp p) (endp q))
175 ;; NOTE: R contains the result in reverse order. Can it
176 ;; be more efficient to produce the terms in correct order?
177 (unless (endp q)
178 ;; Upon subtraction, we must change the sign of
179 ;; all coefficients in q
180 ,@(when uminus-fn
181 `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
182 (setf r (nreconc r q)))
183 r)
184 (multiple-value-bind
185 (greater-p equal-p)
186 (funcall ,order-fn (car p) (car q))
187 (cond
188 (greater-p
189 (rotatef (cdr p) r p)
190 )
191 (equal-p
192 (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
193 (cond
194 ((r-zerop s)
195 (setf p (cdr p))
196 )
197 (t
198 (setf (lc p) s)
199 (rotatef (cdr p) r p))))
200 (setf q (cdr q))
201 )
202 (t
203 ;;Negate the term of Q if UMINUS provided, signallig
204 ;;that we are doing subtraction
205 ,(when uminus-fn
206 `(setf (lc q) (funcall ,uminus-fn (lc q))))
207 (rotatef (cdr q) r q)))))))
208
209
210(defmacro def-add/subtract-method (add/subtract-method-name
211 uminus-method-name
212 &optional
213 (doc-string nil doc-string-supplied-p))
214 "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
215 `(defmethod ,add/subtract-method-name ((self poly) (other poly))
216 ,@(when doc-string-supplied-p `(,doc-string))
217 ;; Ensure orders are compatible
218 (change-term-order other self)
219 (setf (poly-termlist self) (fast-add/subtract
220 (poly-termlist self) (poly-termlist other)
221 (poly-term-order self)
222 #',add/subtract-method-name
223 ,(when uminus-method-name `(function ,uminus-method-name))))
224 self))
225
226(eval-when (:compile-toplevel :load-toplevel :execute)
227
228 (def-add/subtract-method add-to nil
229 "Adds to polynomial SELF another polynomial OTHER.
230This operation destructively modifies both polynomials.
231The result is stored in SELF. This implementation does
232no consing, entirely reusing the sells of SELF and OTHER.")
233
234 (def-add/subtract-method subtract-from unary-minus
235 "Subtracts from polynomial SELF another polynomial OTHER.
236This operation destructively modifies both polynomials.
237The result is stored in SELF. This implementation does
238no consing, entirely reusing the sells of SELF and OTHER.")
239 )
240
241(defmethod unary-minus ((self poly))
242 "Destructively modifies the coefficients of the polynomial SELF,
243by changing their sign."
244 (mapc #'unary-minus (poly-termlist self))
245 self)
246
247(defun add-termlists (p q order-fn)
248 "Destructively adds two termlists P and Q ordered according to ORDER-FN."
249 (fast-add/subtract p q order-fn #'add-to nil))
250
251(defmacro multiply-term-by-termlist-dropping-zeros (term termlist
252 &optional (reverse-arg-order-P nil))
253 "Multiplies term TERM by a list of term, TERMLIST.
254Takes into accound divisors of zero in the ring, by
255deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
256is T, change the order of arguments; this may be important
257if we extend the package to non-commutative rings."
258 `(mapcan #'(lambda (other-term)
259 (let ((prod (r*
260 ,@(cond
261 (reverse-arg-order-p
262 `(other-term ,term))
263 (t
264 `(,term other-term))))))
265 (cond
266 ((r-zerop prod) nil)
267 (t (list prod)))))
268 ,termlist))
269
270(defun multiply-termlists (p q order-fn)
271 "A version of polynomial multiplication, operating
272directly on termlists."
273 (cond
274 ((or (endp p) (endp q))
275 ;;p or q is 0 (represented by NIL)
276 nil)
277 ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
278 ((endp (cdr p))
279 (multiply-term-by-termlist-dropping-zeros (car p) q))
280 ((endp (cdr q))
281 (multiply-term-by-termlist-dropping-zeros (car q) p t))
282 (t
283 (cons (r* (car p) (car q))
284 (add-termlists
285 (multiply-term-by-termlist-dropping-zeros (car p) (cdr q))
286 (multiply-termlists (cdr p) q order-fn)
287 order-fn)))))
288
289(defmethod multiply-by ((self poly) (other poly))
290 (change-term-order other self)
291 (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
292 (poly-termlist other)
293 (poly-term-order self)))
294 self)
295
296(defmethod r* ((poly1 poly) (poly2 poly))
297 "Non-destructively multiply POLY1 by POLY2."
298 (multiply-by (copy-instance POLY1) (copy-instance POLY2)))
299
300(defmethod left-tensor-product-by ((self poly) (other term))
301 (setf (poly-termlist self)
302 (mapcan #'(lambda (term)
303 (let ((prod (left-tensor-product-by term other)))
304 (cond
305 ((r-zerop prod) nil)
306 (t (list prod)))))
307 (poly-termlist self)))
308 self)
309
310(defmethod right-tensor-product-by ((self poly) (other term))
311 (setf (poly-termlist self)
312 (mapcan #'(lambda (term)
313 (let ((prod (right-tensor-product-by term other)))
314 (cond
315 ((r-zerop prod) nil)
316 (t (list prod)))))
317 (poly-termlist self)))
318 self)
319
320(defmethod left-tensor-product-by ((self poly) (other monom))
321 (setf (poly-termlist self)
322 (mapcan #'(lambda (term)
323 (let ((prod (left-tensor-product-by term other)))
324 (cond
325 ((r-zerop prod) nil)
326 (t (list prod)))))
327 (poly-termlist self)))
328 (incf (poly-dimension self) (monom-dimension other))
329 self)
330
331(defmethod right-tensor-product-by ((self poly) (other monom))
332 (setf (poly-termlist self)
333 (mapcan #'(lambda (term)
334 (let ((prod (right-tensor-product-by term other)))
335 (cond
336 ((r-zerop prod) nil)
337 (t (list prod)))))
338 (poly-termlist self)))
339 (incf (poly-dimension self) (monom-dimension other))
340 self)
341
342
343(defun standard-extension (plist &aux (k (length plist)) (i 0))
344 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
345is a list of polynomials. Destructively modifies PLIST elements."
346 (mapc #'(lambda (poly)
347 (left-tensor-product-by
348 poly
349 (prog1
350 (make-monom-variable k i)
351 (incf i))))
352 plist))
353
354(defun standard-extension-1 (plist
355 &aux
356 (plist (standard-extension plist))
357 (nvars (poly-dimension (car plist))))
358 "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
359Firstly, new K variables U1, U2, ..., UK, are inserted into each
360polynomial. Subsequently, P1, P2, ..., PK are destructively modified
361tantamount to replacing PI with UI*PI-1. It assumes that all
362polynomials have the same dimension, and only the first polynomial
363is examined to determine this dimension."
364 ;; Implementation note: we use STANDARD-EXTENSION and then subtract
365 ;; 1 from each polynomial; since UI*PI has no constant term,
366 ;; we just need to append the constant term at the end
367 ;; of each termlist.
368 (flet ((subtract-1 (p)
369 (append-item p (make-instance 'term :coeff -1 :dimension nvars))))
370 (setf plist (mapc #'subtract-1 plist)))
371 plist)
372
373
374(defun standard-sum (plist
375 &aux
376 (plist (standard-extension plist))
377 (nvars (poly-dimension (car plist))))
378 "Calculate the polynomial U1*P1+U2*P2+...+UK*PK-1, where PLIST=[P1,P2,...,PK].
379Firstly, new K variables, U1, U2, ..., UK, are inserted into each
380polynomial. Subsequently, P1, P2, ..., PK are destructively modified
381tantamount to replacing PI with UI*PI, and the resulting polynomials
382are added. Finally, 1 is subtracted. It should be noted that the term
383order is not modified, which is equivalent to using a lexicographic
384order on the first K variables."
385 (flet ((subtract-1 (p)
386 (append-item p (make-instance 'term :coeff -1 :dimension nvars))))
387 (subtract-1
388 (make-instance
389 'poly
390 :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
391
392#|
393
394(defun saturation-extension-1 (ring f p)
395 "Calculate [F, U*P-1]. It destructively modifies F."
396 (declare (type ring ring))
397 (polysaturation-extension ring f (list p)))
398
399
400
401
402(defun spoly (ring-and-order f g
403 &aux
404 (ring (ro-ring ring-and-order)))
405 "It yields the S-polynomial of polynomials F and G."
406 (declare (type ring-and-order ring-and-order) (type poly f g))
407 (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
408 (mf (monom-div lcm (poly-lm f)))
409 (mg (monom-div lcm (poly-lm g))))
410 (declare (type monom mf mg))
411 (multiple-value-bind (c cf cg)
412 (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
413 (declare (ignore c))
414 (poly-sub
415 ring-and-order
416 (scalar-times-poly ring cg (monom-times-poly mf f))
417 (scalar-times-poly ring cf (monom-times-poly mg g))))))
418
419
420(defun poly-primitive-part (ring p)
421 "Divide polynomial P with integer coefficients by gcd of its
422coefficients and return the result."
423 (declare (type ring ring) (type poly p))
424 (if (poly-zerop p)
425 (values p 1)
426 (let ((c (poly-content ring p)))
427 (values (make-poly-from-termlist
428 (mapcar
429 #'(lambda (x)
430 (make-term :monom (term-monom x)
431 :coeff (funcall (ring-div ring) (term-coeff x) c)))
432 (poly-termlist p))
433 (poly-sugar p))
434 c))))
435
436(defun poly-content (ring p)
437 "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
438to compute the greatest common divisor."
439 (declare (type ring ring) (type poly p))
440 (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
441
442|#
Note: See TracBrowser for help on using the repository browser.