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/polynomial-sugar.lisp@ 4519

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

* empty log message *

File size: 4.1 KB
Line 
1(in-package "POLYNOMIAL")
2
3(defgeneric static-sugar (object)
4 (:documentation "Return statically calculated sugar of object
5OBJECT. That is, the sugar value which does not assume that the object
6is a result of any prior calculations.")
7 (:method ((object monom))
8 "Static sugar of a monom OBJECT is simply the total degree."
9 (total-degree object))
10 (:method ((object poly))
11 "Static sugar of a poly OBJECT is the maximum sugar of its terms."
12 (with-slots (termlist)
13 object
14 (loop for trm in termlist maximize (static-sugar trm)))))
15
16(defclass sugar ()
17 ((value :initarg :value :initform -1 :accessor sugar-value :type fixnum))
18 (:documentation "Sugar is a quantity added to various objects, such as monomials, terms and polynomials."))
19
20(defclass monom-with-sugar (monom sugar) ())
21
22(defmethod print-object ((self monom-with-sugar) stream)
23 (print-unreadable-object (self stream :type t :identity t)
24 (with-accessors ((exponents monom-exponents) (value sugar-value))
25 self
26 (format stream "EXPONENTS=~A SUGAR=~A"
27 exponents value))))
28
29
30(defmethod shared-initialize :after ((self monom-with-sugar) slot-names &rest initargs &key)
31 "Initialize sugar value based on the exponents."
32 (declare (ignore slot-names initargs))
33 (setf (slot-value self 'value) (static-sugar self)))
34
35(defmethod update-instance-for-different-class :after ((old monom) (new monom-with-sugar) &key)
36 "Add sugar."
37 (reinitialize-instance new :value (static-sugar new)))
38
39
40(defmethod multiply-by :after ((self sugar) (other sugar))
41 (with-slots (value)
42 self
43 (with-slots ((other-value value))
44 other
45 (incf value other-value)))
46 self)
47
48(defmethod divide-by :after ((self sugar) (other sugar))
49 (with-slots (value)
50 self
51 (with-slots ((other-value value))
52 other
53 (decf value other-value)))
54 self)
55
56(defclass term-with-sugar (term sugar) ())
57
58(defmethod print-object ((self term-with-sugar) stream)
59 (print-unreadable-object (self stream :type t :identity t)
60 (with-accessors ((exponents monom-exponents) (value sugar-value) (coeff term-coeff))
61 self
62 (format stream "EXPONENTS=~A COEFF=~A SUGAR=~A"
63 exponents coeff value))))
64
65(defmethod update-instance-for-different-class :after ((old term) (new term-with-sugar) &key)
66 "Add sugar."
67 (reinitialize-instance new :value (static-sugar new)))
68
69(defclass poly-with-sugar (poly sugar) ())
70
71(defmethod print-object ((self poly-with-sugar) stream)
72 (print-unreadable-object (self stream :type t :identity t)
73 (with-accessors ((termlist poly-termlist)
74 (order poly-term-order)
75 (value sugar-value))
76 self
77 (format stream "TERMLIST=~A ORDER=~A SUGAR=~A"
78 termlist order value))))
79
80
81(defmethod shared-initialize :after ((self poly-with-sugar) slot-names &rest initargs &key)
82 "Initialize sugar to its static value, which is the maximum of sugar values of the terms."
83 (declare (ignore slot-names initargs))
84 (setf (slot-value self 'value) (static-sugar self)))
85
86(defmethod update-instance-for-different-class :after ((old poly) (new poly-with-sugar) &key)
87 "Add sugar to every term."
88 (reinitialize-instance new :termlist (mapc #'(lambda (trm) (change-class trm 'term-with-sugar)) (poly-termlist new))))
89
90(defmethod update-instance-for-different-class :after ((old poly-with-sugar) (new poly) &key)
91 "Drop sugar in every term."
92 (reinitialize-instance new :termlist (mapc #'(lambda (trm) (change-class trm 'term)) (poly-termlist new))))
93
94(defmethod add-to ((self poly-with-sugar) (other poly-with-sugar))
95 "Sugar value of the sum of two polynomials, SELF and OTHER, is by definition
96the maximum of the sugar values of the summands."
97 (with-accessors ((value sugar-value))
98 self
99 (with-accessors ((other-value sugar-value))
100 other
101 (setf value (max value other-value))))
102 self)
103
104(defmethod multiply-by ((self poly-with-sugar) (other poly-with-sugar))
105 "Sugar value of the product of two polynomials, SELF and OTHER, is by definition
106the sum of the sugar values of the factors."
107 (with-accessors ((value sugar-value))
108 self
109 (with-accessors ((other-value sugar-value))
110 other
111 (setf value (max value other-value))))
112 self)
Note: See TracBrowser for help on using the repository browser.