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

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