[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"
|
---|
[3948] | 27 | "CRITICAL-PAIR-QUEUE"
|
---|
[3964] | 28 | "SELECTION-STRATEGY"
|
---|
[4135] | 29 | "+NORMAL-STRATEGY+"
|
---|
| 30 | "+MIN-TOTAL-DEGREE-STRATEGY+"
|
---|
| 31 | "+MIN-COMBINED-LENGTH-STRATEGY+"
|
---|
[4145] | 32 | "MAKE-CRITICAL-PAIR-QUEUE"
|
---|
[4143] | 33 | "MAKE-CRITICAL-PAIRS"
|
---|
[3918] | 34 | )
|
---|
| 35 | (:documentation "Critical pair queue implementation. The pair queue is a list of critical
|
---|
| 36 | pairs, ordered by some partial order. Pair queue is a kind of priority queue.")
|
---|
| 37 | )
|
---|
[449] | 38 |
|
---|
[450] | 39 | (in-package :pair-queue)
|
---|
| 40 |
|
---|
[3919] | 41 | (defclass critical-pair ()
|
---|
[3921] | 42 | ((first :initform nil :initarg :first :accessor critical-pair-first :type poly)
|
---|
| 43 | (second :initform nil :initarg :second :accessor critical-pair-second :type poly))
|
---|
[3930] | 44 | (:documentation "Represents a critical pair, i.e. a pair of two polynomials. The derived
|
---|
| 45 | classes may add extra data used in computing the order of critical pairs."))
|
---|
[3919] | 46 |
|
---|
[3921] | 47 | (defmethod print-object ((self critical-pair) stream)
|
---|
[3920] | 48 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 49 | (with-accessors ((first critical-pair-first)
|
---|
| 50 | (second critical-pair-second))
|
---|
| 51 | self
|
---|
| 52 | (format stream "FIRST=~A SECOND=~A"
|
---|
| 53 | first second))))
|
---|
| 54 |
|
---|
[3927] | 55 | (defclass selection-strategy ()
|
---|
[4138] | 56 | ((pair-key-fn :initform (error "Initarg :pair-key-fn must be specified.")
|
---|
[3959] | 57 | :initarg :pair-key-fn
|
---|
| 58 | :accessor pair-key-fn)
|
---|
[4138] | 59 | (pair-order-fn :initform (error "Initarg :pair-order-fn must be specified.")
|
---|
[3959] | 60 | :initarg :pair-order-fn
|
---|
| 61 | :accessor pair-order-fn))
|
---|
[4135] | 62 | (:documentation "The two ingredients of a strategy is a function
|
---|
[3940] | 63 | PAIR-KEY-FUNCTION which computes a key from the critical pair, which
|
---|
| 64 | can be any type of data, and a function PAIR-ORDER-FN used to compaire
|
---|
| 65 | the calculated keys to determine which pair is more promising and
|
---|
[3941] | 66 | should be considered first. The normal selection strategy for a given
|
---|
| 67 | monomial order PAIR-ORDER-FN consists in selecting the pair with the
|
---|
| 68 | minimal LCM of leading monomials first."))
|
---|
[60] | 69 |
|
---|
[3932] | 70 | (defmethod print-object ((self selection-strategy) stream)
|
---|
| 71 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 72 | (with-accessors ((pair-key-fn pair-key-fn)
|
---|
| 73 | (pair-order-fn pair-order-fn))
|
---|
| 74 | self
|
---|
| 75 | (format stream "PAIR-KEY-FN=~A PAIR-ORDER-FN=~A"
|
---|
| 76 | pair-key-fn pair-order-fn))))
|
---|
| 77 |
|
---|
[4185] | 78 | (defparameter +normal-strategy+
|
---|
[4135] | 79 | (make-instance
|
---|
| 80 | 'selection-strategy
|
---|
| 81 | :pair-key-fn #'(lambda (p q) (universal-lcm (leading-monomial p) (leading-monomial q)))
|
---|
| 82 | :pair-order-fn #'lex>)
|
---|
| 83 | "The normal selection strategy where a pair with the largest LCM of
|
---|
| 84 | leading monomials is selected.")
|
---|
[3932] | 85 |
|
---|
[4185] | 86 | (defparameter +min-total-degree-strategy+
|
---|
[4186] | 87 | (make-instance
|
---|
[4135] | 88 | 'selection-strategy
|
---|
| 89 | :pair-key-fn #'(lambda (p q) (total-degree (universal-lcm (leading-monomial p) (leading-monomial q))))
|
---|
| 90 | :pair-order-fn #'<)
|
---|
| 91 | "A selection strategy where a pair with a minimum total degree of
|
---|
| 92 | LCM of leading monomials is selected.")
|
---|
[3933] | 93 |
|
---|
[4185] | 94 | (defparameter +min-combined-length-strategy+
|
---|
[4186] | 95 | (make-instance
|
---|
[4135] | 96 | 'selection-strategy
|
---|
| 97 | :pair-key-fn #'(lambda (p q) (+ (poly-length p) (poly-length q)))
|
---|
| 98 | :pair-order-fn #'<)
|
---|
| 99 | "A selection strategy where a pair with the minimum combined length of both
|
---|
| 100 | polynomials is selected.")
|
---|
[3938] | 101 |
|
---|
[4148] | 102 | (defclass critical-pair-queue (priority-queue)
|
---|
| 103 | ()
|
---|
[4160] | 104 | (:documentation "Specializes class PRIORITY-QUEUE to ELEMENT-TYPE set to CRITICAL-PAIR."))
|
---|
[4148] | 105 |
|
---|
[4159] | 106 | (defmethod initialize-instance :before ((self critical-pair-queue) &key)
|
---|
[4152] | 107 | "Initializes the slot ELEMENT-TYPE to symbol CRITICAL-PAIR-QUEUE.
|
---|
| 108 | This overrides the default ELEMENT-TYPE in the superclass."
|
---|
[4151] | 109 | (with-slots ((element-type-x priority-queue::element-type))
|
---|
| 110 | self
|
---|
| 111 | (setf element-type-x 'critical-pair))
|
---|
| 112 | self)
|
---|
[4148] | 113 |
|
---|
[4146] | 114 | (defun make-critical-pairs (poly-list
|
---|
| 115 | &optional (start 0)
|
---|
[4142] | 116 | &aux
|
---|
| 117 | (s (1- (length poly-list))))
|
---|
[4183] | 118 | "Create a list of critical pairs. The argument POLY-LIST is the
|
---|
| 119 | initial list of polynomials and START is the first position beyond the
|
---|
| 120 | elements which form a partial Grobner basis, i.e. satisfy the
|
---|
| 121 | Buchberger criterion."
|
---|
[4142] | 122 | (nconc (makelist (make-instance 'critical-pair :first (elt poly-list i) :second (elt poly-list j))
|
---|
| 123 | (i 0 (1- start)) (j start s))
|
---|
[4146] | 124 | (makelist (make-instance 'critical-pair :first (elt poly-list i) :second (elt poly-list j))
|
---|
| 125 | (i start (1- s)) (j (1+ i) s))))
|
---|
[4142] | 126 |
|
---|
[4170] | 127 | (defgeneric enqueue-critical-pairs (self pair-lst)
|
---|
| 128 | (:documentation "Place pairs in PAIR-LST on the queue SELF.")
|
---|
[4143] | 129 | (:method ((self critical-pair-queue) pair-lst)
|
---|
[4170] | 130 | "Enqueue into queue QUEUE the elements of the list PAIR-LST."
|
---|
[4143] | 131 | (dolist (pair pair-lst self)
|
---|
| 132 | (enqueue self pair))))
|
---|
[4135] | 133 |
|
---|
[4162] | 134 | (defgeneric make-critical-pair-queue (object &optional poly-lst start)
|
---|
| 135 | (:documentation "Creates a CRITICAL-PAIR-QUEUE from an object OBJECT.")
|
---|
| 136 | (:method ((object selection-strategy) &optional (poly-lst nil) (start 0))
|
---|
[4182] | 137 | "Creates a CRITICAL-PAIR-QUEUE from a selection strategy OBJECT."
|
---|
[4162] | 138 | (with-slots (pair-key-fn pair-order-fn)
|
---|
| 139 | object
|
---|
[4170] | 140 | (let ((queue (make-instance 'critical-pair-queue
|
---|
| 141 | :element-key #'(lambda (pair)
|
---|
| 142 | (funcall pair-key-fn
|
---|
| 143 | (critical-pair-first pair)
|
---|
| 144 | (critical-pair-second pair)))
|
---|
| 145 | :test pair-order-fn)))
|
---|
| 146 | (enqueue-critical-pairs queue (make-critical-pairs poly-lst start))))))
|
---|
[4162] | 147 |
|
---|