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

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

* empty log message *

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