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

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

* empty log message *

File size: 14.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 :ring :monom :order :term #| :infix |# )
24 (:export "POLY"
25 "POLY-TERMLIST"
26 "POLY-TERM-ORDER")
27 (:documentation "Implements polynomials"))
28
29(in-package :polynomial)
30
31(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
32
33(defclass poly ()
34 ((termlist :initarg :termlist :accessor poly-termlist)
35 (order :initarg :order :accessor poly-term-order))
36 (:default-initargs :termlist nil :order #'lex>))
37
38(defmethod print-object ((self poly) stream)
39 (format stream "#<POLY TERMLIST=~A ORDER=~A>"
40 (poly-termlist self)
41 (poly-term-order self)))
42
43(defmethod insert-item ((self poly) (item term))
44 (push item (poly-termlist self))
45 self)
46
47(defmethod append-item ((self poly) (item term))
48 (setf (cdr (last (poly-termlist self))) (list item))
49 self)
50
51;; Leading term
52(defgeneric leading-term (object)
53 (:method ((self poly))
54 (car (poly-termlist self)))
55 (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
56
57;; Second term
58(defgeneric second-leading-term (object)
59 (:method ((self poly))
60 (cadar (poly-termlist self)))
61 (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
62
63;; Leading coefficient
64(defgeneric leading-coefficient (object)
65 (:method ((self poly))
66 (r-coeff (leading-term self)))
67 (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
68
69;; Second coefficient
70(defgeneric second-leading-coefficient (object)
71 (:method ((self poly))
72 (r-coeff (second-leading-term self)))
73 (:documentation "The second leading coefficient of a polynomial. It signals error for a polynomial with at most one term."))
74
75;; Testing for a zero polynomial
76(defmethod r-zerop ((self poly))
77 (null (poly-termlist self)))
78
79;; The number of terms
80(defmethod r-length ((self poly))
81 (length (poly-termlist self)))
82
83(defmethod multiply-by ((self poly) (other monom))
84 (mapc #'(lambda (term) (multiply-by term other))
85 (poly-termlist self))
86 self)
87
88(defmethod multiply-by ((self poly) (other scalar))
89 (mapc #'(lambda (term) (multiply-by term other))
90 (poly-termlist self))
91 self)
92
93(defun fast-add-to (p q order-fn)
94 "Fast destructive addition of termlists P and Q, ordered by
95predicate ORDER-FN. Note that this assumes the presence of a dummy
96header."
97 (macrolet ((lt (x) `(cadr ,x))
98 (lc (x) `(r-coeff (cadr ,x))))
99 (do ((p p)
100 (q q))
101 ((or (endp (cdr p)) (endp (cdr q)))
102 p)
103 (multiple-value-bind
104 (greater-p equal-p)
105 (funcall order-fn (lt q) (lt p))
106 (cond
107 (greater-p
108 (rotatef (cdr p) (cdr q)))
109 (equal-p
110 (let ((s (add-to (lc p) (lc q))))
111 (if (r-zerop s)
112 (setf (cdr p) (cddr p))
113 (setf (lc p) s
114 q (cdr q)))))))
115 (setf p (cdr p)))))
116
117(defmethod add-to ((self poly) (other poly))
118 "Adds to polynomial SELF another polynomial OTHER.
119This operation destructively modifies both polynomials.
120The result is stored in SELF. This implementation does
121no consing, entirely reusing the sells of SELF and OTHER."
122 (with-slots ((termlist1 termlist) (order1 order))
123 self
124 (with-slots ((termlist2 termlist) (order2 order))
125 other
126 ;; Ensure orders are compatible
127 (unless (eq order1 order2)
128 (setf termlist2 (sort termlist2 order1)
129 order2 order1))
130 ;; Create dummy head
131 (push nil termlist1)
132 (push nil termlist2)
133 (fast-add-to termlist1 termlist2 order1)
134 ;; Remove dummy head
135 (pop termlist1)))
136 self)
137
138(defmethod unary-uminus ((self poly)))
139
140#|
141
142(defun poly-standard-extension (plist &aux (k (length plist)))
143 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]."
144 (declare (list plist) (fixnum k))
145 (labels ((incf-power (g i)
146 (dolist (x (poly-termlist g))
147 (incf (monom-elt (term-monom x) i)))
148 (incf (poly-sugar g))))
149 (setf plist (poly-list-add-variables plist k))
150 (dotimes (i k plist)
151 (incf-power (nth i plist) i))))
152
153(defun saturation-extension (ring f plist
154 &aux
155 (k (length plist))
156 (d (monom-dimension (poly-lm (car plist))))
157 f-x plist-x)
158 "Calculate [F, U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK]."
159 (declare (type ring ring))
160 (setf f-x (poly-list-add-variables f k)
161 plist-x (mapcar #'(lambda (x)
162 (setf (poly-termlist x)
163 (nconc (poly-termlist x)
164 (list (make-term :monom (make-monom :dimension d)
165 :coeff (funcall (ring-uminus ring)
166 (funcall (ring-unit ring)))))))
167 x)
168 (poly-standard-extension plist)))
169 (append f-x plist-x))
170
171
172(defun polysaturation-extension (ring f plist
173 &aux
174 (k (length plist))
175 (d (+ k (monom-dimension (poly-lm (car plist)))))
176 ;; Add k variables to f
177 (f (poly-list-add-variables f k))
178 ;; Set PLIST to [U1*P1,U2*P2,...,UK*PK]
179 (plist (apply #'poly-append (poly-standard-extension plist))))
180 "Calculate [F, U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]. It destructively modifies F."
181 ;; Add -1 as the last term
182 (declare (type ring ring))
183 (setf (cdr (last (poly-termlist plist)))
184 (list (make-term :monom (make-monom :dimension d)
185 :coeff (funcall (ring-uminus ring) (funcall (ring-unit ring))))))
186 (append f (list plist)))
187
188(defun saturation-extension-1 (ring f p)
189 "Calculate [F, U*P-1]. It destructively modifies F."
190 (declare (type ring ring))
191 (polysaturation-extension ring f (list p)))
192
193;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
194;;
195;; Evaluation of polynomial (prefix) expressions
196;;
197;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
198
199(defun coerce-coeff (ring expr vars)
200 "Coerce an element of the coefficient ring to a constant polynomial."
201 ;; Modular arithmetic handler by rat
202 (declare (type ring ring))
203 (make-poly-from-termlist (list (make-term :monom (make-monom :dimension (length vars))
204 :coeff (funcall (ring-parse ring) expr)))
205 0))
206
207(defun poly-eval (expr vars
208 &optional
209 (ring +ring-of-integers+)
210 (order #'lex>)
211 (list-marker :[)
212 &aux
213 (ring-and-order (make-ring-and-order :ring ring :order order)))
214 "Evaluate Lisp form EXPR to a polynomial or a list of polynomials in
215variables VARS. Return the resulting polynomial or list of
216polynomials. Standard arithmetical operators in form EXPR are
217replaced with their analogues in the ring of polynomials, and the
218resulting expression is evaluated, resulting in a polynomial or a list
219of polynomials in internal form. A similar operation in another computer
220algebra system could be called 'expand' or so."
221 (declare (type ring ring))
222 (labels ((p-eval (arg) (poly-eval arg vars ring order))
223 (p-eval-scalar (arg) (poly-eval-scalar arg))
224 (p-eval-list (args) (mapcar #'p-eval args))
225 (p-add (x y) (poly-add ring-and-order x y)))
226 (cond
227 ((null expr) (error "Empty expression"))
228 ((eql expr 0) (make-poly-zero))
229 ((member expr vars :test #'equalp)
230 (let ((pos (position expr vars :test #'equalp)))
231 (make-poly-variable ring (length vars) pos)))
232 ((atom expr)
233 (coerce-coeff ring expr vars))
234 ((eq (car expr) list-marker)
235 (cons list-marker (p-eval-list (cdr expr))))
236 (t
237 (case (car expr)
238 (+ (reduce #'p-add (p-eval-list (cdr expr))))
239 (- (case (length expr)
240 (1 (make-poly-zero))
241 (2 (poly-uminus ring (p-eval (cadr expr))))
242 (3 (poly-sub ring-and-order (p-eval (cadr expr)) (p-eval (caddr expr))))
243 (otherwise (poly-sub ring-and-order (p-eval (cadr expr))
244 (reduce #'p-add (p-eval-list (cddr expr)))))))
245 (*
246 (if (endp (cddr expr)) ;unary
247 (p-eval (cdr expr))
248 (reduce #'(lambda (p q) (poly-mul ring-and-order p q)) (p-eval-list (cdr expr)))))
249 (/
250 ;; A polynomial can be divided by a scalar
251 (cond
252 ((endp (cddr expr))
253 ;; A special case (/ ?), the inverse
254 (coerce-coeff ring (apply (ring-div ring) (cdr expr)) vars))
255 (t
256 (let ((num (p-eval (cadr expr)))
257 (denom-inverse (apply (ring-div ring)
258 (cons (funcall (ring-unit ring))
259 (mapcar #'p-eval-scalar (cddr expr))))))
260 (scalar-times-poly ring denom-inverse num)))))
261 (expt
262 (cond
263 ((member (cadr expr) vars :test #'equalp)
264 ;;Special handling of (expt var pow)
265 (let ((pos (position (cadr expr) vars :test #'equalp)))
266 (make-poly-variable ring (length vars) pos (caddr expr))))
267 ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
268 ;; Negative power means division in coefficient ring
269 ;; Non-integer power means non-polynomial coefficient
270 (coerce-coeff ring expr vars))
271 (t (poly-expt ring-and-order (p-eval (cadr expr)) (caddr expr)))))
272 (otherwise
273 (coerce-coeff ring expr vars)))))))
274
275(defun poly-eval-scalar (expr
276 &optional
277 (ring +ring-of-integers+)
278 &aux
279 (order #'lex>))
280 "Evaluate a scalar expression EXPR in ring RING."
281 (declare (type ring ring))
282 (poly-lc (poly-eval expr nil ring order)))
283
284(defun spoly (ring-and-order f g
285 &aux
286 (ring (ro-ring ring-and-order)))
287 "It yields the S-polynomial of polynomials F and G."
288 (declare (type ring-and-order ring-and-order) (type poly f g))
289 (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
290 (mf (monom-div lcm (poly-lm f)))
291 (mg (monom-div lcm (poly-lm g))))
292 (declare (type monom mf mg))
293 (multiple-value-bind (c cf cg)
294 (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
295 (declare (ignore c))
296 (poly-sub
297 ring-and-order
298 (scalar-times-poly ring cg (monom-times-poly mf f))
299 (scalar-times-poly ring cf (monom-times-poly mg g))))))
300
301
302(defun poly-primitive-part (ring p)
303 "Divide polynomial P with integer coefficients by gcd of its
304coefficients and return the result."
305 (declare (type ring ring) (type poly p))
306 (if (poly-zerop p)
307 (values p 1)
308 (let ((c (poly-content ring p)))
309 (values (make-poly-from-termlist
310 (mapcar
311 #'(lambda (x)
312 (make-term :monom (term-monom x)
313 :coeff (funcall (ring-div ring) (term-coeff x) c)))
314 (poly-termlist p))
315 (poly-sugar p))
316 c))))
317
318(defun poly-content (ring p)
319 "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
320to compute the greatest common divisor."
321 (declare (type ring ring) (type poly p))
322 (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
323
324(defun read-infix-form (&key (stream t))
325 "Parser of infix expressions with integer/rational coefficients
326The parser will recognize two kinds of polynomial expressions:
327
328- polynomials in fully expanded forms with coefficients
329 written in front of symbolic expressions; constants can be optionally
330 enclosed in (); for example, the infix form
331 X^2-Y^2+(-4/3)*U^2*W^3-5
332 parses to
333 (+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))
334
335- lists of polynomials; for example
336 [X-Y, X^2+3*Z]
337 parses to
338 (:[ (- X Y) (+ (EXPT X 2) (* 3 Z)))
339 where the first symbol [ marks a list of polynomials.
340
341-other infix expressions, for example
342 [(X-Y)*(X+Y)/Z,(X+1)^2]
343parses to:
344 (:[ (/ (* (- X Y) (+ X Y)) Z) (EXPT (+ X 1) 2))
345Currently this function is implemented using M. Kantrowitz's INFIX package."
346 (read-from-string
347 (concatenate 'string
348 "#I("
349 (with-output-to-string (s)
350 (loop
351 (multiple-value-bind (line eof)
352 (read-line stream t)
353 (format s "~A" line)
354 (when eof (return)))))
355 ")")))
356
357(defun read-poly (vars &key
358 (stream t)
359 (ring +ring-of-integers+)
360 (order #'lex>))
361 "Reads an expression in prefix form from a stream STREAM.
362The expression read from the strem should represent a polynomial or a
363list of polynomials in variables VARS, over the ring RING. The
364polynomial or list of polynomials is returned, with terms in each
365polynomial ordered according to monomial order ORDER."
366 (poly-eval (read-infix-form :stream stream) vars ring order))
367
368(defun string->poly (str vars
369 &optional
370 (ring +ring-of-integers+)
371 (order #'lex>))
372 "Converts a string STR to a polynomial in variables VARS."
373 (with-input-from-string (s str)
374 (read-poly vars :stream s :ring ring :order order)))
375
376(defun poly->alist (p)
377 "Convert a polynomial P to an association list. Thus, the format of the
378returned value is ((MONOM[0] . COEFF[0]) (MONOM[1] . COEFF[1]) ...), where
379MONOM[I] is a list of exponents in the monomial and COEFF[I] is the
380corresponding coefficient in the ring."
381 (cond
382 ((poly-p p)
383 (mapcar #'term->cons (poly-termlist p)))
384 ((and (consp p) (eq (car p) :[))
385 (cons :[ (mapcar #'poly->alist (cdr p))))))
386
387(defun string->alist (str vars
388 &optional
389 (ring +ring-of-integers+)
390 (order #'lex>))
391 "Convert a string STR representing a polynomial or polynomial list to
392an association list (... (MONOM . COEFF) ...)."
393 (poly->alist (string->poly str vars ring order)))
394
395(defun poly-equal-no-sugar-p (p q)
396 "Compare polynomials for equality, ignoring sugar."
397 (declare (type poly p q))
398 (equalp (poly-termlist p) (poly-termlist q)))
399
400(defun poly-set-equal-no-sugar-p (p q)
401 "Compare polynomial sets P and Q for equality, ignoring sugar."
402 (null (set-exclusive-or p q :test #'poly-equal-no-sugar-p )))
403
404(defun poly-list-equal-no-sugar-p (p q)
405 "Compare polynomial lists P and Q for equality, ignoring sugar."
406 (every #'poly-equal-no-sugar-p p q))
407|#
Note: See TracBrowser for help on using the repository browser.