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

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

* empty log message *

File size: 24.8 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 :ring :integer-ring)
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"
37 "DIVIDES-P"
38 "DIVIDES-LCM-P"
39 "LCM-DIVIDES-LCM-P"
40 "LCM-EQUAL-LCM-P"
41 "DIVISIBLE-BY-P"
42 "REL-PRIME-P"
43 "UNIVERSAL-LCM"
44 "UNIVERSAL-GCD"
45 "DEPENDS-P"
46 "LEFT-TENSOR-PRODUCT-BY"
47 "RIGHT-TENSOR-PRODUCT-BY"
48 "LEFT-CONTRACT"
49 "MAKE-MONOM-VARIABLE"
50 "MAKE-MONOM-CONSTANT"
51 "MAKE-TERM-CONSTANT"
52 "->LIST"
53 "->SEXP"
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 "UNARY-INVERSE"
64 "UNIVERSAL-ZEROP")
65 (:documentation
66 "This package implements basic operations on monomials, including
67various monomial orders.
68
69DATA STRUCTURES: Conceptually, monomials can be represented as lists:
70
71 monom: (n1 n2 ... nk) where ni are non-negative integers
72
73However, lists may be implemented as other sequence types, so the
74flexibility to change the representation should be maintained in the
75code to use general operations on sequences whenever possible. The
76optimization for the actual representation should be left to
77declarations and the compiler.
78
79EXAMPLES: Suppose that variables are x and y. Then
80
81 Monom x*y^2 ---> (1 2) "))
82
83(in-package :monom)
84
85(proclaim '(optimize (speed 0) (space 0) (safety 3) (debug 0)))
86
87(deftype exponent ()
88 "Type of exponent in a monomial."
89 'fixnum)
90
91(defclass monom ()
92 ((exponents :initarg :exponents :accessor monom-exponents
93 :documentation "The powers of the variables."))
94 ;; default-initargs are not needed, they are handled by SHARED-INITIALIZE
95 ;;(:default-initargs :dimension 'foo :exponents 'bar :exponent 'baz)
96 (:documentation
97 "Implements a monomial, i.e. a product of powers
98of variables, like X*Y^2."))
99
100(defmethod print-object ((self monom) stream)
101 (print-unreadable-object (self stream :type t :identity t)
102 (with-accessors ((exponents monom-exponents))
103 self
104 (format stream "EXPONENTS=~A"
105 exponents))))
106
107(defmethod initialize-instance :after ((self monom)
108 &key
109 (dimension 0 dimension-supplied-p)
110 (exponents nil exponents-supplied-p)
111 (exponent 0)
112 &allow-other-keys
113 )
114 "The following INITIALIZE-INSTANCE method allows instance initialization
115of a MONOM in a style similar to MAKE-ARRAY, e.g.:
116
117 (MAKE-INSTANCE 'MONOM :EXPONENTS '(1 2 3)) --> #<MONOM EXPONENTS=#(1 2 3)>
118 (MAKE-INSTANCE 'MONOM :DIMENSION 3) --> #<MONOM EXPONENTS=#(0 0 0)>
119 (MAKE-INSTANCE 'MONOM :DIMENSION 3 :EXPONENT 7) --> #<MONOM EXPONENTS=#(7 7 7)>
120
121If both DIMENSION and EXPONENTS are supplied, they must be compatible,
122i.e. the length of EXPONENTS must be equal DIMENSION. If EXPONENTS
123is not supplied, a monom with repeated value EXPONENT is created.
124By default EXPONENT is 0, which results in a constant monomial.
125"
126 (cond
127 (exponents-supplied-p
128 (when (and dimension-supplied-p
129 (/= dimension (length exponents)))
130 (error "EXPONENTS (~A) must have supplied length DIMENSION (~A)"
131 exponents dimension))
132 (let ((dim (length exponents)))
133 (setf (slot-value self 'exponents) (make-array dim :initial-contents exponents))))
134 (dimension-supplied-p
135 ;; when all exponents are to be identical
136 (setf (slot-value self 'exponents) (make-array (list dimension)
137 :initial-element exponent
138 :element-type 'exponent)))
139 (t
140 (error "Initarg DIMENSION or EXPONENTS must be supplied."))))
141
142(defgeneric monom-dimension (self)
143 (:method ((self monom))
144 (length (monom-exponents self))))
145
146(defmethod universal-equalp ((self monom) (other monom))
147 "Returns T iff monomials SELF and OTHER have identical EXPONENTS."
148 (equalp (monom-exponents self) (monom-exponents other))))
149
150(defgeneric monom-elt (m index)
151 (:documentation "Return the power in the monomial M of variable number INDEX.")
152 (:method ((m monom) index)
153 "Return the power in the monomial M of variable number INDEX."
154 (with-slots (exponents)
155 m
156 (elt exponents index))))
157
158(defgeneric (setf monom-elt) (new-value m index)
159 (:documentation "Set the power in the monomial M of variable number INDEX.")
160 (:method (new-value (m monom) index)
161 (with-slots (exponents)
162 m
163 (setf (elt exponents index) new-value))))
164
165(defgeneric total-degree (m &optional start end)
166 (:documentation "Return the total degree of a monomoal M. Optinally, a range
167of variables may be specified with arguments START and END.")
168 (:method ((m monom) &optional (start 0) (end (monom-dimension m)))
169 (declare (type fixnum start end))
170 (with-slots (exponents)
171 m
172 (reduce #'+ exponents :start start :end end))))
173
174(defgeneric sugar (m &optional start end)
175 (:documentation "Return the sugar of a monomial M. Optinally, a range
176of variables may be specified with arguments START and END.")
177 (:method ((m monom) &optional (start 0) (end (monom-dimension m)))
178 (declare (type fixnum start end))
179 (total-degree m start end)))
180
181(defgeneric multiply-by (self other)
182 (:documentation "Multiply SELF by OTHER, return SELF. The object SELF
183may be destructively modified in the process, while OTHER should remain
184unmodified.")
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
196(defun multiply (factor &rest more-factors)
197 "Successively multiplies factor FACTOR by the remaining arguments
198MORE-FACTORS, using MULTIPLY-BY to multiply two factors. Thus
199FACTOR may be destructively modified."
200 (reduce #'multiply-by more-factors :initial-value (copy-instance factor)))
201
202(defgeneric divide-by (self other)
203 (:documentation "Divide SELF by OTHER, return SELF.")
204 (:method ((self number) (other number)) (/ self other))
205 (:method ((self monom) (other monom))
206 (with-slots ((exponents1 exponents))
207 self
208 (with-slots ((exponents2 exponents))
209 other
210 (unless (= (length exponents1) (length exponents2))
211 (error "divide-by: Incompatible dimensions."))
212 (unless (every #'>= exponents1 exponents2)
213 (error "divide-by: Negative power would result."))
214 (map-into exponents1 #'- exponents1 exponents2)))
215 self))
216
217(defmethod copy-instance :around ((object monom) &rest initargs &key &allow-other-keys)
218 "An :AROUND method of COPY-INSTANCE. It replaces exponents with a fresh copy of the sequence."
219 (declare (ignore object initargs))
220 (let ((copy (call-next-method)))
221 (setf (monom-exponents copy) (copy-seq (monom-exponents copy)))
222 copy))
223
224(defgeneric unary-inverse (self)
225 (:documentation "Returns the unary inverse of SELF.")
226 (:method ((self number)) (/ self))
227 (:method :before ((self monom))
228 (assert (zerop (total-degree self))
229 nil
230 "Monom ~A must have total degree 0 to be invertible." self))
231 (:method ((self monom)) self))
232
233(defun divide (numerator &rest denominators)
234 "Successively divides NUMERATOR by elements of DENOMINATORS. The operation
235destructively modifies NUMERATOR."
236 (cond ((endp denominators)
237 (unary-inverse numerator))
238 (t (reduce #'divide-by denominators :initial-value (copy-instance numerator)))))
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 ((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;; pure lexicographic
395(defgeneric lex> (p q &optional start end)
396 (:documentation "Return T if P>Q with respect to lexicographic
397order, otherwise NIL. The second returned value is T if P=Q,
398otherwise it is NIL.")
399 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
400 (declare (type fixnum start end))
401 (do ((i start (1+ i)))
402 ((>= i end) (values nil t))
403 (cond
404 ((> (monom-elt p i) (monom-elt q i))
405 (return-from lex> (values t nil)))
406 ((< (monom-elt p i) (monom-elt q i))
407 (return-from lex> (values nil nil)))))))
408
409;; total degree order, ties broken by lexicographic
410(defgeneric grlex> (p q &optional start end)
411 (:documentation "Return T if P>Q with respect to graded
412lexicographic order, otherwise NIL. The second returned value is T if
413P=Q, otherwise it is NIL.")
414 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
415 (declare (type monom p q) (type fixnum start end))
416 (let ((d1 (total-degree p start end))
417 (d2 (total-degree q start end)))
418 (declare (type fixnum d1 d2))
419 (cond
420 ((> d1 d2) (values t nil))
421 ((< d1 d2) (values nil nil))
422 (t
423 (lex> p q start end))))))
424
425;; reverse lexicographic
426(defgeneric revlex> (p q &optional start end)
427 (:documentation "Return T if P>Q with respect to reverse
428lexicographic order, NIL otherwise. The second returned value is T if
429P=Q, otherwise it is NIL. This is not and admissible monomial order
430because some sets do not have a minimal element. This order is useful
431in constructing other orders.")
432 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
433 (declare (type fixnum start end))
434 (do ((i (1- end) (1- i)))
435 ((< i start) (values nil t))
436 (declare (type fixnum i))
437 (cond
438 ((< (monom-elt p i) (monom-elt q i))
439 (return-from revlex> (values t nil)))
440 ((> (monom-elt p i) (monom-elt q i))
441 (return-from revlex> (values nil nil)))))))
442
443
444;; total degree, ties broken by reverse lexicographic
445(defgeneric grevlex> (p q &optional start end)
446 (:documentation "Return T if P>Q with respect to graded reverse
447lexicographic order, NIL otherwise. The second returned value is T if
448P=Q, otherwise it is NIL.")
449 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
450 (declare (type fixnum start end))
451 (let ((d1 (total-degree p start end))
452 (d2 (total-degree q start end)))
453 (declare (type fixnum d1 d2))
454 (cond
455 ((> d1 d2) (values t nil))
456 ((< d1 d2) (values nil nil))
457 (t
458 (revlex> p q start end))))))
459
460(defgeneric invlex> (p q &optional start end)
461 (:documentation "Return T if P>Q with respect to inverse
462lexicographic order, NIL otherwise The second returned value is T if
463P=Q, otherwise it is NIL.")
464 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
465 (declare (type fixnum start end))
466 (do ((i (1- end) (1- i)))
467 ((< i start) (values nil t))
468 (declare (type fixnum i))
469 (cond
470 ((> (monom-elt p i) (monom-elt q i))
471 (return-from invlex> (values t nil)))
472 ((< (monom-elt p i) (monom-elt q i))
473 (return-from invlex> (values nil nil)))))))
474
475(defun reverse-monomial-order (order)
476 "Create the inverse monomial order to the given monomial order ORDER."
477 #'(lambda (p q &optional (start 0) (end (monom-dimension q)))
478 (declare (type monom p q) (type fixnum start end))
479 (funcall order q p start end)))
480
481;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
482;;
483;; Order making functions
484;;
485;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
486
487;; This returns a closure with the same signature
488;; as all orders such as #'LEX>.
489(defun make-elimination-order-factory-1 (&optional (secondary-elimination-order #'lex>))
490 "It constructs an elimination order used for the 1-st elimination ideal,
491i.e. for eliminating the first variable. Thus, the order compares the degrees of the
492first variable in P and Q first, with ties broken by SECONDARY-ELIMINATION-ORDER."
493 #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
494 (declare (type monom p q) (type fixnum start end))
495 (cond
496 ((> (monom-elt p start) (monom-elt q start))
497 (values t nil))
498 ((< (monom-elt p start) (monom-elt q start))
499 (values nil nil))
500 (t
501 (funcall secondary-elimination-order p q (1+ start) end)))))
502
503;; This returns a closure which is called with an integer argument.
504;; The result is *another closure* with the same signature as all
505;; orders such as #'LEX>.
506(defun make-elimination-order-factory (&optional
507 (primary-elimination-order #'lex>)
508 (secondary-elimination-order #'lex>))
509 "Return a function with a single integer argument K. This should be
510the number of initial K variables X[0],X[1],...,X[K-1], which precede
511remaining variables. The call to the closure creates a predicate
512which compares monomials according to the K-th elimination order. The
513monomial orders PRIMARY-ELIMINATION-ORDER and
514SECONDARY-ELIMINATION-ORDER are used to compare the first K and the
515remaining variables, respectively, with ties broken by lexicographical
516order. That is, if PRIMARY-ELIMINATION-ORDER yields (VALUES NIL T),
517which indicates that the first K variables appear with identical
518powers, then the result is that of a call to
519SECONDARY-ELIMINATION-ORDER applied to the remaining variables
520X[K],X[K+1],..."
521 #'(lambda (k)
522 (cond
523 ((<= k 0)
524 (error "K must be at least 1"))
525 ((= k 1)
526 (make-elimination-order-factory-1 secondary-elimination-order))
527 (t
528 #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
529 (declare (type monom p q) (type fixnum start end))
530 (multiple-value-bind (primary equal)
531 (funcall primary-elimination-order p q start k)
532 (if equal
533 (funcall secondary-elimination-order p q k end)
534 (values primary nil))))))))
535
536(defclass term (monom)
537 ((coeff :initarg :coeff :accessor term-coeff :type ring))
538 (:default-initargs :coeff 1)
539 (:documentation "Implements a term, i.e. a product of a scalar
540and powers of some variables, such as 5*X^2*Y^3."))
541
542(defmethod initialize-instance :around ((self term) &rest initargs &key coeff)
543 "A convenience method. If a coefficient is an integer, wrap it in the INTEGER-RING object"
544 ;; Dispatch on supplied type of coefficient
545 (typecase coeff
546 (integer
547 (setf (getf initargs :coeff) (make-instance 'integer-ring :value coeff))))
548 ;; Now pass new initargs to the next method
549 (apply #'call-next-method (cons self initargs)))
550
551(defmethod update-instance-for-different-class :after ((old monom) (new term) &key (coeff 1))
552 "Converts OLD of class MONOM to a NEW of class TERM, initializing coefficient to COEFF."
553 (reinitialize-instance new :coeff coeff))
554
555(defmethod update-instance-for-different-class :after ((old term) (new term) &key (coeff (term-coeff old)))
556 "Converts OLD of class TERM to a NEW of class TERM, initializing coefficient to COEFF."
557 (reinitialize-instance new :coeff coeff))
558
559
560(defmethod print-object ((self term) stream)
561 (print-unreadable-object (self stream :type t :identity t)
562 (with-accessors ((exponents monom-exponents)
563 (coeff term-coeff))
564 self
565 (format stream "EXPONENTS=~A COEFF=~A"
566 exponents coeff))))
567
568(defmethod copy-instance :around ((object term) &rest initargs &key &allow-other-keys)
569 "An :AROUND method of COPY-INSTANCE. It replaces the coefficient with a fresh copy."
570 (declare (ignore object initargs))
571 (let ((copy (call-next-method)))
572 (setf (term-coeff copy) (copy-instance (term-coeff object)))
573 copy))
574
575(defmethod multiply-by ((self term) (other number))
576 (reinitialize-instance self :coeff (multiply-by (term-coeff self) other)))
577
578(defmethod divide-by ((self term) (other number))
579 (reinitialize-instance self :coeff (divide-by (term-coeff self) other)))
580
581(defmethod unary-inverse :after ((self term))
582 (with-slots (coeff)
583 self
584 (setf coeff (unary-inverse coeff))))
585
586(defun make-term-constant (dimension &optional (coeff 1))
587 (make-instance 'term :dimension dimension :coeff coeff))
588
589(defmethod universal-equalp ((term1 term) (term2 term))
590 "Returns T if TERM1 and TERM2 are equal as MONOM, and coefficients
591are UNIVERSAL-EQUALP."
592 (and (call-next-method)
593 (universal-equalp (term-coeff term1) (term-coeff term2))))
594
595(defmethod multiply-by :before ((self term) (other term))
596 "Destructively multiply terms SELF and OTHER and store the result into SELF.
597It returns SELF."
598 (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
599
600
601(defmethod left-tensor-product-by :before ((self term) (other term))
602 (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
603
604(defmethod right-tensor-product-by :before ((self term) (other term))
605 (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
606
607(defmethod divide-by :before ((self term) (other term))
608 (setf (term-coeff self) (divide-by (term-coeff self) (term-coeff other))))
609
610(defgeneric unary-minus (self)
611 (:documentation "Negate object SELF and return it.")
612 (:method ((self number)) (- self))
613 (:method ((self term))
614 (setf (term-coeff self) (unary-minus (term-coeff self)))
615 self))
616
617(defgeneric universal-zerop (self)
618 (:documentation "Return T iff SELF is zero.")
619 (:method ((self number)) (zerop self))
620 (:method ((self term))
621 (universal-zerop (term-coeff self))))
622
623(defgeneric ->list (self)
624 (:method ((self monom))
625 "A human-readable representation of a monomial SELF as a list of exponents."
626 (coerce (monom-exponents self) 'list))
627 (:method ((self term))
628 "A human-readable representation of a term SELF as a cons of the list of exponents and the coefficient."
629 (cons (coerce (monom-exponents self) 'list) (term-coeff self))))
630
631(defgeneric ->sexp (object &optional vars)
632 (:documentation "Convert a polynomial OBJECT to an S-expression, using variables VARS.")
633 (:method :before ((object monom) &optional vars)
634 "Check the length of variables VARS against the length of exponents in OBJECT."
635 (with-slots (exponents)
636 object
637 (assert (= (length vars) (length exponents))
638 nil
639 "Variables ~A and exponents ~A must have the same length." vars exponents)))
640 (:method ((object monom) &optional vars)
641 "Convert a monomial OBJECT to infix form, using variable VARS to build the representation."
642 (with-slots (exponents)
643 object
644 (let ((m (mapcan #'(lambda (var power)
645 (cond ((= power 0) nil)
646 ((= power 1) (list var))
647 (t (list `(expt ,var ,power)))))
648 vars (coerce exponents 'list))))
649 (cond ((endp m) 1)
650 ((endp (cdr m)) (car m))
651 (t
652 (cons '* m))))))
653 (:method :around ((object term) &optional vars)
654 "Convert a term OBJECT to infix form, using variable VARS to build the representation."
655 (declare (ignore vars))
656 (with-slots (coeff)
657 object
658 (let ((monom-sexp (call-next-method))
659 (coeff-sexp (->sexp coeff)))
660 (cond ((eql coeff-sexp 1) monom-sexp)
661 ((atom monom-sexp)
662 (cond ((eql monom-sexp 1) coeff-sexp)
663 (t (list '* coeff-sexp monom-sexp))))
664 ((eql (car monom-sexp) '*)
665 (list* '* coeff-sexp (cdr monom-sexp)))
666 (t
667 (list '* coeff-sexp monom-sexp)))))))
Note: See TracBrowser for help on using the repository browser.