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

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

* empty log message *

File size: 10.5 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
[1610]22(defpackage "MONOM"
[2025]23 (:use :cl :ring)
[422]24 (:export "MONOM"
[423]25 "EXPONENT"
[2781]26 "MONOM-DIMENSION"
27 "MONOM-EXPONENTS"
[2524]28 "MAKE-MONOM-VARIABLE")
29 (:documentation
30 "This package implements basic operations on monomials.
31DATA STRUCTURES: Conceptually, monomials can be represented as lists:
[81]32
[2524]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
[1610]45(in-package :monom)
[48]46
[1925]47(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
[1923]48
[48]49(deftype exponent ()
50 "Type of exponent in a monomial."
51 'fixnum)
52
[2022]53(defclass monom ()
[3312]54 ((exponents :initarg :exponents :accessor monom-exponents
[3054]55 :documentation "The powers of the variables."))
[3289]56 ;; default-initargs are not needed, they are handled by SHARED-INITIALIZE
57 ;;(:default-initargs :dimension 'foo :exponents 'bar :exponent 'baz)
[2779]58 (:documentation
59 "Implements a monomial, i.e. a product of powers
60of variables, like X*Y^2."))
[880]61
[2245]62(defmethod print-object ((self monom) stream)
[3196]63 (print-unreadable-object (self stream :type t :identity t)
[3313]64 (with-accessors ((exponents monom-exponents))
[3216]65 self
[3313]66 (format stream "EXPONENTS=~A"
67 exponents))))
[2027]68
[3299]69(defmethod initialize-instance :after ((self monom)
[3297]70 &key
71 (dimension 0 dimension-supplied-p)
72 (exponents nil exponents-supplied-p)
[3318]73 (exponent 0)
[3297]74 &allow-other-keys
[2390]75 )
[3329]76 "The following INITIALIZE-INSTANCE method allows instance initialization
77of a MONOM in a style similar to MAKE-ARRAY, e.g.:
[3328]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)>
[3329]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.
[3328]87"
[3315]88 (cond
89 (exponents-supplied-p
[3327]90 (when (and dimension-supplied-p
91 (/= dimension (length exponents)))
92 (error "EXPONENTS (~A) must have supplied length DIMENSION (~A)"
93 exponents dimension))
[3315]94 (let ((dim (length exponents)))
95 (setf (slot-value self 'exponents) (make-array dim :initial-contents exponents))))
[3321]96 (dimension-supplied-p
[3315]97 ;; when all exponents are to be identical
[3321]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."))))
[3293]103
[3441]104(defmethod monom-dimension (m)
105 (length (monom-exponents ,m)))
[3317]106
[3441]107(defmethod monom-equalp (m1 m2)
[2778]108 "Returns T iff monomials M1 and M2 have identical
109EXPONENTS."
[3441]110 `(equalp (monom-exponents ,m1) (monom-exponents ,m2)))
[2547]111
[3441]112(defmethod monom-elt (m index)
[48]113 "Return the power in the monomial M of variable number INDEX."
[2023]114 (with-slots (exponents)
115 m
[2154]116 (elt exponents index)))
[48]117
[3441]118(defmethod (setf monom-elt) (new-value m index)
[2023]119 "Return the power in the monomial M of variable number INDEX."
120 (with-slots (exponents)
121 m
[2154]122 (setf (elt exponents index) new-value)))
[2023]123
[3441]124(defmethod monom-total-degree (m &optional (start 0) (end (monom-dimension m)))
[48]125 "Return the todal degree of a monomoal M. Optinally, a range
126of variables may be specified with arguments START and END."
[2023]127 (declare (type fixnum start end))
128 (with-slots (exponents)
129 m
[2154]130 (reduce #'+ exponents :start start :end end)))
[48]131
[2064]132
[3441]133(defmethod monom-sugar (m &aux (start 0) (end (monom-dimension m)))
[48]134 "Return the sugar of a monomial M. Optinally, a range
135of variables may be specified with arguments START and END."
[2032]136 (declare (type fixnum start end))
[3441]137 (monom-total-degree m start end))
[48]138
[2478]139(defmethod multiply-by ((self monom) (other monom))
[3322]140 (with-slots ((exponents1 exponents))
[2478]141 self
[3322]142 (with-slots ((exponents2 exponents))
[2478]143 other
[3322]144 (unless (= (length exponents1) (length exponents2))
145 (error "Incompatible dimensions"))
[2811]146 (map-into exponents1 #'+ exponents1 exponents2)))
[2480]147 self)
[2069]148
[2818]149(defmethod divide-by ((self monom) (other monom))
[3322]150 (with-slots ((exponents1 exponents))
[2818]151 self
[3322]152 (with-slots ((exponents2 exponents))
[2818]153 other
[3322]154 (unless (= (length exponents1) (length exponents2))
[3409]155 (error "divide-by: Incompatible dimensions."))
156 (unless (every #'>= exponents1 exponents2)
[3410]157 (error "divide-by: Negative power would result."))
[2818]158 (map-into exponents1 #'- exponents1 exponents2)))
159 self)
160
[3004]161(defmethod copy-instance :around ((object monom) &rest initargs &key &allow-other-keys)
[3018]162 "An :AROUNT method for COPY-INSTANCE. The primary method is a shallow copy,
163 while for monomials we typically need a fresh copy of the
164 exponents."
[3004]165 (declare (ignore object initargs))
[3002]166 (let ((copy (call-next-method)))
[3003]167 (setf (monom-exponents copy) (copy-seq (monom-exponents copy)))
[3002]168 copy))
[2950]169
[3441]170(defmethod monom* ((m1 monom) (m2 monom))
[2816]171 "Non-destructively multiply monomial M1 by M2."
[3032]172 (multiply-by (copy-instance m1) (copy-instance m2)))
[2816]173
[3441]174(defmethod monom* ((numerator monom) &rest denominators)
[3416]175 "Non-destructively divide monomial NUMERATOR by product of DENOMINATORS."
[3441]176 (divide-by (copy-instance numerator) (reduce #'
177 monom* denominators)))
[48]178
[3441]179(defmethod monom-divides-p ((m1 monom) (m2 monom))
[48]180 "Returns T if monomial M1 divides monomial M2, NIL otherwise."
[2039]181 (with-slots ((exponents1 exponents))
182 m1
183 (with-slots ((exponents2 exponents))
184 m2
185 (every #'<= exponents1 exponents2))))
[48]186
[2075]187
[3441]188(defmethod monom-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom))
[2055]189 "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
[875]190 (every #'(lambda (x y z) (<= x (max y z)))
[869]191 m1 m2 m3))
[48]192
[2049]193
[3441]194(defmethod monom-lcm-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
[48]195 "Returns T if monomial MONOM-LCM(M1,M2) divides MONOM-LCM(M3,M4), NIL otherwise."
[1890]196 (declare (type monom m1 m2 m3 m4))
[869]197 (every #'(lambda (x y z w) (<= (max x y) (max z w)))
198 m1 m2 m3 m4))
199
[3441]200(defmethod monom-lcm-equal-lcm-p (m1 m2 m3 m4)
[2075]201 "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
[2171]202 (with-slots ((exponents1 exponents))
[2076]203 m1
[2171]204 (with-slots ((exponents2 exponents))
[2076]205 m2
[2171]206 (with-slots ((exponents3 exponents))
[2076]207 m3
[2171]208 (with-slots ((exponents4 exponents))
[2076]209 m4
[2077]210 (every
211 #'(lambda (x y z w) (= (max x y) (max z w)))
212 exponents1 exponents2 exponents3 exponents4))))))
[48]213
[3441]214(defmethod monom-divisible-by-p ((m1 monom) (m2 monom))
[48]215 "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
[2171]216 (with-slots ((exponents1 exponents))
[2144]217 m1
[2171]218 (with-slots ((exponents2 exponents))
[2144]219 m2
220 (every #'>= exponents1 exponents2))))
[2078]221
[3441]222(defmethod monom-rel-prime-p ((m1 monom) (m2 monom))
[48]223 "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
[2171]224 (with-slots ((exponents1 exponents))
[2078]225 m1
[2171]226 (with-slots ((exponents2 exponents))
[2078]227 m2
[2154]228 (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2))))
[48]229
[2076]230
[3441]231(defmethod monom-lcm ((m1 monom) (m2 monom))
[48]232 "Returns least common multiple of monomials M1 and M2."
[3322]233 (with-slots ((exponents1 exponents))
[2082]234 m1
[2171]235 (with-slots ((exponents2 exponents))
[2082]236 m2
[3324]237 (let* ((exponents (copy-seq exponents1)))
[2082]238 (map-into exponents #'max exponents1 exponents2)
[3322]239 (make-instance 'monom :exponents exponents)))))
[48]240
[2080]241
[3441]242(defmethod monom-gcd ((m1 monom) (m2 monom))
[48]243 "Returns greatest common divisor of monomials M1 and M2."
[3322]244 (with-slots ((exponents1 exponents))
[2082]245 m1
[2171]246 (with-slots ((exponents2 exponents))
[2082]247 m2
[3322]248 (let* ((exponents (copy-seq exponents1)))
[2082]249 (map-into exponents #'min exponents1 exponents2)
[3322]250 (make-instance 'monom :exponents exponents)))))
[48]251
[3441]252(defmethod monom-depends-p ((m monom) k)
[48]253 "Return T if the monomial M depends on variable number K."
[2083]254 (declare (type fixnum k))
255 (with-slots (exponents)
256 m
[2154]257 (plusp (elt exponents k))))
[48]258
[3441]259(defmethod monom-left-tensor-product-by ((self monom) (other monom))
[3323]260 (with-slots ((exponents1 exponents))
[3020]261 self
[3323]262 (with-slots ((exponents2 exponents))
[3020]263 other
[3323]264 (setf exponents1 (concatenate 'vector exponents2 exponents1))))
[3036]265 self)
[48]266
[3441]267(defmethod monom-right-tensor-product-by ((self monom) (other monom))
[3323]268 (with-slots ((exponents1 exponents))
[3026]269 self
[3323]270 (with-slots ((exponents2 exponents))
[3026]271 other
[3323]272 (setf exponents1 (concatenate 'vector exponents1 exponents2))))
[3036]273 self)
[3026]274
[3441]275(defmethod monom-left-contract ((self monom) k)
[1638]276 "Drop the first K variables in monomial M."
[2085]277 (declare (fixnum k))
[3323]278 (with-slots (exponents)
[3040]279 self
[3323]280 (setf exponents (subseq exponents k)))
[3039]281 self)
[886]282
283(defun make-monom-variable (nvars pos &optional (power 1)
[2218]284 &aux (m (make-instance 'monom :dimension nvars)))
[886]285 "Construct a monomial in the polynomial ring
286RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
287which represents a single variable. It assumes number of variables
288NVARS and the variable is at position POS. Optionally, the variable
289may appear raised to power POWER. "
[1924]290 (declare (type fixnum nvars pos power) (type monom m))
[2089]291 (with-slots (exponents)
292 m
[2154]293 (setf (elt exponents pos) power)
[2089]294 m))
[1151]295
[3441]296(defmethod monom->list ((m monom))
[1152]297 "A human-readable representation of a monomial M as a list of exponents."
[2779]298 (coerce (monom-exponents m) 'list))
Note: See TracBrowser for help on using the repository browser.