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

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

* empty log message *

File size: 15.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(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-contract (p &optional (k 1))
195 (declare (type poly p))
196 (make-poly-from-termlist (termlist-contract (poly-termlist p) k)
197 (poly-sugar p)))
198
199(defun poly-extend (p &optional (m (make-monom :dimension 1)))
200 (declare (type poly p))
201 (make-poly-from-termlist
202 (termlist-extend (poly-termlist p) m)
203 (+ (poly-sugar p) (monom-sugar m))))
204
205(defun poly-add-variables (p k)
206 (declare (type poly p))
207 (setf (poly-termlist p) (termlist-add-variables (poly-termlist p) k))
208 p)
209
210(defun poly-list-add-variables (plist k)
211 (mapcar #'(lambda (p) (poly-add-variables p k)) plist))
212
213(defun poly-standard-extension (plist &aux (k (length plist)))
214 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]."
215 (declare (list plist) (fixnum k))
216 (labels ((incf-power (g i)
217 (dolist (x (poly-termlist g))
218 (incf (monom-elt (term-monom x) i)))
219 (incf (poly-sugar g))))
220 (setf plist (poly-list-add-variables plist k))
221 (dotimes (i k plist)
222 (incf-power (nth i plist) i))))
223
224(defun saturation-extension (ring f plist &aux (k (length plist)) (d (monom-dimension (poly-lm (car plist)))))
225 "Calculate [F, U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK]."
226 (setf f (poly-list-add-variables f k)
227 plist (mapcar #'(lambda (x)
228 (setf (poly-termlist x) (nconc (poly-termlist x)
229 (list (make-term (make-monom :dimension d)
230 (funcall (ring-uminus ring) (funcall (ring-unit ring)))))))
231 x)
232 (poly-standard-extension plist)))
233 (append f plist))
234
235
236(defun polysaturation-extension (ring f plist &aux (k (length plist))
237 (d (+ k (monom-dimension (poly-lm (car plist))))))
238 "Calculate [F, U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]."
239 (setf f (poly-list-add-variables f k)
240 plist (apply #'poly-append (poly-standard-extension plist))
241 (cdr (last (poly-termlist plist))) (list (make-term (make-monom :dimension d)
242 (funcall (ring-uminus ring) (funcall (ring-unit ring))))))
243 (append f (list plist)))
244
245(defun saturation-extension-1 (ring f p) (polysaturation-extension ring f (list p)))
246
247;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
248;;
249;; Evaluation of polynomial (prefix) expressions
250;;
251;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
252
253(defun coerce-coeff (ring expr vars)
254 "Coerce an element of the coefficient ring to a constant polynomial."
255 ;; Modular arithmetic handler by rat
256 (make-poly-from-termlist (list (make-term (make-monom :dimension (length vars))
257 (funcall (ring-parse ring) expr)))
258 0))
259
260(defun poly-eval (expr vars
261 &optional
262 (ring *ring-of-integers*)
263 (order #'lex>)
264 (list-marker :[)
265 &aux
266 (ring-and-order (make-ring-and-order :ring ring :order order)))
267 "Evaluate Lisp form EXPR to a polynomial or a list of polynomials in
268variables VARS. Return the resulting polynomial or list of
269polynomials. Standard arithmetical operators in form EXPR are
270replaced with their analogues in the ring of polynomials, and the
271resulting expression is evaluated, resulting in a polynomial or a list
272of polynomials in internal form. A similar operation in another computer
273algebra system could be called 'expand' or so."
274 (labels ((p-eval (arg) (poly-eval arg vars ring order))
275 (p-eval-scalar (arg) (poly-eval-scalar arg))
276 (p-eval-list (args) (mapcar #'p-eval args))
277 (p-add (x y) (poly-add ring-and-order x y)))
278 (cond
279 ((null expr) (error "Empty expression"))
280 ((eql expr 0) (make-poly-zero))
281 ((member expr vars :test #'equalp)
282 (let ((pos (position expr vars :test #'equalp)))
283 (make-variable ring (length vars) pos)))
284 ((atom expr)
285 (coerce-coeff ring expr vars))
286 ((eq (car expr) list-marker)
287 (cons list-marker (p-eval-list (cdr expr))))
288 (t
289 (case (car expr)
290 (+ (reduce #'p-add (p-eval-list (cdr expr))))
291 (- (case (length expr)
292 (1 (make-poly-zero))
293 (2 (poly-uminus ring (p-eval (cadr expr))))
294 (3 (poly-sub ring-and-order (p-eval (cadr expr)) (p-eval (caddr expr))))
295 (otherwise (poly-sub ring-and-order (p-eval (cadr expr))
296 (reduce #'p-add (p-eval-list (cddr expr)))))))
297 (*
298 (if (endp (cddr expr)) ;unary
299 (p-eval (cdr expr))
300 (reduce #'(lambda (p q) (poly-mul ring-and-order p q)) (p-eval-list (cdr expr)))))
301 (/
302 ;; A polynomial can be divided by a scalar
303 (cond
304 ((endp (cddr expr))
305 ;; A special case (/ ?), the inverse
306 (coerce-coeff ring (apply (ring-div ring) (cdr expr)) vars))
307 (t
308 (let ((num (p-eval (cadr expr)))
309 (denom-inverse (apply (ring-div ring)
310 (cons (funcall (ring-unit ring))
311 (mapcar #'p-eval-scalar (cddr expr))))))
312 (scalar-times-poly ring denom-inverse num)))))
313 (expt
314 (cond
315 ((member (cadr expr) vars :test #'equalp)
316 ;;Special handling of (expt var pow)
317 (let ((pos (position (cadr expr) vars :test #'equalp)))
318 (make-variable ring (length vars) pos (caddr expr))))
319 ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
320 ;; Negative power means division in coefficient ring
321 ;; Non-integer power means non-polynomial coefficient
322 (coerce-coeff ring expr vars))
323 (t (poly-expt ring-and-order (p-eval (cadr expr)) (caddr expr)))))
324 (otherwise
325 (coerce-coeff ring expr vars)))))))
326
327(defun poly-eval-scalar (expr
328 &optional
329 (ring *ring-of-integers*)
330 &aux
331 (order #'lex>))
332 "Evaluate a scalar expression EXPR in ring RING."
333 (poly-lc (poly-eval expr nil ring order)))
334
335(defun spoly (ring-and-order f g
336 &aux
337 (ring (ro-ring ring-and-order)))
338 "It yields the S-polynomial of polynomials F and G."
339 (declare (type poly f g))
340 (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
341 (mf (monom-div lcm (poly-lm f)))
342 (mg (monom-div lcm (poly-lm g))))
343 (declare (type monom mf mg))
344 (multiple-value-bind (c cf cg)
345 (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
346 (declare (ignore c))
347 (poly-sub
348 ring-and-order
349 (scalar-times-poly ring cg (monom-times-poly mf f))
350 (scalar-times-poly ring cf (monom-times-poly mg g))))))
351
352
353(defun poly-primitive-part (ring p)
354 "Divide polynomial P with integer coefficients by gcd of its
355coefficients and return the result."
356 (declare (type poly p))
357 (if (poly-zerop p)
358 (values p 1)
359 (let ((c (poly-content ring p)))
360 (values (make-poly-from-termlist
361 (mapcar
362 #'(lambda (x)
363 (make-term (term-monom x)
364 (funcall (ring-div ring) (term-coeff x) c)))
365 (poly-termlist p))
366 (poly-sugar p))
367 c))))
368
369(defun poly-content (ring p)
370 "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
371to compute the greatest common divisor."
372 (declare (type poly p))
373 (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
374
375(defun read-infix-form (&key (stream t))
376 "Parser of infix expressions with integer/rational coefficients
377The parser will recognize two kinds of polynomial expressions:
378
379- polynomials in fully expanded forms with coefficients
380 written in front of symbolic expressions; constants can be optionally
381 enclosed in (); for example, the infix form
382 X^2-Y^2+(-4/3)*U^2*W^3-5
383 parses to
384 (+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))
385
386- lists of polynomials; for example
387 [X-Y, X^2+3*Z]
388 parses to
389 (:[ (- X Y) (+ (EXPT X 2) (* 3 Z)))
390 where the first symbol [ marks a list of polynomials.
391
392-other infix expressions, for example
393 [(X-Y)*(X+Y)/Z,(X+1)^2]
394parses to:
395 (:[ (/ (* (- X Y) (+ X Y)) Z) (EXPT (+ X 1) 2))
396Currently this function is implemented using M. Kantrowitz's INFIX package."
397 (read-from-string
398 (concatenate 'string
399 "#I("
400 (with-output-to-string (s)
401 (loop
402 (multiple-value-bind (line eof)
403 (read-line stream t)
404 (format s "~A" line)
405 (when eof (return)))))
406 ")")))
407
408(defun read-poly (vars &key
409 (stream t)
410 (ring *ring-of-integers*)
411 (order #'lex>))
412 "Reads an expression in prefix form from a stream STREAM.
413The expression read from the strem should represent a polynomial or a
414list of polynomials in variables VARS, over the ring RING. The
415polynomial or list of polynomials is returned, with terms in each
416polynomial ordered according to monomial order ORDER."
417 (poly-eval (read-infix-form :stream stream) vars ring order))
418
419(defun string->poly (str vars
420 &optional
421 (ring *ring-of-integers*)
422 (order #'lex>))
423 "Converts a string STR to a polynomial in variables VARS."
424 (with-input-from-string (s str)
425 (read-poly vars :stream s :ring ring :order order)))
426
427(defun poly->alist (p)
428 "Convert a polynomial P to an association list. Thus, the format of the
429returned value is ((MONOM[0] . COEFF[0]) (MONOM[1] . COEFF[1]) ...), where
430MONOM[I] is a list of exponents in the monomial and COEFF[I] is the
431corresponding coefficient in the ring."
432 (cond
433 ((poly-p p)
434 (mapcar #'term->cons (poly-termlist p)))
435 ((and (consp p) (eq (car p) :[))
436 (cons :[ (mapcar #'poly->alist (cdr p))))))
437
438(defun string->alist (str vars
439 &optional
440 (ring *ring-of-integers*)
441 (order #'lex>))
442 "Convert a string STR representing a polynomial or polynomial list to
443an association list (... (MONOM . COEFF) ...)."
444 (poly->alist (string->poly str vars ring order)))
Note: See TracBrowser for help on using the repository browser.