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

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

* empty log message *

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