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

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

* empty log message *

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