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

Last change on this file since 1450 was 1441, checked in by Marek Rychlik, 10 years ago

* empty log message *

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