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

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

* empty log message *

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