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

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

* empty log message *

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