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

Last change on this file since 4471 was 4463, checked in by Marek Rychlik, 8 years ago
File size: 24.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
[1610]22(defpackage "MONOM"
[4325]23 (:use :cl :utils :copy :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"
[4360]35 "MULTIPLY"
36 "DIVIDE"
[3592]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
[4245]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
[4235]146(defmethod universal-equalp ((self monom) (other monom))
147 "Returns T iff monomials SELF and OTHER have identical EXPONENTS."
[4236]148 (equalp (monom-exponents self) (monom-exponents other)))
[2547]149
[3443]150(defgeneric monom-elt (m index)
[3574]151 (:documentation "Return the power in the monomial M of variable number INDEX.")
[3443]152 (:method ((m monom) index)
[3550]153 "Return the power in the monomial M of variable number INDEX."
[3443]154 (with-slots (exponents)
155 m
156 (elt exponents index))))
[48]157
[3443]158(defgeneric (setf monom-elt) (new-value m index)
[3550]159 (:documentation "Set the power in the monomial M of variable number INDEX.")
[3443]160 (:method (new-value (m monom) index)
161 (with-slots (exponents)
162 m
[3453]163 (setf (elt exponents index) new-value))))
[2023]164
[3551]165(defgeneric total-degree (m &optional start end)
166 (:documentation "Return the total degree of a monomoal M. Optinally, a range
[3449]167of variables may be specified with arguments START and END.")
168 (:method ((m monom) &optional (start 0) (end (monom-dimension m)))
169 (declare (type fixnum start end))
170 (with-slots (exponents)
171 m
172 (reduce #'+ exponents :start start :end end))))
[48]173
[3552]174(defgeneric sugar (m &optional start end)
[3446]175 (:documentation "Return the sugar of a monomial M. Optinally, a range
176of variables may be specified with arguments START and END.")
177 (:method ((m monom) &optional (start 0) (end (monom-dimension m)))
178 (declare (type fixnum start end))
[3552]179 (total-degree m start end)))
[48]180
[4240]181(defmethod multiply-by ((self monom) (other monom))
182 (with-slots ((exponents1 exponents))
183 self
184 (with-slots ((exponents2 exponents))
185 other
186 (unless (= (length exponents1) (length exponents2))
187 (error "Incompatible dimensions"))
188 (map-into exponents1 #'+ exponents1 exponents2)))
189 self)
[2069]190
[4240]191(defmethod divide-by ((self monom) (other monom))
192 (with-slots ((exponents1 exponents))
193 self
194 (with-slots ((exponents2 exponents))
195 other
196 (unless (= (length exponents1) (length exponents2))
197 (error "divide-by: Incompatible dimensions."))
198 (unless (every #'>= exponents1 exponents2)
199 (error "divide-by: Negative power would result."))
200 (map-into exponents1 #'- exponents1 exponents2)))
201 self)
[2818]202
[3448]203(defmethod copy-instance :around ((object monom) &rest initargs &key &allow-other-keys)
[4129]204 "An :AROUND method of COPY-INSTANCE. It replaces exponents with a fresh copy of the sequence."
[4130]205 (declare (ignore object initargs))
206 (let ((copy (call-next-method)))
207 (setf (monom-exponents copy) (copy-seq (monom-exponents copy)))
208 copy))
[2950]209
[4240]210(defmethod unary-inverse :before ((self monom))
211 (assert (zerop (total-degree self))
212 nil
213 "Monom ~A must have total degree 0 to be invertible.")
214 self)
[4032]215
[4240]216(defmethod unary-inverse ((self monom)) self)
217
[3591]218(defgeneric divides-p (object1 object2)
219 (:documentation "Returns T if OBJECT1 divides OBJECT2.")
220 (:method ((m1 monom) (m2 monom))
221 "Returns T if monomial M1 divides monomial M2, NIL otherwise."
222 (with-slots ((exponents1 exponents))
223 m1
224 (with-slots ((exponents2 exponents))
225 m2
226 (every #'<= exponents1 exponents2)))))
[48]227
[3585]228(defgeneric divides-lcm-p (object1 object2 object3)
[3594]229 (:documentation "Returns T if OBJECT1 divides LCM(OBJECT2,OBJECT3), NIL otherwise.")
[3585]230 (:method ((m1 monom) (m2 monom) (m3 monom))
231 "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
[3596]232 (with-slots ((exponents1 exponents))
233 m1
234 (with-slots ((exponents2 exponents))
235 m2
236 (with-slots ((exponents3 exponents))
237 m3
238 (every #'(lambda (x y z) (<= x (max y z)))
239 exponents1 exponents2 exponents3))))))
[48]240
[3588]241(defgeneric lcm-divides-lcm-p (object1 object2 object3 object4)
242 (:method ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
243 "Returns T if monomial LCM(M1,M2) divides LCM(M3,M4), NIL otherwise."
244 (with-slots ((exponents1 exponents))
245 m1
246 (with-slots ((exponents2 exponents))
247 m2
248 (with-slots ((exponents3 exponents))
249 m3
250 (with-slots ((exponents4 exponents))
251 m4
252 (every #'(lambda (x y z w) (<= (max x y) (max z w)))
[3590]253 exponents1 exponents2 exponents3 exponents4)))))))
[869]254
[3589]255(defgeneric monom-lcm-equal-lcm-p (object1 object2 object3 object4)
256 (:method ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
257 "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
258 (with-slots ((exponents1 exponents))
259 m1
260 (with-slots ((exponents2 exponents))
261 m2
262 (with-slots ((exponents3 exponents))
263 m3
264 (with-slots ((exponents4 exponents))
265 m4
266 (every
267 #'(lambda (x y z w) (= (max x y) (max z w)))
268 exponents1 exponents2 exponents3 exponents4)))))))
[48]269
[3563]270(defgeneric divisible-by-p (object1 object2)
271 (:documentation "Return T if OBJECT1 is divisible by OBJECT2.")
272 (:method ((m1 monom) (m2 monom))
273 "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
274 (with-slots ((exponents1 exponents))
275 m1
276 (with-slots ((exponents2 exponents))
277 m2
278 (every #'>= exponents1 exponents2)))))
[2078]279
[3565]280(defgeneric rel-prime-p (object1 object2)
[3575]281 (:documentation "Returns T if objects OBJECT1 and OBJECT2 are relatively prime.")
[3563]282 (:method ((m1 monom) (m2 monom))
283 "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
284 (with-slots ((exponents1 exponents))
285 m1
286 (with-slots ((exponents2 exponents))
287 m2
288 (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2)))))
[48]289
[3595]290(defgeneric universal-lcm (object1 object2)
[3566]291 (:documentation "Returns the multiple of objects OBJECT1 and OBJECT2.")
292 (:method ((m1 monom) (m2 monom))
293 "Returns least common multiple of monomials M1 and M2."
294 (with-slots ((exponents1 exponents))
295 m1
296 (with-slots ((exponents2 exponents))
297 m2
[4336]298 (let* ((exponents (copy-seq exponents1)))
299 (map-into exponents #'max exponents1 exponents2)
300 (make-instance 'monom :exponents exponents))))))
[48]301
[2080]302
[4253]303(defmethod universal-gcd ((m1 monom) (m2 monom))
304 "Returns greatest common divisor of monomials M1 and M2."
305 (with-slots ((exponents1 exponents))
306 m1
307 (with-slots ((exponents2 exponents))
308 m2
309 (let* ((exponents (copy-seq exponents1)))
310 (map-into exponents #'min exponents1 exponents2)
311 (make-instance 'monom :exponents exponents)))))
[48]312
[3569]313(defgeneric depends-p (object k)
314 (:documentation "Returns T iff object OBJECT depends on variable K.")
315 (:method ((m monom) k)
316 "Return T if the monomial M depends on variable number K."
317 (declare (type fixnum k))
318 (with-slots (exponents)
319 m
320 (plusp (elt exponents k)))))
[48]321
[3570]322(defgeneric left-tensor-product-by (self other)
323 (:documentation "Returns a tensor product SELF by OTHER, stored into
324 SELF. Return SELF.")
325 (:method ((self monom) (other monom))
326 (with-slots ((exponents1 exponents))
327 self
328 (with-slots ((exponents2 exponents))
329 other
330 (setf exponents1 (concatenate 'vector exponents2 exponents1))))
331 self))
[48]332
[3570]333(defgeneric right-tensor-product-by (self other)
334 (:documentation "Returns a tensor product of OTHER by SELF, stored
335 into SELF. Returns SELF.")
336 (:method ((self monom) (other monom))
337 (with-slots ((exponents1 exponents))
338 self
339 (with-slots ((exponents2 exponents))
340 other
341 (setf exponents1 (concatenate 'vector exponents1 exponents2))))
342 self))
[3026]343
[3571]344(defgeneric left-contract (self k)
345 (:documentation "Drop the first K variables in object SELF.")
346 (:method ((self monom) k)
347 "Drop the first K variables in monomial M."
348 (declare (fixnum k))
349 (with-slots (exponents)
350 self
351 (setf exponents (subseq exponents k)))
352 self))
[886]353
354(defun make-monom-variable (nvars pos &optional (power 1)
[2218]355 &aux (m (make-instance 'monom :dimension nvars)))
[886]356 "Construct a monomial in the polynomial ring
357RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
358which represents a single variable. It assumes number of variables
359NVARS and the variable is at position POS. Optionally, the variable
360may appear raised to power POWER. "
[1924]361 (declare (type fixnum nvars pos power) (type monom m))
[2089]362 (with-slots (exponents)
363 m
[2154]364 (setf (elt exponents pos) power)
[2089]365 m))
[1151]366
[3811]367(defun make-monom-constant (dimension)
368 (make-instance 'monom :dimension dimension))
369
[3474]370;; pure lexicographic
[3472]371(defgeneric lex> (p q &optional start end)
372 (:documentation "Return T if P>Q with respect to lexicographic
373order, otherwise NIL. The second returned value is T if P=Q,
374otherwise it is NIL.")
[3483]375 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
[3472]376 (declare (type fixnum start end))
377 (do ((i start (1+ i)))
378 ((>= i end) (values nil t))
379 (cond
[3483]380 ((> (monom-elt p i) (monom-elt q i))
[3472]381 (return-from lex> (values t nil)))
[3483]382 ((< (monom-elt p i) (monom-elt q i))
[3472]383 (return-from lex> (values nil nil)))))))
384
[3475]385;; total degree order, ties broken by lexicographic
[3472]386(defgeneric grlex> (p q &optional start end)
387 (:documentation "Return T if P>Q with respect to graded
388lexicographic order, otherwise NIL. The second returned value is T if
389P=Q, otherwise it is NIL.")
[3483]390 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
[3472]391 (declare (type monom p q) (type fixnum start end))
[3583]392 (let ((d1 (total-degree p start end))
393 (d2 (total-degree q start end)))
[3472]394 (declare (type fixnum d1 d2))
395 (cond
396 ((> d1 d2) (values t nil))
397 ((< d1 d2) (values nil nil))
398 (t
399 (lex> p q start end))))))
400
401;; reverse lexicographic
402(defgeneric revlex> (p q &optional start end)
403 (:documentation "Return T if P>Q with respect to reverse
404lexicographic order, NIL otherwise. The second returned value is T if
405P=Q, otherwise it is NIL. This is not and admissible monomial order
406because some sets do not have a minimal element. This order is useful
407in constructing other orders.")
[3483]408 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
[3472]409 (declare (type fixnum start end))
410 (do ((i (1- end) (1- i)))
411 ((< i start) (values nil t))
412 (declare (type fixnum i))
413 (cond
[3483]414 ((< (monom-elt p i) (monom-elt q i))
[3472]415 (return-from revlex> (values t nil)))
[3483]416 ((> (monom-elt p i) (monom-elt q i))
[3472]417 (return-from revlex> (values nil nil)))))))
418
419
420;; total degree, ties broken by reverse lexicographic
421(defgeneric grevlex> (p q &optional start end)
422 (:documentation "Return T if P>Q with respect to graded reverse
423lexicographic order, NIL otherwise. The second returned value is T if
424P=Q, otherwise it is NIL.")
[3483]425 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
[3472]426 (declare (type fixnum start end))
[3584]427 (let ((d1 (total-degree p start end))
428 (d2 (total-degree q start end)))
[3472]429 (declare (type fixnum d1 d2))
430 (cond
431 ((> d1 d2) (values t nil))
432 ((< d1 d2) (values nil nil))
433 (t
434 (revlex> p q start end))))))
435
436(defgeneric invlex> (p q &optional start end)
437 (:documentation "Return T if P>Q with respect to inverse
438lexicographic order, NIL otherwise The second returned value is T if
439P=Q, otherwise it is NIL.")
[3483]440 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
[3472]441 (declare (type fixnum start end))
442 (do ((i (1- end) (1- i)))
443 ((< i start) (values nil t))
444 (declare (type fixnum i))
445 (cond
[3483]446 ((> (monom-elt p i) (monom-elt q i))
[3472]447 (return-from invlex> (values t nil)))
[3483]448 ((< (monom-elt p i) (monom-elt q i))
[3472]449 (return-from invlex> (values nil nil)))))))
450
451(defun reverse-monomial-order (order)
452 "Create the inverse monomial order to the given monomial order ORDER."
[3483]453 #'(lambda (p q &optional (start 0) (end (monom-dimension q)))
[3472]454 (declare (type monom p q) (type fixnum start end))
455 (funcall order q p start end)))
456
457;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
458;;
459;; Order making functions
460;;
461;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
462
463;; This returns a closure with the same signature
464;; as all orders such as #'LEX>.
[3487]465(defun make-elimination-order-factory-1 (&optional (secondary-elimination-order #'lex>))
[3472]466 "It constructs an elimination order used for the 1-st elimination ideal,
467i.e. for eliminating the first variable. Thus, the order compares the degrees of the
468first variable in P and Q first, with ties broken by SECONDARY-ELIMINATION-ORDER."
[3483]469 #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
[3472]470 (declare (type monom p q) (type fixnum start end))
471 (cond
[3483]472 ((> (monom-elt p start) (monom-elt q start))
[3472]473 (values t nil))
[3483]474 ((< (monom-elt p start) (monom-elt q start))
[3472]475 (values nil nil))
476 (t
477 (funcall secondary-elimination-order p q (1+ start) end)))))
478
479;; This returns a closure which is called with an integer argument.
480;; The result is *another closure* with the same signature as all
481;; orders such as #'LEX>.
[3486]482(defun make-elimination-order-factory (&optional
[3472]483 (primary-elimination-order #'lex>)
484 (secondary-elimination-order #'lex>))
485 "Return a function with a single integer argument K. This should be
486the number of initial K variables X[0],X[1],...,X[K-1], which precede
487remaining variables. The call to the closure creates a predicate
488which compares monomials according to the K-th elimination order. The
489monomial orders PRIMARY-ELIMINATION-ORDER and
490SECONDARY-ELIMINATION-ORDER are used to compare the first K and the
491remaining variables, respectively, with ties broken by lexicographical
492order. That is, if PRIMARY-ELIMINATION-ORDER yields (VALUES NIL T),
493which indicates that the first K variables appear with identical
494powers, then the result is that of a call to
495SECONDARY-ELIMINATION-ORDER applied to the remaining variables
496X[K],X[K+1],..."
497 #'(lambda (k)
498 (cond
499 ((<= k 0)
500 (error "K must be at least 1"))
501 ((= k 1)
[3485]502 (make-elimination-order-factory-1 secondary-elimination-order))
[3472]503 (t
[3483]504 #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
[3472]505 (declare (type monom p q) (type fixnum start end))
506 (multiple-value-bind (primary equal)
507 (funcall primary-elimination-order p q start k)
508 (if equal
509 (funcall secondary-elimination-order p q k end)
510 (values primary nil))))))))
511
[3531]512(defclass term (monom)
[4241]513 ((coeff :initarg :coeff :initform 1 :accessor term-coeff :type ring))
[4226]514 (:default-initargs :coeff 1)
[3531]515 (:documentation "Implements a term, i.e. a product of a scalar
516and powers of some variables, such as 5*X^2*Y^3."))
517
[4242]518(defmethod shared-initialize :around ((self term) slot-names &rest initargs &key (coeff 1))
[4446]519 "A convenience method, implementing auto-wrapping of integer and
520rational coefficients into a RING object. If a coefficient is an
521integer or rational, wrap it in the INTEGER-RING or RATIONAL-FIELD
522object, respectively."
[4242]523 ;; Dispatch on the type of supplied :COEFF arg
[4226]524 (typecase coeff
[4367]525 (integer
[4368]526 (setf (getf initargs :coeff) (make-instance 'integer-ring :value coeff)))
[4282]527 (rational
528 (setf (getf initargs :coeff) (make-instance 'rational-field :value coeff))))
[4226]529 ;; Now pass new initargs to the next method
[4242]530 (apply #'call-next-method (list* self slot-names initargs)))
531
[4445]532(defmethod (setf term-coeff) :after (new-value (object term))
[4446]533 "A conveniense method, implementing auto-wrapping of integer and
534rational coefficients into a RING object. Upon setting the COEFF slot,
535if an integer or rational value is assigned to the slot, it is wrapped
536into an INTEGER-RING or RATIONAL-FIELD object, respectively."
[4445]537 (with-slots (coeff)
538 object
539 (typecase coeff
540 (integer
541 (setf coeff (make-instance 'integer-ring :value coeff)))
542 (rational
543 (setf coeff (make-instance 'rational-field :value coeff))))))
544
[4226]545
[4243]546(defmethod update-instance-for-different-class :after ((old monom) (new term) &key (coeff 1))
[3794]547 "Converts OLD of class MONOM to a NEW of class TERM, initializing coefficient to COEFF."
[3792]548 (reinitialize-instance new :coeff coeff))
[3785]549
[3876]550(defmethod update-instance-for-different-class :after ((old term) (new term) &key (coeff (term-coeff old)))
551 "Converts OLD of class TERM to a NEW of class TERM, initializing coefficient to COEFF."
552 (reinitialize-instance new :coeff coeff))
[3875]553
[3876]554
[3531]555(defmethod print-object ((self term) stream)
556 (print-unreadable-object (self stream :type t :identity t)
557 (with-accessors ((exponents monom-exponents)
[3532]558 (coeff term-coeff))
[3531]559 self
560 (format stream "EXPONENTS=~A COEFF=~A"
561 exponents coeff))))
562
[4127]563(defmethod copy-instance :around ((object term) &rest initargs &key &allow-other-keys)
564 "An :AROUND method of COPY-INSTANCE. It replaces the coefficient with a fresh copy."
565 (declare (ignore object initargs))
566 (let ((copy (call-next-method)))
[4128]567 (setf (term-coeff copy) (copy-instance (term-coeff object)))
[4127]568 copy))
569
[4463]570(defmethod multiply-by ((self term) (other ring))
[4091]571 (reinitialize-instance self :coeff (multiply-by (term-coeff self) other)))
[3846]572
[4463]573(defmethod divide-by ((self term) (other ring))
[4091]574 (reinitialize-instance self :coeff (divide-by (term-coeff self) other)))
[3845]575
[4463]576
[4037]577(defmethod unary-inverse :after ((self term))
[4284]578 (with-slots (coeff)
579 self
580 (setf coeff (unary-inverse coeff))))
[4037]581
[3812]582(defun make-term-constant (dimension &optional (coeff 1))
583 (make-instance 'term :dimension dimension :coeff coeff))
584
[3542]585(defmethod universal-equalp ((term1 term) (term2 term))
586 "Returns T if TERM1 and TERM2 are equal as MONOM, and coefficients
587are UNIVERSAL-EQUALP."
[3540]588 (and (call-next-method)
589 (universal-equalp (term-coeff term1) (term-coeff term2))))
[3531]590
[3556]591(defmethod multiply-by :before ((self term) (other term))
[3531]592 "Destructively multiply terms SELF and OTHER and store the result into SELF.
593It returns SELF."
[3580]594 (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
[3531]595
[3581]596(defmethod left-tensor-product-by :before ((self term) (other term))
[3579]597 (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
[3531]598
[3581]599(defmethod right-tensor-product-by :before ((self term) (other term))
[3556]600 (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
[3531]601
[3556]602(defmethod divide-by :before ((self term) (other term))
603 (setf (term-coeff self) (divide-by (term-coeff self) (term-coeff other))))
[3531]604
[4240]605(defmethod unary-minus ((self term))
606 (setf (term-coeff self) (unary-minus (term-coeff self)))
607 self)
[3531]608
[4241]609(defmethod universal-zerop ((self term))
610 (universal-zerop (term-coeff self)))
[3823]611
612(defgeneric ->list (self)
613 (:method ((self monom))
614 "A human-readable representation of a monomial SELF as a list of exponents."
615 (coerce (monom-exponents self) 'list))
616 (:method ((self term))
617 "A human-readable representation of a term SELF as a cons of the list of exponents and the coefficient."
[4239]618 (cons (coerce (monom-exponents self) 'list) (->sexp (term-coeff self)))))
[3826]619
[4241]620(defmethod ->sexp :before ((object monom) &optional vars)
621 "Check the length of variables VARS against the length of exponents in OBJECT."
622 (with-slots (exponents)
623 object
624 (assert (= (length vars) (length exponents))
625 nil
626 "Variables ~A and exponents ~A must have the same length." vars exponents)))
627
628(defmethod ->sexp ((object monom) &optional vars)
629 "Convert a monomial OBJECT to infix form, using variable VARS to build the representation."
[3828]630 (with-slots (exponents)
[4168]631 object
[4010]632 (let ((m (mapcan #'(lambda (var power)
633 (cond ((= power 0) nil)
634 ((= power 1) (list var))
635 (t (list `(expt ,var ,power)))))
636 vars (coerce exponents 'list))))
[4024]637 (cond ((endp m) 1)
638 ((endp (cdr m)) (car m))
[4010]639 (t
640 (cons '* m))))))
[4241]641
642(defmethod ->sexp :around ((object term) &optional vars)
[4317]643 "Convert a term OBJECT to S-expression, using variable VARS to build the representation."
[4241]644 (declare (ignore vars))
645 (with-slots (coeff)
646 object
647 (let ((monom-sexp (call-next-method))
648 (coeff-sexp (->sexp coeff)))
649 (cond ((eql coeff-sexp 1) monom-sexp)
650 ((atom monom-sexp)
651 (cond ((eql monom-sexp 1) coeff-sexp)
652 (t (list '* coeff-sexp monom-sexp))))
653 ((eql (car monom-sexp) '*)
654 (list* '* coeff-sexp (cdr monom-sexp)))
655 (t
656 (list '* coeff-sexp monom-sexp))))))
Note: See TracBrowser for help on using the repository browser.