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/monom.lisp@ 3291

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

* empty log message *

File size: 11.2 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(defpackage "MONOM"
23 (:use :cl :ring)
24 (:export "MONOM"
25 "EXPONENT"
26 "MONOM-DIMENSION"
27 "MONOM-EXPONENTS"
28 "MAKE-MONOM-VARIABLE")
29 (:documentation
30 "This package implements basic operations on monomials.
31DATA STRUCTURES: Conceptually, monomials can be represented as lists:
32
33 monom: (n1 n2 ... nk) where ni are non-negative integers
34
35However, lists may be implemented as other sequence types, so the
36flexibility to change the representation should be maintained in the
37code to use general operations on sequences whenever possible. The
38optimization for the actual representation should be left to
39declarations and the compiler.
40
41EXAMPLES: Suppose that variables are x and y. Then
42
43 Monom x*y^2 ---> (1 2) "))
44
45(in-package :monom)
46
47(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
48
49(deftype exponent ()
50 "Type of exponent in a monomial."
51 'fixnum)
52
53(defclass monom ()
54 ((dimension :initarg :dimension :accessor monom-dimension
55 :documentation "The number of variables.")
56 (exponents :initarg :exponents :accessor monom-exponents
57 :documentation "The powers of the variables."))
58 ;; default-initargs are not needed, they are handled by SHARED-INITIALIZE
59 ;;(:default-initargs :dimension 'foo :exponents 'bar :exponent 'baz)
60 (:documentation
61 "Implements a monomial, i.e. a product of powers
62of variables, like X*Y^2."))
63
64(defmethod print-object ((self monom) stream)
65 (print-unreadable-object (self stream :type t :identity t)
66 (with-accessors ((dimension monom-dimension) (exponents monom-exponents))
67 self
68 (format stream "DIMENSION=~A EXPONENTS=~A"
69 dimension exponents))))
70
71;; SHARED-INITIALIZE allows instance initialization in a style similar to MAKE-ARRAY, e.g.
72;;
73;; (MAKE-INSTANCE :EXPONENTS '(1 2 3)) --> #<MONOM DIMENSION=3 EXPONENTS=#(1 2 3)>
74;; (MAKE-INSTANCE :DIMENSION 3) --> #<MONOM DIMENSION=3 EXPONENTS=#(0 0 0)>
75;; (MAKE-INSTANCE :DIMENSION 3 :EXPONENT 7) --> #<MONOM DIMENSION=3 EXPONENTS=#(7 7 7)>
76;;
77(defmethod shared-initialize :after ((self monom) slot-names
78 &key
79 (dimension 0 dimension-supplied-p)
80 (exponents nil exponents-supplied-p)
81 (exponent nil exponent-supplied-p)
82 &allow-other-keys
83 )
84 (if (eq slot-names t) (setf slot-names '(dimension exponents)))
85 (dolist (slot-name slot-names)
86 (case slot-name
87 (dimension
88 (cond (dimension-supplied-p
89 (setf (slot-value self 'dimension) dimension))
90 (exponents-supplied-p
91 (setf (slot-value self 'dimension) (length exponents)))
92 (t
93 (error "DIMENSION or EXPONENTS must be supplied."))))
94 (exponents
95 (cond
96 (exponents-supplied-p
97 (let ((dim (length exponents)))
98 (when (and dimension-supplied-p (/= dimension dim))
99 (error "EXPONENTS must have length DIMENSION"))
100 (setf (slot-value self 'dimension) dim
101 (slot-value self 'exponents) (make-array dim :initial-contents exponents))))
102 ;; when all exponents are to be identical
103 (t
104 (let ((dim (slot-value self 'dimension)))
105 (setf (slot-value self 'exponents)
106 (make-array (list dim) :initial-element (or exponent 0)
107 :element-type 'exponent)))))))))
108
109(defmethod r-equalp ((m1 monom) (m2 monom))
110 "Returns T iff monomials M1 and M2 have identical
111EXPONENTS."
112 (equalp (monom-exponents m1) (monom-exponents m2)))
113
114(defmethod r-coeff ((m monom))
115 "A MONOM can be treated as a special case of TERM,
116where the coefficient is 1."
117 1)
118
119(defmethod r-elt ((m monom) index)
120 "Return the power in the monomial M of variable number INDEX."
121 (with-slots (exponents)
122 m
123 (elt exponents index)))
124
125(defmethod (setf r-elt) (new-value (m monom) index)
126 "Return the power in the monomial M of variable number INDEX."
127 (with-slots (exponents)
128 m
129 (setf (elt exponents index) new-value)))
130
131(defmethod r-total-degree ((m monom) &optional (start 0) (end (monom-dimension m)))
132 "Return the todal degree of a monomoal M. Optinally, a range
133of variables may be specified with arguments START and END."
134 (declare (type fixnum start end))
135 (with-slots (exponents)
136 m
137 (reduce #'+ exponents :start start :end end)))
138
139
140(defmethod r-sugar ((m monom) &aux (start 0) (end (monom-dimension m)))
141 "Return the sugar of a monomial M. Optinally, a range
142of variables may be specified with arguments START and END."
143 (declare (type fixnum start end))
144 (r-total-degree m start end))
145
146(defmethod multiply-by ((self monom) (other monom))
147 (with-slots ((exponents1 exponents) (dimension1 dimension))
148 self
149 (with-slots ((exponents2 exponents) (dimension2 dimension))
150 other
151 (unless (= dimension1 dimension2)
152 (error "Incompatible dimensions: ~A and ~A.~%" dimension1 dimension2))
153 (map-into exponents1 #'+ exponents1 exponents2)))
154 self)
155
156(defmethod divide-by ((self monom) (other monom))
157 (with-slots ((exponents1 exponents) (dimension1 dimension))
158 self
159 (with-slots ((exponents2 exponents) (dimension2 dimension))
160 other
161 (unless (= dimension1 dimension2)
162 (error "Incompatible dimensions: ~A and ~A.~%" dimension1 dimension2))
163 (map-into exponents1 #'- exponents1 exponents2)))
164 self)
165
166(defmethod copy-instance :around ((object monom) &rest initargs &key &allow-other-keys)
167 "An :AROUNT method for COPY-INSTANCE. The primary method is a shallow copy,
168 while for monomials we typically need a fresh copy of the
169 exponents."
170 (declare (ignore object initargs))
171 (let ((copy (call-next-method)))
172 (setf (monom-exponents copy) (copy-seq (monom-exponents copy)))
173 copy))
174
175(defmethod r* ((m1 monom) (m2 monom))
176 "Non-destructively multiply monomial M1 by M2."
177 (multiply-by (copy-instance m1) (copy-instance m2)))
178
179(defmethod r/ ((m1 monom) (m2 monom))
180 "Non-destructively divide monomial M1 by monomial M2."
181 (divide-by (copy-instance m1) (copy-instance m2)))
182
183(defmethod r-divides-p ((m1 monom) (m2 monom))
184 "Returns T if monomial M1 divides monomial M2, NIL otherwise."
185 (with-slots ((exponents1 exponents))
186 m1
187 (with-slots ((exponents2 exponents))
188 m2
189 (every #'<= exponents1 exponents2))))
190
191
192(defmethod r-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom))
193 "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
194 (every #'(lambda (x y z) (<= x (max y z)))
195 m1 m2 m3))
196
197
198(defmethod r-lcm-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
199 "Returns T if monomial MONOM-LCM(M1,M2) divides MONOM-LCM(M3,M4), NIL otherwise."
200 (declare (type monom m1 m2 m3 m4))
201 (every #'(lambda (x y z w) (<= (max x y) (max z w)))
202 m1 m2 m3 m4))
203
204(defmethod r-lcm-equal-lcm-p (m1 m2 m3 m4)
205 "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
206 (with-slots ((exponents1 exponents))
207 m1
208 (with-slots ((exponents2 exponents))
209 m2
210 (with-slots ((exponents3 exponents))
211 m3
212 (with-slots ((exponents4 exponents))
213 m4
214 (every
215 #'(lambda (x y z w) (= (max x y) (max z w)))
216 exponents1 exponents2 exponents3 exponents4))))))
217
218(defmethod r-divisible-by-p ((m1 monom) (m2 monom))
219 "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
220 (with-slots ((exponents1 exponents))
221 m1
222 (with-slots ((exponents2 exponents))
223 m2
224 (every #'>= exponents1 exponents2))))
225
226(defmethod r-rel-prime-p ((m1 monom) (m2 monom))
227 "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
228 (with-slots ((exponents1 exponents))
229 m1
230 (with-slots ((exponents2 exponents))
231 m2
232 (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2))))
233
234
235(defmethod r-lcm ((m1 monom) (m2 monom))
236 "Returns least common multiple of monomials M1 and M2."
237 (with-slots ((exponents1 exponents) (dimension1 dimension))
238 m1
239 (with-slots ((exponents2 exponents))
240 m2
241 (let* ((exponents (copy-seq exponents1))
242 (dimension dimension1))
243 (map-into exponents #'max exponents1 exponents2)
244 (make-instance 'monom :dimension dimension :exponents exponents)))))
245
246
247(defmethod r-gcd ((m1 monom) (m2 monom))
248 "Returns greatest common divisor of monomials M1 and M2."
249 (with-slots ((exponents1 exponents) (dimension1 dimension))
250 m1
251 (with-slots ((exponents2 exponents))
252 m2
253 (let* ((exponents (copy-seq exponents1))
254 (dimension dimension1))
255 (map-into exponents #'min exponents1 exponents2)
256 (make-instance 'monom :dimension dimension :exponents exponents)))))
257
258(defmethod r-depends-p ((m monom) k)
259 "Return T if the monomial M depends on variable number K."
260 (declare (type fixnum k))
261 (with-slots (exponents)
262 m
263 (plusp (elt exponents k))))
264
265(defmethod left-tensor-product-by ((self monom) (other monom))
266 (with-slots ((exponents1 exponents) (dimension1 dimension))
267 self
268 (with-slots ((exponents2 exponents) (dimension2 dimension))
269 other
270 (setf dimension1 (+ dimension1 dimension2)
271 exponents1 (concatenate 'vector exponents2 exponents1))))
272 self)
273
274(defmethod right-tensor-product-by ((self monom) (other monom))
275 (with-slots ((exponents1 exponents) (dimension1 dimension))
276 self
277 (with-slots ((exponents2 exponents) (dimension2 dimension))
278 other
279 (setf dimension1 (+ dimension1 dimension2)
280 exponents1 (concatenate 'vector exponents1 exponents2))))
281 self)
282
283(defmethod left-contract ((self monom) k)
284 "Drop the first K variables in monomial M."
285 (declare (fixnum k))
286 (with-slots (dimension exponents)
287 self
288 (setf dimension (- dimension k)
289 exponents (subseq exponents k)))
290 self)
291
292(defun make-monom-variable (nvars pos &optional (power 1)
293 &aux (m (make-instance 'monom :dimension nvars)))
294 "Construct a monomial in the polynomial ring
295RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
296which represents a single variable. It assumes number of variables
297NVARS and the variable is at position POS. Optionally, the variable
298may appear raised to power POWER. "
299 (declare (type fixnum nvars pos power) (type monom m))
300 (with-slots (exponents)
301 m
302 (setf (elt exponents pos) power)
303 m))
304
305(defmethod r->list ((m monom))
306 "A human-readable representation of a monomial M as a list of exponents."
307 (coerce (monom-exponents m) 'list))
308
309(defmethod r-dimension ((self monom))
310 (monom-dimension self))
311
312(defmethod r-exponents ((self monom))
313 (monom-exponents self))
Note: See TracBrowser for help on using the repository browser.