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

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

* empty log message *

File size: 4.5 KB
RevLine 
[4502]1(in-package "POLYNOMIAL")
2
3(defgeneric static-sugar (object)
[4519]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.")
[4502]7 (:method ((object monom))
[4505]8 "Static sugar of a monom OBJECT is simply the total degree."
[4502]9 (total-degree object))
10 (:method ((object poly))
[4505]11 "Static sugar of a poly OBJECT is the maximum sugar of its terms."
12 (with-slots (termlist)
[4502]13 object
[4511]14 (loop for trm in termlist maximize (static-sugar trm)))))
[4502]15
16(defclass sugar ()
[4507]17 ((value :initarg :value :initform -1 :accessor sugar-value :type fixnum))
[4509]18 (:documentation "Sugar is a quantity added to various objects, such as monomials, terms and polynomials."))
[4502]19
20(defclass monom-with-sugar (monom sugar) ())
21
[4505]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
[4502]30(defmethod shared-initialize :after ((self monom-with-sugar) slot-names &rest initargs &key)
[4504]31 "Initialize sugar value based on the exponents."
[4502]32 (declare (ignore slot-names initargs))
[4504]33 (setf (slot-value self 'value) (static-sugar self)))
[4502]34
[4514]35(defmethod update-instance-for-different-class :after ((old monom) (new monom-with-sugar) &key)
[4515]36 "Add sugar."
[4514]37 (reinitialize-instance new :value (static-sugar new)))
38
39
[4511]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
[4512]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
[4505]56(defclass term-with-sugar (term sugar) ())
[4502]57
[4505]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))))
[4510]64
[4522]65(defmethod shared-initialize :after ((self term-with-sugar) slot-names &rest initargs &key)
66 (declare (ignore slot-names initargs))
67 (setf (slot-value self 'value) (static-sugar self)))
68
69
[4515]70(defmethod update-instance-for-different-class :after ((old term) (new term-with-sugar) &key)
71 "Add sugar."
72 (reinitialize-instance new :value (static-sugar new)))
73
[4510]74(defclass poly-with-sugar (poly sugar) ())
75
[4517]76(defmethod print-object ((self poly-with-sugar) stream)
77 (print-unreadable-object (self stream :type t :identity t)
78 (with-accessors ((termlist poly-termlist)
79 (order poly-term-order)
80 (value sugar-value))
81 self
82 (format stream "TERMLIST=~A ORDER=~A SUGAR=~A"
83 termlist order value))))
84
85
[4510]86(defmethod shared-initialize :after ((self poly-with-sugar) slot-names &rest initargs &key)
[4511]87 "Initialize sugar to its static value, which is the maximum of sugar values of the terms."
[4510]88 (declare (ignore slot-names initargs))
89 (setf (slot-value self 'value) (static-sugar self)))
[4513]90
[4514]91(defmethod update-instance-for-different-class :after ((old poly) (new poly-with-sugar) &key)
92 "Add sugar to every term."
93 (reinitialize-instance new :termlist (mapc #'(lambda (trm) (change-class trm 'term-with-sugar)) (poly-termlist new))))
94
95(defmethod update-instance-for-different-class :after ((old poly-with-sugar) (new poly) &key)
96 "Drop sugar in every term."
97 (reinitialize-instance new :termlist (mapc #'(lambda (trm) (change-class trm 'term)) (poly-termlist new))))
98
[4513]99(defmethod add-to ((self poly-with-sugar) (other poly-with-sugar))
100 "Sugar value of the sum of two polynomials, SELF and OTHER, is by definition
[4514]101the maximum of the sugar values of the summands."
[4513]102 (with-accessors ((value sugar-value))
103 self
104 (with-accessors ((other-value sugar-value))
105 other
[4514]106 (setf value (max value other-value))))
107 self)
108
[4520]109(defmethod poly-insert-term :after ((self poly-with-sugar) (other term-with-sugar))
110 (with-accessors ((value sugar-value))
111 self
112 (with-accessors ((other-value sugar-value))
113 other
114 (setf value (max value other-value))))
115 self)
116
117
[4514]118(defmethod multiply-by ((self poly-with-sugar) (other poly-with-sugar))
119 "Sugar value of the product of two polynomials, SELF and OTHER, is by definition
120the sum of the sugar values of the factors."
121 (with-accessors ((value sugar-value))
122 self
123 (with-accessors ((other-value sugar-value))
124 other
125 (setf value (max value other-value))))
126 self)
Note: See TracBrowser for help on using the repository browser.