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

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

* empty log message *

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