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

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

* empty log message *

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