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

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

* empty log message *

File size: 14.0 KB
Line 
1;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*-
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 :term :termlist)
25 (:export "POLY"
26 "POLY-TERMLIST"
27 "POLY-SUGAR"
28 "POLY-LT"
29 "MAKE-POLY-FROM-TERMLIST"
30 "MAKE-POLY-ZERO"
31 "MAKE-VARIABLE"
32 "POLY-UNIT"
33 "POLY-LM"
34 "POLY-SECOND-LM"
35 "POLY-SECOND-LT"
36 "POLY-LC"
37 "POLY-SECOND-LC"
38 "POLY-ZEROP"
39 "POLY-LENGTH"
40 "SCALAR-TIMES-POLY"
41 "SCALAR-TIMES-POLY-1"
42 "MONOM-TIMES-POLY"
43 "TERM-TIMES-POLY"
44 "POLY-ADD"
45 "POLY-SUB"
46 "POLY-UMINUS"
47 "POLY-MUL"
48 "POLY-EXPT"
49 "POLY-APPEND"
50 "POLY-NREVERSE"
51 "POLY-CONTRACT"
52 "POLY-EXTEND"
53 "POLY-ADD-VARIABLES"
54 "POLY-LIST-ADD-VARIABLES"
55 "POLY-STANDARD-EXTENSION"
56 "SATURATION-EXTENSION"
57 "POLYSATURATION-EXTENSION"
58 "SATURATION-EXTENSION-1"
59 "COERCE-COEFF"
60 "POLY-EVAL"
61 "SPOLY"
62 "POLY-PRIMITIVE-PART"
63 "POLY-CONTENT"
64 ))
65
66(in-package :polynomial)
67
68;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
69;;
70;; Polynomials
71;;
72;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
73
74(defstruct (poly
75 ;;
76 ;; BOA constructor, by default constructs zero polynomial
77 (:constructor make-poly-from-termlist (termlist &optional (sugar (termlist-sugar termlist))))
78 (:constructor make-poly-zero (&aux (termlist nil) (sugar -1)))
79 ;; Constructor of polynomials representing a variable
80 (:constructor make-variable (ring nvars pos &optional (power 1)
81 &aux
82 (termlist (list
83 (make-term-variable ring nvars pos power)))
84 (sugar power)))
85 (:constructor poly-unit (ring dimension
86 &aux
87 (termlist (termlist-unit ring dimension))
88 (sugar 0))))
89 (termlist nil :type list)
90 (sugar -1 :type fixnum))
91
92;; Leading term
93(defmacro poly-lt (p) `(car (poly-termlist ,p)))
94
95;; Second term
96(defmacro poly-second-lt (p) `(cadar (poly-termlist ,p)))
97
98;; Leading monomial
99(defun poly-lm (p) (term-monom (poly-lt p)))
100
101;; Second monomial
102(defun poly-second-lm (p) (term-monom (poly-second-lt p)))
103
104;; Leading coefficient
105(defun poly-lc (p) (term-coeff (poly-lt p)))
106
107;; Second coefficient
108(defun poly-second-lc (p) (term-coeff (poly-second-lt p)))
109
110;; Testing for a zero polynomial
111(defun poly-zerop (p) (null (poly-termlist p)))
112
113;; The number of terms
114(defun poly-length (p) (length (poly-termlist p)))
115
116(defun scalar-times-poly (ring c p)
117 (declare (type ring ring) (poly p))
118 (make-poly-from-termlist (scalar-times-termlist ring c (poly-termlist p)) (poly-sugar p)))
119
120;; The scalar product omitting the head term
121(defun scalar-times-poly-1 (ring c p)
122 (declare (type ring ring) (poly p))
123 (make-poly-from-termlist (scalar-times-termlist ring c (cdr (poly-termlist p))) (poly-sugar p)))
124
125(defun monom-times-poly (m p)
126 (declare (poly p))
127 (make-poly-from-termlist
128 (monom-times-termlist m (poly-termlist p))
129 (+ (poly-sugar p) (monom-sugar m))))
130
131(defun term-times-poly (ring term p)
132 (declare (type ring ring) (type term term) (type poly p))
133 (make-poly-from-termlist
134 (term-times-termlist ring term (poly-termlist p))
135 (+ (poly-sugar p) (term-sugar term))))
136
137(defun poly-add (ring-and-order p q)
138 (declare (type ring-and-order ring-and-order) (type poly p q))
139 (make-poly-from-termlist
140 (termlist-add ring-and-order
141 (poly-termlist p)
142 (poly-termlist q))
143 (max (poly-sugar p) (poly-sugar q))))
144
145(defun poly-sub (ring-and-order p q)
146 (declare (type ring-and-order ring-and-order) (type poly p q))
147 (make-poly-from-termlist
148 (termlist-sub ring-and-order (poly-termlist p) (poly-termlist q))
149 (max (poly-sugar p) (poly-sugar q))))
150
151(defun poly-uminus (ring p)
152 (declare (type ring ring) (type poly p))
153 (make-poly-from-termlist
154 (termlist-uminus ring (poly-termlist p))
155 (poly-sugar p)))
156
157(defun poly-mul (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-mul ring-and-order (poly-termlist p) (poly-termlist q))
161 (+ (poly-sugar p) (poly-sugar q))))
162
163(defun poly-expt (ring-and-order p n)
164 (declare (type ring-and-order ring-and-order) (type poly p))
165 (make-poly-from-termlist (termlist-expt ring-and-order (poly-termlist p) n) (* n (poly-sugar p))))
166
167(defun poly-append (&rest plist)
168 (make-poly-from-termlist (apply #'append (mapcar #'poly-termlist plist))
169 (apply #'max (mapcar #'poly-sugar plist))))
170
171(defun poly-nreverse (p)
172 (declare (type poly p))
173 (setf (poly-termlist p) (nreverse (poly-termlist p)))
174 p)
175
176(defun poly-contract (p &optional (k 1))
177 (declare (type poly p))
178 (make-poly-from-termlist (termlist-contract (poly-termlist p) k)
179 (poly-sugar p)))
180
181(defun poly-extend (p &optional (m (make-monom :dimension 1)))
182 (declare (type poly p))
183 (make-poly-from-termlist
184 (termlist-extend (poly-termlist p) m)
185 (+ (poly-sugar p) (monom-sugar m))))
186
187(defun poly-add-variables (p k)
188 (declare (type poly p))
189 (setf (poly-termlist p) (termlist-add-variables (poly-termlist p) k))
190 p)
191
192(defun poly-list-add-variables (plist k)
193 (mapcar #'(lambda (p) (poly-add-variables p k)) plist))
194
195(defun poly-standard-extension (plist &aux (k (length plist)))
196 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]."
197 (declare (list plist) (fixnum k))
198 (labels ((incf-power (g i)
199 (dolist (x (poly-termlist g))
200 (incf (monom-elt (term-monom x) i)))
201 (incf (poly-sugar g))))
202 (setf plist (poly-list-add-variables plist k))
203 (dotimes (i k plist)
204 (incf-power (nth i plist) i))))
205
206(defun saturation-extension (ring f plist &aux (k (length plist)) (d (monom-dimension (poly-lm (car plist)))))
207 "Calculate [F, U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK]."
208 (setf f (poly-list-add-variables f k)
209 plist (mapcar #'(lambda (x)
210 (setf (poly-termlist x) (nconc (poly-termlist x)
211 (list (make-term (make-monom :dimension d)
212 (funcall (ring-uminus ring) (funcall (ring-unit ring)))))))
213 x)
214 (poly-standard-extension plist)))
215 (append f plist))
216
217
218(defun polysaturation-extension (ring f plist &aux (k (length plist))
219 (d (+ k (monom-dimension (poly-lm (car plist))))))
220 "Calculate [F, U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]."
221 (setf f (poly-list-add-variables f k)
222 plist (apply #'poly-append (poly-standard-extension plist))
223 (cdr (last (poly-termlist plist))) (list (make-term (make-monom :dimension d)
224 (funcall (ring-uminus ring) (funcall (ring-unit ring))))))
225 (append f (list plist)))
226
227(defun saturation-extension-1 (ring f p) (polysaturation-extension ring f (list p)))
228
229;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
230;;
231;; Evaluation of polynomial (prefix) expressions
232;;
233;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
234
235(defun coerce-coeff (ring expr vars)
236 "Coerce an element of the coefficient ring to a constant polynomial."
237 ;; Modular arithmetic handler by rat
238 (make-poly-from-termlist (list (make-term (make-monom :dimension (length vars))
239 (funcall (ring-parse ring) expr)))
240 0))
241
242(defun poly-eval (expr vars
243 &optional
244 (ring *ring-of-integers*)
245 (order #'lex)
246 (list-marker '[)
247 &aux (ring (ro-ring ring-and-order)))
248 (labels ((p-eval (arg) (poly-eval ring-and-order arg vars))
249 (p-eval-list (args) (mapcar #'p-eval args))
250 (p-add (x y) (poly-add ring-and-order x y)))
251 (cond
252 ((eql expr 0) (make-poly-zero))
253 ((member expr vars :test #'equalp)
254 (let ((pos (position expr vars :test #'equalp)))
255 (make-variable ring (length vars) pos)))
256 ((atom expr)
257 (coerce-coeff ring expr vars))
258 ((eq (car expr) list-marker)
259 (cons list-marker (p-eval-list (cdr expr))))
260 (t
261 (case (car expr)
262 (+ (reduce #'p-add (p-eval-list (cdr expr))))
263 (- (case (length expr)
264 (1 (make-poly-zero))
265 (2 (poly-uminus ring (p-eval (cadr expr))))
266 (3 (poly-sub ring-and-order (p-eval (cadr expr)) (p-eval (caddr expr))))
267 (otherwise (poly-sub ring-and-order (p-eval (cadr expr))
268 (reduce #'p-add (p-eval-list (cddr expr)))))))
269 (*
270 (if (endp (cddr expr)) ;unary
271 (p-eval (cdr expr))
272 (reduce #'(lambda (p q) (poly-mul ring-and-order p q)) (p-eval-list (cdr expr)))))
273 (expt
274 (cond
275 ((member (cadr expr) vars :test #'equalp)
276 ;;Special handling of (expt var pow)
277 (let ((pos (position (cadr expr) vars :test #'equalp)))
278 (make-variable ring (length vars) pos (caddr expr))))
279 ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
280 ;; Negative power means division in coefficient ring
281 ;; Non-integer power means non-polynomial coefficient
282 (coerce-coeff ring expr vars))
283 (t (poly-expt ring-and-order (p-eval (cadr expr)) (caddr expr)))))
284 (otherwise
285 (coerce-coeff ring expr vars)))))))
286
287(defun spoly (ring f g)
288 "It yields the S-polynomial of polynomials F and G."
289 (declare (type poly f g))
290 (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
291 (mf (monom-div lcm (poly-lm f)))
292 (mg (monom-div lcm (poly-lm g))))
293 (declare (type monom mf mg))
294 (multiple-value-bind (c cf cg)
295 (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
296 (declare (ignore c))
297 (poly-sub
298 ring
299 (scalar-times-poly ring cg (monom-times-poly mf f))
300 (scalar-times-poly ring cf (monom-times-poly mg g))))))
301
302
303(defun poly-primitive-part (ring p)
304 "Divide polynomial P with integer coefficients by gcd of its
305coefficients and return the result."
306 (declare (type poly p))
307 (if (poly-zerop p)
308 (values p 1)
309 (let ((c (poly-content ring p)))
310 (values (make-poly-from-termlist (mapcar
311 #'(lambda (x)
312 (make-term (term-monom x)
313 (funcall (ring-div ring) (term-coeff x) c)))
314 (poly-termlist p))
315 (poly-sugar p))
316 c))))
317
318(defun poly-content (ring p)
319 "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
320to compute the greatest common divisor."
321 (declare (type poly p))
322 (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
323
324;; Return the standard basis of the monomials in n variables
325(defun variable-basis (ring n &aux (basis (make-list n)))
326 "Generate a list of polynomials X[i], i=0,1,...,N-1."
327 (dotimes (i n basis)
328 (setf (elt basis i) (make-variable ring n i))))
329
330#|
331(defun poly-eval-1 (expr vars &optional (ring *ring-of-integers*) (order #'lex>)
332 &aux
333 (ring-and-order (make-ring-and-order :ring ring :order order))
334 (n (length vars))
335 (basis (variable-basis ring (length vars))))
336 "Evaluate an expression EXPR as polynomial by substituting operators
337+ - * expt with corresponding polynomial operators and variables VARS
338with the corresponding polynomials in internal form. We use special
339versions of binary operators $poly+, $poly-, $minus-poly, $poly* and
340$poly-expt which work like the corresponding functions in the POLY
341package, but accept scalars as arguments as well. The result is a
342polynomial in internal form. This operation is somewhat similar to
343the function EXPAND in CAS."
344 (cond
345 ((numberp expr)
346 (cond
347 ((zerop expr) NIL)
348 (t (make-poly-from-termlist (list (make-term (make-monom :dimension n) expr))))))
349 ((symbolp expr)
350 (nth (position expr vars) basis))
351 ((consp expr)
352 (case (car expr)
353 (expt
354 (if (= (length expr) 3)
355 ($poly-expt ring-and-order
356 (poly-eval-1 (cadr expr) vars ring order)
357 (caddr expr)
358 n)
359 (error "Too many arguments to EXPT")))
360 (/
361 (if (and (= (length expr) 3)
362 (numberp (caddr expr)))
363 ($poly/ ring (cadr expr) (caddr expr))
364 (error "The second argument to / must be a number")))
365 (otherwise
366 (let ((r (mapcar
367 #'(lambda (e) (poly-eval-1 e vars ring order))
368 (cdr expr))))
369 (ecase (car expr)
370 (+ (reduce #'(lambda (p q) ($poly+ ring-and-order p q n)) r))
371 (-
372 (if (endp (cdr r))
373 ($minus-poly ring (car r) n)
374 ($poly- ring-and-order
375 (car r)
376 (reduce #'(lambda (p q) ($poly+ ring-and-order p q n)) (cdr r))
377 n)))
378 (*
379 (reduce #'(lambda (p q) ($poly* ring-and-order p q n)) r))
380 )))))))
381
382
383
384(defun poly-eval (expr vars &optional (order #'lex>) (ring *ring-of-integers*))
385 "Evaluate an expression EXPR, which should be a polynomial
386expression or a list of polynomial expressions (a list of expressions
387marked by prepending keyword :[ to it) given in Lisp prefix notation,
388in variables VARS, which should be a list of symbols. The result of
389the evaluation is a polynomial or a list of polynomials (marked by
390prepending symbol '[) in the internal alist form. This evaluator is
391used by the PARSE package to convert input from strings directly to
392internal form."
393 (cond
394 ((numberp expr)
395 (unless (zerop expr)
396 (make-poly-from-termlist
397 (list (make-term (make-monom :dimension (length vars)) expr)))))
398 ((or (symbolp expr) (not (eq (car expr) :[)))
399 (poly-eval-1 expr vars ring order))
400 (t (cons '[ (mapcar #'(lambda (p) (poly-eval-1 p vars ring order)) (rest expr))))))
401
402|#
Note: See TracBrowser for help on using the repository browser.