| [1201] | 1 | ;;; -*-  Mode: Lisp -*- | 
|---|
| [73] | 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 |  | 
|---|
| [67] | 22 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 
|---|
|  | 23 | ;; | 
|---|
|  | 24 | ;; Operations in ideal theory | 
|---|
|  | 25 | ;; | 
|---|
|  | 26 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 
|---|
|  | 27 |  | 
|---|
| [502] | 28 | (defpackage "IDEAL" | 
|---|
| [1379] | 29 | (:use :cl :ring :monomial :order :term :polynomial :division :grobner-wrap :ring-and-order) | 
|---|
| [531] | 30 | (:export "POLY-DEPENDS-P" | 
|---|
|  | 31 | "RING-INTERSECTION" | 
|---|
|  | 32 | "ELIMINATION-IDEAL" | 
|---|
|  | 33 | "COLON-IDEAL" | 
|---|
|  | 34 | "COLON-IDEAL-1" | 
|---|
|  | 35 | "IDEAL-INTERSECTION" | 
|---|
|  | 36 | "POLY-LCM" | 
|---|
|  | 37 | "GROBNER-EQUAL" | 
|---|
|  | 38 | "GROBNER-SUBSETP" | 
|---|
|  | 39 | "GROBNER-MEMBER" | 
|---|
|  | 40 | "IDEAL-SATURATION-1" | 
|---|
|  | 41 | "IDEAL-SATURATION" | 
|---|
|  | 42 | "IDEAL-POLYSATURATION-1" | 
|---|
|  | 43 | "IDEAL-POLYSATURATION" | 
|---|
|  | 44 | )) | 
|---|
| [502] | 45 |  | 
|---|
|  | 46 | (in-package :ideal) | 
|---|
|  | 47 |  | 
|---|
| [67] | 48 | ;; Does the term depend on variable K? | 
|---|
|  | 49 | (defun term-depends-p (term k) | 
|---|
|  | 50 | "Return T if the term TERM depends on variable number K." | 
|---|
|  | 51 | (monom-depends-p (term-monom term) k)) | 
|---|
|  | 52 |  | 
|---|
|  | 53 | ;; Does the polynomial P depend on variable K? | 
|---|
|  | 54 | (defun poly-depends-p (p k) | 
|---|
|  | 55 | "Return T if the term polynomial P depends on variable number K." | 
|---|
|  | 56 | (some #'(lambda (term) (term-depends-p term k)) (poly-termlist p))) | 
|---|
|  | 57 |  | 
|---|
|  | 58 | (defun ring-intersection (plist k) | 
|---|
|  | 59 | "This function assumes that polynomial list PLIST is a Grobner basis | 
|---|
|  | 60 | and it calculates the intersection with the ring R[x[k+1],...,x[n]], i.e. | 
|---|
|  | 61 | it discards polynomials which depend on variables x[0], x[1], ..., x[k]." | 
|---|
|  | 62 | (dotimes (i k plist) | 
|---|
|  | 63 | (setf plist | 
|---|
|  | 64 | (remove-if #'(lambda (p) | 
|---|
|  | 65 | (poly-depends-p p i)) | 
|---|
|  | 66 | plist)))) | 
|---|
|  | 67 |  | 
|---|
| [902] | 68 | (defun elimination-ideal (ring-and-order flist k &optional (top-reduction-only $poly_top_reduction_only) (start 0)) | 
|---|
|  | 69 | (ring-intersection (reduced-grobner ring-and-order flist start top-reduction-only) k)) | 
|---|
| [67] | 70 |  | 
|---|
| [1380] | 71 | (defun colon-ideal (ring-and-order f g | 
|---|
|  | 72 | &optional | 
|---|
|  | 73 | (top-reduction-only $poly_top_reduction_only) | 
|---|
|  | 74 | &aux | 
|---|
|  | 75 | (ring (ro-ring ring-and-order))) | 
|---|
| [67] | 76 | "Returns the reduced Grobner basis of the colon ideal Id(F):Id(G), | 
|---|
|  | 77 | where F and G are two lists of polynomials. The colon ideal I:J is | 
|---|
|  | 78 | defined as the set of polynomials H such that for all polynomials W in | 
|---|
|  | 79 | J the polynomial W*H belongs to I." | 
|---|
| [1380] | 80 | (declare (type ring-and-order ring-and-order)) | 
|---|
| [67] | 81 | (cond | 
|---|
|  | 82 | ((endp g) | 
|---|
|  | 83 | ;;Id(G) consists of 0 only so W*0=0 belongs to Id(F) | 
|---|
|  | 84 | (if (every #'poly-zerop f) | 
|---|
|  | 85 | (error "First ideal must be non-zero.") | 
|---|
| [156] | 86 | (list (make-poly-from-termlist | 
|---|
| [67] | 87 | (list (make-term | 
|---|
| [994] | 88 | (make-monom :dimension (monom-dimension (poly-lm (find-if-not #'poly-zerop f)))) | 
|---|
| [67] | 89 | (funcall (ring-unit ring)))))))) | 
|---|
|  | 90 | ((endp (cdr g)) | 
|---|
| [1382] | 91 | (colon-ideal-1 ring-and-order f (car g) top-reduction-only)) | 
|---|
| [67] | 92 | (t | 
|---|
| [1381] | 93 | (ideal-intersection ring-and-order | 
|---|
| [1429] | 94 | (colon-ideal-1 ring-and-order f (car g) top-reduction-only) | 
|---|
| [1381] | 95 | (colon-ideal ring-and-order f (rest g) top-reduction-only) | 
|---|
| [67] | 96 | top-reduction-only)))) | 
|---|
|  | 97 |  | 
|---|
| [1383] | 98 | (defun colon-ideal-1 (ring-and-order f g | 
|---|
|  | 99 | &optional | 
|---|
|  | 100 | (top-reduction-only $poly_top_reduction_only) | 
|---|
|  | 101 | &aux | 
|---|
|  | 102 | (ring (ro-ring ring-and-order))) | 
|---|
| [67] | 103 | "Returns the reduced Grobner basis of the colon ideal Id(F):Id({G}), where | 
|---|
|  | 104 | F is a list of polynomials and G is a polynomial." | 
|---|
| [1427] | 105 | (declare (type ring-and-order ring-and-order)) | 
|---|
| [1384] | 106 | (mapcar #'(lambda (x) | 
|---|
|  | 107 | (poly-exact-divide ring-and-order x g)) | 
|---|
|  | 108 | (ideal-intersection ring-and-order f (list g) top-reduction-only))) | 
|---|
| [67] | 109 |  | 
|---|
| [1385] | 110 | (defun ideal-intersection (ring-and-order f g | 
|---|
|  | 111 | &optional | 
|---|
|  | 112 | (top-reduction-only $poly_top_reduction_only) | 
|---|
|  | 113 | &aux | 
|---|
|  | 114 | (ring (ro-ring ring-and-order))) | 
|---|
| [1428] | 115 | (declare (type ring-and-order ring-and-order)) | 
|---|
| [67] | 116 | (mapcar #'poly-contract | 
|---|
|  | 117 | (ring-intersection | 
|---|
|  | 118 | (reduced-grobner | 
|---|
| [902] | 119 | ring-and-order | 
|---|
| [994] | 120 | (append (mapcar #'(lambda (p) (poly-extend p (make-monom :dimension 1 :initial-exponent 1))) f) | 
|---|
| [67] | 121 | (mapcar #'(lambda (p) | 
|---|
| [902] | 122 | (poly-append (poly-extend (poly-uminus ring-and-order p) | 
|---|
| [994] | 123 | (make-monom :dimension 1 :initial-exponent 1)) | 
|---|
| [67] | 124 | (poly-extend p))) | 
|---|
|  | 125 | g)) | 
|---|
|  | 126 | 0 | 
|---|
|  | 127 | top-reduction-only) | 
|---|
|  | 128 | 1))) | 
|---|
|  | 129 |  | 
|---|
| [1386] | 130 | (defun poly-lcm (ring-and-order f g &aux (ring (ro-ring ring-and-order))) | 
|---|
| [67] | 131 | "Return LCM (least common multiple) of two polynomials F and G. | 
|---|
|  | 132 | The polynomials must be ordered according to monomial order PRED | 
|---|
|  | 133 | and their coefficients must be compatible with the RING structure | 
|---|
|  | 134 | defined in the COEFFICIENT-RING package." | 
|---|
|  | 135 | (cond | 
|---|
|  | 136 | ((poly-zerop f) f) | 
|---|
|  | 137 | ((poly-zerop g) g) | 
|---|
|  | 138 | ((and (endp (cdr (poly-termlist f))) (endp (cdr (poly-termlist g)))) | 
|---|
|  | 139 | (let ((m (monom-lcm (poly-lm f) (poly-lm g)))) | 
|---|
|  | 140 | (make-poly-from-termlist (list (make-term m (funcall (ring-lcm ring) (poly-lc f) (poly-lc g))))))) | 
|---|
|  | 141 | (t | 
|---|
|  | 142 | (multiple-value-bind (f f-cont) | 
|---|
|  | 143 | (poly-primitive-part ring f) | 
|---|
|  | 144 | (multiple-value-bind (g g-cont) | 
|---|
|  | 145 | (poly-primitive-part ring g) | 
|---|
|  | 146 | (scalar-times-poly | 
|---|
|  | 147 | ring | 
|---|
|  | 148 | (funcall (ring-lcm ring) f-cont g-cont) | 
|---|
| [1387] | 149 | (poly-primitive-part ring (car (ideal-intersection ring-and-order (list f) (list g) nil))))))))) | 
|---|
| [67] | 150 |  | 
|---|
|  | 151 | ;; Do two Grobner bases yield the same ideal? | 
|---|
| [1388] | 152 | (defun grobner-equal (ring-and-order g1 g2) | 
|---|
| [67] | 153 | "Returns T if two lists of polynomials G1 and G2, assumed to be Grobner bases, | 
|---|
|  | 154 | generate  the same ideal, and NIL otherwise." | 
|---|
| [1388] | 155 | (declare (type ring-and-order ring-and-order)) | 
|---|
|  | 156 | (and (grobner-subsetp ring-and-order g1 g2) (grobner-subsetp ring-and-order g2 g1))) | 
|---|
| [67] | 157 |  | 
|---|
| [1389] | 158 | (defun grobner-subsetp (ring-and-order g1 g2) | 
|---|
| [67] | 159 | "Returns T if a list of polynomials G1 generates | 
|---|
|  | 160 | an ideal contained in the ideal generated by a polynomial list G2, | 
|---|
|  | 161 | both G1 and G2 assumed to be Grobner bases. Returns NIL otherwise." | 
|---|
| [1389] | 162 | (declare (type ring-and-order ring-and-order)) | 
|---|
| [1390] | 163 | (every #'(lambda (p) (grobner-member ring-and-order p g2)) g1)) | 
|---|
| [67] | 164 |  | 
|---|
| [1391] | 165 | (defun grobner-member (ring-and-order p g) | 
|---|
| [67] | 166 | "Returns T if a polynomial P belongs to the ideal generated by the | 
|---|
|  | 167 | polynomial list G, which is assumed to be a Grobner basis. Returns NIL otherwise." | 
|---|
| [1391] | 168 | (declare (type ring-and-order ring-and-order)) | 
|---|
|  | 169 | (poly-zerop (normal-form ring-and-order p g nil))) | 
|---|
| [67] | 170 |  | 
|---|
|  | 171 | ;; Calculate F : p^inf | 
|---|
| [1392] | 172 | (defun ideal-saturation-1 (ring-and-order f p start | 
|---|
|  | 173 | &optional | 
|---|
|  | 174 | (top-reduction-only $poly_top_reduction_only)) | 
|---|
| [67] | 175 | "Returns the reduced Grobner basis of the saturation of the ideal | 
|---|
|  | 176 | generated by a polynomial list F in the ideal generated by a single | 
|---|
|  | 177 | polynomial P. The saturation ideal is defined as the set of | 
|---|
|  | 178 | polynomials H such for some natural number n (* (EXPT P N) H) is in the ideal | 
|---|
|  | 179 | F. Geometrically, over an algebraically closed field, this is the set | 
|---|
|  | 180 | of polynomials in the ideal generated by F which do not identically | 
|---|
|  | 181 | vanish on the variety of P." | 
|---|
| [1392] | 182 | (declare (type ring-and-order ring-and-order)) | 
|---|
| [67] | 183 | (mapcar | 
|---|
|  | 184 | #'poly-contract | 
|---|
|  | 185 | (ring-intersection | 
|---|
|  | 186 | (reduced-grobner | 
|---|
| [902] | 187 | ring-and-order | 
|---|
|  | 188 | (saturation-extension-1 ring-and-order f p) | 
|---|
| [67] | 189 | start top-reduction-only) | 
|---|
|  | 190 | 1))) | 
|---|
|  | 191 |  | 
|---|
|  | 192 |  | 
|---|
|  | 193 | ;; Calculate F : p1^inf : p2^inf : ... : ps^inf | 
|---|
| [1395] | 194 | (defun ideal-polysaturation-1 (ring-and-order f plist start | 
|---|
|  | 195 | &optional | 
|---|
|  | 196 | (top-reduction-only $poly_top_reduction_only)) | 
|---|
| [67] | 197 | "Returns the reduced Grobner basis of the ideal obtained by a | 
|---|
|  | 198 | sequence of successive saturations in the polynomials | 
|---|
|  | 199 | of the polynomial list PLIST of the ideal generated by the | 
|---|
|  | 200 | polynomial list F." | 
|---|
|  | 201 | (cond | 
|---|
| [1394] | 202 | ((endp plist) | 
|---|
| [1396] | 203 | (reduced-grobner ring-and-order f start top-reduction-only)) | 
|---|
| [1397] | 204 | (t (let ((g (ideal-saturation-1 ring-and-order f (car plist) start top-reduction-only))) | 
|---|
|  | 205 | (ideal-polysaturation-1 ring-and-order g (rest plist) (length g) top-reduction-only))))) | 
|---|
| [67] | 206 |  | 
|---|
| [903] | 207 | (defun ideal-saturation (ring-and-order f g start &optional (top-reduction-only $poly_top_reduction_only) | 
|---|
| [67] | 208 | &aux | 
|---|
| [903] | 209 | (k (length g))) | 
|---|
| [67] | 210 | "Returns the reduced Grobner basis of the saturation of the ideal | 
|---|
|  | 211 | generated by a polynomial list F in the ideal generated a polynomial | 
|---|
|  | 212 | list G. The saturation ideal is defined as the set of polynomials H | 
|---|
|  | 213 | such for some natural number n and some P in the ideal generated by G | 
|---|
|  | 214 | the polynomial P**N * H is in the ideal spanned by F.  Geometrically, | 
|---|
|  | 215 | over an algebraically closed field, this is the set of polynomials in | 
|---|
|  | 216 | the ideal generated by F which do not identically vanish on the | 
|---|
|  | 217 | variety of G." | 
|---|
|  | 218 | (mapcar | 
|---|
|  | 219 | #'(lambda (q) (poly-contract q k)) | 
|---|
|  | 220 | (ring-intersection | 
|---|
| [903] | 221 | (reduced-grobner ring-and-order | 
|---|
|  | 222 | (polysaturation-extension ring-and-order f g) | 
|---|
| [67] | 223 | start | 
|---|
|  | 224 | top-reduction-only) | 
|---|
|  | 225 | k))) | 
|---|
|  | 226 |  | 
|---|
| [1398] | 227 | (defun ideal-polysaturation (ring-and-order f ideal-list start | 
|---|
|  | 228 | &optional | 
|---|
|  | 229 | (top-reduction-only $poly_top_reduction_only)) | 
|---|
| [67] | 230 | "Returns the reduced Grobner basis of the ideal obtained by a | 
|---|
|  | 231 | successive applications of IDEAL-SATURATION to F and lists of | 
|---|
|  | 232 | polynomials in the list IDEAL-LIST." | 
|---|
|  | 233 | (cond | 
|---|
|  | 234 | ((endp ideal-list) f) | 
|---|
| [903] | 235 | (t (let ((h (ideal-saturation ring-and-order f (car ideal-list) start top-reduction-only))) | 
|---|
|  | 236 | (ideal-polysaturation ring-and-order h (rest ideal-list) (length h) top-reduction-only))))) | 
|---|