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

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