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

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

* empty log message *

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