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

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

* empty log message *

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