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

Last change on this file since 1906 was 1906, checked in by Marek Rychlik, 10 years ago

* empty log message *

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