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

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

* empty log message *

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