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

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