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

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

* empty log message *

File size: 16.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
23(defpackage "POLYNOMIAL"
24 (:use :cl :ring :ring-and-order :monom :order :term :termlist :infix)
25 (:export "POLY"
26 "POLY-TERMLIST"
27 "POLY-SUGAR"
28 "POLY-RESET-SUGAR"
29 "POLY-LT"
30 "MAKE-POLY-FROM-TERMLIST"
31 "MAKE-POLY-ZERO"
32 "MAKE-POLY-VARIABLE"
33 "POLY-UNIT"
34 "POLY-LM"
35 "POLY-SECOND-LM"
36 "POLY-SECOND-LT"
37 "POLY-LC"
38 "POLY-SECOND-LC"
39 "POLY-ZEROP"
40 "POLY-LENGTH"
41 "SCALAR-TIMES-POLY"
42 "SCALAR-TIMES-POLY-1"
43 "MONOM-TIMES-POLY"
44 "TERM-TIMES-POLY"
45 "POLY-ADD"
46 "POLY-SUB"
47 "POLY-UMINUS"
48 "POLY-MUL"
49 "POLY-EXPT"
50 "POLY-APPEND"
51 "POLY-NREVERSE"
52 "POLY-REVERSE"
53 "POLY-CONTRACT"
54 "POLY-EXTEND"
55 "POLY-ADD-VARIABLES"
56 "POLY-LIST-ADD-VARIABLES"
57 "POLY-STANDARD-EXTENSION"
58 "SATURATION-EXTENSION"
59 "POLYSATURATION-EXTENSION"
60 "SATURATION-EXTENSION-1"
61 "COERCE-COEFF"
62 "POLY-EVAL"
63 "POLY-EVAL-SCALAR"
64 "SPOLY"
65 "POLY-PRIMITIVE-PART"
66 "POLY-CONTENT"
67 "READ-INFIX-FORM"
68 "READ-POLY"
69 "STRING->POLY"
70 "POLY->ALIST"
71 "STRING->ALIST"
72 "POLY-EQUAL-NO-SUGAR-P"
73 "POLY-SET-EQUAL-NO-SUGAR-P"
74 "POLY-LIST-EQUAL-NO-SUGAR-P"
75 ))
76
77(in-package :polynomial)
78
79;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
80;;
81;; Polynomials
82;;
83;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
84
85(defstruct (poly
86 ;;
87 ;; BOA constructor, by default constructs zero polynomial
88 (:constructor make-poly-from-termlist (termlist &optional (sugar (termlist-sugar termlist))))
89 (:constructor make-poly-zero (&aux (termlist nil) (sugar -1)))
90 ;; Constructor of polynomials representing a variable
91 (:constructor make-poly-variable (ring nvars pos &optional (power 1)
92 &aux
93 (termlist (list
94 (make-term-variable ring nvars pos power)))
95 (sugar power)))
96 (:constructor poly-unit (ring dimension
97 &aux
98 (termlist (termlist-unit ring dimension))
99 (sugar 0))))
100 (termlist nil :type list)
101 (sugar -1 :type fixnum))
102
103;; Leading term
104(defmacro poly-lt (p) `(car (poly-termlist ,p)))
105
106;; Second term
107(defmacro poly-second-lt (p) `(cadar (poly-termlist ,p)))
108
109;; Leading monomial
110(defun poly-lm (p) (term-monom (poly-lt p)))
111
112;; Second monomial
113(defun poly-second-lm (p) (term-monom (poly-second-lt p)))
114
115;; Leading coefficient
116(defun poly-lc (p) (term-coeff (poly-lt p)))
117
118;; Second coefficient
119(defun poly-second-lc (p) (term-coeff (poly-second-lt p)))
120
121;; Testing for a zero polynomial
122(defun poly-zerop (p) (null (poly-termlist p)))
123
124;; The number of terms
125(defun poly-length (p) (length (poly-termlist p)))
126
127(defun poly-reset-sugar (p)
128 "(Re)sets the sugar of a polynomial P to the sugar of (POLY-TERMLIST P).
129Thus, the sugar is set to the maximum sugar of all monomials of P, or -1
130if P is a zero polynomial."
131 (declare (type poly p))
132 (setf (poly-sugar p) (termlist-sugar (poly-termlist p)))
133 p)
134
135(defun scalar-times-poly (ring c p)
136 "The scalar product of scalar C by a polynomial P. The sugar of the
137original polynomial becomes the sugar of the result."
138 (declare (type ring ring) (type poly p))
139 (make-poly-from-termlist (scalar-times-termlist ring c (poly-termlist p)) (poly-sugar p)))
140
141(defun scalar-times-poly-1 (ring c p)
142 "The scalar product of scalar C by a polynomial P, omitting the head term. The sugar of the
143original polynomial becomes the sugar of the result."
144 (declare (type ring ring) (type poly p))
145 (make-poly-from-termlist (scalar-times-termlist ring c (cdr (poly-termlist p))) (poly-sugar p)))
146
147(defun monom-times-poly (m p)
148 (declare (type poly p))
149 (make-poly-from-termlist
150 (monom-times-termlist m (poly-termlist p))
151 (+ (poly-sugar p) (monom-sugar m))))
152
153(defun term-times-poly (ring term p)
154 (declare (type ring ring) (type term term) (type poly p))
155 (make-poly-from-termlist
156 (term-times-termlist ring term (poly-termlist p))
157 (+ (poly-sugar p) (term-sugar term))))
158
159(defun poly-add (ring-and-order p q)
160 (declare (type ring-and-order ring-and-order) (type poly p q))
161 (make-poly-from-termlist
162 (termlist-add ring-and-order
163 (poly-termlist p)
164 (poly-termlist q))
165 (max (poly-sugar p) (poly-sugar q))))
166
167(defun poly-sub (ring-and-order p q)
168 (declare (type ring-and-order ring-and-order) (type poly p q))
169 (make-poly-from-termlist
170 (termlist-sub ring-and-order (poly-termlist p) (poly-termlist q))
171 (max (poly-sugar p) (poly-sugar q))))
172
173(defun poly-uminus (ring p)
174 (declare (type ring ring) (type poly p))
175 (make-poly-from-termlist
176 (termlist-uminus ring (poly-termlist p))
177 (poly-sugar p)))
178
179(defun poly-mul (ring-and-order p q)
180 (declare (type ring-and-order ring-and-order) (type poly p q))
181 (make-poly-from-termlist
182 (termlist-mul ring-and-order (poly-termlist p) (poly-termlist q))
183 (+ (poly-sugar p) (poly-sugar q))))
184
185(defun poly-expt (ring-and-order p n)
186 (declare (type ring-and-order ring-and-order) (type poly p))
187 (make-poly-from-termlist (termlist-expt ring-and-order (poly-termlist p) n) (* n (poly-sugar p))))
188
189(defun poly-append (&rest plist)
190 (make-poly-from-termlist (apply #'append (mapcar #'poly-termlist plist))
191 (apply #'max (mapcar #'poly-sugar plist))))
192
193(defun poly-nreverse (p)
194 "Destructively reverse the order of terms in polynomial P. Returns P"
195 (declare (type poly p))
196 (setf (poly-termlist p) (nreverse (poly-termlist p)))
197 p)
198
199(defun poly-reverse (p)
200 "Returns a copy of the polynomial P with terms in reverse order."
201 (declare (type poly p))
202 (make-poly-from-termlist (reverse (poly-termlist p))
203 (poly-sugar p)))
204
205
206(defun poly-contract (p &optional (k 1))
207 (declare (type poly p))
208 (make-poly-from-termlist (termlist-contract (poly-termlist p) k)
209 (poly-sugar p)))
210
211(defun poly-extend (p &optional (m (make-monom :dimension 1)))
212 (declare (type poly p))
213 (make-poly-from-termlist
214 (termlist-extend (poly-termlist p) m)
215 (+ (poly-sugar p) (monom-sugar m))))
216
217(defun poly-add-variables (p k)
218 (declare (type poly p))
219 (setf (poly-termlist p) (termlist-add-variables (poly-termlist p) k))
220 p)
221
222(defun poly-list-add-variables (plist k)
223 (mapcar #'(lambda (p) (poly-add-variables p k)) plist))
224
225(defun poly-standard-extension (plist &aux (k (length plist)))
226 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]."
227 (declare (list plist) (fixnum k))
228 (labels ((incf-power (g i)
229 (dolist (x (poly-termlist g))
230 (incf (monom-elt (term-monom x) i)))
231 (incf (poly-sugar g))))
232 (setf plist (poly-list-add-variables plist k))
233 (dotimes (i k plist)
234 (incf-power (nth i plist) i))))
235
236(defun saturation-extension (ring f plist
237 &aux
238 (k (length plist))
239 (d (monom-dimension (poly-lm (car plist))))
240 f-x plist-x)
241 "Calculate [F, U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK]."
242 (setf f-x (poly-list-add-variables f k)
243 plist-x (mapcar #'(lambda (x)
244 (setf (poly-termlist x)
245 (nconc (poly-termlist x)
246 (list (make-term :monom (make-monom :dimension d)
247 :coeff (funcall (ring-uminus ring) (funcall (ring-unit ring)))))))
248 x)
249 (poly-standard-extension plist)))
250 (append f-x plist-x))
251
252
253(defun polysaturation-extension (ring f plist
254 &aux
255 (k (length plist))
256 (d (+ k (monom-dimension (poly-lm (car plist)))))
257 ;; Add k variables to f
258 (f (poly-list-add-variables f k))
259 ;; Set PLIST to [U1*P1,U2*P2,...,UK*PK]
260 (plist (apply #'poly-append (poly-standard-extension plist))))
261 "Calculate [F, U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]. It destructively modifies F."
262 ;; Add -1 as the last term
263 (setf (cdr (last (poly-termlist plist)))
264 (list (make-term (make-monom :dimension d)
265 (funcall (ring-uminus ring) (funcall (ring-unit ring))))))
266 (append f (list plist)))
267
268(defun saturation-extension-1 (ring f p)
269 "Calculate [F, U*P-1]. It destructively modifies F."
270 (polysaturation-extension ring f (list p)))
271
272;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
273;;
274;; Evaluation of polynomial (prefix) expressions
275;;
276;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
277
278(defun coerce-coeff (ring expr vars)
279 "Coerce an element of the coefficient ring to a constant polynomial."
280 ;; Modular arithmetic handler by rat
281 (make-poly-from-termlist (list (make-term (make-monom :dimension (length vars))
282 (funcall (ring-parse ring) expr)))
283 0))
284
285(defun poly-eval (expr vars
286 &optional
287 (ring +ring-of-integers+)
288 (order #'lex>)
289 (list-marker :[)
290 &aux
291 (ring-and-order (make-ring-and-order :ring ring :order order)))
292 "Evaluate Lisp form EXPR to a polynomial or a list of polynomials in
293variables VARS. Return the resulting polynomial or list of
294polynomials. Standard arithmetical operators in form EXPR are
295replaced with their analogues in the ring of polynomials, and the
296resulting expression is evaluated, resulting in a polynomial or a list
297of polynomials in internal form. A similar operation in another computer
298algebra system could be called 'expand' or so."
299 (labels ((p-eval (arg) (poly-eval arg vars ring order))
300 (p-eval-scalar (arg) (poly-eval-scalar arg))
301 (p-eval-list (args) (mapcar #'p-eval args))
302 (p-add (x y) (poly-add ring-and-order x y)))
303 (cond
304 ((null expr) (error "Empty expression"))
305 ((eql expr 0) (make-poly-zero))
306 ((member expr vars :test #'equalp)
307 (let ((pos (position expr vars :test #'equalp)))
308 (make-poly-variable ring (length vars) pos)))
309 ((atom expr)
310 (coerce-coeff ring expr vars))
311 ((eq (car expr) list-marker)
312 (cons list-marker (p-eval-list (cdr expr))))
313 (t
314 (case (car expr)
315 (+ (reduce #'p-add (p-eval-list (cdr expr))))
316 (- (case (length expr)
317 (1 (make-poly-zero))
318 (2 (poly-uminus ring (p-eval (cadr expr))))
319 (3 (poly-sub ring-and-order (p-eval (cadr expr)) (p-eval (caddr expr))))
320 (otherwise (poly-sub ring-and-order (p-eval (cadr expr))
321 (reduce #'p-add (p-eval-list (cddr expr)))))))
322 (*
323 (if (endp (cddr expr)) ;unary
324 (p-eval (cdr expr))
325 (reduce #'(lambda (p q) (poly-mul ring-and-order p q)) (p-eval-list (cdr expr)))))
326 (/
327 ;; A polynomial can be divided by a scalar
328 (cond
329 ((endp (cddr expr))
330 ;; A special case (/ ?), the inverse
331 (coerce-coeff ring (apply (ring-div ring) (cdr expr)) vars))
332 (t
333 (let ((num (p-eval (cadr expr)))
334 (denom-inverse (apply (ring-div ring)
335 (cons (funcall (ring-unit ring))
336 (mapcar #'p-eval-scalar (cddr expr))))))
337 (scalar-times-poly ring denom-inverse num)))))
338 (expt
339 (cond
340 ((member (cadr expr) vars :test #'equalp)
341 ;;Special handling of (expt var pow)
342 (let ((pos (position (cadr expr) vars :test #'equalp)))
343 (make-poly-variable ring (length vars) pos (caddr expr))))
344 ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
345 ;; Negative power means division in coefficient ring
346 ;; Non-integer power means non-polynomial coefficient
347 (coerce-coeff ring expr vars))
348 (t (poly-expt ring-and-order (p-eval (cadr expr)) (caddr expr)))))
349 (otherwise
350 (coerce-coeff ring expr vars)))))))
351
352(defun poly-eval-scalar (expr
353 &optional
354 (ring +ring-of-integers+)
355 &aux
356 (order #'lex>))
357 "Evaluate a scalar expression EXPR in ring RING."
358 (poly-lc (poly-eval expr nil ring order)))
359
360(defun spoly (ring-and-order f g
361 &aux
362 (ring (ro-ring ring-and-order)))
363 "It yields the S-polynomial of polynomials F and G."
364 (declare (type poly f g))
365 (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
366 (mf (monom-div lcm (poly-lm f)))
367 (mg (monom-div lcm (poly-lm g))))
368 (declare (type monom mf mg))
369 (multiple-value-bind (c cf cg)
370 (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
371 (declare (ignore c))
372 (poly-sub
373 ring-and-order
374 (scalar-times-poly ring cg (monom-times-poly mf f))
375 (scalar-times-poly ring cf (monom-times-poly mg g))))))
376
377
378(defun poly-primitive-part (ring p)
379 "Divide polynomial P with integer coefficients by gcd of its
380coefficients and return the result."
381 (declare (type poly p))
382 (if (poly-zerop p)
383 (values p 1)
384 (let ((c (poly-content ring p)))
385 (values (make-poly-from-termlist
386 (mapcar
387 #'(lambda (x)
388 (make-term (term-monom x)
389 (funcall (ring-div ring) (term-coeff x) c)))
390 (poly-termlist p))
391 (poly-sugar p))
392 c))))
393
394(defun poly-content (ring p)
395 "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
396to compute the greatest common divisor."
397 (declare (type poly p))
398 (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
399
400(defun read-infix-form (&key (stream t))
401 "Parser of infix expressions with integer/rational coefficients
402The parser will recognize two kinds of polynomial expressions:
403
404- polynomials in fully expanded forms with coefficients
405 written in front of symbolic expressions; constants can be optionally
406 enclosed in (); for example, the infix form
407 X^2-Y^2+(-4/3)*U^2*W^3-5
408 parses to
409 (+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))
410
411- lists of polynomials; for example
412 [X-Y, X^2+3*Z]
413 parses to
414 (:[ (- X Y) (+ (EXPT X 2) (* 3 Z)))
415 where the first symbol [ marks a list of polynomials.
416
417-other infix expressions, for example
418 [(X-Y)*(X+Y)/Z,(X+1)^2]
419parses to:
420 (:[ (/ (* (- X Y) (+ X Y)) Z) (EXPT (+ X 1) 2))
421Currently this function is implemented using M. Kantrowitz's INFIX package."
422 (read-from-string
423 (concatenate 'string
424 "#I("
425 (with-output-to-string (s)
426 (loop
427 (multiple-value-bind (line eof)
428 (read-line stream t)
429 (format s "~A" line)
430 (when eof (return)))))
431 ")")))
432
433(defun read-poly (vars &key
434 (stream t)
435 (ring +ring-of-integers+)
436 (order #'lex>))
437 "Reads an expression in prefix form from a stream STREAM.
438The expression read from the strem should represent a polynomial or a
439list of polynomials in variables VARS, over the ring RING. The
440polynomial or list of polynomials is returned, with terms in each
441polynomial ordered according to monomial order ORDER."
442 (poly-eval (read-infix-form :stream stream) vars ring order))
443
444(defun string->poly (str vars
445 &optional
446 (ring +ring-of-integers+)
447 (order #'lex>))
448 "Converts a string STR to a polynomial in variables VARS."
449 (with-input-from-string (s str)
450 (read-poly vars :stream s :ring ring :order order)))
451
452(defun poly->alist (p)
453 "Convert a polynomial P to an association list. Thus, the format of the
454returned value is ((MONOM[0] . COEFF[0]) (MONOM[1] . COEFF[1]) ...), where
455MONOM[I] is a list of exponents in the monomial and COEFF[I] is the
456corresponding coefficient in the ring."
457 (cond
458 ((poly-p p)
459 (mapcar #'term->cons (poly-termlist p)))
460 ((and (consp p) (eq (car p) :[))
461 (cons :[ (mapcar #'poly->alist (cdr p))))))
462
463(defun string->alist (str vars
464 &optional
465 (ring +ring-of-integers+)
466 (order #'lex>))
467 "Convert a string STR representing a polynomial or polynomial list to
468an association list (... (MONOM . COEFF) ...)."
469 (poly->alist (string->poly str vars ring order)))
470
471(defun poly-equal-no-sugar-p (p q)
472 "Compare polynomials for equality, ignoring sugar."
473 (equalp (poly-termlist p) (poly-termlist q)))
474
475(defun poly-set-equal-no-sugar-p (p q)
476 "Compare polynomial sets P and Q for equality, ignoring sugar."
477 (null (set-exclusive-or p q :test #'poly-equal-no-sugar-p )))
478
479(defun poly-list-equal-no-sugar-p (p q)
480 "Compare polynomial lists P and Q for equality, ignoring sugar."
481 (every #'poly-equal-no-sugar-p p q))
Note: See TracBrowser for help on using the repository browser.