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/termlist.lisp@ 1951

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

* empty log message *

File size: 8.0 KB
RevLine 
[1201]1;;; -*- Mode: Lisp -*-
[150]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
[411]22(defpackage "TERMLIST"
[1604]23 (:use :cl :monom :ring :ring-and-order :term)
[411]24 (:export "TERMLIST-SUGAR"
25 "TERMLIST-CONTRACT"
26 "TERMLIST-EXTEND"
27 "TERMLIST-ADD-VARIABLES"
28 "TERMLIST-LT"
29 "TERMLIST-LM"
30 "TERMLIST-LC"
31 "SCALAR-MUL"
32 "SCALAR-TIMES-TERMLIST"
33 "TERM-MUL-LST"
34 "TERMLIST-TIMES-TERM"
35 "TERM-TIMES-TERMLIST"
36 "MONOM-TIMES-TERM"
37 "MONOM-TIMES-TERMLIST"
38 "TERMLIST-UMINUS"
39 "TERMLIST-ADD"
40 "TERMLIST-SUB"
41 "TERMLIST-MUL"
42 "TERMLIST-UNIT"
43 "TERMLIST-EXPT"))
[150]44
[436]45(in-package :termlist)
46
[1928]47(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
48
[401]49(defun termlist-sugar (p &aux (sugar -1))
50 (declare (fixnum sugar))
51 (dolist (term p sugar)
52 (setf sugar (max sugar (term-sugar term)))))
53
54(defun termlist-contract (p &optional (k 1))
55 "Eliminate first K variables from a polynomial P."
[1829]56 (mapcar #'(lambda (term) (make-term :monom (monom-contract (term-monom term) k)
57 :coeff (term-coeff term)))
[401]58 p))
59
[894]60(defun termlist-extend (p &optional (m (make-monom :dimension 1)))
[401]61 "Extend every monomial in a polynomial P by inserting at the
62beginning of every monomial the list of powers M."
[1951]63 (mapcar #'(lambda (term)
64 (declare (type term term))
65 (make-term :monom (monom-append m (term-monom term))
66 :coeff (term-coeff term)))
[401]67 p))
68
69(defun termlist-add-variables (p n)
70 "Add N variables to a polynomial P by inserting zero powers
71at the beginning of each monomial."
72 (declare (fixnum n))
73 (mapcar #'(lambda (term)
[1950]74 (declare (type term term))
[1831]75 (make-term :monom (monom-append (make-monom :dimension n)
76 (term-monom term))
77 :coeff (term-coeff term)))
[401]78 p))
79
80
[150]81;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
82;;
83;; Low-level polynomial arithmetic done on
84;; lists of terms
85;;
86;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
87
88(defmacro termlist-lt (p) `(car ,p))
89(defun termlist-lm (p) (term-monom (termlist-lt p)))
90(defun termlist-lc (p) (term-coeff (termlist-lt p)))
91
92(define-modify-macro scalar-mul (c) coeff-mul)
93
94(defun scalar-times-termlist (ring c p)
95 "Multiply scalar C by a polynomial P. This function works
96even if there are divisors of 0."
[706]97 (declare (ring ring))
[150]98 (mapcan
99 #'(lambda (term)
100 (let ((c1 (funcall (ring-mul ring) c (term-coeff term))))
101 (unless (funcall (ring-zerop ring) c1)
[1832]102 (list (make-term :monom (term-monom term) :coeff c1)))))
[150]103 p))
104
105
[379]106(defun term-mul-lst (ring term1 term2)
[380]107 "A special version of term multiplication. Returns (LIST TERM) where
108TERM is the product of the terms TERM1 TERM2, or NIL when the product
109is 0. This definition takes care of divisors of 0 in the coefficient
110ring."
[1949]111 (declare (ring ring) (type term1 term2))
[150]112 (let ((c (funcall (ring-mul ring) (term-coeff term1) (term-coeff term2))))
113 (unless (funcall (ring-zerop ring) c)
[1833]114 (list (make-term :monom (monom-mul (term-monom term1) (term-monom term2))
115 :coeff c)))))
[150]116
117(defun term-times-termlist (ring term f)
[1948]118 (declare (type ring ring) (type term term))
[379]119 (mapcan #'(lambda (term-f) (term-mul-lst ring term term-f)) f))
[150]120
121(defun termlist-times-term (ring f term)
[1947]122 (declare (ring ring) (type term term))
[379]123 (mapcan #'(lambda (term-f) (term-mul-lst ring term-f term)) f))
[150]124
125(defun monom-times-term (m term)
[1946]126 (declare (type monom m) (type term term))
[1834]127 (make-term :monom (monom-mul m (term-monom term)) :coeff (term-coeff term)))
[150]128
129(defun monom-times-termlist (m f)
[1945]130 (declare (type monom m))
[150]131 (cond
[1227]132 ((null f) nil)
133 (t
134 (mapcar #'(lambda (x) (monom-times-term m x)) f))))
[150]135
136(defun termlist-uminus (ring f)
[705]137 (declare (ring ring))
[150]138 (mapcar #'(lambda (x)
[1835]139 (make-term :monom (term-monom x)
140 :coeff (funcall (ring-uminus ring) (term-coeff x))))
[150]141 f))
142
[935]143(defun termlist-add (ring-and-order p q
144 &aux
145 (ring (ro-ring ring-and-order))
146 (order (ro-order ring-and-order)))
[1944]147 (declare (ring-and-order ring-and-order) (type list p q))
[150]148 (do (r)
149 ((cond
[935]150 ((endp p)
151 (setf r (revappend r q)) t)
152 ((endp q)
153 (setf r (revappend r p)) t)
154 (t
155 (multiple-value-bind
156 (lm-greater lm-equal)
157 (funcall order (termlist-lm p) (termlist-lm q))
158 (cond
159 (lm-equal
160 (let ((s (funcall (ring-add ring) (termlist-lc p) (termlist-lc q))))
161 (unless (funcall (ring-zerop ring) s) ;check for cancellation
[1836]162 (setf r (cons (make-term :monom (termlist-lm p) :coeff s) r)))
[935]163 (setf p (cdr p) q (cdr q))))
164 (lm-greater
165 (setf r (cons (car p) r)
166 p (cdr p)))
167 (t (setf r (cons (car q) r)
168 q (cdr q)))))
169 nil))
[150]170 r)))
171
[942]172(defun termlist-sub (ring-and-order p q
[935]173 &aux
174 (ring (ro-ring ring-and-order))
175 (order (ro-order ring-and-order)))
[1943]176 (declare (ring-and-order ring-and-order) (type list p q))
[150]177 (do (r)
178 ((cond
[935]179 ((endp p)
180 (setf r (revappend r (termlist-uminus ring q)))
181 t)
182 ((endp q)
183 (setf r (revappend r p))
184 t)
185 (t
186 (multiple-value-bind
187 (mgreater mequal)
188 (funcall order (termlist-lm p) (termlist-lm q))
189 (cond
190 (mequal
191 (let ((s (funcall (ring-sub ring) (termlist-lc p) (termlist-lc q))))
192 (unless (funcall (ring-zerop ring) s) ;check for cancellation
[1837]193 (setf r (cons (make-term :monom (termlist-lm p) :coeff s) r)))
[935]194 (setf p (cdr p) q (cdr q))))
195 (mgreater
196 (setf r (cons (car p) r)
197 p (cdr p)))
[1838]198 (t (setf r (cons (make-term :monom (termlist-lm q)
199 :coeff (funcall (ring-uminus ring) (termlist-lc q))) r)
[935]200 q (cdr q)))))
201 nil))
[150]202 r)))
203
204;; Multiplication of polynomials
205;; Non-destructive version
[936]206(defun termlist-mul (ring-and-order p q
[941]207 &aux (ring (ro-ring ring-and-order)))
[936]208 (declare (ring-and-order ring-and-order))
[150]209 (cond ((or (endp p) (endp q)) nil) ;p or q is 0 (represented by NIL)
210 ;; If p=p0+p1 and q=q0+q1 then pq=p0q0+p0q1+p1q
211 ((endp (cdr p))
212 (term-times-termlist ring (car p) q))
213 ((endp (cdr q))
214 (termlist-times-term ring p (car q)))
215 (t
[379]216 (let ((head (term-mul-lst ring (termlist-lt p) (termlist-lt q)))
[936]217 (tail (termlist-add ring-and-order
218 (term-times-termlist ring (car p) (cdr q))
219 (termlist-mul ring-and-order (cdr p) q))))
[150]220 (cond ((null head) tail)
221 ((null tail) head)
222 (t (nconc head tail)))))))
[1227]223
[815]224(defun termlist-unit (ring dim)
[1942]225 (declare (ring ring) (fixnum dim))
[1839]226 (list (make-term :monom (make-monom :dimension dim)
227 :coeff (funcall (ring-unit ring)))))
[150]228
[1227]229
[937]230(defun termlist-expt (ring-and-order poly n
231 &aux
232 (ring (ro-ring ring-and-order))
233 (dim (monom-dimension (termlist-lm poly))))
[1942]234 (declare (ring-and-order ring-and-order) (type fixnum n dim))
[150]235 (cond
[1227]236 ((minusp n) (error "termlist-expt: Negative exponent."))
237 ((endp poly) (if (zerop n) (termlist-unit ring dim) nil))
238 (t
239 (do ((k 1 (ash k 1))
240 (q poly (termlist-mul ring-and-order q q)) ;keep squaring
241 (p (termlist-unit ring dim) (if (not (zerop (logand k n))) (termlist-mul ring-and-order p q) p)))
242 ((> k n) p)
243 (declare (fixnum k))))))
Note: See TracBrowser for help on using the repository browser.