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

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

* empty log message *

File size: 24.7 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"
[4225]23 (:use :cl :utils :copy :ring :integer-ring)
[422]24 (:export "MONOM"
[3602]25 "TERM"
[423]26 "EXPONENT"
[2781]27 "MONOM-DIMENSION"
28 "MONOM-EXPONENTS"
[3592]29 "UNIVERSAL-EQUALP"
[3442]30 "MONOM-ELT"
[3592]31 "TOTAL-DEGREE"
32 "SUGAR"
33 "MULTIPLY-BY"
34 "DIVIDE-BY"
[3599]35 "DIVIDE"
[3592]36 "MULTIPLY"
37 "DIVIDES-P"
38 "DIVIDES-LCM-P"
39 "LCM-DIVIDES-LCM-P"
40 "LCM-EQUAL-LCM-P"
41 "DIVISIBLE-BY-P"
42 "REL-PRIME-P"
43 "UNIVERSAL-LCM"
44 "UNIVERSAL-GCD"
45 "DEPENDS-P"
46 "LEFT-TENSOR-PRODUCT-BY"
47 "RIGHT-TENSOR-PRODUCT-BY"
48 "LEFT-CONTRACT"
[3442]49 "MAKE-MONOM-VARIABLE"
[3811]50 "MAKE-MONOM-CONSTANT"
[3812]51 "MAKE-TERM-CONSTANT"
[3610]52 "->LIST"
[4023]53 "->SEXP"
[3472]54 "LEX>"
55 "GRLEX>"
56 "REVLEX>"
57 "GREVLEX>"
58 "INVLEX>"
59 "REVERSE-MONOMIAL-ORDER"
[3606]60 "MAKE-ELIMINATION-ORDER-FACTORY"
[3644]61 "TERM-COEFF"
[3616]62 "UNARY-MINUS"
[4031]63 "UNARY-INVERSE"
[3616]64 "UNIVERSAL-ZEROP")
[2524]65 (:documentation
[3477]66 "This package implements basic operations on monomials, including
67various monomial orders.
68
[2524]69DATA STRUCTURES: Conceptually, monomials can be represented as lists:
[81]70
[2524]71 monom: (n1 n2 ... nk) where ni are non-negative integers
72
73However, lists may be implemented as other sequence types, so the
74flexibility to change the representation should be maintained in the
75code to use general operations on sequences whenever possible. The
76optimization for the actual representation should be left to
77declarations and the compiler.
78
79EXAMPLES: Suppose that variables are x and y. Then
80
81 Monom x*y^2 ---> (1 2) "))
82
[1610]83(in-package :monom)
[48]84
[3802]85(proclaim '(optimize (speed 0) (space 0) (safety 3) (debug 0)))
[1923]86
[48]87(deftype exponent ()
88 "Type of exponent in a monomial."
89 'fixnum)
90
[2022]91(defclass monom ()
[3312]92 ((exponents :initarg :exponents :accessor monom-exponents
[3054]93 :documentation "The powers of the variables."))
[3289]94 ;; default-initargs are not needed, they are handled by SHARED-INITIALIZE
95 ;;(:default-initargs :dimension 'foo :exponents 'bar :exponent 'baz)
[2779]96 (:documentation
97 "Implements a monomial, i.e. a product of powers
98of variables, like X*Y^2."))
[880]99
[2245]100(defmethod print-object ((self monom) stream)
[3196]101 (print-unreadable-object (self stream :type t :identity t)
[3313]102 (with-accessors ((exponents monom-exponents))
[3216]103 self
[3313]104 (format stream "EXPONENTS=~A"
105 exponents))))
[2027]106
[3299]107(defmethod initialize-instance :after ((self monom)
[3297]108 &key
109 (dimension 0 dimension-supplied-p)
110 (exponents nil exponents-supplied-p)
[3318]111 (exponent 0)
[3297]112 &allow-other-keys
[2390]113 )
[3329]114 "The following INITIALIZE-INSTANCE method allows instance initialization
115of a MONOM in a style similar to MAKE-ARRAY, e.g.:
[3328]116
[3788]117 (MAKE-INSTANCE 'MONOM :EXPONENTS '(1 2 3)) --> #<MONOM EXPONENTS=#(1 2 3)>
118 (MAKE-INSTANCE 'MONOM :DIMENSION 3) --> #<MONOM EXPONENTS=#(0 0 0)>
119 (MAKE-INSTANCE 'MONOM :DIMENSION 3 :EXPONENT 7) --> #<MONOM EXPONENTS=#(7 7 7)>
[3329]120
121If both DIMENSION and EXPONENTS are supplied, they must be compatible,
122i.e. the length of EXPONENTS must be equal DIMENSION. If EXPONENTS
123is not supplied, a monom with repeated value EXPONENT is created.
124By default EXPONENT is 0, which results in a constant monomial.
[3328]125"
[3315]126 (cond
127 (exponents-supplied-p
[3327]128 (when (and dimension-supplied-p
129 (/= dimension (length exponents)))
130 (error "EXPONENTS (~A) must have supplied length DIMENSION (~A)"
131 exponents dimension))
[3315]132 (let ((dim (length exponents)))
133 (setf (slot-value self 'exponents) (make-array dim :initial-contents exponents))))
[3321]134 (dimension-supplied-p
[3315]135 ;; when all exponents are to be identical
[3321]136 (setf (slot-value self 'exponents) (make-array (list dimension)
137 :initial-element exponent
138 :element-type 'exponent)))
139 (t
140 (error "Initarg DIMENSION or EXPONENTS must be supplied."))))
[3293]141
[3807]142(defgeneric monom-dimension (self)
143 (:method ((self monom))
144 (length (monom-exponents self))))
[3317]145
[3541]146(defgeneric universal-equalp (object1 object2)
[3443]147 (:method ((m1 monom) (m2 monom))
[3541]148 "Returns T iff monomials M1 and M2 have identical EXPONENTS."
[3535]149 (equalp (monom-exponents m1) (monom-exponents m2))))
[2547]150
[3443]151(defgeneric monom-elt (m index)
[3574]152 (:documentation "Return the power in the monomial M of variable number INDEX.")
[3443]153 (:method ((m monom) index)
[3550]154 "Return the power in the monomial M of variable number INDEX."
[3443]155 (with-slots (exponents)
156 m
157 (elt exponents index))))
[48]158
[3443]159(defgeneric (setf monom-elt) (new-value m index)
[3550]160 (:documentation "Set the power in the monomial M of variable number INDEX.")
[3443]161 (:method (new-value (m monom) index)
162 (with-slots (exponents)
163 m
[3453]164 (setf (elt exponents index) new-value))))
[2023]165
[3551]166(defgeneric total-degree (m &optional start end)
167 (:documentation "Return the total degree of a monomoal M. Optinally, a range
[3449]168of variables may be specified with arguments START and END.")
169 (:method ((m monom) &optional (start 0) (end (monom-dimension m)))
170 (declare (type fixnum start end))
171 (with-slots (exponents)
172 m
173 (reduce #'+ exponents :start start :end end))))
[48]174
[3552]175(defgeneric sugar (m &optional start end)
[3446]176 (:documentation "Return the sugar of a monomial M. Optinally, a range
177of variables may be specified with arguments START and END.")
178 (:method ((m monom) &optional (start 0) (end (monom-dimension m)))
179 (declare (type fixnum start end))
[3552]180 (total-degree m start end)))
[48]181
[3553]182(defgeneric multiply-by (self other)
[4098]183 (:documentation "Multiply SELF by OTHER, return SELF. The object SELF
184may be destructively modified in the process, while OTHER should remain
185unmodified.")
[3612]186 (:method ((self number) (other number)) (* self other))
[3446]187 (:method ((self monom) (other monom))
188 (with-slots ((exponents1 exponents))
189 self
190 (with-slots ((exponents2 exponents))
191 other
192 (unless (= (length exponents1) (length exponents2))
193 (error "Incompatible dimensions"))
194 (map-into exponents1 #'+ exponents1 exponents2)))
[3846]195 self))
[2069]196
[4093]197(defun multiply (factor &rest more-factors)
198 "Successively multiplies factor FACTOR by the remaining arguments
[4097]199MORE-FACTORS, using MULTIPLY-BY to multiply two factors. Thus
200FACTOR may be destructively modified."
[4099]201 (reduce #'multiply-by more-factors :initial-value (copy-instance factor)))
[4093]202
[3553]203(defgeneric divide-by (self other)
[3544]204 (:documentation "Divide SELF by OTHER, return SELF.")
[3613]205 (:method ((self number) (other number)) (/ self other))
[3446]206 (:method ((self monom) (other monom))
207 (with-slots ((exponents1 exponents))
208 self
209 (with-slots ((exponents2 exponents))
210 other
211 (unless (= (length exponents1) (length exponents2))
212 (error "divide-by: Incompatible dimensions."))
213 (unless (every #'>= exponents1 exponents2)
214 (error "divide-by: Negative power would result."))
215 (map-into exponents1 #'- exponents1 exponents2)))
[3845]216 self))
[2818]217
[3448]218(defmethod copy-instance :around ((object monom) &rest initargs &key &allow-other-keys)
[4129]219 "An :AROUND method of COPY-INSTANCE. It replaces exponents with a fresh copy of the sequence."
[4130]220 (declare (ignore object initargs))
221 (let ((copy (call-next-method)))
222 (setf (monom-exponents copy) (copy-seq (monom-exponents copy)))
223 copy))
[2950]224
[4032]225(defgeneric unary-inverse (self)
226 (:documentation "Returns the unary inverse of SELF.")
[4033]227 (:method ((self number)) (/ self))
228 (:method :before ((self monom))
[4037]229 (assert (zerop (total-degree self))
230 nil
231 "Monom ~A must have total degree 0 to be invertible." self))
[4033]232 (:method ((self monom)) self))
[4032]233
[3849]234(defun divide (numerator &rest denominators)
[4096]235 "Successively divides NUMERATOR by elements of DENOMINATORS. The operation
236destructively modifies NUMERATOR."
[3849]237 (cond ((endp denominators)
[4030]238 (unary-inverse numerator))
[4100]239 (t (reduce #'divide-by denominators :initial-value (copy-instance numerator)))))
[48]240
[3591]241(defgeneric divides-p (object1 object2)
242 (:documentation "Returns T if OBJECT1 divides OBJECT2.")
243 (:method ((m1 monom) (m2 monom))
244 "Returns T if monomial M1 divides monomial M2, NIL otherwise."
245 (with-slots ((exponents1 exponents))
246 m1
247 (with-slots ((exponents2 exponents))
248 m2
249 (every #'<= exponents1 exponents2)))))
[48]250
[3585]251(defgeneric divides-lcm-p (object1 object2 object3)
[3594]252 (:documentation "Returns T if OBJECT1 divides LCM(OBJECT2,OBJECT3), NIL otherwise.")
[3585]253 (:method ((m1 monom) (m2 monom) (m3 monom))
254 "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
[3596]255 (with-slots ((exponents1 exponents))
256 m1
257 (with-slots ((exponents2 exponents))
258 m2
259 (with-slots ((exponents3 exponents))
260 m3
261 (every #'(lambda (x y z) (<= x (max y z)))
262 exponents1 exponents2 exponents3))))))
[48]263
[3588]264(defgeneric lcm-divides-lcm-p (object1 object2 object3 object4)
265 (:method ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
266 "Returns T if monomial LCM(M1,M2) divides LCM(M3,M4), NIL otherwise."
267 (with-slots ((exponents1 exponents))
268 m1
269 (with-slots ((exponents2 exponents))
270 m2
271 (with-slots ((exponents3 exponents))
272 m3
273 (with-slots ((exponents4 exponents))
274 m4
275 (every #'(lambda (x y z w) (<= (max x y) (max z w)))
[3590]276 exponents1 exponents2 exponents3 exponents4)))))))
[869]277
[3589]278(defgeneric monom-lcm-equal-lcm-p (object1 object2 object3 object4)
279 (:method ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
280 "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
281 (with-slots ((exponents1 exponents))
282 m1
283 (with-slots ((exponents2 exponents))
284 m2
285 (with-slots ((exponents3 exponents))
286 m3
287 (with-slots ((exponents4 exponents))
288 m4
289 (every
290 #'(lambda (x y z w) (= (max x y) (max z w)))
291 exponents1 exponents2 exponents3 exponents4)))))))
[48]292
[3563]293(defgeneric divisible-by-p (object1 object2)
294 (:documentation "Return T if OBJECT1 is divisible by OBJECT2.")
295 (:method ((m1 monom) (m2 monom))
296 "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
297 (with-slots ((exponents1 exponents))
298 m1
299 (with-slots ((exponents2 exponents))
300 m2
301 (every #'>= exponents1 exponents2)))))
[2078]302
[3565]303(defgeneric rel-prime-p (object1 object2)
[3575]304 (:documentation "Returns T if objects OBJECT1 and OBJECT2 are relatively prime.")
[3563]305 (:method ((m1 monom) (m2 monom))
306 "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
307 (with-slots ((exponents1 exponents))
308 m1
309 (with-slots ((exponents2 exponents))
310 m2
311 (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2)))))
[48]312
[3595]313(defgeneric universal-lcm (object1 object2)
[3566]314 (:documentation "Returns the multiple of objects OBJECT1 and OBJECT2.")
315 (:method ((m1 monom) (m2 monom))
316 "Returns least common multiple of monomials M1 and M2."
317 (with-slots ((exponents1 exponents))
318 m1
319 (with-slots ((exponents2 exponents))
320 m2
321 (let* ((exponents (copy-seq exponents1)))
322 (map-into exponents #'max exponents1 exponents2)
323 (make-instance 'monom :exponents exponents))))))
[48]324
[2080]325
[3567]326(defgeneric universal-gcd (object1 object2)
327 (:documentation "Returns GCD of objects OBJECT1 and OBJECT2")
328 (:method ((m1 monom) (m2 monom))
[3568]329 "Returns greatest common divisor of monomials M1 and M2."
330 (with-slots ((exponents1 exponents))
331 m1
332 (with-slots ((exponents2 exponents))
333 m2
334 (let* ((exponents (copy-seq exponents1)))
335 (map-into exponents #'min exponents1 exponents2)
336 (make-instance 'monom :exponents exponents))))))
[48]337
[3569]338(defgeneric depends-p (object k)
339 (:documentation "Returns T iff object OBJECT depends on variable K.")
340 (:method ((m monom) k)
341 "Return T if the monomial M depends on variable number K."
342 (declare (type fixnum k))
343 (with-slots (exponents)
344 m
345 (plusp (elt exponents k)))))
[48]346
[3570]347(defgeneric left-tensor-product-by (self other)
348 (:documentation "Returns a tensor product SELF by OTHER, stored into
349 SELF. Return SELF.")
350 (:method ((self monom) (other monom))
351 (with-slots ((exponents1 exponents))
352 self
353 (with-slots ((exponents2 exponents))
354 other
355 (setf exponents1 (concatenate 'vector exponents2 exponents1))))
356 self))
[48]357
[3570]358(defgeneric right-tensor-product-by (self other)
359 (:documentation "Returns a tensor product of OTHER by SELF, stored
360 into SELF. Returns SELF.")
361 (:method ((self monom) (other monom))
362 (with-slots ((exponents1 exponents))
363 self
364 (with-slots ((exponents2 exponents))
365 other
366 (setf exponents1 (concatenate 'vector exponents1 exponents2))))
367 self))
[3026]368
[3571]369(defgeneric left-contract (self k)
370 (:documentation "Drop the first K variables in object SELF.")
371 (:method ((self monom) k)
372 "Drop the first K variables in monomial M."
373 (declare (fixnum k))
374 (with-slots (exponents)
375 self
376 (setf exponents (subseq exponents k)))
377 self))
[886]378
379(defun make-monom-variable (nvars pos &optional (power 1)
[2218]380 &aux (m (make-instance 'monom :dimension nvars)))
[886]381 "Construct a monomial in the polynomial ring
382RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
383which represents a single variable. It assumes number of variables
384NVARS and the variable is at position POS. Optionally, the variable
385may appear raised to power POWER. "
[1924]386 (declare (type fixnum nvars pos power) (type monom m))
[2089]387 (with-slots (exponents)
388 m
[2154]389 (setf (elt exponents pos) power)
[2089]390 m))
[1151]391
[3811]392(defun make-monom-constant (dimension)
393 (make-instance 'monom :dimension dimension))
394
[3474]395;; pure lexicographic
[3472]396(defgeneric lex> (p q &optional start end)
397 (:documentation "Return T if P>Q with respect to lexicographic
398order, otherwise NIL. The second returned value is T if P=Q,
399otherwise it is NIL.")
[3483]400 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
[3472]401 (declare (type fixnum start end))
402 (do ((i start (1+ i)))
403 ((>= i end) (values nil t))
404 (cond
[3483]405 ((> (monom-elt p i) (monom-elt q i))
[3472]406 (return-from lex> (values t nil)))
[3483]407 ((< (monom-elt p i) (monom-elt q i))
[3472]408 (return-from lex> (values nil nil)))))))
409
[3475]410;; total degree order, ties broken by lexicographic
[3472]411(defgeneric grlex> (p q &optional start end)
412 (:documentation "Return T if P>Q with respect to graded
413lexicographic order, otherwise NIL. The second returned value is T if
414P=Q, otherwise it is NIL.")
[3483]415 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
[3472]416 (declare (type monom p q) (type fixnum start end))
[3583]417 (let ((d1 (total-degree p start end))
418 (d2 (total-degree q start end)))
[3472]419 (declare (type fixnum d1 d2))
420 (cond
421 ((> d1 d2) (values t nil))
422 ((< d1 d2) (values nil nil))
423 (t
424 (lex> p q start end))))))
425
426;; reverse lexicographic
427(defgeneric revlex> (p q &optional start end)
428 (:documentation "Return T if P>Q with respect to reverse
429lexicographic order, NIL otherwise. The second returned value is T if
430P=Q, otherwise it is NIL. This is not and admissible monomial order
431because some sets do not have a minimal element. This order is useful
432in constructing other orders.")
[3483]433 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
[3472]434 (declare (type fixnum start end))
435 (do ((i (1- end) (1- i)))
436 ((< i start) (values nil t))
437 (declare (type fixnum i))
438 (cond
[3483]439 ((< (monom-elt p i) (monom-elt q i))
[3472]440 (return-from revlex> (values t nil)))
[3483]441 ((> (monom-elt p i) (monom-elt q i))
[3472]442 (return-from revlex> (values nil nil)))))))
443
444
445;; total degree, ties broken by reverse lexicographic
446(defgeneric grevlex> (p q &optional start end)
447 (:documentation "Return T if P>Q with respect to graded reverse
448lexicographic order, NIL otherwise. The second returned value is T if
449P=Q, otherwise it is NIL.")
[3483]450 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
[3472]451 (declare (type fixnum start end))
[3584]452 (let ((d1 (total-degree p start end))
453 (d2 (total-degree q start end)))
[3472]454 (declare (type fixnum d1 d2))
455 (cond
456 ((> d1 d2) (values t nil))
457 ((< d1 d2) (values nil nil))
458 (t
459 (revlex> p q start end))))))
460
461(defgeneric invlex> (p q &optional start end)
462 (:documentation "Return T if P>Q with respect to inverse
463lexicographic order, NIL otherwise The second returned value is T if
464P=Q, otherwise it is NIL.")
[3483]465 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
[3472]466 (declare (type fixnum start end))
467 (do ((i (1- end) (1- i)))
468 ((< i start) (values nil t))
469 (declare (type fixnum i))
470 (cond
[3483]471 ((> (monom-elt p i) (monom-elt q i))
[3472]472 (return-from invlex> (values t nil)))
[3483]473 ((< (monom-elt p i) (monom-elt q i))
[3472]474 (return-from invlex> (values nil nil)))))))
475
476(defun reverse-monomial-order (order)
477 "Create the inverse monomial order to the given monomial order ORDER."
[3483]478 #'(lambda (p q &optional (start 0) (end (monom-dimension q)))
[3472]479 (declare (type monom p q) (type fixnum start end))
480 (funcall order q p start end)))
481
482;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
483;;
484;; Order making functions
485;;
486;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
487
488;; This returns a closure with the same signature
489;; as all orders such as #'LEX>.
[3487]490(defun make-elimination-order-factory-1 (&optional (secondary-elimination-order #'lex>))
[3472]491 "It constructs an elimination order used for the 1-st elimination ideal,
492i.e. for eliminating the first variable. Thus, the order compares the degrees of the
493first variable in P and Q first, with ties broken by SECONDARY-ELIMINATION-ORDER."
[3483]494 #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
[3472]495 (declare (type monom p q) (type fixnum start end))
496 (cond
[3483]497 ((> (monom-elt p start) (monom-elt q start))
[3472]498 (values t nil))
[3483]499 ((< (monom-elt p start) (monom-elt q start))
[3472]500 (values nil nil))
501 (t
502 (funcall secondary-elimination-order p q (1+ start) end)))))
503
504;; This returns a closure which is called with an integer argument.
505;; The result is *another closure* with the same signature as all
506;; orders such as #'LEX>.
[3486]507(defun make-elimination-order-factory (&optional
[3472]508 (primary-elimination-order #'lex>)
509 (secondary-elimination-order #'lex>))
510 "Return a function with a single integer argument K. This should be
511the number of initial K variables X[0],X[1],...,X[K-1], which precede
512remaining variables. The call to the closure creates a predicate
513which compares monomials according to the K-th elimination order. The
514monomial orders PRIMARY-ELIMINATION-ORDER and
515SECONDARY-ELIMINATION-ORDER are used to compare the first K and the
516remaining variables, respectively, with ties broken by lexicographical
517order. That is, if PRIMARY-ELIMINATION-ORDER yields (VALUES NIL T),
518which indicates that the first K variables appear with identical
519powers, then the result is that of a call to
520SECONDARY-ELIMINATION-ORDER applied to the remaining variables
521X[K],X[K+1],..."
522 #'(lambda (k)
523 (cond
524 ((<= k 0)
525 (error "K must be at least 1"))
526 ((= k 1)
[3485]527 (make-elimination-order-factory-1 secondary-elimination-order))
[3472]528 (t
[3483]529 #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
[3472]530 (declare (type monom p q) (type fixnum start end))
531 (multiple-value-bind (primary equal)
532 (funcall primary-elimination-order p q start k)
533 (if equal
534 (funcall secondary-elimination-order p q k end)
535 (values primary nil))))))))
536
[3531]537(defclass term (monom)
[4226]538 ((coeff :initarg :coeff :accessor term-coeff :type ring))
539 (:default-initargs :coeff 1)
[3531]540 (:documentation "Implements a term, i.e. a product of a scalar
541and powers of some variables, such as 5*X^2*Y^3."))
542
[4226]543(defmethod initialize-instance :around ((self term) &rest initargs &key coeff)
544 "A convenience method. If a coefficient is an integer, wrap it in the INTEGER-RING object"
545 ;; Dispatch on supplied type of coefficient
546 (typecase coeff
547 (integer
548 (setf (getf initargs :coeff) (make-instance 'integer-ring :value coeff))))
549 ;; Now pass new initargs to the next method
550 (apply #'call-next-method (cons self initargs)))
551
[3875]552(defmethod update-instance-for-different-class :after ((old monom) (new term) &key (coeff 1))
[3794]553 "Converts OLD of class MONOM to a NEW of class TERM, initializing coefficient to COEFF."
[3792]554 (reinitialize-instance new :coeff coeff))
[3785]555
[3876]556(defmethod update-instance-for-different-class :after ((old term) (new term) &key (coeff (term-coeff old)))
557 "Converts OLD of class TERM to a NEW of class TERM, initializing coefficient to COEFF."
558 (reinitialize-instance new :coeff coeff))
[3875]559
[3876]560
[3531]561(defmethod print-object ((self term) stream)
562 (print-unreadable-object (self stream :type t :identity t)
563 (with-accessors ((exponents monom-exponents)
[3532]564 (coeff term-coeff))
[3531]565 self
566 (format stream "EXPONENTS=~A COEFF=~A"
567 exponents coeff))))
568
[4127]569(defmethod copy-instance :around ((object term) &rest initargs &key &allow-other-keys)
570 "An :AROUND method of COPY-INSTANCE. It replaces the coefficient with a fresh copy."
571 (declare (ignore object initargs))
572 (let ((copy (call-next-method)))
[4128]573 (setf (term-coeff copy) (copy-instance (term-coeff object)))
[4127]574 copy))
575
[3846]576(defmethod multiply-by ((self term) (other number))
[4091]577 (reinitialize-instance self :coeff (multiply-by (term-coeff self) other)))
[3846]578
[3845]579(defmethod divide-by ((self term) (other number))
[4091]580 (reinitialize-instance self :coeff (divide-by (term-coeff self) other)))
[3845]581
[4037]582(defmethod unary-inverse :after ((self term))
583 (with-slots (coeff)
584 self
585 (setf coeff (unary-inverse coeff))))
586
[3812]587(defun make-term-constant (dimension &optional (coeff 1))
588 (make-instance 'term :dimension dimension :coeff coeff))
589
[3542]590(defmethod universal-equalp ((term1 term) (term2 term))
591 "Returns T if TERM1 and TERM2 are equal as MONOM, and coefficients
592are UNIVERSAL-EQUALP."
[3540]593 (and (call-next-method)
594 (universal-equalp (term-coeff term1) (term-coeff term2))))
[3531]595
[3556]596(defmethod multiply-by :before ((self term) (other term))
[3531]597 "Destructively multiply terms SELF and OTHER and store the result into SELF.
598It returns SELF."
[3580]599 (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
[3531]600
[4093]601
[3581]602(defmethod left-tensor-product-by :before ((self term) (other term))
[3579]603 (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
[3531]604
[3581]605(defmethod right-tensor-product-by :before ((self term) (other term))
[3556]606 (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
[3531]607
[3556]608(defmethod divide-by :before ((self term) (other term))
609 (setf (term-coeff self) (divide-by (term-coeff self) (term-coeff other))))
[3531]610
[3582]611(defgeneric unary-minus (self)
[3615]612 (:documentation "Negate object SELF and return it.")
613 (:method ((self number)) (- self))
[3582]614 (:method ((self term))
615 (setf (term-coeff self) (unary-minus (term-coeff self)))
616 self))
[3531]617
[3578]618(defgeneric universal-zerop (self)
[3617]619 (:documentation "Return T iff SELF is zero.")
[3618]620 (:method ((self number)) (zerop self))
[3578]621 (:method ((self term))
622 (universal-zerop (term-coeff self))))
[3823]623
624(defgeneric ->list (self)
625 (:method ((self monom))
626 "A human-readable representation of a monomial SELF as a list of exponents."
627 (coerce (monom-exponents self) 'list))
628 (:method ((self term))
629 "A human-readable representation of a term SELF as a cons of the list of exponents and the coefficient."
[3824]630 (cons (coerce (monom-exponents self) 'list) (term-coeff self))))
[3826]631
[4168]632(defgeneric ->sexp (object &optional vars)
633 (:documentation "Convert a polynomial OBJECT to an S-expression, using variables VARS.")
634 (:method :before ((object monom) &optional vars)
635 "Check the length of variables VARS against the length of exponents in OBJECT."
[4024]636 (with-slots (exponents)
[4168]637 object
[4024]638 (assert (= (length vars) (length exponents))
[4026]639 nil
[4024]640 "Variables ~A and exponents ~A must have the same length." vars exponents)))
[4168]641 (:method ((object monom) &optional vars)
642 "Convert a monomial OBJECT to infix form, using variable VARS to build the representation."
[3828]643 (with-slots (exponents)
[4168]644 object
[4010]645 (let ((m (mapcan #'(lambda (var power)
646 (cond ((= power 0) nil)
647 ((= power 1) (list var))
648 (t (list `(expt ,var ,power)))))
649 vars (coerce exponents 'list))))
[4024]650 (cond ((endp m) 1)
651 ((endp (cdr m)) (car m))
[4010]652 (t
653 (cons '* m))))))
[4168]654 (:method :around ((object term) &optional vars)
655 "Convert a term OBJECT to infix form, using variable VARS to build the representation."
[4009]656 (declare (ignore vars))
[4168]657 (with-slots (coeff)
658 object
659 (let ((sexp (call-next-method)))
660 (cond ((eql coeff 1) sexp)
661 ((atom sexp)
662 (cond ((eql sexp 1) coeff)
663 (t (list '* coeff sexp))))
664 ((eql (car sexp) '*)
665 (list* '* coeff (cdr sexp)))
[4010]666 (t
[4168]667 (list '* coeff sexp)))))))
Note: See TracBrowser for help on using the repository browser.