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

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

* empty log message *

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