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

Last change on this file since 2596 was 2596, 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
95Note that this assumes the presence of a
96dummy header."
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 order1 termlist2)))
129 ;; Create dummy head
130 (push nil termlist1)
131 (push nil termlist2)
132 (fast-add-to termlist1 termlist2 order1)
133 ;; Remove dummy head
134 (pop termlist1)))
135 self)
136
137(defmethod subtract-from ((self poly) (other poly)))
138
139(defmethod unary-uminus ((self poly)))
140
141#|
142
143(defun poly-standard-extension (plist &aux (k (length plist)))
144 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]."
145 (declare (list plist) (fixnum k))
146 (labels ((incf-power (g i)
147 (dolist (x (poly-termlist g))
148 (incf (monom-elt (term-monom x) i)))
149 (incf (poly-sugar g))))
150 (setf plist (poly-list-add-variables plist k))
151 (dotimes (i k plist)
152 (incf-power (nth i plist) i))))
153
154(defun saturation-extension (ring f plist
155 &aux
156 (k (length plist))
157 (d (monom-dimension (poly-lm (car plist))))
158 f-x plist-x)
159 "Calculate [F, U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK]."
160 (declare (type ring ring))
161 (setf f-x (poly-list-add-variables f k)
162 plist-x (mapcar #'(lambda (x)
163 (setf (poly-termlist x)
164 (nconc (poly-termlist x)
165 (list (make-term :monom (make-monom :dimension d)
166 :coeff (funcall (ring-uminus ring)
167 (funcall (ring-unit ring)))))))
168 x)
169 (poly-standard-extension plist)))
170 (append f-x plist-x))
171
172
173(defun polysaturation-extension (ring f plist
174 &aux
175 (k (length plist))
176 (d (+ k (monom-dimension (poly-lm (car plist)))))
177 ;; Add k variables to f
178 (f (poly-list-add-variables f k))
179 ;; Set PLIST to [U1*P1,U2*P2,...,UK*PK]
180 (plist (apply #'poly-append (poly-standard-extension plist))))
181 "Calculate [F, U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]. It destructively modifies F."
182 ;; Add -1 as the last term
183 (declare (type ring ring))
184 (setf (cdr (last (poly-termlist plist)))
185 (list (make-term :monom (make-monom :dimension d)
186 :coeff (funcall (ring-uminus ring) (funcall (ring-unit ring))))))
187 (append f (list plist)))
188
189(defun saturation-extension-1 (ring f p)
190 "Calculate [F, U*P-1]. It destructively modifies F."
191 (declare (type ring ring))
192 (polysaturation-extension ring f (list p)))
193
194;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
195;;
196;; Evaluation of polynomial (prefix) expressions
197;;
198;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
199
200(defun coerce-coeff (ring expr vars)
201 "Coerce an element of the coefficient ring to a constant polynomial."
202 ;; Modular arithmetic handler by rat
203 (declare (type ring ring))
204 (make-poly-from-termlist (list (make-term :monom (make-monom :dimension (length vars))
205 :coeff (funcall (ring-parse ring) expr)))
206 0))
207
208(defun poly-eval (expr vars
209 &optional
210 (ring +ring-of-integers+)
211 (order #'lex>)
212 (list-marker :[)
213 &aux
214 (ring-and-order (make-ring-and-order :ring ring :order order)))
215 "Evaluate Lisp form EXPR to a polynomial or a list of polynomials in
216variables VARS. Return the resulting polynomial or list of
217polynomials. Standard arithmetical operators in form EXPR are
218replaced with their analogues in the ring of polynomials, and the
219resulting expression is evaluated, resulting in a polynomial or a list
220of polynomials in internal form. A similar operation in another computer
221algebra system could be called 'expand' or so."
222 (declare (type ring ring))
223 (labels ((p-eval (arg) (poly-eval arg vars ring order))
224 (p-eval-scalar (arg) (poly-eval-scalar arg))
225 (p-eval-list (args) (mapcar #'p-eval args))
226 (p-add (x y) (poly-add ring-and-order x y)))
227 (cond
228 ((null expr) (error "Empty expression"))
229 ((eql expr 0) (make-poly-zero))
230 ((member expr vars :test #'equalp)
231 (let ((pos (position expr vars :test #'equalp)))
232 (make-poly-variable ring (length vars) pos)))
233 ((atom expr)
234 (coerce-coeff ring expr vars))
235 ((eq (car expr) list-marker)
236 (cons list-marker (p-eval-list (cdr expr))))
237 (t
238 (case (car expr)
239 (+ (reduce #'p-add (p-eval-list (cdr expr))))
240 (- (case (length expr)
241 (1 (make-poly-zero))
242 (2 (poly-uminus ring (p-eval (cadr expr))))
243 (3 (poly-sub ring-and-order (p-eval (cadr expr)) (p-eval (caddr expr))))
244 (otherwise (poly-sub ring-and-order (p-eval (cadr expr))
245 (reduce #'p-add (p-eval-list (cddr expr)))))))
246 (*
247 (if (endp (cddr expr)) ;unary
248 (p-eval (cdr expr))
249 (reduce #'(lambda (p q) (poly-mul ring-and-order p q)) (p-eval-list (cdr expr)))))
250 (/
251 ;; A polynomial can be divided by a scalar
252 (cond
253 ((endp (cddr expr))
254 ;; A special case (/ ?), the inverse
255 (coerce-coeff ring (apply (ring-div ring) (cdr expr)) vars))
256 (t
257 (let ((num (p-eval (cadr expr)))
258 (denom-inverse (apply (ring-div ring)
259 (cons (funcall (ring-unit ring))
260 (mapcar #'p-eval-scalar (cddr expr))))))
261 (scalar-times-poly ring denom-inverse num)))))
262 (expt
263 (cond
264 ((member (cadr expr) vars :test #'equalp)
265 ;;Special handling of (expt var pow)
266 (let ((pos (position (cadr expr) vars :test #'equalp)))
267 (make-poly-variable ring (length vars) pos (caddr expr))))
268 ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
269 ;; Negative power means division in coefficient ring
270 ;; Non-integer power means non-polynomial coefficient
271 (coerce-coeff ring expr vars))
272 (t (poly-expt ring-and-order (p-eval (cadr expr)) (caddr expr)))))
273 (otherwise
274 (coerce-coeff ring expr vars)))))))
275
276(defun poly-eval-scalar (expr
277 &optional
278 (ring +ring-of-integers+)
279 &aux
280 (order #'lex>))
281 "Evaluate a scalar expression EXPR in ring RING."
282 (declare (type ring ring))
283 (poly-lc (poly-eval expr nil ring order)))
284
285(defun spoly (ring-and-order f g
286 &aux
287 (ring (ro-ring ring-and-order)))
288 "It yields the S-polynomial of polynomials F and G."
289 (declare (type ring-and-order ring-and-order) (type poly f g))
290 (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
291 (mf (monom-div lcm (poly-lm f)))
292 (mg (monom-div lcm (poly-lm g))))
293 (declare (type monom mf mg))
294 (multiple-value-bind (c cf cg)
295 (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
296 (declare (ignore c))
297 (poly-sub
298 ring-and-order
299 (scalar-times-poly ring cg (monom-times-poly mf f))
300 (scalar-times-poly ring cf (monom-times-poly mg g))))))
301
302
303(defun poly-primitive-part (ring p)
304 "Divide polynomial P with integer coefficients by gcd of its
305coefficients and return the result."
306 (declare (type ring ring) (type poly p))
307 (if (poly-zerop p)
308 (values p 1)
309 (let ((c (poly-content ring p)))
310 (values (make-poly-from-termlist
311 (mapcar
312 #'(lambda (x)
313 (make-term :monom (term-monom x)
314 :coeff (funcall (ring-div ring) (term-coeff x) c)))
315 (poly-termlist p))
316 (poly-sugar p))
317 c))))
318
319(defun poly-content (ring p)
320 "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
321to compute the greatest common divisor."
322 (declare (type ring ring) (type poly p))
323 (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
324
325(defun read-infix-form (&key (stream t))
326 "Parser of infix expressions with integer/rational coefficients
327The parser will recognize two kinds of polynomial expressions:
328
329- polynomials in fully expanded forms with coefficients
330 written in front of symbolic expressions; constants can be optionally
331 enclosed in (); for example, the infix form
332 X^2-Y^2+(-4/3)*U^2*W^3-5
333 parses to
334 (+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))
335
336- lists of polynomials; for example
337 [X-Y, X^2+3*Z]
338 parses to
339 (:[ (- X Y) (+ (EXPT X 2) (* 3 Z)))
340 where the first symbol [ marks a list of polynomials.
341
342-other infix expressions, for example
343 [(X-Y)*(X+Y)/Z,(X+1)^2]
344parses to:
345 (:[ (/ (* (- X Y) (+ X Y)) Z) (EXPT (+ X 1) 2))
346Currently this function is implemented using M. Kantrowitz's INFIX package."
347 (read-from-string
348 (concatenate 'string
349 "#I("
350 (with-output-to-string (s)
351 (loop
352 (multiple-value-bind (line eof)
353 (read-line stream t)
354 (format s "~A" line)
355 (when eof (return)))))
356 ")")))
357
358(defun read-poly (vars &key
359 (stream t)
360 (ring +ring-of-integers+)
361 (order #'lex>))
362 "Reads an expression in prefix form from a stream STREAM.
363The expression read from the strem should represent a polynomial or a
364list of polynomials in variables VARS, over the ring RING. The
365polynomial or list of polynomials is returned, with terms in each
366polynomial ordered according to monomial order ORDER."
367 (poly-eval (read-infix-form :stream stream) vars ring order))
368
369(defun string->poly (str vars
370 &optional
371 (ring +ring-of-integers+)
372 (order #'lex>))
373 "Converts a string STR to a polynomial in variables VARS."
374 (with-input-from-string (s str)
375 (read-poly vars :stream s :ring ring :order order)))
376
377(defun poly->alist (p)
378 "Convert a polynomial P to an association list. Thus, the format of the
379returned value is ((MONOM[0] . COEFF[0]) (MONOM[1] . COEFF[1]) ...), where
380MONOM[I] is a list of exponents in the monomial and COEFF[I] is the
381corresponding coefficient in the ring."
382 (cond
383 ((poly-p p)
384 (mapcar #'term->cons (poly-termlist p)))
385 ((and (consp p) (eq (car p) :[))
386 (cons :[ (mapcar #'poly->alist (cdr p))))))
387
388(defun string->alist (str vars
389 &optional
390 (ring +ring-of-integers+)
391 (order #'lex>))
392 "Convert a string STR representing a polynomial or polynomial list to
393an association list (... (MONOM . COEFF) ...)."
394 (poly->alist (string->poly str vars ring order)))
395
396(defun poly-equal-no-sugar-p (p q)
397 "Compare polynomials for equality, ignoring sugar."
398 (declare (type poly p q))
399 (equalp (poly-termlist p) (poly-termlist q)))
400
401(defun poly-set-equal-no-sugar-p (p q)
402 "Compare polynomial sets P and Q for equality, ignoring sugar."
403 (null (set-exclusive-or p q :test #'poly-equal-no-sugar-p )))
404
405(defun poly-list-equal-no-sugar-p (p q)
406 "Compare polynomial lists P and Q for equality, ignoring sugar."
407 (every #'poly-equal-no-sugar-p p q))
408|#
Note: See TracBrowser for help on using the repository browser.