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

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

* empty log message *

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