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

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

* empty log message *

File size: 6.1 KB
Line 
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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
22
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"
34 )
35 (:documentation "Implements 'sugar'."))
36
37(in-package "POLYNOMIAL-SUGAR")
38
39;;(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
40
41(defgeneric static-sugar (object)
42 (:documentation "Return statically calculated sugar of object
43OBJECT. That is, the sugar value which does not assume that the object
44is a result of any prior calculations.")
45 (:method ((object monom))
46 "Static sugar of a monom OBJECT is simply the total degree."
47 (total-degree object))
48 (:method ((object poly))
49 "Static sugar of a poly OBJECT is the maximum sugar of its terms."
50 (with-slots (termlist)
51 object
52 (loop for trm in termlist maximize (static-sugar trm)))))
53
54(defclass sugar ()
55 ((value :initarg :value :initform -1 :accessor sugar-value :type fixnum))
56 (:documentation "Sugar is a quantity added to various objects, such as monomials, terms and polynomials."))
57
58(defclass monom-with-sugar (monom sugar) ())
59
60(defmethod print-object ((self monom-with-sugar) stream)
61 (print-unreadable-object (self stream :type t :identity t)
62 (with-accessors ((exponents monom-exponents) (value sugar-value))
63 self
64 (format stream "EXPONENTS=~A SUGAR=~A"
65 exponents value))))
66
67(defmethod shared-initialize :after ((self monom-with-sugar) slot-names &rest initargs &key)
68 "Initialize sugar value based on the exponents."
69 (declare (ignore slot-names initargs))
70 (setf (slot-value self 'value) (static-sugar self)))
71
72(defmethod update-instance-for-different-class :after ((old monom) (new monom-with-sugar) &key)
73 "Add sugar."
74 (reinitialize-instance new :value (static-sugar new)))
75
76
77(defmethod multiply-by :after ((self sugar) (other sugar))
78 (with-slots (value)
79 self
80 (with-slots ((other-value value))
81 other
82 (incf value other-value)))
83 self)
84
85(defmethod divide-by :after ((self sugar) (other sugar))
86 (with-slots (value)
87 self
88 (with-slots ((other-value value))
89 other
90 (decf value other-value)))
91 self)
92
93(defclass term-with-sugar (term sugar) ())
94
95(defmethod print-object ((self term-with-sugar) stream)
96 (print-unreadable-object (self stream :type t :identity t)
97 (with-accessors ((exponents monom-exponents) (value sugar-value) (coeff term-coeff))
98 self
99 (format stream "EXPONENTS=~A COEFF=~A SUGAR=~A"
100 exponents coeff value))))
101
102(defmethod shared-initialize :after ((self term-with-sugar) slot-names &rest initargs &key)
103 (declare (ignore slot-names initargs))
104 (setf (slot-value self 'value) (static-sugar self)))
105
106
107(defmethod update-instance-for-different-class :after ((old term) (new term-with-sugar) &key)
108 "Add sugar."
109 (reinitialize-instance new :value (static-sugar new)))
110
111(defclass poly-with-sugar (poly sugar) ())
112
113(defmethod print-object ((self poly-with-sugar) stream)
114 (print-unreadable-object (self stream :type t :identity t)
115 (with-accessors ((termlist poly-termlist)
116 (order poly-term-order)
117 (value sugar-value))
118 self
119 (format stream "TERMLIST=~A ORDER=~A SUGAR=~A"
120 termlist order value))))
121
122
123(defmethod shared-initialize :after ((self poly-with-sugar) slot-names &rest initargs &key)
124 "Initialize sugar to its static value, which is the maximum of sugar values of the terms."
125 (declare (ignore slot-names initargs))
126 (setf (slot-value self 'value) (static-sugar self)))
127
128(defmethod update-instance-for-different-class :after ((old poly) (new poly-with-sugar) &key)
129 "Add sugar to every term."
130 (reinitialize-instance new :termlist (mapc #'(lambda (trm) (change-class trm 'term-with-sugar)) (poly-termlist new))))
131
132(defmethod update-instance-for-different-class :after ((old poly-with-sugar) (new poly) &key)
133 "Drop sugar in every term."
134 (reinitialize-instance new :termlist (mapc #'(lambda (trm) (change-class trm 'term)) (poly-termlist new))))
135
136(defmethod add-to ((self poly-with-sugar) (other poly-with-sugar))
137 "Sugar value of the sum of two polynomials, SELF and OTHER, is by definition
138the maximum of the sugar values of the summands."
139 (with-accessors ((value sugar-value))
140 self
141 (with-accessors ((other-value sugar-value))
142 other
143 (setf value (max value other-value))))
144 self)
145
146(defmethod poly-insert-term :after ((self poly-with-sugar) (other term-with-sugar))
147 (with-accessors ((value sugar-value))
148 self
149 (with-accessors ((other-value sugar-value))
150 other
151 (setf value (max value other-value))))
152 self)
153
154
155(defmethod multiply-by ((self poly-with-sugar) (other poly-with-sugar))
156 "Sugar value of the product of two polynomials, SELF and OTHER, is by definition
157the sum of the sugar values of the factors."
158 (with-accessors ((value sugar-value))
159 self
160 (with-accessors ((other-value sugar-value))
161 other
162 (setf value (max value other-value))))
163 self)
Note: See TracBrowser for help on using the repository browser.