/usr/share/maxima/5.32.1/src/spgcd.lisp is in maxima-src 5.32.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 | ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; The data in this file contains enhancments. ;;;;;
;;; ;;;;;
;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
;;; All rights reserved ;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; *****************************************************************
;;; ***** SPGCD ******* Sparse polynomial routines for GCD **********
;;; *****************************************************************
;;; ** (C) COPYRIGHT 1982 MASSACHUSETTS INSTITUTE OF TECHNOLOGY *****
;;; ****** THIS IS A READ-ONLY FILE! (ALL WRITES RESERVED) **********
;;; *****************************************************************
(in-package :maxima)
(macsyma-module spgcd)
(declare-top (special modulus genvar *alpha *which-factor*
$algebraic algfac* $gcd))
(load-macsyma-macros ratmac)
(defmvar $pointbound *alpha)
(defmacro len (lobj) `(cadr ,lobj))
(defmacro skel (lobj) `(caddr ,lobj))
(defmacro matrix (lobj) `(cadddr ,lobj))
(defmacro currow (lobj) `(cadr (cdddr ,lobj)))
(defmacro badrows (lobj) `(cadr (cddddr ,lobj)))
(defun pinterp (x pts vals)
(do ((u (car vals)
(pplus u (ptimes
(pctimes (crecip (pcsubstz (car xk) qk))
qk)
(pdifference (car uk)
(pcsubstz (car xk) u)))))
(uk (cdr vals) (cdr uk))
(qk (list x 1 1 0 (cminus (car pts)))
(ptimes qk (list x 1 1 0 (cminus (car xk)))))
(xk (cdr pts) (cdr xk)))
((null xk) u)))
(defun pcsubstz (val p)
(if (pcoefp p) p
(do ((l (p-terms p) (pt-red l))
(ans 0
(ctimes
(cplus ans (pt-lc l))
(cexpt val (- (pt-le l) (pt-le (pt-red l)))))))
((null (pt-red l))
(ctimes
(cplus ans (pt-lc l))
(if (zerop (pt-le l))
1
(cexpt val (pt-le l))))))))
;;skeletons consist of list of exponent vectors
;;lifting structures are as follows:
;;(matrix <n> ;length of <skel>
;; <skel> ;skeleton of poly being lifted
;; <matrix> ; partially diagonalized matrix used to obtain sol.
;; <row> ;we have diagonalized this far
;; <bad rows>) ; after we get
(defun eval-mon (mon pt)
(do ((l mon (cdr l))
(ll pt (cdr ll))
(ans 1 (ctimes
(if (zerop (car l)) 1
(cexpt (car ll) (car l)))
ans)))
((null l) ans)))
;; ONE-STEP causes the (row,col) element of mat to be made to be zero. It is
;; assumed that row > col, and that the matrix is diagonal above the row.
;; n indicates how wide the rows are suppoded to be.
(defun one-step (mat row col n)
(do ((i col (1+ i))
(c (aref mat row col))) ;Got to save this away before it is zeroed
((> i n) mat)
(setf (aref mat row i)
(cdifference (aref mat row i)
(ctimes c (aref mat col i))))))
;; MONICIZE-ROW assumes the (row,row) element is non-zero and that this element
;; is to be made equal to one. It merely divides the entire row by the
;; (row,row) element. It is assumed that the (row,n) elements with n < row are
;; already zero. Again n is the width of the rows.
(defun monicize-row (mat row n)
(do ((i n (1- i))
(c (crecip (aref mat row row))))
((= i row)
(setf (aref mat row row) 1)) ;Don't bother doing the division on the diagonal
(setf (aref mat row i) (ctimes (aref mat row i) c))))
;; FILL-ROW is given a point and the value of the skeleton at the point and
;; fills the apropriate row in the matrix. with the values of the monomials
;; n is the length of the skeleton
(defun fill-row (skel mat n row pt val)
(do ((i 0 (1+ i))
(l skel (cdr l)))
((= i n) ;l should be nil now,
(if (not (null l)) ;but lets check just to make sure
(merror "FILL-ROW: skeleton too long: ~S"
(list n '= skel)))
(setf (aref mat row n) val)) ;The value is stored in the last column
(setf (aref mat row i)
(eval-mon (car l) pt)))) ;Should think about a better
;way to do this evaluation.
#-allegro
(defun swap-rows (mat m n) ;Interchange row m and n
(do ((k 0 (1+ k))
(l (array-dimension mat 0)))
((> k l) mat)
(setf (aref mat m k)
(prog1
(aref mat n k)
(setf (aref mat n k) (aref mat m k))))))
;; PARTIAL-DIAG fills in one more row of the matrix and tries to diagonalize
;; what it has so far. If the row which has just been introduced has a
;; zero element on the diagonal then it is scanned for its first non-zero
;; element and it is placed in the matrix so that the non-zero element is
;; on the diagonal. The rows which are missing are added to the list in
;; badrows. The current row pointer may skip a couple of numbers here, so
;; when it is equal to n, the only empty rows to add thing to are on the
;; bad rows list.
(defun partial-diag (lobj pt val) ; Does one step of diagonalization of
(let ((n (len lobj)) ;the matrix in lobj
(skel (skel lobj))
(mat (matrix lobj)) ;The matrix, obviously
(row (currow lobj)) ;This is the row which is to be
;introduced
(badrows (badrows lobj)) ;Rows which could not be used when
;their time came, and will be used later
crow)
(cond ((= row n) ;All rows already done, must start
;using the rows in badrows.
(fill-row skel mat n (setq crow (car badrows)) pt val))
((fill-row skel mat n row pt val) ;Fill up the data
(setq crow row)))
;; This loop kills all the elements in the row up to the diagonal.
(do ((i 0 (1+ i)) ;runs over the columns of mat
(l (setq badrows (cons nil badrows)))
(flag))
((= i crow)
(setq badrows (cdr badrows))) ;update badrows
(if (cdr l) ;Any bad rows around?
(if (= (cadr l) i) ;No one for this row,
(if (and (null flag) ;So if this guy can fill in
(not (zerop (aref mat crow i))))
(progn
(swap-rows mat crow i) ;Put this guy in the right spot
(rplacd l (cddr l))
(setq flag t crow i)) ; and make him a winner
(setq l (cdr l))) ;At any rate this guy isn't
;used any more.
(one-step mat crow i n))
(one-step mat crow i n)))
(if (zerop (aref mat crow crow)) ;diagonal is zero?
(setq badrows (cons crow badrows))
(progn
(monicize-row mat crow n) ;Monicize the diagonal element on this
;row
(do ((j 0 (1+ j))) ;For each element in the rows above
;this one zero the the crow column
((= j crow)) ;Don't zero the diagonal element
(one-step mat j crow n))))
(cond ((and (= (1+ row) n)
(progn (setq row (1- row)) t) ;Decrement it just in case
(null (cdr badrows)))
(do ((l nil (cons (aref mat i n) l))
(i (1- n) (1- i)))
((< i 0)
(list 'solution n skel mat l))))
(t (list 'matrix n skel mat (1+ row) badrows)))))
(defun gen-point (vars)
(do ((l vars (cdr l))
(ans nil (cons (cmod (random $pointbound)) ans)))
((null l) ans)))
;; PDIAG-ALL introduces a new row in each matrix in the list of l-lobjs.
;; The RHS of the equations is stripped off of poly.
(defun pdiag-all (l-lobjs poly pt)
(do ((l l-lobjs (cdr l))
(lp (p-terms poly))
(solved t) (c))
((null l)
(if solved (cons 'solved l-lobjs)
l-lobjs))
(if (and lp (= (caar l) (pt-le lp))) ;This term corresponds to the
;current lobj, set c to the coefficient
(setq c (pt-lc lp) lp (pt-red lp))
(setq c 0))
;;FIXTHIS ;Should put it some check for extra
;terms in the polynomial
(if (not (eq (cadar l) 'solution))
(progn (rplacd (car l)
(partial-diag (cdar l) pt c))
(and solved (null (eq (cadar l) 'solution))
(setq solved nil))))))
;; not currently called
;; (defun CREATE-INTVECT (h)
;; (do ((l (cdr h) (cddr l))
;; (ans nil (cons (list (car l) (cadr l))
;; ans)))
;; ((null l)
;; (nreverse ans))))
;; (defun MERGE-INTVECT (iv h)
;; (do ((l iv (cdr l))
;; (h (cdr h)))
;; ((null l) iv)
;; (cond ((or (null h) (> (caar l) (car h)))
;; (rplacd (car l) (cons 0 (cdar l))))
;; ((= (caar l) (car h))
;; (rplacd (car l) (cons (cadr h) (cdar l)))
;; (setq h (cddr h)))
;; (t (error '|Bad Evaluation point - MERGE-INTVECT|)))))
(defun merge-skel (mon poly)
(cond ((pcoefp poly)
(list (cons 0 mon)))
((do ((l (cdr poly) (cddr l))
(ans nil
(cons (cons (car l) mon) ans)))
((null l) ans)))))
(defun new-skel (skel polys)
(list
(mapcan #'(lambda (mon poly) (merge-skel mon poly)) skel polys)
(mapcan #'(lambda (q)
(cond ((pcoefp q) (list q))
((do ((l (cdr q) (cddr l))
(ans nil (cons (cadr l) ans)))
((null l) ans)))))
polys)))
(defun create-lobjs (prev-lift)
(mapcar #'(lambda (q)
(let ((n (length (cadr q))))
(cons (car q)
(list 'matrix n (cadr q)
(make-array (list n (1+ n)) :initial-element 0 :element-type 'fixnum)
0 nil))))
prev-lift))
(defun clear-lobjs (lobjs)
(mapcar #'(lambda (q)
(cons (car q)
(list 'matrix (caddr q) (cadddr q)
(caddr (cddr q)) 0 nil)))
lobjs))
(defun sparse-lift (c f g l-lobjs vars)
(do ((pt (gen-point vars) (gen-point vars))
(gcd))
((eq (car l-lobjs) 'solved)
(cdr l-lobjs))
(setq gcd (lifting-factors-image
(pcsub c pt vars) (pcsub f pt vars) (pcsub g pt vars)))
(if (or (pcoefp gcd)
(not (= (pt-le (p-terms gcd)) (caar l-lobjs))))
(throw 'bad-point nil)
(setq l-lobjs (pdiag-all l-lobjs gcd pt)))))
(defun lifting-factors-image (c f g)
(let ((gcd (pgcdu f g)))
(case *which-factor*
(1 (pctimes c gcd))
(2 (pquotient f gcd))
(3 (pquotient g gcd)))))
(defun zgcd-lift* (c f g vars degb)
(do ((vals (gen-point vars) (gen-point vars))
(ans))
((not (null ans))
ans)
(setq ans
(catch 'bad-point
(zgcd-lift c f g vars vals degb)))))
;; ZGCD-LIFT returns objects called lifts. These have the the following
;; structure
;; ((n <skel> <poly>) ... )
;; n corresponds to the degree in the main variable to which this guy
;; corresponds.
(defun zgcd-lift (c f g vars vals degb)
(cond ((null vars) ;No variables left, just the main one
(let ((p (lifting-factors-image c f g))) ;Compute factor and
;enforce leading coefficient
(if (pcoefp p) (throw 'relprime 1) ;if the GCD is 1 quit
(do ((l (p-terms p) (pt-red l)) ;otherwise march
;though the polynomial
(ans nil ;constructing a lift for each term.
(cons (list (pt-le l) '(nil) (list (pt-lc l)))
ans)))
((null l)
(nreverse ans))))))
((let ((prev-lift ;Recurse if possible
(zgcd-lift (pcsubsty (car vals) (car vars) c)
(pcsubsty (car vals) (car vars) f)
(pcsubsty (car vals) (car vars) g)
(cdr vars) (cdr vals) (cdr degb))))
(do ((i 0 (1+ i)) ;counts to the degree bound
(lobjs (create-lobjs prev-lift) ;need to create
;the appropriate matrices
(clear-lobjs lobjs)) ;but reuse them at each
;step
(pts (add-point (list (car vals))) ;List of random
(add-point pts)) ;points
(linsols (mapcar 'make-linsols prev-lift)
(merge-sol-lin
linsols
(sparse-lift
(pcsubsty (car pts) (car vars) c)
(pcsubsty (car pts) (car vars) f)
(pcsubsty (car pts) (car vars) g)
lobjs (cdr vars)))))
((= i (car degb))
(interp-polys linsols (cdr pts) (car vars))))))))
(defun make-linsols (prev-lift)
(list (car prev-lift)
(cadr prev-lift)
(mapcan #'(lambda (q)
(cond ((pcoefp q) (list (list q)))
(t (do ((l (p-terms q) (pt-red l))
(ans nil (cons (list (pt-lc l)) ans)))
((null l) ans)))))
(caddr prev-lift))))
(defun add-point (l)
(do ((try (cmod (random $pointbound))
(cmod (random $pointbound))))
((null (member try l :test #'equal))
(cons try l))))
(defun merge-sol-lin (l1 l2)
(do ((l l1 (cdr l))
(l2 l2 (cdr l2)))
((null l) l1)
(cond ((= (caar l) (caar l2))
(rplaca (cddar l)
(mapcar 'cons (cadddr (cddar l2)) (caddar l)))))))
(defun interp-polys (l pts var)
(mapcar #'(lambda (q)
(cons (car q)
(new-skel
(cadr q)
(mapcar #'(lambda (r) (pinterp var pts r))
(caddr q)))))
l))
(defun zgcd (f g &aux $algebraic algfac*)
(let ((f (oldcontent f)) ;This is a good spot to
(g (oldcontent g)) ;initialize random
(gcd) (mon)
(*which-factor*))
;; *WHICH-FACTOR* is interpreted as follows. It is set fairly deep in the
;; algorithm, inside ZGCD1.
;; 1 -> Lift the GCD
;; 2 -> Lift the cofactor of F
;; 3 -> Lift the cofactor of G
(setq mon (pgcd (car f) (car g)) ;compute GCD of content
f (cadr f) g (cadr g)) ;f and g are now primitive
(if (or (pcoefp f) (pcoefp g)
(not (eq (car f) (car g))))
(merror "ZGCD: incorrect arguments."))
(ptimes mon
(do ((test))
(nil)
(setq gcd (catch 'relprime (zgcd1 f g)))
(setq test
(case *which-factor*
(1 (testdivide f gcd))
(2 (testdivide f gcd))
(3 (testdivide g gcd))))
(cond ((not (null test))
(return
(cond ((equal *which-factor* 1)
gcd)
(t test))))
((not (null modulus))
(return (let (($gcd '$red))
(pgcd f g)))))))))
(defun zgcd1 (f g)
(let* ((modulus modulus)
first-lift
h degb c
(vars (sort (union1 (listovars f) (listovars g))
#'pointergp))
(genvar (reverse vars))
;; the elements of the following degree vectors all correspond to the
;; contents of var. Thus there may be variables missing that are in
;;GENVAR.
;; (f-degv (zpdegreevector f vars)) ;;WHY NOT JUST PUT GENVAR THE REVERSE
;; (g-degv (zpdegreevector g vars)) ;;THE REVERSE OF VARS AND JUST USE PDEGREEVECTOR--wfs
(f-degv (pdegreevector f))
(g-degv (pdegreevector g))
(gcd-degv (gcd-degree-vector f g vars)))
;; First we try to decide which of the gcd and the cofactors of f and g
;; is smallest. The result of this decision is indicated by *which-factor*.
;; Then the leading coefficient that is to be enforced is changed if a
;; cofactor has been chosen.
(case (setq *which-factor*
(determine-lifting-factor f-degv g-degv gcd-degv))
(1 (setq c (pgcd (p-lc f) (p-lc g))))
(2 (setq c (p-lc f)))
(3 (setq c (p-lc g))))
;; Next a degree bound must be chosen.
(setq degb
(reverse
(mapcar #'+
(case *which-factor*
(1 gcd-degv)
(2 (mapcar #'- f-degv gcd-degv))
(3 (mapcar #'- g-degv gcd-degv)))
(zpdegreevector c vars))))
(cond ((not (null modulus))
(lobj->poly (car vars) (cdr vars)
(zgcd-lift* c f g (cdr vars) (cdr degb))))
(t
(setq h (* (maxcoefficient f)
(maxcoefficient g)))
(setq modulus *alpha) ;Really should randomize
(setq first-lift
(zgcd-lift* (pmod c) (pmod f) (pmod g)
(cdr vars) (cdr degb)))
(do ((linsols (mapcar #'(lambda (q)
(cons (car q)
(new-skel (cadr q) (caddr q))))
first-lift)
(merge-sol-lin-z linsols
(sparse-lift cm fm gm lobjs (cdr vars))
(* coef-bound
(crecip (cmod coef-bound)))
(* modulus coef-bound)))
(lobjs (create-lobjs first-lift)
(clear-lobjs lobjs))
(coef-bound *alpha (* modulus coef-bound))
(cm) (fm) (gm))
((> coef-bound h)
(setq modulus nil)
(lobj->poly (car vars) (cdr vars) linsols))
(setq modulus (newprime modulus))
(setq cm (pmod c)
fm (pmod f)
gm (pmod g)))))))
(defun lobj->poly (var vars lobj)
(primpart
(cons var
(mapcan
#'(lambda (q)
(list (car q)
(do ((x (cadr q) (cdr x))
(y (caddr q) (cdr y))
(ans 0
(pplus ans
(disrep-monom (cdar x) (car y)
vars))))
((null x) ans))))
lobj))))
(defun disrep-monom (monom c vars)
(cond ((null monom) c)
((equal 0 (car monom))
(disrep-monom (cdr monom) c (cdr vars)))
((list (car vars) (car monom)
(disrep-monom (cdr monom) c (cdr vars))))))
(defun merge-sol-lin-z (l1 l2 c new-coef-bound)
(do ((l l1 (cdr l))
(l2 l2 (cdr l2))
(modulus new-coef-bound)
(n))
((null l) l1)
(cond ((= (caar l) (caar l2))
(rplaca (cddar l)
(mapcar
#'(lambda (a b)
(cond ((> (abs (setq n (cplus b (ctimes c (cdifference a b)))))
new-coef-bound)
(throw 'relprime 1))
(n)))
(cadddr (cddar l2)) (caddar l)))))))
;; The following function tries to determine the degree of gcd(f, g) in each
;; variable. This is done in the following manner: All but one of the
;; variables in f and g are replaced by randomly chosen integers. The
;; resulting polynomials are called f* and g*. The degree of gcd(f*, g*) is
;; used as the degree of gcd(f, g) in that variable.
;;
;; The univariate gcd's are computed with modulus=*alpha.
(defun gcd-degree-vector (f g vars)
(let ((modulus *alpha))
(setq f (pmod f) g (pmod g))
(do ((vn (cdr vars) (cdr vn))
(vs (delete (car vars) (copy-list vars) :test #'equal)
(delete (car vn) (copy-list vars) :test #'equal))
(l) (f*) (g*) (gcd*) (rand))
(nil)
(setq rand (gen-point vs))
(setq f* (pcsub f rand vs)
g* (pcsub g rand vs))
(cond ((or (pcoefp f*) (pcoefp g*)
(pcoefp (setq gcd* (pgcdu f* g*))))
(push 0 l))
(t (push (pt-le (p-terms gcd*)) l)))
(cond ((null vn)
(return l)))))) ;No reverse needed here
;; DETERMINE-LIFTING-FACTOR returns a number indicating which factor of f or g
;; to which to lift
(defun dlf-mumblify (a b)
(loop for x in a for y in b sum (- x y)))
(defun determine-lifting-factor (f-degv g-degv gcd-degv)
(let* ((fv (dlf-mumblify f-degv gcd-degv))
(gv (dlf-mumblify g-degv gcd-degv))
(gcdv (apply '+ gcd-degv)))
(if (< fv gcdv)
(if (< fv gv) 2 3)
(if (< gv gcdv) 3 1))))
(defun excise-extra-variables (degv vars)
(do ((l (reverse degv) (cdr l))
(lv (reverse genvar) (cdr lv))
(ndegv))
((null l)
ndegv)
(cond ((eq (car lv) (car vars))
(push (car l) ndegv)
(setq vars (cdr vars))))))
(defun zpdegreevector (p vars)
(excise-extra-variables (pdegreevector p) vars))
;; Local Modes:
;; Mode:LISP
;; Fill Column:76
;; Auto Fill Mode:1
;; Comment Column:40
;; END:
|