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

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

* empty log message *

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