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

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