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

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