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