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