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

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

* empty log message *

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