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

Last change on this file since 4456 was 4456, checked in by Marek Rychlik, 8 years ago

* empty log message *

File size: 26.5 KB
Line 
1;;----------------------------------------------------------------
2;;; -*- Mode: Lisp -*-
3;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4;;;
5;;; Copyright (C) 1999, 2002, 2009, 2015 Marek Rychlik <rychlik@u.arizona.edu>
6;;;
7;;; This program is free software; you can redistribute it and/or modify
8;;; it under the terms of the GNU General Public License as published by
9;;; the Free Software Foundation; either version 2 of the License, or
10;;; (at your option) any later version.
11;;;
12;;; This program is distributed in the hope that it will be useful,
13;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;;; GNU General Public License for more details.
16;;;
17;;; You should have received a copy of the GNU General Public License
18;;; along with this program; if not, write to the Free Software
19;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20;;;
21;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
22
23(defpackage "POLYNOMIAL"
24 (:use :cl :utils :monom :copy :ring)
25 (:export "POLY"
26 "POLY-DIMENSION"
27 "POLY-TERMLIST"
28 "POLY-TERM-ORDER"
29 "POLY-INSERT-TERM"
30 "POLY-REMOVE-TERM"
31 "SCALAR-MULTIPLY-BY"
32 "SCALAR-DIVIDE-BY"
33 "LEADING-TERM"
34 "LEADING-MONOMIAL"
35 "LEADING-COEFFICIENT"
36 "SECOND-LEADING-TERM"
37 "SECOND-LEADING-MONOMIAL"
38 "SECOND-LEADING-COEFFICIENT"
39 "ADD-TO"
40 "ADD"
41 "SUBTRACT-FROM"
42 "SUBTRACT"
43 "CHANGE-TERM-ORDER"
44 "STANDARD-EXTENSION"
45 "STANDARD-EXTENSION-1"
46 "STANDARD-SUM"
47 "SATURATION-EXTENSION"
48 "ALIST->POLY"
49 "POLY->ALIST"
50 "->INFIX"
51 "UNIVERSAL-EZGCD"
52 "S-POLYNOMIAL"
53 "POLY-CONTENT"
54 "POLY-PRIMITIVE-PART"
55 "SATURATION-EXTENSION-1"
56 "MAKE-POLY-VARIABLE"
57 "MAKE-POLY-CONSTANT"
58 "MAKE-ZERO-FOR"
59 "MAKE-UNIT-FOR"
60 "UNIVERSAL-EXPT"
61 "UNIVERSAL-EQUALP"
62 "UNIVERSAL-ZEROP"
63 "POLY-LENGTH"
64 "POLY-REVERSE"
65 "POLY-P"
66 "+LIST-MARKER+"
67 "POLY-EVAL"
68 "*COEFFICIENT-CLASS*")
69 (:documentation "Implements polynomials. A polynomial is essentially
70a mapping of monomials of the same degree to coefficients. The
71momomials are ordered according to a monomial order."))
72
73(in-package :polynomial)
74
75(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
76
77(defclass poly (ring)
78 ((dimension :initform nil
79 :initarg :dimension
80 :accessor poly-dimension
81 :documentation "Shared dimension of all terms, the number of variables")
82 (termlist :initform nil :initarg :termlist :accessor poly-termlist
83 :documentation "List of terms.")
84 (order :initform #'lex> :initarg :order :accessor poly-term-order
85 :documentation "Monomial/term order."))
86 (:default-initargs :dimension nil :termlist nil :order #'lex>)
87 (:documentation "A polynomial with a list of terms TERMLIST, ordered
88according to term order ORDER, which defaults to LEX>."))
89
90(defmethod print-object ((self poly) stream)
91 (print-unreadable-object (self stream :type t :identity t)
92 (with-accessors ((dimension poly-dimension)
93 (termlist poly-termlist)
94 (order poly-term-order))
95 self
96 (format stream "DIMENSION=~A TERMLIST=~A ORDER=~A"
97 dimension termlist order))))
98
99(defmethod copy-instance :around ((object poly) &rest initargs &key &allow-other-keys)
100 "Returns a deep copy of the polynomial POLY, by copying the TERMLIST and its terms."
101 (declare (ignore object initargs))
102 (let ((copy (call-next-method)))
103 (with-slots (termlist)
104 copy
105 (setf termlist (mapcar #'copy-instance termlist)))
106 copy))
107
108
109(defgeneric change-term-order (self other)
110 (:documentation "Change term order of SELF to the term order of OTHER.")
111 (:method ((self poly) (other poly))
112 (unless (eq (poly-term-order self) (poly-term-order other))
113 (setf (poly-termlist self) (sort (poly-termlist self) (poly-term-order other))
114 (poly-term-order self) (poly-term-order other)))
115 self))
116
117(defgeneric poly-insert-term (self term)
118 (:documentation "Insert a term TERM into SELF before all other
119terms. Order is not enforced.")
120 (:method ((self poly) (term term))
121 (cond ((null (poly-dimension self))
122 (setf (poly-dimension self) (monom-dimension term)))
123 (t (assert (= (poly-dimension self) (monom-dimension term)))))
124 (push term (poly-termlist self))
125 self))
126
127(defgeneric poly-remove-term (object)
128 (:documentation "Remove leading term of polynomial OBJECT. Returns the removed term.")
129 (:method ((object poly))
130 (pop (poly-termlist object))))
131
132(defgeneric poly-append-term (self term)
133 (:documentation "Append a term TERM to SELF after all other terms. Order is not enforced.")
134 (:method ((self poly) (term term))
135 (cond ((null (poly-dimension self))
136 (setf (poly-dimension self) (monom-dimension term)))
137 (t (assert (= (poly-dimension self) (monom-dimension term)))))
138 (setf (cdr (last (poly-termlist self))) (list term))
139 self))
140
141(defun alist->poly (alist &aux (poly (make-instance 'poly)))
142 "It reads polynomial from an alist formatted as ( ... (exponents . coeff) ...).
143It can be used to enter simple polynomials by hand, e.g the polynomial
144in two variables, X and Y, given in standard notation as:
145
146 3*X^2*Y^3+2*Y+7
147
148can be entered as
149(ALIST->POLY '(((0 0) . 7) ((0 1) . 2) ((2 3) . 3) )). NOTE: the
150terms are entered in the increasing order.
151
152NOTE: The primary use is for low-level debugging of the package."
153 (dolist (x alist poly)
154 (poly-insert-term poly (make-instance 'term :exponents (car x) :coeff (cdr x)))))
155
156(defun poly->alist (p)
157 "Convert a polynomial P to an association list. Thus, the format of the
158returned value is ((MONOM[0] . COEFF[0]) (MONOM[1] . COEFF[1]) ...), where
159MONOM[I] is a list of exponents in the monomial and COEFF[I] is the
160corresponding coefficient in the ring."
161 (cond
162 ((poly-p p)
163 (mapcar #'->list (poly-termlist p)))
164 ((and (consp p) (eq (car p) :[))
165 (cons :[ (mapcar #'poly->alist (cdr p))))))
166
167
168#+nil
169(defmethod shared-initialize :after ((self poly) slot-names
170 &rest initargs
171 &key)
172 "If TERMLIST is supplied and non-empty, and DIMENSION is NIL, set
173the dimension to the dimension of the first term in TERMLIST."
174 (declare (ignore initargs))
175 (let ((dims (mapcar #'monom-dimension (slot-value self 'termlist))))
176 (format t "Dimensions: ~A~%" dims)
177 (assert (apply #'= dims))
178 (unless (endp dims)
179 (setf (slot-value self 'dimension) (car dims))))
180 self)
181
182(defmethod update-instance-for-different-class :after ((old term) (new poly) &key)
183 "Converts OLD of class TERM to a NEW of class POLY, by making it into a 1-element TERMLIST."
184 (reinitialize-instance new
185 :dimension (monom-dimension old)
186 :termlist (list old)))
187
188(defmethod update-instance-for-different-class :after ((old monom) (new poly) &key)
189 "Converts OLD of class MONOM to a NEW of class POLY, by making it into a 1-element TERMLIST."
190 (reinitialize-instance new
191 :dimension (monom-dimension old)
192 :termlist (list (change-class old 'term))))
193
194(defmethod universal-equalp ((self poly) (other poly))
195 "Implements equality of polynomials."
196 (and (eql (poly-dimension self) (poly-dimension other))
197 (every #'universal-equalp (poly-termlist self) (poly-termlist other))
198 (eq (poly-term-order self) (poly-term-order other))))
199
200(defgeneric leading-term (object)
201 (:method ((self poly))
202 (car (poly-termlist self)))
203 (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
204
205(defgeneric second-leading-term (object)
206 (:method ((self poly))
207 (cadar (poly-termlist self)))
208 (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
209
210(defgeneric leading-monomial (object)
211 (:method ((self poly))
212 (change-class (copy-instance (leading-term self)) 'monom))
213 (:documentation "The leading monomial of a polynomial, or NIL for zero polynomial."))
214
215(defgeneric second-leading-monomial (object)
216 (:method ((self poly))
217 (change-class (copy-instance (second-leading-term self)) 'monom))
218 (:documentation "The leading monomial of a polynomial, or NIL for zero polynomial."))
219
220(defgeneric leading-coefficient (object)
221 (:method ((self poly))
222 (term-coeff (leading-term self)))
223 (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
224
225(defgeneric second-leading-coefficient (object)
226 (:method ((self poly))
227 (term-coeff (second-leading-term self)))
228 (:documentation "The second leading coefficient of a polynomial. It
229 signals error for a polynomial with at most one term."))
230
231(defmethod universal-zerop ((self poly))
232 "Return T iff SELF is a zero polynomial."
233 (null (poly-termlist self)))
234
235(defgeneric poly-length (self)
236 (:documentation "Return the number of terms.")
237 (:method ((self poly))
238 (length (poly-termlist self))))
239
240(defgeneric scalar-multiply-by (self other)
241 (:documentation "Multiply vector SELF by a scalar OTHER.")
242 (:method ((self poly) other)
243 (mapc #'(lambda (term) (setf (term-coeff term) (multiply-by (term-coeff term) other)))
244 (poly-termlist self))
245 self))
246
247(defgeneric scalar-divide-by (self other)
248 (:documentation "Divide vector SELF by a scalar OTHER.")
249 (:method ((self poly) other)
250 (mapc #'(lambda (term) (setf (term-coeff term) (divide-by (term-coeff term) other)))
251 (poly-termlist self))
252 self))
253
254(defmethod unary-inverse :before ((self poly))
255 "Checks invertibility of a polynomial SELF. To be invertable, the
256polynomial must be an invertible, constant polynomial."
257 (with-slots (termlist)
258 self
259 (assert (and (= (length termlist) 1) (zerop (total-degree (car termlist))))
260 nil
261 "To be invertible, the polynomial must have 1 term of total degree 0.")))
262
263(defmethod unary-inverse ((self poly))
264 "Returns the unary inverse of a polynomial SELF."
265 (with-slots (termlist)
266 self
267 (setf (car termlist) (unary-inverse (car termlist)))
268 self))
269
270(defmethod multiply-by ((self poly) (other monom))
271 "Multiply a polynomial SELF by OTHER."
272 (mapc #'(lambda (term) (multiply-by term other))
273 (poly-termlist self))
274 self)
275
276(defmethod multiply-by ((self poly) (other term))
277 "Multiply a polynomial SELF by OTHER."
278 (mapc #'(lambda (term) (multiply-by term other))
279 (poly-termlist self))
280 self)
281
282#|
283(defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
284 "Return an expression which will efficiently adds/subtracts two
285polynomials, P and Q. The addition/subtraction of coefficients is
286performed by calling ADD/SUBTRACT-FN. If UMINUS-FN is supplied, it is
287used to negate the coefficients of Q which do not have a corresponding
288coefficient in P. The code implements an efficient algorithm to add
289two polynomials represented as sorted lists of terms. The code
290destroys both arguments, reusing the terms to build the result."
291 `(macrolet ((lc (x) `(term-coeff (car ,x))))
292 (do ((p ,p)
293 (q ,q)
294 r)
295 ((or (endp p) (endp q))
296 ;; NOTE: R contains the result in reverse order. Can it
297 ;; be more efficient to produce the terms in correct order?
298 (unless (endp q)
299 ;; Upon subtraction, we must change the sign of
300 ;; all coefficients in q
301 ,@(when uminus-fn
302 `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
303 (setf r (nreconc r q)))
304 (unless (endp p)
305 (setf r (nreconc r p)))
306 r)
307 (multiple-value-bind
308 (greater-p equal-p)
309 (funcall ,order-fn (car p) (car q))
310 (cond
311 (greater-p
312 (rotatef (cdr p) r p)
313 )
314 (equal-p
315 (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
316 (cond
317 ((universal-zerop s)
318 (setf p (cdr p))
319 )
320 (t
321 (setf (lc p) s)
322 (rotatef (cdr p) r p))))
323 (setf q (cdr q))
324 )
325 (t
326 ;;Negate the term of Q if UMINUS provided, signallig
327 ;;that we are doing subtraction
328 ,(when uminus-fn
329 `(setf (lc q) (funcall ,uminus-fn (lc q))))
330 (rotatef (cdr q) r q))))
331 ;;(format t "P:~A~%" p)
332 ;;(format t "Q:~A~%" q)
333 ;;(format t "R:~A~%" r)
334 )))
335|#
336
337
338
339
340#|
341(defun fast-add (p q order-fn add-fn)
342 "Add two polynomials, P and Q, represented as lists of terms.
343The operation is destructive to both polynomials, as the terms
344of both lists are combined into the result. The operation does not
345create any new instance of TERM."
346 (macrolet ((lc (x) `(term-coeff (car ,x))))
347 (do (r)
348 ((or (endp p) (endp q))
349 ;; NOTE: R contains the result in reverse order. Can it
350 ;; be more efficient to produce the terms in correct order?
351 (unless (endp q)
352 (setf r (nreconc r q)))
353 (unless (endp p)
354 (setf r (nreconc r p)))
355 r)
356 (multiple-value-bind
357 (greater-p equal-p)
358 (funcall order-fn (car p) (car q))
359 (cond
360 (greater-p
361 (rotatef (cdr p) r p)
362 )
363 (equal-p
364 (let ((s (funcall add-fn (lc p) (lc q))))
365 (cond
366 ((universal-zerop s)
367 (setf p (cdr p))
368 )
369 (t
370 (setf (lc p) s)
371 (rotatef (cdr p) r p))))
372 (setf q (cdr q))
373 )
374 (t
375 (rotatef (cdr q) r q)))))))
376|#
377
378;; Getter/setter of leading coefficient
379(defun lc (x) (term-coeff (car x)))
380(defun (setf lc) (new-value x) (setf (term-coeff (car x)) new-value))
381
382
383(defun slow-add (p q order-fn add-fn)
384 (cond
385 ((endp p) q)
386 ((endp q) p)
387 (t
388 (multiple-value-bind
389 (greater-p equal-p)
390 (funcall order-fn (car p) (car q))
391 (cond
392 (greater-p ; (> (car p) (car q))
393 (cons (car p) (slow-add (cdr p) q order-fn add-fn))
394 )
395 (equal-p ; (= (car p)) (car q))
396 (let ((s (funcall add-fn (lc p) (lc q))))
397 (cond
398 ((universal-zerop s)
399 (slow-add (cdr p) (cdr q) order-fn add-fn))
400 (t
401 ;; Adjust the lc of p
402 (setf (lc p) s)
403 (cons (car p) (slow-add (cdr p) (cdr q) order-fn add-fn))
404 ))))
405 (t ;(< (car p) (car q))
406 (cons (car q) (slow-add p (cdr q) order-fn add-fn))
407 ))))))
408
409
410(defun fast-and-risky-add (p q order-fn add-fn &aux result result-last)
411 (when (and p q (eq p q)) (warn "FAST-AND-RISKY-ADD: ~S is EQ to ~S" p q))
412 (flet ((add-to-result (x)
413 (assert (consp x))
414 (setf (cdr x) nil)
415 (if (endp result)
416 (setf result x
417 result-last x)
418 (setf (cdr result-last) x
419 result-last (cdr result-last)))))
420 (loop
421 (cond
422 ((endp p) (unless (endp q) (add-to-result q)) (return result))
423 ((endp q) (unless (endp p) (add-to-result p)) (return result))
424 (t
425 (multiple-value-bind
426 (greater-p equal-p)
427 (funcall order-fn (car p) (car q))
428 (cond
429 (greater-p ; (> (car p) (car q))
430 (let ((tmp (cdr p)))
431 (add-to-result p)
432 (setf p tmp)))
433 (equal-p ; (= (car p)) (car q))
434 (let ((s (funcall add-fn (lc p) (lc q))))
435 (cond
436 ((universal-zerop s)
437 ;; Terms cancel, discard both
438 (setf p (cdr p)
439 q (cdr q)))
440 (t
441 ;; Terms do not cancel, store the
442 ;; sum of coefficients in (lc p)
443 (setf (lc p) s)
444 (let ((tmp (cdr p)))
445 (add-to-result p)
446 (setf p tmp
447 q (cdr q)))))))
448 (t ;(< (car p) (car q))
449 (let ((tmp (cdr q)))
450 (add-to-result q)
451 (setf q tmp))
452 ))))))))
453
454(defun fast-add (p q order-fn add-fn)
455 "This version calls SLOW-ADD and is bullet-proof."
456 (slow-add p q order-fn add-fn)
457 ;;(fast-and-risky-add p q order-fn add-fn)
458 )
459
460#|
461;; NOTE: The stuff below works, but may not be worth the trouble.
462
463(defmacro def-add/subtract-method (add/subtract-method-name
464 uminus-method-name
465 &optional
466 (doc-string nil doc-string-supplied-p))
467 "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
468 `(defmethod ,add/subtract-method-name ((self poly) (other poly))
469 ,@(when doc-string-supplied-p `(,doc-string))
470 ;; Ensure orders are compatible
471 (change-term-order other self)
472 (setf (poly-termlist self) (fast-add/subtract
473 (poly-termlist self) (poly-termlist other)
474 (poly-term-order self)
475 #',add/subtract-method-name
476 ,(when uminus-method-name `(function ,uminus-method-name))))
477 self))
478
479(eval-when (:load-toplevel :execute)
480
481 (def-add/subtract-method add-to nil
482 "Adds to polynomial SELF another polynomial OTHER.
483This operation destructively modifies both polynomials.
484The result is stored in SELF. This implementation does
485no consing, entirely reusing the sells of SELF and OTHER.")
486
487 (def-add/subtract-method subtract-from unary-minus
488 "Subtracts from polynomial SELF another polynomial OTHER.
489This operation destructively modifies both polynomials.
490The result is stored in SELF. This implementation does
491no consing, entirely reusing the sells of SELF and OTHER.")
492 )
493
494|#
495
496(defmethod unary-minus ((self poly))
497 "Destructively modifies the coefficients of the polynomial SELF,
498by changing their sign."
499 (mapc #'unary-minus (poly-termlist self))
500 self)
501
502(defun add-termlists (p q order-fn)
503 "Destructively adds two termlists P and Q ordered according to ORDER-FN."
504 (fast-add p q order-fn #'add-to))
505
506(defun subtract-termlists (p q order-fn)
507 "Destructively subtracts two termlists P and Q ordered according to ORDER-FN."
508 (setf q (mapc #'unary-minus q))
509 (add-termlists p q order-fn))
510
511(defmethod add-to ((self poly) (other poly))
512 "Adds to polynomial SELF another polynomial OTHER.
513This operation destructively modifies both polynomials.
514The result is stored in SELF. This implementation does
515no consing, entirely reusing the sells of SELF and OTHER."
516 (change-term-order other self)
517 (setf (poly-termlist self) (add-termlists
518 (poly-termlist self) (poly-termlist other)
519 (poly-term-order self)))
520 self)
521
522
523(defmethod subtract-from ((self poly) (other poly))
524 "Subtracts from polynomial SELF another polynomial OTHER.
525This operation destructively modifies both polynomials.
526The result is stored in SELF. This implementation does
527no consing, entirely reusing the sells of SELF and OTHER."
528 (change-term-order other self)
529 (setf (poly-termlist self) (subtract-termlists
530 (poly-termlist self) (poly-termlist other)
531 (poly-term-order self)))
532 self)
533
534
535(defmethod add-to ((self poly) (other term))
536 "Adds to a polynomial SELF a term OTHER. The term OTHER is not
537modified."
538 (add-to self (change-class other 'poly)))
539
540(defmethod subtract-from ((self poly) (other term))
541 "Subtracts from a polynomial SELF a term OTHER. The term OTHER is not
542modified."
543 (subtract-from self (change-class other 'poly)))
544
545
546(defmacro multiply-term-by-termlist-dropping-zeros (term termlist
547 &optional (reverse-arg-order-P nil))
548 "Multiplies term TERM by a list of term, TERMLIST.
549Takes into accound divisors of zero in the ring, by
550deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
551is T, change the order of arguments; this may be important
552if we extend the package to non-commutative rings."
553 `(mapcan #'(lambda (other-term)
554 (let ((prod (multiply
555 ,@(cond
556 (reverse-arg-order-p
557 `(other-term ,term))
558 (t
559 `(,term other-term))))))
560 (cond
561 ((universal-zerop prod) nil)
562 (t (list prod)))))
563 ,termlist))
564
565(defun multiply-termlists (p q order-fn)
566 "A version of polynomial multiplication, operating
567directly on termlists."
568 (cond
569 ((or (endp p) (endp q))
570 ;;p or q is 0 (represented by NIL)
571 nil)
572 ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
573 ((endp (cdr p))
574 (multiply-term-by-termlist-dropping-zeros (car p) q))
575 ((endp (cdr q))
576 (multiply-term-by-termlist-dropping-zeros (car q) p t))
577 (t
578 (cons (multiply (car p) (car q))
579 (add-termlists
580 (multiply-term-by-termlist-dropping-zeros (car p) (cdr q))
581 (multiply-termlists (cdr p) q order-fn)
582 order-fn)))))
583
584(defmethod multiply-by ((self poly) (other poly) &aux (other-copy (copy-instance other)))
585 (change-term-order other-copy self)
586 (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
587 (poly-termlist other-copy)
588 (poly-term-order self)))
589 self)
590
591(defmethod left-tensor-product-by ((self poly) (other monom))
592 (setf (poly-termlist self)
593 (mapcan #'(lambda (term)
594 (let ((prod (left-tensor-product-by term other)))
595 (cond
596 ((universal-zerop prod) nil)
597 (t (list prod)))))
598 (poly-termlist self)))
599 (incf (poly-dimension self) (monom-dimension other))
600 self)
601
602(defmethod right-tensor-product-by ((self poly) (other monom))
603 (setf (poly-termlist self)
604 (mapcan #'(lambda (term)
605 (let ((prod (right-tensor-product-by term other)))
606 (cond
607 ((universal-zerop prod) nil)
608 (t (list prod)))))
609 (poly-termlist self)))
610 (incf (poly-dimension self) (monom-dimension other))
611 self)
612
613
614(defun standard-extension (plist &aux (k (length plist)) (i 0))
615 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
616is a list of polynomials. Destructively modifies PLIST elements."
617 (mapc #'(lambda (poly)
618 (left-tensor-product-by
619 poly
620 (prog1
621 (make-monom-variable k i)
622 (incf i))))
623 plist))
624
625(defun standard-extension-1 (plist
626 &aux
627 (plist (standard-extension plist))
628 (nvars (poly-dimension (car plist))))
629 "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
630Firstly, new K variables U1, U2, ..., UK, are inserted into each
631polynomial. Subsequently, P1, P2, ..., PK are destructively modified
632tantamount to replacing PI with UI*PI-1. It assumes that all
633polynomials have the same dimension, and only the first polynomial
634is examined to determine this dimension."
635 ;; Implementation note: we use STANDARD-EXTENSION and then subtract
636 ;; 1 from each polynomial; since UI*PI has no constant term,
637 ;; we just need to append the constant term at the end
638 ;; of each termlist.
639 (flet ((subtract-1 (p)
640 (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
641 (setf plist (mapc #'subtract-1 plist)))
642 plist)
643
644
645(defun standard-sum (plist
646 &aux
647 (plist (standard-extension plist))
648 (nvars (poly-dimension (car plist))))
649 "Calculate the polynomial U1*P1+U2*P2+...+UK*PK-1, where PLIST=[P1,P2,...,PK].
650Firstly, new K variables, U1, U2, ..., UK, are inserted into each
651polynomial. Subsequently, P1, P2, ..., PK are destructively modified
652tantamount to replacing PI with UI*PI, and the resulting polynomials
653are added. Finally, 1 is subtracted. It should be noted that the term
654order is not modified, which is equivalent to using a lexicographic
655order on the first K variables."
656 (flet ((subtract-1 (p)
657 (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
658 (subtract-1
659 (make-instance
660 'poly
661 :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
662
663(defgeneric s-polynomial (object1 object2)
664 (:documentation "Yields the S-polynomial of OBJECT1 and OBJECT2.")
665 (:method ((f poly) (g poly))
666 (let* ((lcm (universal-lcm (leading-monomial f) (leading-monomial g)))
667 (mf (divide lcm (leading-monomial f)))
668 (mg (divide lcm (leading-monomial g))))
669 (multiple-value-bind (c cf cg)
670 (universal-ezgcd (leading-coefficient f) (leading-coefficient g))
671 (declare (ignore c))
672 (subtract
673 (multiply f mf cg)
674 (multiply g mg cf))))))
675
676(defgeneric poly-content (object)
677 (:documentation "Greatest common divisor of the coefficients of the polynomial object OBJECT.")
678 (:method ((self poly))
679 (reduce #'universal-gcd
680 (mapcar #'term-coeff (rest (poly-termlist self)))
681 :initial-value (leading-coefficient self))))
682
683(defun poly-primitive-part (self)
684 "Divide polynomial SELF by gcd of its
685coefficients. Return the resulting polynomial."
686 (scalar-divide-by self (poly-content self)))
687
688(defun poly-insert-variables (self k)
689 (left-tensor-product-by self (make-instance 'monom :dimension k)))
690
691(defun saturation-extension (f plist &aux (k (length plist)))
692 "Calculate [F', U1*P1-1,U2*P2-1,...,UK*PK-1], where
693PLIST=[P1,P2,...,PK] and F' is F with variables U1,U2,...,UK inserted
694as first K variables. It destructively modifies F and PLIST."
695 (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
696 (standard-extension-1 plist)))
697
698(defun polysaturation-extension (f plist &aux (k (length plist)))
699 "Calculate [F', U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]
700and F' is F with variables U1,U2,...,UK inserted as first K
701variables. It destructively modifies F and PLIST."
702 (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
703 (list (standard-sum plist))))
704
705(defun saturation-extension-1 (f p)
706 "Given family of polynomials F and a polynomial P, calculate [F',
707U*P-1], where F' is F with variable inserted as the first variable. It
708destructively modifies F and P."
709 (polysaturation-extension f (list p)))
710
711(defmethod multiply-by ((self poly) (other ring))
712 (scalar-multiply-by self other))
713
714(defun make-poly-variable (nvars pos &optional (power 1))
715 (change-class (make-monom-variable nvars pos power) 'poly))
716
717(defun make-poly-constant (nvars coeff)
718 (change-class (make-term-constant nvars coeff) 'poly))
719
720(defgeneric universal-expt (x y)
721 (:documentation "Raises X to power Y.")
722 (:method ((x number) (y integer)) (expt x y))
723 (:method ((x t) (y integer))
724 (declare (type fixnum y))
725 (cond
726 ((minusp y) (error "universal-expt: Negative exponent."))
727 ((universal-zerop x) (if (zerop y) 1))
728 (t
729 (do ((k 1 (ash k 1))
730 (q x (multiply q q)) ;keep squaring
731 (p (make-unit-for x) (if (not (zerop (logand k y))) (multiply p q) p)))
732 ((> k y) p)
733 (declare (fixnum k)))))))
734
735(defgeneric poly-p (object)
736 (:documentation "Checks if an object is a polynomial.")
737 (:method ((self poly)) t)
738 (:method ((self t)) nil))
739
740(defmethod ->sexp :before ((self poly) &optional vars)
741 "Ensures that the number of variables in VARS maches the polynomial dimension of the
742polynomial SELF."
743 (with-slots (dimension)
744 self
745 (assert (= (length vars) dimension)
746 nil
747 "Number of variables ~S does not match the dimension ~S"
748 vars dimension)))
749
750(defmethod ->sexp ((self poly) &optional vars)
751 "Converts a polynomial SELF to a sexp."
752 (let ((m (mapcar #'(lambda (trm) (->sexp trm vars))
753 (poly-termlist self))))
754 (cond ((endp m) 0)
755 ((endp (cdr m)) (car m))
756 (t (cons '+ m)))))
757
758(defconstant +list-marker+ :[
759 "A sexp with this head is considered a list of polynomials.")
760
761(defmethod ->sexp ((self cons) &optional vars)
762 (assert (eql (car self) +list-marker+))
763 (cons +list-marker+ (mapcar #'(lambda (p) (->sexp p vars)) (cdr self))))
764
765(defmethod make-zero-for ((self poly))
766 (make-instance 'poly :dimension (poly-dimension self)))
767
768(defmethod make-unit-for ((self poly))
769 (make-poly-constant (poly-dimension self) 1))
770
771(defgeneric poly-reverse (self)
772 (:documentation "Reverse the order of terms in a polynomial SELF.")
773 (:method ((self poly))
774 (with-slots (termlist)
775 self
776 (setf termlist (nreverse termlist)))
777 self))
778
779
780
Note: See TracBrowser for help on using the repository browser.