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

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

* empty log message *

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