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

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

* empty log message *

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