[4523] | 1 | ;;----------------------------------------------------------------
|
---|
| 2 | ;;; -*- Mode: Lisp -*-
|
---|
| 3 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 4 | ;;;
|
---|
| 5 | ;;; Copyright (C) 1999, 2002, 2009, 2015 Marek Rychlik <rychlik@u.arizona.edu>
|
---|
| 6 | ;;;
|
---|
| 7 | ;;; This program is free software; you can redistribute it and/or modify
|
---|
| 8 | ;;; it under the terms of the GNU General Public License as published by
|
---|
| 9 | ;;; the Free Software Foundation; either version 2 of the License, or
|
---|
| 10 | ;;; (at your option) any later version.
|
---|
| 11 | ;;;
|
---|
| 12 | ;;; This program is distributed in the hope that it will be useful,
|
---|
| 13 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | ;;; GNU General Public License for more details.
|
---|
| 16 | ;;;
|
---|
| 17 | ;;; You should have received a copy of the GNU General Public License
|
---|
| 18 | ;;; along with this program; if not, write to the Free Software
|
---|
| 19 | ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
---|
| 20 | ;;;
|
---|
| 21 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
[4502] | 22 |
|
---|
[4523] | 23 | (defpackage "POLYNOMIAL-SUGAR"
|
---|
| 24 | (:use :cl :utils :monom :copy :ring :polynomial)
|
---|
| 25 | (:export "MONOM-WITH-SUGAR"
|
---|
| 26 | "TERM-WITH-SUGAR"
|
---|
| 27 | "POLY-WITH-SUGAR"
|
---|
| 28 | "STATIC-SUGAR"
|
---|
| 29 | "SUGAR"
|
---|
| 30 | "SUGAR-VALUE"
|
---|
| 31 | "POLY-INSERT-TERM"
|
---|
| 32 | "ADD-TO"
|
---|
| 33 | "MULTIPLY-BY"
|
---|
[4530] | 34 | "MAKE-UNIT-FOR"
|
---|
[4523] | 35 | )
|
---|
| 36 | (:documentation "Implements 'sugar'."))
|
---|
| 37 |
|
---|
| 38 | (in-package "POLYNOMIAL-SUGAR")
|
---|
| 39 |
|
---|
| 40 | ;;(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
|
---|
| 41 |
|
---|
[4502] | 42 | (defgeneric static-sugar (object)
|
---|
[4519] | 43 | (:documentation "Return statically calculated sugar of object
|
---|
| 44 | OBJECT. That is, the sugar value which does not assume that the object
|
---|
| 45 | is a result of any prior calculations.")
|
---|
[4502] | 46 | (:method ((object monom))
|
---|
[4505] | 47 | "Static sugar of a monom OBJECT is simply the total degree."
|
---|
[4502] | 48 | (total-degree object))
|
---|
| 49 | (:method ((object poly))
|
---|
[4505] | 50 | "Static sugar of a poly OBJECT is the maximum sugar of its terms."
|
---|
[4530] | 51 | (with-accessors ((termlist poly-termlist))
|
---|
[4502] | 52 | object
|
---|
[4511] | 53 | (loop for trm in termlist maximize (static-sugar trm)))))
|
---|
[4502] | 54 |
|
---|
| 55 | (defclass sugar ()
|
---|
[4507] | 56 | ((value :initarg :value :initform -1 :accessor sugar-value :type fixnum))
|
---|
[4509] | 57 | (:documentation "Sugar is a quantity added to various objects, such as monomials, terms and polynomials."))
|
---|
[4502] | 58 |
|
---|
| 59 | (defclass monom-with-sugar (monom sugar) ())
|
---|
| 60 |
|
---|
[4505] | 61 | (defmethod print-object ((self monom-with-sugar) stream)
|
---|
| 62 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 63 | (with-accessors ((exponents monom-exponents) (value sugar-value))
|
---|
| 64 | self
|
---|
| 65 | (format stream "EXPONENTS=~A SUGAR=~A"
|
---|
| 66 | exponents value))))
|
---|
| 67 |
|
---|
[4502] | 68 | (defmethod shared-initialize :after ((self monom-with-sugar) slot-names &rest initargs &key)
|
---|
[4504] | 69 | "Initialize sugar value based on the exponents."
|
---|
[4502] | 70 | (declare (ignore slot-names initargs))
|
---|
[4530] | 71 | (with-slots (value)
|
---|
| 72 | self
|
---|
| 73 | (setf value (static-sugar self))))
|
---|
[4502] | 74 |
|
---|
[4514] | 75 | (defmethod update-instance-for-different-class :after ((old monom) (new monom-with-sugar) &key)
|
---|
[4535] | 76 | "Add sugar to a monom OLD."
|
---|
[4514] | 77 | (reinitialize-instance new :value (static-sugar new)))
|
---|
| 78 |
|
---|
| 79 |
|
---|
[4511] | 80 | (defmethod multiply-by :after ((self sugar) (other sugar))
|
---|
[4535] | 81 | "By definition, sugar is additive under multiplication."
|
---|
[4511] | 82 | (with-slots (value)
|
---|
| 83 | self
|
---|
| 84 | (with-slots ((other-value value))
|
---|
| 85 | other
|
---|
| 86 | (incf value other-value)))
|
---|
| 87 | self)
|
---|
| 88 |
|
---|
[4505] | 89 | (defclass term-with-sugar (term sugar) ())
|
---|
[4502] | 90 |
|
---|
[4505] | 91 | (defmethod print-object ((self term-with-sugar) stream)
|
---|
| 92 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 93 | (with-accessors ((exponents monom-exponents) (value sugar-value) (coeff term-coeff))
|
---|
| 94 | self
|
---|
| 95 | (format stream "EXPONENTS=~A COEFF=~A SUGAR=~A"
|
---|
| 96 | exponents coeff value))))
|
---|
[4510] | 97 |
|
---|
[4522] | 98 | (defmethod shared-initialize :after ((self term-with-sugar) slot-names &rest initargs &key)
|
---|
| 99 | (declare (ignore slot-names initargs))
|
---|
[4530] | 100 | (with-slots (value)
|
---|
| 101 | self
|
---|
| 102 | (setf value (static-sugar self))))
|
---|
[4522] | 103 |
|
---|
| 104 |
|
---|
[4515] | 105 | (defmethod update-instance-for-different-class :after ((old term) (new term-with-sugar) &key)
|
---|
| 106 | "Add sugar."
|
---|
| 107 | (reinitialize-instance new :value (static-sugar new)))
|
---|
| 108 |
|
---|
[4510] | 109 | (defclass poly-with-sugar (poly sugar) ())
|
---|
| 110 |
|
---|
[4517] | 111 | (defmethod print-object ((self poly-with-sugar) stream)
|
---|
| 112 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 113 | (with-accessors ((termlist poly-termlist)
|
---|
| 114 | (order poly-term-order)
|
---|
| 115 | (value sugar-value))
|
---|
| 116 | self
|
---|
| 117 | (format stream "TERMLIST=~A ORDER=~A SUGAR=~A"
|
---|
| 118 | termlist order value))))
|
---|
| 119 |
|
---|
| 120 |
|
---|
[4510] | 121 | (defmethod shared-initialize :after ((self poly-with-sugar) slot-names &rest initargs &key)
|
---|
[4511] | 122 | "Initialize sugar to its static value, which is the maximum of sugar values of the terms."
|
---|
[4510] | 123 | (declare (ignore slot-names initargs))
|
---|
[4530] | 124 | (setf (slot-value self 'value) (static-sugar self)))
|
---|
[4513] | 125 |
|
---|
[4514] | 126 | (defmethod update-instance-for-different-class :after ((old poly) (new poly-with-sugar) &key)
|
---|
| 127 | "Add sugar to every term."
|
---|
| 128 | (reinitialize-instance new :termlist (mapc #'(lambda (trm) (change-class trm 'term-with-sugar)) (poly-termlist new))))
|
---|
| 129 |
|
---|
| 130 | (defmethod update-instance-for-different-class :after ((old poly-with-sugar) (new poly) &key)
|
---|
| 131 | "Drop sugar in every term."
|
---|
| 132 | (reinitialize-instance new :termlist (mapc #'(lambda (trm) (change-class trm 'term)) (poly-termlist new))))
|
---|
| 133 |
|
---|
[4513] | 134 | (defmethod add-to ((self poly-with-sugar) (other poly-with-sugar))
|
---|
| 135 | "Sugar value of the sum of two polynomials, SELF and OTHER, is by definition
|
---|
[4514] | 136 | the maximum of the sugar values of the summands."
|
---|
[4513] | 137 | (with-accessors ((value sugar-value))
|
---|
| 138 | self
|
---|
| 139 | (with-accessors ((other-value sugar-value))
|
---|
| 140 | other
|
---|
[4514] | 141 | (setf value (max value other-value))))
|
---|
| 142 | self)
|
---|
| 143 |
|
---|
[4520] | 144 | (defmethod poly-insert-term :after ((self poly-with-sugar) (other term-with-sugar))
|
---|
| 145 | (with-accessors ((value sugar-value))
|
---|
| 146 | self
|
---|
| 147 | (with-accessors ((other-value sugar-value))
|
---|
| 148 | other
|
---|
| 149 | (setf value (max value other-value))))
|
---|
| 150 | self)
|
---|
| 151 |
|
---|
| 152 |
|
---|
[4514] | 153 | (defmethod multiply-by ((self poly-with-sugar) (other poly-with-sugar))
|
---|
| 154 | "Sugar value of the product of two polynomials, SELF and OTHER, is by definition
|
---|
| 155 | the sum of the sugar values of the factors."
|
---|
| 156 | (with-accessors ((value sugar-value))
|
---|
| 157 | self
|
---|
| 158 | (with-accessors ((other-value sugar-value))
|
---|
| 159 | other
|
---|
| 160 | (setf value (max value other-value))))
|
---|
| 161 | self)
|
---|
[4530] | 162 |
|
---|
| 163 | (defmethod make-unit-for ((object poly-with-sugar))
|
---|
| 164 | (change-class (call-next-method) 'poly-with-sugar))
|
---|