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

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

* empty log message *

File size: 24.3 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"
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(defgeneric universal-equalp (object1 object2)
147 (:documentation "Returns T iff OBJECT1 and OBJECT2 are equal.")
148 (:method ((object1 cons) (object2 cons)) (every #'universal-equalp object1 object2))
149 (:method ((object1 number) (object2 number)) (= object1 object2))
150 (:method ((m1 monom) (m2 monom))
151 "Returns T iff monomials M1 and M2 have identical EXPONENTS."
152 (equalp (monom-exponents m1) (monom-exponents m2))))
153
154(defgeneric monom-elt (m index)
155 (:documentation "Return the power in the monomial M of variable number INDEX.")
156 (:method ((m monom) index)
157 "Return the power in the monomial M of variable number INDEX."
158 (with-slots (exponents)
159 m
160 (elt exponents index))))
161
162(defgeneric (setf monom-elt) (new-value m index)
163 (:documentation "Set the power in the monomial M of variable number INDEX.")
164 (:method (new-value (m monom) index)
165 (with-slots (exponents)
166 m
167 (setf (elt exponents index) new-value))))
168
169(defgeneric total-degree (m &optional start end)
170 (:documentation "Return the total degree of a monomoal M. Optinally, a range
171of variables may be specified with arguments START and END.")
172 (:method ((m monom) &optional (start 0) (end (monom-dimension m)))
173 (declare (type fixnum start end))
174 (with-slots (exponents)
175 m
176 (reduce #'+ exponents :start start :end end))))
177
178(defgeneric sugar (m &optional start end)
179 (:documentation "Return the sugar of a monomial M. Optinally, a range
180of variables may be specified with arguments START and END.")
181 (:method ((m monom) &optional (start 0) (end (monom-dimension m)))
182 (declare (type fixnum start end))
183 (total-degree m start end)))
184
185(defgeneric multiply-by (self other)
186 (:documentation "Multiply SELF by OTHER, return SELF.")
187 (:method ((self number) (other number)) (* self other))
188 (:method ((self monom) (other monom))
189 (with-slots ((exponents1 exponents))
190 self
191 (with-slots ((exponents2 exponents))
192 other
193 (unless (= (length exponents1) (length exponents2))
194 (error "Incompatible dimensions"))
195 (map-into exponents1 #'+ exponents1 exponents2)))
196 self))
197
198(defun multiply (factor &rest more-factors)
199 "Successively multiplies factor FACTOR by the remaining arguments
200MORE-FACTORS."
201 (reduce #'multiply-by more-factors :initial-value factor))
202
203(defgeneric divide-by (self other)
204 (:documentation "Divide SELF by OTHER, return SELF.")
205 (:method ((self number) (other number)) (/ self other))
206 (:method ((self monom) (other monom))
207 (with-slots ((exponents1 exponents))
208 self
209 (with-slots ((exponents2 exponents))
210 other
211 (unless (= (length exponents1) (length exponents2))
212 (error "divide-by: Incompatible dimensions."))
213 (unless (every #'>= exponents1 exponents2)
214 (error "divide-by: Negative power would result."))
215 (map-into exponents1 #'- exponents1 exponents2)))
216 self))
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))))
235
236(defgeneric unary-inverse (self)
237 (:documentation "Returns the unary inverse of SELF.")
238 (:method ((self number)) (/ self))
239 (:method :before ((self monom))
240 (assert (zerop (total-degree self))
241 nil
242 "Monom ~A must have total degree 0 to be invertible." self))
243 (:method ((self monom)) self))
244
245(defun divide (numerator &rest denominators)
246 "Non-destructively divide object NUMERATOR by product of DENOMINATORS."
247 (cond ((endp denominators)
248 (unary-inverse numerator))
249 (t (divide-by (copy-instance numerator) (apply #'multiply denominators)))))
250
251(defgeneric divides-p (object1 object2)
252 (:documentation "Returns T if OBJECT1 divides OBJECT2.")
253 (:method ((m1 monom) (m2 monom))
254 "Returns T if monomial M1 divides monomial M2, NIL otherwise."
255 (with-slots ((exponents1 exponents))
256 m1
257 (with-slots ((exponents2 exponents))
258 m2
259 (every #'<= exponents1 exponents2)))))
260
261(defgeneric divides-lcm-p (object1 object2 object3)
262 (:documentation "Returns T if OBJECT1 divides LCM(OBJECT2,OBJECT3), NIL otherwise.")
263 (:method ((m1 monom) (m2 monom) (m3 monom))
264 "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
265 (with-slots ((exponents1 exponents))
266 m1
267 (with-slots ((exponents2 exponents))
268 m2
269 (with-slots ((exponents3 exponents))
270 m3
271 (every #'(lambda (x y z) (<= x (max y z)))
272 exponents1 exponents2 exponents3))))))
273
274(defgeneric lcm-divides-lcm-p (object1 object2 object3 object4)
275 (:method ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
276 "Returns T if monomial LCM(M1,M2) divides LCM(M3,M4), NIL otherwise."
277 (with-slots ((exponents1 exponents))
278 m1
279 (with-slots ((exponents2 exponents))
280 m2
281 (with-slots ((exponents3 exponents))
282 m3
283 (with-slots ((exponents4 exponents))
284 m4
285 (every #'(lambda (x y z w) (<= (max x y) (max z w)))
286 exponents1 exponents2 exponents3 exponents4)))))))
287
288(defgeneric monom-lcm-equal-lcm-p (object1 object2 object3 object4)
289 (:method ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
290 "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
291 (with-slots ((exponents1 exponents))
292 m1
293 (with-slots ((exponents2 exponents))
294 m2
295 (with-slots ((exponents3 exponents))
296 m3
297 (with-slots ((exponents4 exponents))
298 m4
299 (every
300 #'(lambda (x y z w) (= (max x y) (max z w)))
301 exponents1 exponents2 exponents3 exponents4)))))))
302
303(defgeneric divisible-by-p (object1 object2)
304 (:documentation "Return T if OBJECT1 is divisible by OBJECT2.")
305 (:method ((m1 monom) (m2 monom))
306 "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
307 (with-slots ((exponents1 exponents))
308 m1
309 (with-slots ((exponents2 exponents))
310 m2
311 (every #'>= exponents1 exponents2)))))
312
313(defgeneric rel-prime-p (object1 object2)
314 (:documentation "Returns T if objects OBJECT1 and OBJECT2 are relatively prime.")
315 (:method ((m1 monom) (m2 monom))
316 "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
317 (with-slots ((exponents1 exponents))
318 m1
319 (with-slots ((exponents2 exponents))
320 m2
321 (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2)))))
322
323(defgeneric universal-lcm (object1 object2)
324 (:documentation "Returns the multiple of objects OBJECT1 and OBJECT2.")
325 (:method ((m1 monom) (m2 monom))
326 "Returns least common multiple of monomials M1 and M2."
327 (with-slots ((exponents1 exponents))
328 m1
329 (with-slots ((exponents2 exponents))
330 m2
331 (let* ((exponents (copy-seq exponents1)))
332 (map-into exponents #'max exponents1 exponents2)
333 (make-instance 'monom :exponents exponents))))))
334
335
336(defgeneric universal-gcd (object1 object2)
337 (:documentation "Returns GCD of objects OBJECT1 and OBJECT2")
338 (:method ((object1 number) (object2 number)) (gcd object1 object2))
339 (:method ((m1 monom) (m2 monom))
340 "Returns greatest common divisor of monomials M1 and M2."
341 (with-slots ((exponents1 exponents))
342 m1
343 (with-slots ((exponents2 exponents))
344 m2
345 (let* ((exponents (copy-seq exponents1)))
346 (map-into exponents #'min exponents1 exponents2)
347 (make-instance 'monom :exponents exponents))))))
348
349(defgeneric depends-p (object k)
350 (:documentation "Returns T iff object OBJECT depends on variable K.")
351 (:method ((m monom) k)
352 "Return T if the monomial M depends on variable number K."
353 (declare (type fixnum k))
354 (with-slots (exponents)
355 m
356 (plusp (elt exponents k)))))
357
358(defgeneric left-tensor-product-by (self other)
359 (:documentation "Returns a tensor product SELF by OTHER, stored into
360 SELF. Return 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 exponents2 exponents1))))
367 self))
368
369(defgeneric right-tensor-product-by (self other)
370 (:documentation "Returns a tensor product of OTHER by SELF, stored
371 into SELF. Returns SELF.")
372 (:method ((self monom) (other monom))
373 (with-slots ((exponents1 exponents))
374 self
375 (with-slots ((exponents2 exponents))
376 other
377 (setf exponents1 (concatenate 'vector exponents1 exponents2))))
378 self))
379
380(defgeneric left-contract (self k)
381 (:documentation "Drop the first K variables in object SELF.")
382 (:method ((self monom) k)
383 "Drop the first K variables in monomial M."
384 (declare (fixnum k))
385 (with-slots (exponents)
386 self
387 (setf exponents (subseq exponents k)))
388 self))
389
390(defun make-monom-variable (nvars pos &optional (power 1)
391 &aux (m (make-instance 'monom :dimension nvars)))
392 "Construct a monomial in the polynomial ring
393RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
394which represents a single variable. It assumes number of variables
395NVARS and the variable is at position POS. Optionally, the variable
396may appear raised to power POWER. "
397 (declare (type fixnum nvars pos power) (type monom m))
398 (with-slots (exponents)
399 m
400 (setf (elt exponents pos) power)
401 m))
402
403(defun make-monom-constant (dimension)
404 (make-instance 'monom :dimension dimension))
405
406;; pure lexicographic
407(defgeneric lex> (p q &optional start end)
408 (:documentation "Return T if P>Q with respect to lexicographic
409order, otherwise NIL. The second returned value is T if P=Q,
410otherwise it is NIL.")
411 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
412 (declare (type fixnum start end))
413 (do ((i start (1+ i)))
414 ((>= i end) (values nil t))
415 (cond
416 ((> (monom-elt p i) (monom-elt q i))
417 (return-from lex> (values t nil)))
418 ((< (monom-elt p i) (monom-elt q i))
419 (return-from lex> (values nil nil)))))))
420
421;; total degree order, ties broken by lexicographic
422(defgeneric grlex> (p q &optional start end)
423 (:documentation "Return T if P>Q with respect to graded
424lexicographic order, otherwise NIL. The second returned value is T if
425P=Q, otherwise it is NIL.")
426 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
427 (declare (type monom p q) (type fixnum start end))
428 (let ((d1 (total-degree p start end))
429 (d2 (total-degree q start end)))
430 (declare (type fixnum d1 d2))
431 (cond
432 ((> d1 d2) (values t nil))
433 ((< d1 d2) (values nil nil))
434 (t
435 (lex> p q start end))))))
436
437;; reverse lexicographic
438(defgeneric revlex> (p q &optional start end)
439 (:documentation "Return T if P>Q with respect to reverse
440lexicographic order, NIL otherwise. The second returned value is T if
441P=Q, otherwise it is NIL. This is not and admissible monomial order
442because some sets do not have a minimal element. This order is useful
443in constructing other orders.")
444 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
445 (declare (type fixnum start end))
446 (do ((i (1- end) (1- i)))
447 ((< i start) (values nil t))
448 (declare (type fixnum i))
449 (cond
450 ((< (monom-elt p i) (monom-elt q i))
451 (return-from revlex> (values t nil)))
452 ((> (monom-elt p i) (monom-elt q i))
453 (return-from revlex> (values nil nil)))))))
454
455
456;; total degree, ties broken by reverse lexicographic
457(defgeneric grevlex> (p q &optional start end)
458 (:documentation "Return T if P>Q with respect to graded reverse
459lexicographic order, NIL otherwise. The second returned value is T if
460P=Q, otherwise it is NIL.")
461 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
462 (declare (type fixnum start end))
463 (let ((d1 (total-degree p start end))
464 (d2 (total-degree q start end)))
465 (declare (type fixnum d1 d2))
466 (cond
467 ((> d1 d2) (values t nil))
468 ((< d1 d2) (values nil nil))
469 (t
470 (revlex> p q start end))))))
471
472(defgeneric invlex> (p q &optional start end)
473 (:documentation "Return T if P>Q with respect to inverse
474lexicographic order, NIL otherwise The second returned value is T if
475P=Q, otherwise it is NIL.")
476 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
477 (declare (type fixnum start end))
478 (do ((i (1- end) (1- i)))
479 ((< i start) (values nil t))
480 (declare (type fixnum i))
481 (cond
482 ((> (monom-elt p i) (monom-elt q i))
483 (return-from invlex> (values t nil)))
484 ((< (monom-elt p i) (monom-elt q i))
485 (return-from invlex> (values nil nil)))))))
486
487(defun reverse-monomial-order (order)
488 "Create the inverse monomial order to the given monomial order ORDER."
489 #'(lambda (p q &optional (start 0) (end (monom-dimension q)))
490 (declare (type monom p q) (type fixnum start end))
491 (funcall order q p start end)))
492
493;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
494;;
495;; Order making functions
496;;
497;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
498
499;; This returns a closure with the same signature
500;; as all orders such as #'LEX>.
501(defun make-elimination-order-factory-1 (&optional (secondary-elimination-order #'lex>))
502 "It constructs an elimination order used for the 1-st elimination ideal,
503i.e. for eliminating the first variable. Thus, the order compares the degrees of the
504first variable in P and Q first, with ties broken by SECONDARY-ELIMINATION-ORDER."
505 #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
506 (declare (type monom p q) (type fixnum start end))
507 (cond
508 ((> (monom-elt p start) (monom-elt q start))
509 (values t nil))
510 ((< (monom-elt p start) (monom-elt q start))
511 (values nil nil))
512 (t
513 (funcall secondary-elimination-order p q (1+ start) end)))))
514
515;; This returns a closure which is called with an integer argument.
516;; The result is *another closure* with the same signature as all
517;; orders such as #'LEX>.
518(defun make-elimination-order-factory (&optional
519 (primary-elimination-order #'lex>)
520 (secondary-elimination-order #'lex>))
521 "Return a function with a single integer argument K. This should be
522the number of initial K variables X[0],X[1],...,X[K-1], which precede
523remaining variables. The call to the closure creates a predicate
524which compares monomials according to the K-th elimination order. The
525monomial orders PRIMARY-ELIMINATION-ORDER and
526SECONDARY-ELIMINATION-ORDER are used to compare the first K and the
527remaining variables, respectively, with ties broken by lexicographical
528order. That is, if PRIMARY-ELIMINATION-ORDER yields (VALUES NIL T),
529which indicates that the first K variables appear with identical
530powers, then the result is that of a call to
531SECONDARY-ELIMINATION-ORDER applied to the remaining variables
532X[K],X[K+1],..."
533 #'(lambda (k)
534 (cond
535 ((<= k 0)
536 (error "K must be at least 1"))
537 ((= k 1)
538 (make-elimination-order-factory-1 secondary-elimination-order))
539 (t
540 #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
541 (declare (type monom p q) (type fixnum start end))
542 (multiple-value-bind (primary equal)
543 (funcall primary-elimination-order p q start k)
544 (if equal
545 (funcall secondary-elimination-order p q k end)
546 (values primary nil))))))))
547
548(defclass term (monom)
549 ((coeff :initarg :coeff :accessor term-coeff))
550 (:default-initargs :coeff nil)
551 (:documentation "Implements a term, i.e. a product of a scalar
552and powers of some variables, such as 5*X^2*Y^3."))
553
554(defmethod update-instance-for-different-class :after ((old monom) (new term) &key (coeff 1))
555 "Converts OLD of class MONOM to a NEW of class TERM, initializing coefficient to COEFF."
556 (reinitialize-instance new :coeff coeff))
557
558(defmethod update-instance-for-different-class :after ((old term) (new term) &key (coeff (term-coeff old)))
559 "Converts OLD of class TERM to a NEW of class TERM, initializing coefficient to COEFF."
560 (reinitialize-instance new :coeff coeff))
561
562
563(defmethod print-object ((self term) stream)
564 (print-unreadable-object (self stream :type t :identity t)
565 (with-accessors ((exponents monom-exponents)
566 (coeff term-coeff))
567 self
568 (format stream "EXPONENTS=~A COEFF=~A"
569 exponents coeff))))
570
571(defmethod multiply-by ((self term) (other number))
572 (reinitialize-instance self :coeff (multiply-by (term-coeff self) other)))
573
574(defmethod divide-by ((self term) (other number))
575 (reinitialize-instance self :coeff (divide-by (term-coeff self) other)))
576
577(defmethod unary-inverse :after ((self term))
578 (with-slots (coeff)
579 self
580 (setf coeff (unary-inverse coeff))))
581
582(defun make-term-constant (dimension &optional (coeff 1))
583 (make-instance 'term :dimension dimension :coeff coeff))
584
585(defmethod universal-equalp ((term1 term) (term2 term))
586 "Returns T if TERM1 and TERM2 are equal as MONOM, and coefficients
587are UNIVERSAL-EQUALP."
588 (and (call-next-method)
589 (universal-equalp (term-coeff term1) (term-coeff term2))))
590
591(defmethod multiply-by :before ((self term) (other term))
592 "Destructively multiply terms SELF and OTHER and store the result into SELF.
593It returns SELF."
594 (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
595
596
597(defmethod left-tensor-product-by :before ((self term) (other term))
598 (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
599
600(defmethod right-tensor-product-by :before ((self term) (other term))
601 (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
602
603(defmethod divide-by :before ((self term) (other term))
604 (setf (term-coeff self) (divide-by (term-coeff self) (term-coeff other))))
605
606(defgeneric unary-minus (self)
607 (:documentation "Negate object SELF and return it.")
608 (:method ((self number)) (- self))
609 (:method ((self term))
610 (setf (term-coeff self) (unary-minus (term-coeff self)))
611 self))
612
613(defgeneric universal-zerop (self)
614 (:documentation "Return T iff SELF is zero.")
615 (:method ((self number)) (zerop self))
616 (:method ((self term))
617 (universal-zerop (term-coeff self))))
618
619(defgeneric ->list (self)
620 (:method ((self monom))
621 "A human-readable representation of a monomial SELF as a list of exponents."
622 (coerce (monom-exponents self) 'list))
623 (:method ((self term))
624 "A human-readable representation of a term SELF as a cons of the list of exponents and the coefficient."
625 (cons (coerce (monom-exponents self) 'list) (term-coeff self))))
626
627(defgeneric ->sexp (self &optional vars)
628 (:documentation "Convert a polynomial SELF to an S-expression, using variables VARS.")
629 (:method :before ((self monom) &optional vars)
630 "Check the length of variables VARS against the length of exponents in SELF."
631 (with-slots (exponents)
632 self
633 (assert (= (length vars) (length exponents))
634 nil
635 "Variables ~A and exponents ~A must have the same length." vars exponents)))
636 (:method ((self monom) &optional vars)
637 "Convert a monomial SELF to infix form, using variable VARS to build the representation."
638 (with-slots (exponents)
639 self
640 (let ((m (mapcan #'(lambda (var power)
641 (cond ((= power 0) nil)
642 ((= power 1) (list var))
643 (t (list `(expt ,var ,power)))))
644 vars (coerce exponents 'list))))
645 (cond ((endp m) 1)
646 ((endp (cdr m)) (car m))
647 (t
648 (cons '* m))))))
649 (:method ((self term) &optional vars)
650 "Convert a term SELF to infix form, using variable VARS to build the representation."
651 (declare (ignore vars))
652 (with-slots (exponents coeff)
653 self
654 (let ((m (call-next-method)))
655 (cond ((eql coeff 1) m)
656 ((atom m)
657 (cond ((eql m 1) coeff)
658 (t (list '* coeff m))))
659 ((eql (car m) '*)
660 (list* '* coeff (cdr m)))
661 (t
662 (list '* coeff m)))))))
Note: See TracBrowser for help on using the repository browser.