| 1 | ;;; -*-  Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- | 
|---|
| 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 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 
|---|
| 23 | ;; | 
|---|
| 24 | ;; Critical pair queue implementation | 
|---|
| 25 | ;; | 
|---|
| 26 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 
|---|
| 27 |  | 
|---|
| 28 | (defpackage "PAIR-QUEUE" | 
|---|
| 29 | (:use :cl :priority-queue) | 
|---|
| 30 | (:export "SUGAR-PAIR-KEY" | 
|---|
| 31 | "PAIR" | 
|---|
| 32 | "PAIR-FIRST" | 
|---|
| 33 | "PAIR-SECOND" | 
|---|
| 34 | "PAIR-SUGAR" | 
|---|
| 35 | "PAIR-DIVISION-DATA" | 
|---|
| 36 | "SUGAR-ORDER" | 
|---|
| 37 | "*PAIR-KEY-FUNCTION*" | 
|---|
| 38 | "*PAIR-ORDER*" | 
|---|
| 39 | "MAKE-PAIR-QUEUE" | 
|---|
| 40 | "PAIR-QUEUE-INITIALIZE" | 
|---|
| 41 | "PAIR-QUEUE-INSERT" | 
|---|
| 42 | "PAIR-QUEUE-REMOVE" | 
|---|
| 43 | "PAIR-QUEUE-SIZE" | 
|---|
| 44 | "PAIR-QUEUE-EMPTY-P" | 
|---|
| 45 | "SET-PAIR-HEURISTIC" | 
|---|
| 46 | )) | 
|---|
| 47 |  | 
|---|
| 48 |  | 
|---|
| 49 | (defun sugar-pair-key (p q &aux (lcm (monom-lcm (poly-lm p) (poly-lm q))) | 
|---|
| 50 | (d (monom-sugar lcm))) | 
|---|
| 51 | "Returns list (S LCM-TOTAL-DEGREE) where S is the sugar of the S-polynomial of | 
|---|
| 52 | polynomials P and Q, and LCM-TOTAL-DEGREE is the degree of is LCM(LM(P),LM(Q))." | 
|---|
| 53 | (declare (type poly p q) (type monom lcm) (type fixnum d)) | 
|---|
| 54 | (cons (max | 
|---|
| 55 | (+  (- d (monom-sugar (poly-lm p))) (poly-sugar p)) | 
|---|
| 56 | (+  (- d (monom-sugar (poly-lm q))) (poly-sugar q))) | 
|---|
| 57 | lcm)) | 
|---|
| 58 |  | 
|---|
| 59 | (defstruct (pair | 
|---|
| 60 | (:constructor make-pair (first second | 
|---|
| 61 | &aux | 
|---|
| 62 | (sugar (car (sugar-pair-key first second))) | 
|---|
| 63 | (division-data nil)))) | 
|---|
| 64 | (first nil :type poly) | 
|---|
| 65 | (second nil :type poly) | 
|---|
| 66 | (sugar 0 :type fixnum) | 
|---|
| 67 | (division-data nil :type list)) | 
|---|
| 68 |  | 
|---|
| 69 | ;;(defun pair-sugar (pair &aux (p (pair-first pair)) (q (pair-second pair))) | 
|---|
| 70 | ;;  (car (sugar-pair-key p q))) | 
|---|
| 71 |  | 
|---|
| 72 | (defun sugar-order (x y) | 
|---|
| 73 | "Pair order based on sugar, ties broken by normal strategy." | 
|---|
| 74 | (declare (type cons x y)) | 
|---|
| 75 | (or (< (car x) (car y)) | 
|---|
| 76 | (and (= (car x) (car y)) | 
|---|
| 77 | (< (monom-total-degree (cdr x)) | 
|---|
| 78 | (monom-total-degree (cdr y)))))) | 
|---|
| 79 |  | 
|---|
| 80 | (defvar *pair-key-function* #'sugar-pair-key | 
|---|
| 81 | "Function that, given two polynomials as argument, computed the key | 
|---|
| 82 | in the pair queue.") | 
|---|
| 83 |  | 
|---|
| 84 | (defvar *pair-order* #'sugar-order | 
|---|
| 85 | "Function that orders the keys of pairs.") | 
|---|
| 86 |  | 
|---|
| 87 | (defun make-pair-queue () | 
|---|
| 88 | "Constructs a priority queue for critical pairs." | 
|---|
| 89 | (make-priority-queue | 
|---|
| 90 | :element-type 'pair | 
|---|
| 91 | :element-key #'(lambda (pair) (funcall *pair-key-function* (pair-first pair) (pair-second pair))) | 
|---|
| 92 | :test *pair-order*)) | 
|---|
| 93 |  | 
|---|
| 94 | (defun pair-queue-initialize (pq f start | 
|---|
| 95 | &aux | 
|---|
| 96 | (s (1- (length f))) | 
|---|
| 97 | (b (nconc (makelist (make-pair (elt f i) (elt f j)) | 
|---|
| 98 | (i 0 (1- start)) (j start s)) | 
|---|
| 99 | (makelist (make-pair (elt f i) (elt f j)) | 
|---|
| 100 | (i start (1- s)) (j (1+ i) s))))) | 
|---|
| 101 | "Initializes the priority for critical pairs. F is the initial list of polynomials. | 
|---|
| 102 | START is the first position beyond the elements which form a partial | 
|---|
| 103 | grobner basis, i.e. satisfy the Buchberger criterion." | 
|---|
| 104 | (declare (type priority-queue pq) (type fixnum start)) | 
|---|
| 105 | (dolist (pair b pq) | 
|---|
| 106 | (priority-queue-insert pq pair))) | 
|---|
| 107 |  | 
|---|
| 108 | (defun pair-queue-insert (b pair) | 
|---|
| 109 | (priority-queue-insert b pair)) | 
|---|
| 110 |  | 
|---|
| 111 | (defun pair-queue-remove (b) | 
|---|
| 112 | (priority-queue-remove b)) | 
|---|
| 113 |  | 
|---|
| 114 | (defun pair-queue-size (b) | 
|---|
| 115 | (priority-queue-size b)) | 
|---|
| 116 |  | 
|---|
| 117 | (defun pair-queue-empty-p (b) | 
|---|
| 118 | (priority-queue-empty-p b)) | 
|---|
| 119 |  | 
|---|
| 120 | (defun set-pair-heuristic (method) | 
|---|
| 121 | "Sets up variables *PAIR-KEY-FUNCTION* and *PAIR-ORDER* used | 
|---|
| 122 | to determine the priority of critical pairs in the priority queue." | 
|---|
| 123 | (ecase method | 
|---|
| 124 | ((sugar :sugar $sugar) | 
|---|
| 125 | (setf *pair-key-function* #'sugar-pair-key | 
|---|
| 126 | *pair-order* #'sugar-order)) | 
|---|
| 127 | ;     ((minimal-mock-spoly :minimal-mock-spoly $minimal_mock_spoly) | 
|---|
| 128 | ;      (setf *pair-key-function* #'mock-spoly | 
|---|
| 129 | ;          *pair-order* #'mock-spoly-order)) | 
|---|
| 130 | ((minimal-lcm :minimal-lcm $minimal_lcm) | 
|---|
| 131 | (setf *pair-key-function* #'(lambda (p q) | 
|---|
| 132 | (monom-lcm (poly-lm p) (poly-lm q))) | 
|---|
| 133 | *pair-order* #'reverse-monomial-order)) | 
|---|
| 134 | ((minimal-total-degree :minimal-total-degree $minimal_total_degree) | 
|---|
| 135 | (setf *pair-key-function* #'(lambda (p q) | 
|---|
| 136 | (monom-total-degree | 
|---|
| 137 | (monom-lcm (poly-lm p) (poly-lm q)))) | 
|---|
| 138 | *pair-order* #'<)) | 
|---|
| 139 | ((minimal-length :minimal-length $minimal_length) | 
|---|
| 140 | (setf *pair-key-function* #'(lambda (p q) | 
|---|
| 141 | (+ (poly-length p) (poly-length q))) | 
|---|
| 142 | *pair-order* #'<)))) | 
|---|
| 143 |  | 
|---|