[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)
|
---|
[4515] | 76 | "Add sugar."
|
---|
[4514] | 77 | (reinitialize-instance new :value (static-sugar new)))
|
---|
| 78 |
|
---|
| 79 |
|
---|
[4511] | 80 | (defmethod multiply-by :after ((self sugar) (other sugar))
|
---|
| 81 | (with-slots (value)
|
---|
| 82 | self
|
---|
| 83 | (with-slots ((other-value value))
|
---|
| 84 | other
|
---|
| 85 | (incf value other-value)))
|
---|
| 86 | self)
|
---|
| 87 |
|
---|
[4512] | 88 | (defmethod divide-by :after ((self sugar) (other sugar))
|
---|
| 89 | (with-slots (value)
|
---|
| 90 | self
|
---|
| 91 | (with-slots ((other-value value))
|
---|
| 92 | other
|
---|
| 93 | (decf value other-value)))
|
---|
| 94 | self)
|
---|
| 95 |
|
---|
[4505] | 96 | (defclass term-with-sugar (term sugar) ())
|
---|
[4502] | 97 |
|
---|
[4505] | 98 | (defmethod print-object ((self term-with-sugar) stream)
|
---|
| 99 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 100 | (with-accessors ((exponents monom-exponents) (value sugar-value) (coeff term-coeff))
|
---|
| 101 | self
|
---|
| 102 | (format stream "EXPONENTS=~A COEFF=~A SUGAR=~A"
|
---|
| 103 | exponents coeff value))))
|
---|
[4510] | 104 |
|
---|
[4522] | 105 | (defmethod shared-initialize :after ((self term-with-sugar) slot-names &rest initargs &key)
|
---|
| 106 | (declare (ignore slot-names initargs))
|
---|
[4530] | 107 | (with-slots (value)
|
---|
| 108 | self
|
---|
| 109 | (setf value (static-sugar self))))
|
---|
[4522] | 110 |
|
---|
| 111 |
|
---|
[4515] | 112 | (defmethod update-instance-for-different-class :after ((old term) (new term-with-sugar) &key)
|
---|
| 113 | "Add sugar."
|
---|
| 114 | (reinitialize-instance new :value (static-sugar new)))
|
---|
| 115 |
|
---|
[4510] | 116 | (defclass poly-with-sugar (poly sugar) ())
|
---|
| 117 |
|
---|
[4517] | 118 | (defmethod print-object ((self poly-with-sugar) stream)
|
---|
| 119 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 120 | (with-accessors ((termlist poly-termlist)
|
---|
| 121 | (order poly-term-order)
|
---|
| 122 | (value sugar-value))
|
---|
| 123 | self
|
---|
| 124 | (format stream "TERMLIST=~A ORDER=~A SUGAR=~A"
|
---|
| 125 | termlist order value))))
|
---|
| 126 |
|
---|
| 127 |
|
---|
[4510] | 128 | (defmethod shared-initialize :after ((self poly-with-sugar) slot-names &rest initargs &key)
|
---|
[4511] | 129 | "Initialize sugar to its static value, which is the maximum of sugar values of the terms."
|
---|
[4510] | 130 | (declare (ignore slot-names initargs))
|
---|
[4530] | 131 | (setf (slot-value self 'value) (static-sugar self)))
|
---|
[4513] | 132 |
|
---|
[4514] | 133 | (defmethod update-instance-for-different-class :after ((old poly) (new poly-with-sugar) &key)
|
---|
| 134 | "Add sugar to every term."
|
---|
| 135 | (reinitialize-instance new :termlist (mapc #'(lambda (trm) (change-class trm 'term-with-sugar)) (poly-termlist new))))
|
---|
| 136 |
|
---|
| 137 | (defmethod update-instance-for-different-class :after ((old poly-with-sugar) (new poly) &key)
|
---|
| 138 | "Drop sugar in every term."
|
---|
| 139 | (reinitialize-instance new :termlist (mapc #'(lambda (trm) (change-class trm 'term)) (poly-termlist new))))
|
---|
| 140 |
|
---|
[4513] | 141 | (defmethod add-to ((self poly-with-sugar) (other poly-with-sugar))
|
---|
| 142 | "Sugar value of the sum of two polynomials, SELF and OTHER, is by definition
|
---|
[4514] | 143 | the maximum of the sugar values of the summands."
|
---|
[4513] | 144 | (with-accessors ((value sugar-value))
|
---|
| 145 | self
|
---|
| 146 | (with-accessors ((other-value sugar-value))
|
---|
| 147 | other
|
---|
[4514] | 148 | (setf value (max value other-value))))
|
---|
| 149 | self)
|
---|
| 150 |
|
---|
[4520] | 151 | (defmethod poly-insert-term :after ((self poly-with-sugar) (other term-with-sugar))
|
---|
| 152 | (with-accessors ((value sugar-value))
|
---|
| 153 | self
|
---|
| 154 | (with-accessors ((other-value sugar-value))
|
---|
| 155 | other
|
---|
| 156 | (setf value (max value other-value))))
|
---|
| 157 | self)
|
---|
| 158 |
|
---|
| 159 |
|
---|
[4514] | 160 | (defmethod multiply-by ((self poly-with-sugar) (other poly-with-sugar))
|
---|
| 161 | "Sugar value of the product of two polynomials, SELF and OTHER, is by definition
|
---|
| 162 | the sum of the sugar values of the factors."
|
---|
| 163 | (with-accessors ((value sugar-value))
|
---|
| 164 | self
|
---|
| 165 | (with-accessors ((other-value sugar-value))
|
---|
| 166 | other
|
---|
| 167 | (setf value (max value other-value))))
|
---|
| 168 | self)
|
---|
[4530] | 169 |
|
---|
| 170 | (defmethod make-unit-for ((object poly-with-sugar))
|
---|
| 171 | (change-class (call-next-method) 'poly-with-sugar))
|
---|