[4502] | 1 | (in-package "POLYNOMIAL")
|
---|
| 2 |
|
---|
| 3 | (defgeneric static-sugar (object)
|
---|
| 4 | (:documentation "Return statically calculated sugar of object OBJECT. That is,
|
---|
| 5 | the sugar value which does not assume that the object is a result of any prior calculations.")
|
---|
| 6 | (:method ((object monom))
|
---|
[4505] | 7 | "Static sugar of a monom OBJECT is simply the total degree."
|
---|
[4502] | 8 | (total-degree object))
|
---|
| 9 | (:method ((object poly))
|
---|
[4505] | 10 | "Static sugar of a poly OBJECT is the maximum sugar of its terms."
|
---|
| 11 | (with-slots (termlist)
|
---|
[4502] | 12 | object
|
---|
[4511] | 13 | (loop for trm in termlist maximize (static-sugar trm)))))
|
---|
[4502] | 14 |
|
---|
| 15 | (defclass sugar ()
|
---|
[4507] | 16 | ((value :initarg :value :initform -1 :accessor sugar-value :type fixnum))
|
---|
[4509] | 17 | (:documentation "Sugar is a quantity added to various objects, such as monomials, terms and polynomials."))
|
---|
[4502] | 18 |
|
---|
| 19 | (defclass monom-with-sugar (monom sugar) ())
|
---|
| 20 |
|
---|
[4505] | 21 | (defmethod print-object ((self monom-with-sugar) stream)
|
---|
| 22 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 23 | (with-accessors ((exponents monom-exponents) (value sugar-value))
|
---|
| 24 | self
|
---|
| 25 | (format stream "EXPONENTS=~A SUGAR=~A"
|
---|
| 26 | exponents value))))
|
---|
| 27 |
|
---|
| 28 |
|
---|
[4502] | 29 | (defmethod shared-initialize :after ((self monom-with-sugar) slot-names &rest initargs &key)
|
---|
[4504] | 30 | "Initialize sugar value based on the exponents."
|
---|
[4502] | 31 | (declare (ignore slot-names initargs))
|
---|
[4504] | 32 | (setf (slot-value self 'value) (static-sugar self)))
|
---|
[4502] | 33 |
|
---|
[4511] | 34 | (defmethod multiply-by :after ((self sugar) (other sugar))
|
---|
| 35 | (with-slots (value)
|
---|
| 36 | self
|
---|
| 37 | (with-slots ((other-value value))
|
---|
| 38 | other
|
---|
| 39 | (incf value other-value)))
|
---|
| 40 | self)
|
---|
| 41 |
|
---|
[4512] | 42 | (defmethod divide-by :after ((self sugar) (other sugar))
|
---|
| 43 | (with-slots (value)
|
---|
| 44 | self
|
---|
| 45 | (with-slots ((other-value value))
|
---|
| 46 | other
|
---|
| 47 | (decf value other-value)))
|
---|
| 48 | self)
|
---|
| 49 |
|
---|
[4505] | 50 | (defclass term-with-sugar (term sugar) ())
|
---|
[4502] | 51 |
|
---|
[4505] | 52 | (defmethod print-object ((self term-with-sugar) stream)
|
---|
| 53 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 54 | (with-accessors ((exponents monom-exponents) (value sugar-value) (coeff term-coeff))
|
---|
| 55 | self
|
---|
| 56 | (format stream "EXPONENTS=~A COEFF=~A SUGAR=~A"
|
---|
| 57 | exponents coeff value))))
|
---|
[4510] | 58 |
|
---|
| 59 | (defclass poly-with-sugar (poly sugar) ())
|
---|
| 60 |
|
---|
| 61 | (defmethod shared-initialize :after ((self poly-with-sugar) slot-names &rest initargs &key)
|
---|
[4511] | 62 | "Initialize sugar to its static value, which is the maximum of sugar values of the terms."
|
---|
[4510] | 63 | (declare (ignore slot-names initargs))
|
---|
| 64 | (setf (slot-value self 'value) (static-sugar self)))
|
---|
[4513] | 65 |
|
---|
| 66 | (defmethod add-to ((self poly-with-sugar) (other poly-with-sugar))
|
---|
| 67 | "Sugar value of the sum of two polynomials, SELF and OTHER, is by definition
|
---|
| 68 | the maximum of the sugar values of the two polynomials."
|
---|
| 69 | (with-accessors ((value sugar-value))
|
---|
| 70 | self
|
---|
| 71 | (with-accessors ((other-value sugar-value))
|
---|
| 72 | other
|
---|
| 73 | (setf value (max value other-value)))))
|
---|