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

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

* empty log message *

File size: 10.1 KB
RevLine 
[1201]1;;; -*- Mode: Lisp -*-
[81]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
[418]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;;
[714]37;; Monom x*y^2 ---> (1 2)
[418]38;;
39;;----------------------------------------------------------------
40
[1610]41(defpackage "MONOM"
[2025]42 (:use :cl :ring)
[2115]43 (:shadowing-import-from-package :ring "ZEROP" "LCM" "+" "-" "*" "/")
[422]44 (:export "MONOM"
[423]45 "EXPONENT"
[422]46 "MAKE-MONOM"
[887]47 "MAKE-MONOM-VARIABLE"
[396]48 "MONOM-ELT"
49 "MONOM-DIMENSION"
50 "MONOM-TOTAL-DEGREE"
51 "MONOM-SUGAR"
52 "MONOM-DIV"
53 "MONOM-MUL"
54 "MONOM-DIVIDES-P"
[395]55 "MONOM-DIVIDES-MONOM-LCM-P"
56 "MONOM-LCM-DIVIDES-MONOM-LCM-P"
[497]57 "MONOM-LCM-EQUAL-MONOM-LCM-P"
[395]58 "MONOM-DIVISIBLE-BY-P"
59 "MONOM-REL-PRIME-P"
60 "MONOM-EQUAL-P"
61 "MONOM-LCM"
62 "MONOM-GCD"
[504]63 "MONOM-DEPENDS-P"
[395]64 "MONOM-MAP"
65 "MONOM-APPEND"
66 "MONOM-CONTRACT"
[1153]67 "MONOM->LIST"))
[81]68
[1610]69(in-package :monom)
[48]70
[1925]71(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
[1923]72
[48]73(deftype exponent ()
74 "Type of exponent in a monomial."
75 'fixnum)
76
[2022]77(defclass monom ()
[2049]78 ((dim :initarg :dim )
[2022]79 (exponents :initarg :exponents))
80 (:default-initargs :dim 0 :exponents nil))
[880]81
[2028]82(defmethod print-object ((m monom) stream)
[2036]83 (princ (slot-value m 'exponents) stream))
[2027]84
[884]85;; If a monomial is redefined as structure with slot EXPONENTS, the function
86;; below can be the BOA constructor.
[873]87(defun make-monom (&key
88 (dimension nil dimension-suppied-p)
89 (initial-exponents nil initial-exponents-supplied-p)
90 (initial-exponent nil initial-exponent-supplied-p)
91 &aux
92 (dim (cond (dimension-suppied-p dimension)
93 (initial-exponents-supplied-p (length initial-exponents))
[2028]94 (t (error "You must provide DIMENSION or INITIAL-EXPONENTS"))))
[2022]95 (exponents (cond
96 ;; when exponents are supplied
97 (initial-exponents-supplied-p
98 (make-array (list dim) :initial-contents initial-exponents
99 :element-type 'exponent))
100 ;; when all exponents are to be identical
101 (initial-exponent-supplied-p
102 (make-array (list dim) :initial-element initial-exponent
103 :element-type 'exponent))
104 ;; otherwise, all exponents are zero
105 (t
106 (make-array (list dim) :element-type 'exponent :initial-element 0)))))
[1600]107 "A constructor (factory) of monomials. If DIMENSION is given, a sequence of
[1599]108DIMENSION elements of type EXPONENT is constructed, where individual
109elements are the value of INITIAL-EXPONENT, which defaults to 0.
110Alternatively, all elements may be specified as a list
111INITIAL-EXPONENTS."
[2022]112 (make-instance 'monom :dim dim :exponents exponents))
[717]113
[48]114;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
115;;
116;; Operations on monomials
117;;
118;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
119
[2023]120(defmethod dimension ((m monom))
[2026]121 (slot-value m 'dim))
[745]122
[2023]123(defmethod ring-elt ((m monom) index)
[48]124 "Return the power in the monomial M of variable number INDEX."
[2023]125 (with-slots (exponents)
126 m
127 (elt exponents index)))
[48]128
[2023]129(defmethod (setf ring-elt) (new-value (m monom) index)
130 "Return the power in the monomial M of variable number INDEX."
131 (with-slots (exponents)
132 m
[2030]133 (setf (elt exponents index) new-value)))
[2023]134
[2055]135(defmethod total-degree ((m monom) &optional (start 0) (end (dimension m)))
[48]136 "Return the todal degree of a monomoal M. Optinally, a range
137of variables may be specified with arguments START and END."
[2023]138 (declare (type fixnum start end))
139 (with-slots (exponents)
140 m
[2112]141 (reduce #'cl:+ exponents :start start :end end)))
[48]142
[2064]143
[2031]144(defmethod sugar ((m monom) &aux (start 0) (end (dimension m)))
[48]145 "Return the sugar of a monomial M. Optinally, a range
146of variables may be specified with arguments START and END."
[2032]147 (declare (type fixnum start end))
[2064]148 (with-slots (exponents)
149 m
150 (total-degree exponents start end)))
[48]151
[2109]152(defmethod + ((m1 monom) (m2 monom))
[2072]153 "Multiply monomial M1 by monomial M2."
[2038]154 (with-slots ((exponents1 exponents))
155 m1
156 (with-slots ((exponents2 exponents))
157 m2
158 (let* ((exponents (copy-seq exponents1))
[2112]159 (dim (reduce #'cl:+ exponents)))
160 (map-into exponents #'cl:+ exponents1 exponents2)
[2038]161 (make-instance 'monom :dim dim :exponents exponents)))))
162
[2069]163
164
[2109]165(defmethod / ((m1 monom) (m2 monom))
[1896]166 "Divide monomial M1 by monomial M2."
[2037]167 (with-slots ((exponents1 exponents))
[2034]168 m1
[2037]169 (with-slots ((exponents2 exponents))
[2034]170 m2
171 (let* ((exponents (copy-seq exponents1))
[2112]172 (dim (reduce #'cl:+ exponents)))
173 (map-into exponents #'cl:- exponents1 exponents2)
[2034]174 (make-instance 'monom :dim dim :exponents exponents)))))
[48]175
[2055]176(defmethod divides-p ((m1 monom) (m2 monom))
[48]177 "Returns T if monomial M1 divides monomial M2, NIL otherwise."
[2039]178 (with-slots ((exponents1 exponents))
179 m1
180 (with-slots ((exponents2 exponents))
181 m2
182 (every #'<= exponents1 exponents2))))
[48]183
[2075]184
[2055]185(defmethod divides-lcm-p ((m1 monom) (m2 monom) (m3 monom))
186 "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
[875]187 (every #'(lambda (x y z) (<= x (max y z)))
[869]188 m1 m2 m3))
[48]189
[2049]190
[2069]191(defmethod lcm-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
[48]192 "Returns T if monomial MONOM-LCM(M1,M2) divides MONOM-LCM(M3,M4), NIL otherwise."
[1890]193 (declare (type monom m1 m2 m3 m4))
[869]194 (every #'(lambda (x y z w) (<= (max x y) (max z w)))
195 m1 m2 m3 m4))
196
[2076]197(defmethod lcm-equal-lcm-p (m1 m2 m3 m4)
[2075]198 "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
[2076]199 (with-slots (exponents1 exponents)
200 m1
201 (with-slots (exponents2 exponents)
202 m2
203 (with-slots (exponents3 exponents)
204 m3
205 (with-slots (exponents4 exponents)
206 m4
[2077]207 (every
208 #'(lambda (x y z w) (= (max x y) (max z w)))
209 exponents1 exponents2 exponents3 exponents4))))))
[48]210
[2076]211(defmethod divisible-by-p ((m1 monom) (m2 monom))
[48]212 "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
[2078]213
[869]214 (every #'>= m1 m2))
[48]215
[2076]216(defmethod rel-prime-p ((m1 monom) (m2 monom))
[48]217 "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
[2078]218 (with-slots (exponents1 exponents)
219 m1
220 (with-slots (exponents2 exponents)
221 m2
[2110]222 (every #'(lambda (x y) (cl:zerop (min x y))) exponents1 exponents2))))
[48]223
[2076]224
[2079]225(defmethod equal-p ((m1 monom) (m2 monom))
[48]226 "Returns T if two monomials M1 and M2 are equal."
[2079]227 (with-slots (exponents1 exponents)
228 m1
229 (with-slots (exponents2 exponents)
230 m2
231 (every #'= exponents1 exponents2))))
[48]232
[2111]233(defmethod lcm ((m1 monom) (m2 monom))
[48]234 "Returns least common multiple of monomials M1 and M2."
[2082]235 (with-slots (exponents1 exponents)
236 m1
237 (with-slots (exponents2 exponents)
238 m2
239 (let* ((exponents (copy-seq exponents1))
[2112]240 (dim (reduce #'cl:+ exponents)))
[2082]241 (map-into exponents #'max exponents1 exponents2)
242 (make-instance 'monom :dim dim :exponents exponents)))))
[48]243
[2080]244
[2113]245(defmethod gcd ((m1 monom) (m2 monom))
[48]246 "Returns greatest common divisor of monomials M1 and M2."
[2082]247 (with-slots (exponents1 exponents)
248 m1
249 (with-slots (exponents2 exponents)
250 m2
251 (let* ((exponents (copy-seq exponents1))
[2112]252 (dim (reduce #'cl:+ exponents)))
[2082]253 (map-into exponents #'min exponents1 exponents2)
254 (make-instance 'monom :dim dim :exponents exponents)))))
[48]255
[2084]256(defmethod depends-p ((m monom) k)
[48]257 "Return T if the monomial M depends on variable number K."
[2083]258 (declare (type fixnum k))
259 (with-slots (exponents)
260 m
261 (plusp (elt exponents k))))
[48]262
[2092]263(defmethod ring-tensor-mul ((m1 monom) (m2 monom)
[2114]264 &aux (dim (cl:+ (dimension m1) (dimension m2))))
[2085]265 (declare (fixnum dim))
[2087]266 (with-slots (exponents1 exponents)
267 m1
268 (with-slots (exponents2 exponents)
269 m2
[2088]270 (make-instance 'monom
271 :dim dim
[2087]272 :exponents (concatenate 'vector exponents1 exponents2)))))
[48]273
[2085]274(defmethod contract ((m monom) k)
[1638]275 "Drop the first K variables in monomial M."
[2085]276 (declare (fixnum k))
277 (with-slots (dim exponents)
278 m
279 (setf dim (- dim k)
280 exponents (subseq exponents k))))
[886]281
282(defun make-monom-variable (nvars pos &optional (power 1)
283 &aux (m (make-monom :dimension nvars)))
284 "Construct a monomial in the polynomial ring
285RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
286which represents a single variable. It assumes number of variables
287NVARS and the variable is at position POS. Optionally, the variable
288may appear raised to power POWER. "
[1924]289 (declare (type fixnum nvars pos power) (type monom m))
[2089]290 (with-slots (exponents)
291 m
292 (setf (elt exponents pos) power)
293 m))
[1151]294
[2086]295(defmethod monom->list ((m monom))
[1152]296 "A human-readable representation of a monomial M as a list of exponents."
[2086]297 (with-slots (exponents)
298 m
299 (coerce exponents 'list)))
Note: See TracBrowser for help on using the repository browser.