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

Last change on this file since 2280 was 2280, checked in by Marek Rychlik, 10 years ago

* empty log message *

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