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

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

* empty log message *

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