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

Last change on this file since 3811 was 3811, checked in by Marek Rychlik, 8 years ago

* empty log message *

File size: 21.9 KB
Line 
1;;; -*- Mode: Lisp -*-
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
22(defpackage "MONOM"
23 (:use :cl :copy)
24 (:export "MONOM"
25 "TERM"
26 "EXPONENT"
27 "MONOM-DIMENSION"
28 "MONOM-EXPONENTS"
29 "UNIVERSAL-EQUALP"
30 "MONOM-ELT"
31 "TOTAL-DEGREE"
32 "SUGAR"
33 "MULTIPLY-BY"
34 "DIVIDE-BY"
35 "DIVIDE"
36 "MULTIPLY-2"
37 "MULTIPLY"
38 "DIVIDES-P"
39 "DIVIDES-LCM-P"
40 "LCM-DIVIDES-LCM-P"
41 "LCM-EQUAL-LCM-P"
42 "DIVISIBLE-BY-P"
43 "REL-PRIME-P"
44 "UNIVERSAL-LCM"
45 "UNIVERSAL-GCD"
46 "DEPENDS-P"
47 "LEFT-TENSOR-PRODUCT-BY"
48 "RIGHT-TENSOR-PRODUCT-BY"
49 "LEFT-CONTRACT"
50 "MAKE-MONOM-VARIABLE"
51 "MAKE-MONOM-CONSTANT"
52 "->LIST"
53 "LEX>"
54 "GRLEX>"
55 "REVLEX>"
56 "GREVLEX>"
57 "INVLEX>"
58 "REVERSE-MONOMIAL-ORDER"
59 "MAKE-ELIMINATION-ORDER-FACTORY"
60 "TERM-COEFF"
61 "UNARY-MINUS"
62 "UNIVERSAL-ZEROP")
63 (:documentation
64 "This package implements basic operations on monomials, including
65various monomial orders.
66
67DATA STRUCTURES: Conceptually, monomials can be represented as lists:
68
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
81(in-package :monom)
82
83(proclaim '(optimize (speed 0) (space 0) (safety 3) (debug 0)))
84
85(deftype exponent ()
86 "Type of exponent in a monomial."
87 'fixnum)
88
89(defclass monom ()
90 ((exponents :initarg :exponents :accessor monom-exponents
91 :documentation "The powers of the variables."))
92 ;; default-initargs are not needed, they are handled by SHARED-INITIALIZE
93 ;;(:default-initargs :dimension 'foo :exponents 'bar :exponent 'baz)
94 (:documentation
95 "Implements a monomial, i.e. a product of powers
96of variables, like X*Y^2."))
97
98(defmethod print-object ((self monom) stream)
99 (print-unreadable-object (self stream :type t :identity t)
100 (with-accessors ((exponents monom-exponents))
101 self
102 (format stream "EXPONENTS=~A"
103 exponents))))
104
105(defmethod initialize-instance :after ((self monom)
106 &key
107 (dimension 0 dimension-supplied-p)
108 (exponents nil exponents-supplied-p)
109 (exponent 0)
110 &allow-other-keys
111 )
112 "The following INITIALIZE-INSTANCE method allows instance initialization
113of a MONOM in a style similar to MAKE-ARRAY, e.g.:
114
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)>
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.
123"
124 (cond
125 (exponents-supplied-p
126 (when (and dimension-supplied-p
127 (/= dimension (length exponents)))
128 (error "EXPONENTS (~A) must have supplied length DIMENSION (~A)"
129 exponents dimension))
130 (let ((dim (length exponents)))
131 (setf (slot-value self 'exponents) (make-array dim :initial-contents exponents))))
132 (dimension-supplied-p
133 ;; when all exponents are to be identical
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."))))
139
140(defgeneric monom-dimension (self)
141 (:method ((self monom))
142 (length (monom-exponents self))))
143
144(defgeneric universal-equalp (object1 object2)
145 (:documentation "Returns T iff OBJECT1 and OBJECT2 are equal.")
146 (:method ((object1 cons) (object2 cons)) (every #'universal-equalp object1 object2))
147 (:method ((object1 number) (object2 number)) (= object1 object2))
148 (:method ((m1 monom) (m2 monom))
149 "Returns T iff monomials M1 and M2 have identical EXPONENTS."
150 (equalp (monom-exponents m1) (monom-exponents m2))))
151
152(defgeneric monom-elt (m index)
153 (:documentation "Return the power in the monomial M of variable number INDEX.")
154 (:method ((m monom) index)
155 "Return the power in the monomial M of variable number INDEX."
156 (with-slots (exponents)
157 m
158 (elt exponents index))))
159
160(defgeneric (setf monom-elt) (new-value m index)
161 (:documentation "Set the power in the monomial M of variable number INDEX.")
162 (:method (new-value (m monom) index)
163 (with-slots (exponents)
164 m
165 (setf (elt exponents index) new-value))))
166
167(defgeneric total-degree (m &optional start end)
168 (:documentation "Return the total degree of a monomoal M. Optinally, a range
169of variables may be specified with arguments START and END.")
170 (:method ((m monom) &optional (start 0) (end (monom-dimension m)))
171 (declare (type fixnum start end))
172 (with-slots (exponents)
173 m
174 (reduce #'+ exponents :start start :end end))))
175
176(defgeneric sugar (m &optional start end)
177 (:documentation "Return the sugar of a monomial M. Optinally, a range
178of variables may be specified with arguments START and END.")
179 (:method ((m monom) &optional (start 0) (end (monom-dimension m)))
180 (declare (type fixnum start end))
181 (total-degree m start end)))
182
183(defgeneric multiply-by (self other)
184 (:documentation "Multiply SELF by OTHER, return SELF.")
185 (:method ((self number) (other number)) (* self other))
186 (:method ((self monom) (other monom))
187 (with-slots ((exponents1 exponents))
188 self
189 (with-slots ((exponents2 exponents))
190 other
191 (unless (= (length exponents1) (length exponents2))
192 (error "Incompatible dimensions"))
193 (map-into exponents1 #'+ exponents1 exponents2)))
194 self)
195 (:method ((self number) (other term))
196 (reinitialize-instance other :coeff (multiply self (term-coeff other))))
197 (:method ((self term) (other number))
198 (reinitialize-instance self :coeff (multiply (term-coeff self) other))))
199
200(defgeneric divide-by (self other)
201 (:documentation "Divide SELF by OTHER, return SELF.")
202 (:method ((self number) (other number)) (/ self other))
203 (:method ((self monom) (other monom))
204 (with-slots ((exponents1 exponents))
205 self
206 (with-slots ((exponents2 exponents))
207 other
208 (unless (= (length exponents1) (length exponents2))
209 (error "divide-by: Incompatible dimensions."))
210 (unless (every #'>= exponents1 exponents2)
211 (error "divide-by: Negative power would result."))
212 (map-into exponents1 #'- exponents1 exponents2)))
213 self)
214 (:method ((self term) (other number))
215 (reinitialize-instance self :coeff (divide (term-coeff self) other))))
216
217(defmethod copy-instance :around ((object monom) &rest initargs &key &allow-other-keys)
218 "An :AROUND method of COPY-INSTANCE. It replaces
219exponents with a fresh copy of the sequence."
220 (declare (ignore object initargs))
221 (let ((copy (call-next-method)))
222 (setf (monom-exponents copy) (copy-seq (monom-exponents copy)))
223 copy))
224
225(defun multiply-2 (object1 object2)
226 "Multiply OBJECT1 by OBJECT2"
227 (multiply-by (copy-instance object1) (copy-instance object2)))
228
229(defun multiply (&rest factors)
230 "Non-destructively multiply list FACTORS."
231 (cond ((endp factors) 1)
232 ((endp (rest factors)) (first factors))
233 (t (reduce #'multiply-2 factors :initial-value 1))))
234
235(defun divide (numerator &rest denominators)
236 "Non-destructively divide object NUMERATOR by product of DENOMINATORS."
237 (divide-by (copy-instance numerator) (apply #'multiply denominators)))
238
239(defgeneric divides-p (object1 object2)
240 (:documentation "Returns T if OBJECT1 divides OBJECT2.")
241 (:method ((m1 monom) (m2 monom))
242 "Returns T if monomial M1 divides monomial M2, NIL otherwise."
243 (with-slots ((exponents1 exponents))
244 m1
245 (with-slots ((exponents2 exponents))
246 m2
247 (every #'<= exponents1 exponents2)))))
248
249(defgeneric divides-lcm-p (object1 object2 object3)
250 (:documentation "Returns T if OBJECT1 divides LCM(OBJECT2,OBJECT3), NIL otherwise.")
251 (:method ((m1 monom) (m2 monom) (m3 monom))
252 "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
253 (with-slots ((exponents1 exponents))
254 m1
255 (with-slots ((exponents2 exponents))
256 m2
257 (with-slots ((exponents3 exponents))
258 m3
259 (every #'(lambda (x y z) (<= x (max y z)))
260 exponents1 exponents2 exponents3))))))
261
262(defgeneric lcm-divides-lcm-p (object1 object2 object3 object4)
263 (:method ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
264 "Returns T if monomial LCM(M1,M2) divides LCM(M3,M4), NIL otherwise."
265 (with-slots ((exponents1 exponents))
266 m1
267 (with-slots ((exponents2 exponents))
268 m2
269 (with-slots ((exponents3 exponents))
270 m3
271 (with-slots ((exponents4 exponents))
272 m4
273 (every #'(lambda (x y z w) (<= (max x y) (max z w)))
274 exponents1 exponents2 exponents3 exponents4)))))))
275
276(defgeneric monom-lcm-equal-lcm-p (object1 object2 object3 object4)
277 (:method ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
278 "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
279 (with-slots ((exponents1 exponents))
280 m1
281 (with-slots ((exponents2 exponents))
282 m2
283 (with-slots ((exponents3 exponents))
284 m3
285 (with-slots ((exponents4 exponents))
286 m4
287 (every
288 #'(lambda (x y z w) (= (max x y) (max z w)))
289 exponents1 exponents2 exponents3 exponents4)))))))
290
291(defgeneric divisible-by-p (object1 object2)
292 (:documentation "Return T if OBJECT1 is divisible by OBJECT2.")
293 (:method ((m1 monom) (m2 monom))
294 "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
295 (with-slots ((exponents1 exponents))
296 m1
297 (with-slots ((exponents2 exponents))
298 m2
299 (every #'>= exponents1 exponents2)))))
300
301(defgeneric rel-prime-p (object1 object2)
302 (:documentation "Returns T if objects OBJECT1 and OBJECT2 are relatively prime.")
303 (:method ((m1 monom) (m2 monom))
304 "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
305 (with-slots ((exponents1 exponents))
306 m1
307 (with-slots ((exponents2 exponents))
308 m2
309 (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2)))))
310
311(defgeneric universal-lcm (object1 object2)
312 (:documentation "Returns the multiple of objects OBJECT1 and OBJECT2.")
313 (:method ((m1 monom) (m2 monom))
314 "Returns least common multiple of monomials M1 and M2."
315 (with-slots ((exponents1 exponents))
316 m1
317 (with-slots ((exponents2 exponents))
318 m2
319 (let* ((exponents (copy-seq exponents1)))
320 (map-into exponents #'max exponents1 exponents2)
321 (make-instance 'monom :exponents exponents))))))
322
323
324(defgeneric universal-gcd (object1 object2)
325 (:documentation "Returns GCD of objects OBJECT1 and OBJECT2")
326 (:method ((object1 number) (object2 number)) (gcd object1 object2))
327 (:method ((m1 monom) (m2 monom))
328 "Returns greatest common divisor of monomials M1 and M2."
329 (with-slots ((exponents1 exponents))
330 m1
331 (with-slots ((exponents2 exponents))
332 m2
333 (let* ((exponents (copy-seq exponents1)))
334 (map-into exponents #'min exponents1 exponents2)
335 (make-instance 'monom :exponents exponents))))))
336
337(defgeneric depends-p (object k)
338 (:documentation "Returns T iff object OBJECT depends on variable K.")
339 (:method ((m monom) k)
340 "Return T if the monomial M depends on variable number K."
341 (declare (type fixnum k))
342 (with-slots (exponents)
343 m
344 (plusp (elt exponents k)))))
345
346(defgeneric left-tensor-product-by (self other)
347 (:documentation "Returns a tensor product SELF by OTHER, stored into
348 SELF. Return SELF.")
349 (:method ((self monom) (other monom))
350 (with-slots ((exponents1 exponents))
351 self
352 (with-slots ((exponents2 exponents))
353 other
354 (setf exponents1 (concatenate 'vector exponents2 exponents1))))
355 self))
356
357(defgeneric right-tensor-product-by (self other)
358 (:documentation "Returns a tensor product of OTHER by SELF, stored
359 into SELF. Returns SELF.")
360 (:method ((self monom) (other monom))
361 (with-slots ((exponents1 exponents))
362 self
363 (with-slots ((exponents2 exponents))
364 other
365 (setf exponents1 (concatenate 'vector exponents1 exponents2))))
366 self))
367
368(defgeneric left-contract (self k)
369 (:documentation "Drop the first K variables in object SELF.")
370 (:method ((self monom) k)
371 "Drop the first K variables in monomial M."
372 (declare (fixnum k))
373 (with-slots (exponents)
374 self
375 (setf exponents (subseq exponents k)))
376 self))
377
378(defun make-monom-variable (nvars pos &optional (power 1)
379 &aux (m (make-instance 'monom :dimension nvars)))
380 "Construct a monomial in the polynomial ring
381RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
382which represents a single variable. It assumes number of variables
383NVARS and the variable is at position POS. Optionally, the variable
384may appear raised to power POWER. "
385 (declare (type fixnum nvars pos power) (type monom m))
386 (with-slots (exponents)
387 m
388 (setf (elt exponents pos) power)
389 m))
390
391(defun make-monom-constant (dimension)
392 (make-instance 'monom :dimension dimension))
393
394(defgeneric ->list (object)
395 (:method ((m monom))
396 "A human-readable representation of a monomial M as a list of exponents."
397 (coerce (monom-exponents m) 'list)))
398
399;; pure lexicographic
400(defgeneric lex> (p q &optional start end)
401 (:documentation "Return T if P>Q with respect to lexicographic
402order, otherwise NIL. The second returned value is T if P=Q,
403otherwise it is NIL.")
404 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
405 (declare (type fixnum start end))
406 (do ((i start (1+ i)))
407 ((>= i end) (values nil t))
408 (cond
409 ((> (monom-elt p i) (monom-elt q i))
410 (return-from lex> (values t nil)))
411 ((< (monom-elt p i) (monom-elt q i))
412 (return-from lex> (values nil nil)))))))
413
414;; total degree order, ties broken by lexicographic
415(defgeneric grlex> (p q &optional start end)
416 (:documentation "Return T if P>Q with respect to graded
417lexicographic order, otherwise NIL. The second returned value is T if
418P=Q, otherwise it is NIL.")
419 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
420 (declare (type monom p q) (type fixnum start end))
421 (let ((d1 (total-degree p start end))
422 (d2 (total-degree q start end)))
423 (declare (type fixnum d1 d2))
424 (cond
425 ((> d1 d2) (values t nil))
426 ((< d1 d2) (values nil nil))
427 (t
428 (lex> p q start end))))))
429
430;; reverse lexicographic
431(defgeneric revlex> (p q &optional start end)
432 (:documentation "Return T if P>Q with respect to reverse
433lexicographic order, NIL otherwise. The second returned value is T if
434P=Q, otherwise it is NIL. This is not and admissible monomial order
435because some sets do not have a minimal element. This order is useful
436in constructing other orders.")
437 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
438 (declare (type fixnum start end))
439 (do ((i (1- end) (1- i)))
440 ((< i start) (values nil t))
441 (declare (type fixnum i))
442 (cond
443 ((< (monom-elt p i) (monom-elt q i))
444 (return-from revlex> (values t nil)))
445 ((> (monom-elt p i) (monom-elt q i))
446 (return-from revlex> (values nil nil)))))))
447
448
449;; total degree, ties broken by reverse lexicographic
450(defgeneric grevlex> (p q &optional start end)
451 (:documentation "Return T if P>Q with respect to graded reverse
452lexicographic order, NIL otherwise. The second returned value is T if
453P=Q, otherwise it is NIL.")
454 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
455 (declare (type fixnum start end))
456 (let ((d1 (total-degree p start end))
457 (d2 (total-degree q start end)))
458 (declare (type fixnum d1 d2))
459 (cond
460 ((> d1 d2) (values t nil))
461 ((< d1 d2) (values nil nil))
462 (t
463 (revlex> p q start end))))))
464
465(defgeneric invlex> (p q &optional start end)
466 (:documentation "Return T if P>Q with respect to inverse
467lexicographic order, NIL otherwise The second returned value is T if
468P=Q, otherwise it is NIL.")
469 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
470 (declare (type fixnum start end))
471 (do ((i (1- end) (1- i)))
472 ((< i start) (values nil t))
473 (declare (type fixnum i))
474 (cond
475 ((> (monom-elt p i) (monom-elt q i))
476 (return-from invlex> (values t nil)))
477 ((< (monom-elt p i) (monom-elt q i))
478 (return-from invlex> (values nil nil)))))))
479
480(defun reverse-monomial-order (order)
481 "Create the inverse monomial order to the given monomial order ORDER."
482 #'(lambda (p q &optional (start 0) (end (monom-dimension q)))
483 (declare (type monom p q) (type fixnum start end))
484 (funcall order q p start end)))
485
486;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
487;;
488;; Order making functions
489;;
490;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
491
492;; This returns a closure with the same signature
493;; as all orders such as #'LEX>.
494(defun make-elimination-order-factory-1 (&optional (secondary-elimination-order #'lex>))
495 "It constructs an elimination order used for the 1-st elimination ideal,
496i.e. for eliminating the first variable. Thus, the order compares the degrees of the
497first variable in P and Q first, with ties broken by SECONDARY-ELIMINATION-ORDER."
498 #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
499 (declare (type monom p q) (type fixnum start end))
500 (cond
501 ((> (monom-elt p start) (monom-elt q start))
502 (values t nil))
503 ((< (monom-elt p start) (monom-elt q start))
504 (values nil nil))
505 (t
506 (funcall secondary-elimination-order p q (1+ start) end)))))
507
508;; This returns a closure which is called with an integer argument.
509;; The result is *another closure* with the same signature as all
510;; orders such as #'LEX>.
511(defun make-elimination-order-factory (&optional
512 (primary-elimination-order #'lex>)
513 (secondary-elimination-order #'lex>))
514 "Return a function with a single integer argument K. This should be
515the number of initial K variables X[0],X[1],...,X[K-1], which precede
516remaining variables. The call to the closure creates a predicate
517which compares monomials according to the K-th elimination order. The
518monomial orders PRIMARY-ELIMINATION-ORDER and
519SECONDARY-ELIMINATION-ORDER are used to compare the first K and the
520remaining variables, respectively, with ties broken by lexicographical
521order. That is, if PRIMARY-ELIMINATION-ORDER yields (VALUES NIL T),
522which indicates that the first K variables appear with identical
523powers, then the result is that of a call to
524SECONDARY-ELIMINATION-ORDER applied to the remaining variables
525X[K],X[K+1],..."
526 #'(lambda (k)
527 (cond
528 ((<= k 0)
529 (error "K must be at least 1"))
530 ((= k 1)
531 (make-elimination-order-factory-1 secondary-elimination-order))
532 (t
533 #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
534 (declare (type monom p q) (type fixnum start end))
535 (multiple-value-bind (primary equal)
536 (funcall primary-elimination-order p q start k)
537 (if equal
538 (funcall secondary-elimination-order p q k end)
539 (values primary nil))))))))
540
541(defclass term (monom)
542 ((coeff :initarg :coeff :accessor term-coeff))
543 (:default-initargs :coeff nil)
544 (:documentation "Implements a term, i.e. a product of a scalar
545and powers of some variables, such as 5*X^2*Y^3."))
546
547(defmethod update-instance-for-different-class :before ((old monom) (new term) &key (coeff 1))
548 "Converts OLD of class MONOM to a NEW of class TERM, initializing coefficient to COEFF."
549 (reinitialize-instance new :coeff coeff))
550
551(defmethod print-object ((self term) stream)
552 (print-unreadable-object (self stream :type t :identity t)
553 (with-accessors ((exponents monom-exponents)
554 (coeff term-coeff))
555 self
556 (format stream "EXPONENTS=~A COEFF=~A"
557 exponents coeff))))
558
559(defmethod universal-equalp ((term1 term) (term2 term))
560 "Returns T if TERM1 and TERM2 are equal as MONOM, and coefficients
561are UNIVERSAL-EQUALP."
562 (and (call-next-method)
563 (universal-equalp (term-coeff term1) (term-coeff term2))))
564
565(defmethod update-instance-for-different-class :after ((old monom) (new term) &key)
566 (setf (term-coeff new) 1))
567
568(defmethod multiply-by :before ((self term) (other term))
569 "Destructively multiply terms SELF and OTHER and store the result into SELF.
570It returns SELF."
571 (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
572
573(defmethod left-tensor-product-by :before ((self term) (other term))
574 (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
575
576(defmethod right-tensor-product-by :before ((self term) (other term))
577 (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
578
579(defmethod divide-by :before ((self term) (other term))
580 (setf (term-coeff self) (divide-by (term-coeff self) (term-coeff other))))
581
582(defgeneric unary-minus (self)
583 (:documentation "Negate object SELF and return it.")
584 (:method ((self number)) (- self))
585 (:method ((self term))
586 (setf (term-coeff self) (unary-minus (term-coeff self)))
587 self))
588
589(defgeneric universal-zerop (self)
590 (:documentation "Return T iff SELF is zero.")
591 (:method ((self number)) (zerop self))
592 (:method ((self term))
593 (universal-zerop (term-coeff self))))
Note: See TracBrowser for help on using the repository browser.