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@ 3285

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

* empty log message *

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