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/pair-queue.lisp@ 4317

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

Added ring classes

File size: 6.4 KB
Line 
1;;; -*- Mode: Lisp -*-
2;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3;;;
4;;; Copyright (C) 1999, 2002, 2009, 2015 Marek Rychlik <rychlik@u.arizona.edu>
5;;;
6;;; This program is free software; you can redistribute it and/or modify
7;;; it under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 2 of the License, or
9;;; (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19;;;
20;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21
22(defpackage "PAIR-QUEUE"
23 (:use :cl :priority-queue :monom :polynomial :symbolic-polynomial :utils)
24 (:export "CRITICAL-PAIR"
25 "CRITICAL-PAIR-FIRST"
26 "CRITICAL-PAIR-SECOND"
27 "CRITICAL-PAIR-DATA"
28 "CRITICAL-PAIR-QUEUE"
29 "SELECTION-STRATEGY"
30 "*NORMAL-STRATEGY*"
31 "*MIN-TOTAL-DEGREE-STRATEGY*"
32 "*MIN-COMBINED-LENGTH-STRATEGY*"
33 "MAKE-CRITICAL-PAIR-QUEUE"
34 "MAKE-CRITICAL-PAIRS"
35 )
36 (:documentation "Critical pair queue implementation. The pair queue is a list of critical
37pairs, ordered by some partial order. Pair queue is a kind of priority queue.")
38 )
39
40(in-package :pair-queue)
41
42(defclass critical-pair ()
43 ((first :initform nil :initarg :first :accessor critical-pair-first :type poly)
44 (second :initform nil :initarg :second :accessor critical-pair-second :type poly)
45 (data :initform nil :accessor critical-pair-data))
46 (:documentation "Represents a critical pair, i.e. a pair of two
47polynomials. The derived classes may add extra data used in computing
48the order of critical pairs."))
49
50(defmethod print-object ((self critical-pair) stream)
51 (print-unreadable-object (self stream :type t :identity t)
52 (with-accessors ((first critical-pair-first)
53 (second critical-pair-second))
54 self
55 (format stream "FIRST=~A SECOND=~A"
56 first second))))
57
58(defclass selection-strategy ()
59 ((pair-key-fn :initform (error "Initarg :pair-key-fn must be specified.")
60 :initarg :pair-key-fn
61 :accessor pair-key-fn)
62 (pair-order-fn :initform (error "Initarg :pair-order-fn must be specified.")
63 :initarg :pair-order-fn
64 :accessor pair-order-fn))
65 (:documentation "The two ingredients of a strategy is a function
66PAIR-KEY-FUNCTION which computes a key from the critical pair, which
67can be any type of data, and a function PAIR-ORDER-FN used to compaire
68the calculated keys to determine which pair is more promising and
69should be considered first. The normal selection strategy for a given
70monomial order PAIR-ORDER-FN consists in selecting the pair with the
71minimal LCM of leading monomials first."))
72
73(defmethod print-object ((self selection-strategy) stream)
74 (print-unreadable-object (self stream :type t :identity t)
75 (with-accessors ((pair-key-fn pair-key-fn)
76 (pair-order-fn pair-order-fn))
77 self
78 (format stream "PAIR-KEY-FN=~A PAIR-ORDER-FN=~A"
79 pair-key-fn pair-order-fn))))
80
81(defparameter *normal-strategy*
82 (make-instance
83 'selection-strategy
84 :pair-key-fn #'(lambda (p q) (universal-lcm (leading-monomial p) (leading-monomial q)))
85 :pair-order-fn #'lex>)
86 "The normal selection strategy where a pair with the largest LCM of
87leading monomials is selected.")
88
89(defparameter *min-total-degree-strategy*
90 (make-instance
91 'selection-strategy
92 :pair-key-fn #'(lambda (p q) (total-degree (universal-lcm (leading-monomial p) (leading-monomial q))))
93 :pair-order-fn #'<)
94 "A selection strategy where a pair with a minimum total degree of
95 LCM of leading monomials is selected.")
96
97(defparameter *min-combined-length-strategy*
98 (make-instance
99 'selection-strategy
100 :pair-key-fn #'(lambda (p q) (+ (poly-length p) (poly-length q)))
101 :pair-order-fn #'<)
102 "A selection strategy where a pair with the minimum combined length of both
103polynomials is selected.")
104
105(defclass critical-pair-queue (priority-queue)
106 ()
107 (:documentation "Specializes class PRIORITY-QUEUE to ELEMENT-TYPE set to CRITICAL-PAIR."))
108
109(defmethod initialize-instance :before ((self critical-pair-queue) &key)
110 "Initializes the slot ELEMENT-TYPE to symbol CRITICAL-PAIR-QUEUE.
111This overrides the default ELEMENT-TYPE in the superclass."
112 (with-slots ((element-type-x priority-queue::element-type))
113 self
114 (setf element-type-x 'critical-pair))
115 self)
116
117(defun make-critical-pairs (poly-list
118 &optional (start 0)
119 &aux
120 (s (1- (length poly-list))))
121 "Create a list of critical pairs. The argument POLY-LIST is the
122initial list of polynomials and START is the first position beyond the
123elements which form a partial Grobner basis, i.e. satisfy the
124Buchberger criterion."
125 (nconc (makelist (make-instance 'critical-pair :first (elt poly-list i) :second (elt poly-list j))
126 (i 0 (1- start)) (j start s))
127 (makelist (make-instance 'critical-pair :first (elt poly-list i) :second (elt poly-list j))
128 (i start (1- s)) (j (1+ i) s))))
129
130(defgeneric enqueue-critical-pairs (self pair-lst)
131 (:documentation "Place pairs in PAIR-LST on the queue SELF.")
132 (:method ((self critical-pair-queue) pair-lst)
133 "Enqueue into queue QUEUE the elements of the list PAIR-LST."
134 (dolist (pair pair-lst self)
135 (enqueue self pair))))
136
137(defgeneric make-critical-pair-queue (object &optional poly-lst start)
138 (:documentation "Creates a CRITICAL-PAIR-QUEUE from an object OBJECT.")
139 (:method ((object selection-strategy) &optional (poly-lst nil) (start 0))
140 "Creates a CRITICAL-PAIR-QUEUE from a selection strategy OBJECT."
141 (with-slots (pair-key-fn pair-order-fn)
142 object
143 (let ((queue (make-instance 'critical-pair-queue
144 :element-key #'(lambda (pair)
145 (funcall pair-key-fn
146 (critical-pair-first pair)
147 (critical-pair-second pair)))
148 :test pair-order-fn)))
149 (enqueue-critical-pairs queue (make-critical-pairs poly-lst start))))))
150
Note: See TracBrowser for help on using the repository browser.