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

Last change on this file since 4554 was 4466, checked in by Marek Rychlik, 9 years ago

* empty log message *

File size: 6.4 KB
RevLine 
[1201]1;;; -*- Mode: Lisp -*-
[76]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
[449]22(defpackage "PAIR-QUEUE"
[3949]23 (:use :cl :priority-queue :monom :polynomial :symbolic-polynomial :utils)
[3920]24 (:export "CRITICAL-PAIR"
25 "CRITICAL-PAIR-FIRST"
26 "CRITICAL-PAIR-SECOND"
[4312]27 "CRITICAL-PAIR-DATA"
[3948]28 "CRITICAL-PAIR-QUEUE"
[3964]29 "SELECTION-STRATEGY"
[4189]30 "*NORMAL-STRATEGY*"
31 "*MIN-TOTAL-DEGREE-STRATEGY*"
32 "*MIN-COMBINED-LENGTH-STRATEGY*"
[4145]33 "MAKE-CRITICAL-PAIR-QUEUE"
[4143]34 "MAKE-CRITICAL-PAIRS"
[3918]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 )
[449]39
[450]40(in-package :pair-queue)
41
[3919]42(defclass critical-pair ()
[3921]43 ((first :initform nil :initarg :first :accessor critical-pair-first :type poly)
[4190]44 (second :initform nil :initarg :second :accessor critical-pair-second :type poly)
[4312]45 (data :initform nil :accessor critical-pair-data))
[4190]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."))
[3919]49
[3921]50(defmethod print-object ((self critical-pair) stream)
[3920]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
[3927]58(defclass selection-strategy ()
[4138]59 ((pair-key-fn :initform (error "Initarg :pair-key-fn must be specified.")
[3959]60 :initarg :pair-key-fn
61 :accessor pair-key-fn)
[4138]62 (pair-order-fn :initform (error "Initarg :pair-order-fn must be specified.")
[3959]63 :initarg :pair-order-fn
64 :accessor pair-order-fn))
[4135]65 (:documentation "The two ingredients of a strategy is a function
[3940]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
[3941]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."))
[60]72
[3932]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
[4466]81(defparameter *max-lcm-strategy*
[4135]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>)
[4466]86 "The selection strategy where a pair with the largest LCM of
[4135]87leading monomials is selected.")
[3932]88
[4466]89(defparameter *normal-strategy*
[4186]90 (make-instance
[4135]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
[4466]95 LCM of leading monomials is selected. This is known as the normal strategy.")
[3933]96
[4187]97(defparameter *min-combined-length-strategy*
[4186]98 (make-instance
[4135]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.")
[3938]104
[4148]105(defclass critical-pair-queue (priority-queue)
106 ()
[4160]107 (:documentation "Specializes class PRIORITY-QUEUE to ELEMENT-TYPE set to CRITICAL-PAIR."))
[4148]108
[4159]109(defmethod initialize-instance :before ((self critical-pair-queue) &key)
[4152]110 "Initializes the slot ELEMENT-TYPE to symbol CRITICAL-PAIR-QUEUE.
111This overrides the default ELEMENT-TYPE in the superclass."
[4151]112 (with-slots ((element-type-x priority-queue::element-type))
113 self
114 (setf element-type-x 'critical-pair))
115 self)
[4148]116
[4146]117(defun make-critical-pairs (poly-list
118 &optional (start 0)
[4142]119 &aux
120 (s (1- (length poly-list))))
[4183]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."
[4142]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))
[4146]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))))
[4142]129
[4170]130(defgeneric enqueue-critical-pairs (self pair-lst)
131 (:documentation "Place pairs in PAIR-LST on the queue SELF.")
[4143]132 (:method ((self critical-pair-queue) pair-lst)
[4170]133 "Enqueue into queue QUEUE the elements of the list PAIR-LST."
[4143]134 (dolist (pair pair-lst self)
135 (enqueue self pair))))
[4135]136
[4162]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))
[4182]140 "Creates a CRITICAL-PAIR-QUEUE from a selection strategy OBJECT."
[4162]141 (with-slots (pair-key-fn pair-order-fn)
142 object
[4170]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))))))
[4162]150
Note: See TracBrowser for help on using the repository browser.