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

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

* empty log message *

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