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

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

* empty log message *

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