/usr/share/maxima/5.32.1/src/algsys.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 | ;;; -*- 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 ;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; (c) Copyright 1981 Massachusetts Institute of Technology ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(in-package :maxima)
(macsyma-module algsys)
(load-macsyma-macros ratmac)
;;This is the algsys package.
;;It solves systems of polynomial equations by straight-forward
;;resultant hackery. Other possible methods seem worse:
;;the Buchberger-Spear canonical ideal basis algorithm is slow,
;;and the "resolvent" method (see van der Waerden, section 79)
;;blows up in time and space. The "resultant"
;;method (see the following sections of van der Waerden and
;;Macaulay's book - Algebraic Theory of Modular Systems) looks
;;good, but it requires the evaluation of large determinants.
;;Unless some hack (such as prs's for evaluating resultants of
;;two polynomials) is developed for multi-polynomial resultants,
;;this method will remain impractical.
;;Some other possible ideas: Keeping the total number of equations constant,
;;in an effort to reduce extraneous solutions, or Reducing to a linear
;;equation before taking resultants.
(declare-top (special $algdelta $ratepsilon $algepsilon $keepfloat
varlist genvar *roots *failures $ratprint $numer $ratfac
$rnum $solvefactors $dispflag $breakup $rootsquad
*tvarxlist* errorsw $programmode *ivar* errset $polyfactor
bindlist loclist $float $infeval))
;;note if $algepsilon is too large you may lose some roots.
(defmvar $algdelta 1e-5 )
(defmvar $%rnum_list '((mlist))
"Upon exit from ALGSYS this is bound to a list of the %RNUMS
which where introduced into the expression. Useful for mapping
over and using as an argument to SUBST.")
(defmvar $realonly nil "If t only real solutions are returned.")
(defmvar realonlyratnum nil
"A REALROOTS hack for RWG. Causes ALGSYS to retain rational numbers
returned by REALROOTS when REALONLY is TRUE."
in-core)
(defmvar $algexact nil "If t ALGSYS always calls SOLVE to try to MAXIMA-FIND exact
solutions.")
(defmvar algnotexact nil
"A hack for RWG for univariate polys. Causes SOLVE not to get called
so that sqrts and cube roots will not be generated."
in-core)
(defmacro merrset (l)
`(let ((errset 'errbreak1) (unbind (cons bindlist loclist)) val)
(setq val (errset ,l nil))
(when (null val) (errlfun1 unbind))
val))
(defmfun $algsys (lhslist varxlist &aux varlist genvar)
;; (declare (special varxlist)) ;;??
(setq $%rnum_list (list '(mlist)))
(cond ((not ($listp lhslist))
(merror (intl:gettext "algsys: first argument must be a list; found ~M") lhslist))
((not ($listp varxlist))
(merror (intl:gettext "algsys: second argument must be a list; found ~M") varxlist)))
(let ((tlhslist nil) (*tvarxlist* nil) (solnlist nil) ($ratprint nil)
;; GCL seems to read 1e-7 as zero, but only when compiling. Incantations
;; based on 1d-7, 1l-7 etc. don't seem to make any difference.
($ratepsilon #-gcl 1e-7
#+gcl (float 1/10000000))
($keepfloat nil)
(varlist (reverse (cdr varxlist)))
(genvar nil) ($ratfac nil) ($breakup nil)
($solvefactors nil) (*roots nil) (*failures nil)
(*ivar* nil) ($polyfactor nil) (varxl nil)
($infeval nil) ($numer nil) ($float nil)
(numerflg $numer))
(dolist (var (cdr ($listofvars (list '(mlist simp) lhslist varxlist))))
(if (and (symbolp var) (not (constant var)))
(setq varxl (cons var varxl))))
(orderpointer varlist)
(setq tlhslist
(mapcar #'(lambda (q) (cadr (ratf (meqhk q))))
(cdr lhslist)))
(setq *ivar* (caadr (ratf '$%i)))
(setq *tvarxlist*
(mapcar #'(lambda (q)
(if (mnump q)
(merror (intl:gettext "algsys: variable cannot be a number; found ~M") q)
(caadr (ratf q))))
(cdr varxlist)))
(putorder *tvarxlist*)
(mbinding (varxl varxl)
(setq solnlist
(mapcar #'(lambda (q)
(addmlist
(bbsorteqns
(addparam (roundroots1 q) varxlist))))
(algsys tlhslist))))
(remorder *tvarxlist*)
(setq solnlist (addmlist solnlist))
(if numerflg
(let (($numer t) ($float t))
(resimplify solnlist))
solnlist)))
(defun condensesolnl (tempsolnl)
(let (solnl)
(mapl #'(lambda (q) (or (subsetl (cdr q) (car q))
(setq solnl (cons (car q) solnl))))
(sort tempsolnl #'(lambda (a b) (> (length a) (length b)))))
solnl))
(defun subsetl (l1 s2)
(or (equal s2 (list nil))
(do ((l l1 (cdr l)))
((null l) nil)
(when (m-subset (car l) s2) (return t)))))
(defun m-subset (s1 s2)
(do ((s s1 (cdr s)))
((null s) t)
(unless (memalike (car s) s2) (return nil))))
(defun algsys (tlhslist &aux answ)
(setq answ
(condensesolnl (apply #'append
(mapcar #' algsys0
(distrep (mapcar #'lofactors tlhslist))))))
;; (displa (cons '(mlist) (loop for v in answ collecting
;; (cons '(mlist) v))))
answ)
(defun algsys0 (tlhslist)
(cond ((null tlhslist) (list nil))
((equal tlhslist (list nil)) nil)
(t (algsys1 tlhslist))))
(defun algsys1 (tlhslist)
(let ((resulteq (findleastvar tlhslist))
(vartorid nil)
(nlhslist nil))
(setq vartorid (cdr resulteq)
resulteq (car resulteq)
nlhslist (mapcar #'(lambda (q)
(if (among vartorid q)
(presultant q resulteq vartorid)
q))
(delete resulteq (copy-list tlhslist) :test #'equal)))
(bakalevel (algsys nlhslist) tlhslist vartorid)))
(defun addmlist (l)
(cons '(mlist) l))
(defmacro what-the-$ev (&rest l)
;; macro for calling $EV when you are not really
;; sure why you are calling it, but you want the
;; features of multiple evaluations and unpredictabiltiy
;; anyway.
`(meval (list '($ev) ,@l)))
(defun rootsp (asolnset eqn) ;eqn is ((MLIST) eq deriv)
(let (rr ($keepfloat t) ($numer t) ($float t))
(setq rr (what-the-$ev eqn asolnset)) ; ratsimp?
(cond ((and (complexnump (cadr rr)) (complexnump (caddr rr)))
(< (cabs (cadr rr))
(* $algdelta (max 1 (cabs (caddr rr))))))
(t nil))))
(defun round1 (a)
(cond ((floatp a)
(setq a (maxima-rationalize a))
(fpcofrat1 (car a) (cdr a)))
(t a)))
(defun roundrhs (eqn)
(list (car eqn) (cadr eqn) (round1 (caddr eqn))))
(defun roundroots1 (lsoln)
(mapcar #'roundrhs lsoln))
(defun bbsorteqns (l)
(sort (copy-list l) #'orderlessp))
(defun putorder (tempvarl)
(do ((n 1 (1+ n))
(tempvarl tempvarl (cdr tempvarl)))
((null tempvarl) nil)
(putprop (car tempvarl) n 'varorder)))
(defun remorder (gvarl)
(mapc #'(lambda (x) (remprop x 'varorder)) gvarl))
(defun orderlessp (eqn1 eqn2)
(< (get (caadr (ratf (cadr eqn1))) 'varorder)
(get (caadr (ratf (cadr eqn2))) 'varorder)))
(defun addparam (asolnsetl varxlist)
(cond ((= (length asolnsetl) (length *tvarxlist*))
asolnsetl)
(t
(do ((tvarxl (cdr varxlist) (cdr tvarxl))
(defvar (mapcar #'cadr asolnsetl))
(var) (param))
((null tvarxl) asolnsetl)
(setq var (car tvarxl))
(cond ((memalike var defvar) nil)
(t (setq param (make-param)
asolnsetl (cons (list '(mequal) var param)
(cdr (maxima-substitute
param var
(addmlist asolnsetl)))))))))))
(declare-top (special *vardegs*))
(defun findleastvar (lhsl)
(do ((tlhsl lhsl (cdr tlhsl))
(teq) (*vardegs*) (tdeg)
;; Largest possible fixnum. The actual degree of any polynomial
;; is supposed to be less than this number.
(leastdeg most-positive-fixnum)
(leasteq) (leastvar))
((null tlhsl) (cons leasteq leastvar))
(declare (special *vardegs*))
(setq teq (car tlhsl))
(setq *vardegs* (getvardegs teq))
(setq tdeg (killvardegsc teq))
(mapc #'(lambda (q) (cond ((not (> (cdr q) leastdeg))
(setq leastdeg (cdr q)
leasteq teq
leastvar (car q)))))
*vardegs*)
(cond ((< tdeg leastdeg) (setq leastdeg tdeg
leasteq teq
leastvar (car teq))))))
(defun killvardegsc (poly)
(cond ((pconstp poly) 0)
(t (do ((poly (cdr poly) (cddr poly))
(tdeg 0 (max tdeg (+ (car poly)
(cond ((= (car poly) 0)
(killvardegsc (cadr poly)))
(t (killvardegsn (cadr poly))))))))
((null poly) tdeg)))))
(defun killvardegsn (poly)
(declare (special *vardegs*))
(cond ((pconstp poly)
0)
(t
(let ((x (assoc (car poly) *vardegs* :test #'eq)))
(and x
(not (> (cdr x) (cadr poly)))
(setq *vardegs* (delete x *vardegs* :test #'equal))))
(do ((poly (cdr poly) (cddr poly))
(tdeg 0 (max tdeg (+ (car poly) (killvardegsn (cadr poly))))))
((null poly) tdeg)))))
(defun getvardegs (poly)
(cond ((pconstp poly) nil)
((pconstp (caddr poly))
(cons (cons (car poly) (cadr poly))
(getvardegs (ptterm (cdr poly) 0))))
(t (getvardegs (ptterm (cdr poly) 0)))))
(declare-top (unspecial *vardegs*))
(defun pconstp (poly)
(or (atom poly) (not (member (car poly) *tvarxlist* :test #'eq))))
(defun pfreeofmainvarsp (poly)
(cond ((atom poly) poly)
((null (member (car poly) *tvarxlist* :test #'eq))
($radcan (pdis poly)))
(t poly)))
(defun lofactors (poly)
(setq poly (pfreeofmainvarsp poly))
(cond ((pzerop poly) ;(signp e poly)
(list 0))
((or (atom poly) (not (atom (car poly)))) nil)
(t (do ((tfactors (pfactor poly) (cddr tfactors))
(lfactors))
((null tfactors) lfactors)
(setq poly (pfreeofmainvarsp (car tfactors)))
(cond ((pzerop poly) ;(signp e poly)
(return (list 0)))
((and (not (atom poly)) (atom (car poly)))
(setq lfactors (cons (pabs poly) lfactors))))))))
(defun combiney (listofl)
(cond ((member nil listofl :test #'eq) nil)
(t (combiney1 (delete '(0) listofl :test #'equal)))))
(defun combiney1 (listofl)
(cond ((null listofl) (list nil))
(t (mapcan #'(lambda (r)
(if (intersection (car listofl) r :test #'equal)
(list r)
(mapcar #'(lambda (q) (cons q r)) (car listofl))))
(combiney1 (cdr listofl))))))
(defun midpnt (l)
(rhalf (rplus* (car l) (cadr l))))
(defun rflot (l)
(let ((rr (midpnt l)))
(if realonlyratnum (list '(rat) (car rr) (cdr rr))
(/ (+ 0.0 (car rr)) (cdr rr)))))
(defun memberroot (a x eps)
(cond ((null x) nil)
((< (abs (- a (car x)))
(/ (+ 0.0 (car eps)) (cdr eps)))
t)
(t (memberroot a (cdr x) eps))))
(defun commonroots (eps solnl1 solnl2)
(cond ((null solnl1) nil)
((memberroot (car solnl1) solnl2 eps)
(cons (car solnl1) (commonroots eps (cdr solnl1) solnl2)))
(t (commonroots eps (cdr solnl1) solnl2))))
(defun deletmult (l)
(and l (cons (car l) (deletmult (cddr l)))))
(defun punivarp (poly)
;; Check if called with the number zero, return nil.
;; Related bugs: SF[609466], SF[1430379], SF[1663399]
(when (and (numberp poly) (= poly 0)) (return-from punivarp nil))
(do ((l (cdr poly) (cddr l)))
((null l) t)
(or (numberp (cadr l))
(and (eq (caadr l) *ivar*)
(punivarp (cadr l)))
(return nil))))
(defun realonly (rootsl)
(cond ((null rootsl) nil)
((equal 0 (sratsimp ($imagpart (caddr (car rootsl)))))
(nconc (list (car rootsl)) (realonly (cdr rootsl))))
(t (realonly (cdr rootsl)))))
(defun presultant (p1 p2 var)
(cadr (ratf ($resultant (pdis p1) (pdis p2) (pdis (list var 1 1))))))
(defun ptimeftrs (l)
(prog (ll)
(setq ll (cddr l))
(cond ((null ll) (return (car l)))
(t (return (ptimes (car l) (ptimeftrs ll)))))))
(defun ebaksubst (solnl lhsl)
(mapcar #'(lambda (q) (cadr (ratf (what-the-$ev (pdis q)
(cons '(mlist) solnl)
'$radcan))))
lhsl))
(defun baksubst (solnl lhsl)
(setq lhsl (delete 't (mapcar #'(lambda (q) (car (merrset (baksubst1 solnl q))))
lhsl)
:test #'eq)) ;catches arith. ovfl
(if (member nil lhsl :test #'eq)
(list nil)
lhsl))
(defun baksubst1 (solnl poly)
(let* (($keepfloat (not $realonly)) ;sturm1 needs poly with
(poly1 ;integer coefs
(cdr (ratf (what-the-$ev (pdis poly)
(cons '(mlist) solnl)
'$numer)))))
(cond ((and (complexnump (pdis (car poly1)))
(numberp (cdr poly1)))
(rootsp (cons '(mlist) solnl)
(list '(mlist) (pdis poly) (tayapprox poly))))
(t (car poly1)))))
(defun complexnump (p)
(let ((p (cadr (ratf ($ratsimp p)))))
(or (numberp p)
(eq (pdis (pget (car p))) '$%i))))
(defun bakalevel (solnl lhsl var)
;;(apply #'append (mapcar #'(lambda (q) (bakalevel1 q lhsl var)) solnl))
(loop for q in solnl append (bakalevel1 q lhsl var)))
(defun bakalevel1 (solnl lhsl var)
(cond ((exactonly solnl)
(cond (solnl (mergesoln solnl (algsys (ebaksubst solnl lhsl))))
((cdr lhsl)
(bakalevel (callsolve (setq solnl (findleastvar lhsl)))
(remove (car solnl) lhsl :test #'equal) var))
(t (callsolve (cons (car lhsl) var)))))
(t (mergesoln solnl (apprsys (baksubst solnl lhsl))))))
(defun exactonly (solnl)
(cond ((atom solnl)
(and (not (floatp solnl))
(or (null realonlyratnum) (not (eq solnl 'rat)))))
(t (and (exactonly (car solnl)) (exactonly (cdr solnl))))))
(defun mergesoln (asoln solnl)
(let ((errorsw t) s (unbind (cons bindlist loclist)))
(mapcan #'(lambda (q)
(setq s (catch 'errorsw
(append
(mapcar #'(lambda (r)
(what-the-$ev r (cons '(mlist) q)))
asoln)
q)))
(cond ((eq s t)
(errlfun1 unbind)
nil)
(t
(list s))))
solnl)))
(defun callsolve (pv)
(let ((poly (car pv))
(var (cdr pv))
(varlist varlist)
(genvar genvar)
(*roots nil)
(*failures nil)
($programmode t))
(cond ((or $algexact (not (punivarp poly))
(biquadraticp poly))
(solve (pdis poly) (pdis (list var 1 1)) 1)
(cond ((null (or *roots *failures))
(list nil))
(t
(append (mapcan #'(lambda (q) (callapprs (cadr (ratf (meqhk q))))) (deletmult *failures))
(mapcar #'list
(if $realonly
(realonly (deletmult *roots))
(deletmult *roots)))))))
(t (callapprs poly)))))
(defun biquadraticp (poly)
(or (atom poly)
(if algnotexact
(< (cadr poly) 2)
(or (< (cadr poly) 3)
(and (= (cadr poly) 4) (biquadp1 (cdddr poly)))))))
(defun biquadp1 (l)
(or (null l)
(and (or (= (car l) 2) (= (car l) 0))
(biquadp1 (cddr l)))))
(defun callapprs (poly)
(or (punivarp poly)
(merror (intl:gettext "algsys: tried and failed to reduce system to a polynomial in one variable; give up.")))
(let ($rootsquad $dispflag)
(cond ($realonly
(mapcar #'(lambda (q)
(list (list '(mequal)
(pdis (list (car poly) 1 1))
(rflot q))))
(sturm1 poly (cons 1 $algepsilon))))
(t (mapcar #'list
(let (($programmode t) l)
(setq l (cdr ($allroots (pdis poly))))
(cond ((not (eq (caaar l) 'mequal)) (cdr l))
(t l))))))))
(defun apprsys (lhsl)
(cond ((null lhsl) (list nil))
(t
(do ((tlhsl lhsl (cdr tlhsl))) (nil)
(cond ((null tlhsl)
;; SHOULD TRY TO BE MORE SPECIFIC: "TOO COMPLICATED" IN WHAT SENSE??
(merror (intl:gettext "algsys: system too complicated; give up.")))
((pconstp (car tlhsl)) (return nil))
((punivarp (car tlhsl))
(return (bakalevel (callapprs (car tlhsl))
lhsl nil))))))))
(defun tayapprox (p)
(cons '(mplus)
(mapcar #'(lambda (x)
(list '(mycabs) (pdis (ptimes (list x 1 1)
(pderivative p x)))))
(listovars p))))
(defmfun mycabs (x)
(and (complexnump x) (cabs x)))
(defun distrep (lol)
(condensesolnl (condensesublist (combiney lol))))
(defun condensey (l)
(let ((result nil))
(mapl #'(lambda (q)
(or (memalike (car q) (cdr q)) (push (car q) result)))
l)
result))
(defun condensesublist (lol)
(mapcar #'condensey lol))
(defun exclude (l1 l2)
(cond ((null l2)
nil)
((member (car l2) l1 :test #'equal)
(exclude l1 (cdr l2)))
(t
(cons (car l2) (exclude l1 (cdr l2))))))
|