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/pol.lisp@ 1981

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

* empty log message *

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