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

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

* empty log message *

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