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

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

* empty log message *

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