File: algsys.lisp

package info (click to toggle)
maxima 5.6-17
  • links: PTS
  • area: main
  • in suites: woody
  • size: 30,572 kB
  • ctags: 47,715
  • sloc: ansic: 154,079; lisp: 147,553; asm: 45,843; tcl: 16,744; sh: 11,057; makefile: 7,198; perl: 1,842; sed: 334; fortran: 24; awk: 5
file content (544 lines) | stat: -rw-r--r-- 17,367 bytes parent folder | download | duplicates (2)
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
;;; -*-  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)

(eval-when (eval compile)
  #+PDP10
  ;; this file contains macysma variable binding calls which
  ;; have been observed to need unwind-protect even on the PDP10,
  ;; since the ERRLIST mechanism can't hack catch and *throw.
  ;; In general unwind-protect is needed, but the bugs caused by not using
  ;; it are rare, and it is not deemed worth using UNWIND-PROTECT in
  ;; general.
  (setq MBINDING-USAGE 'unwind-protect))

;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)
	 (*lexpr $ratsimp)
	 (genprefix alg))

;;note if  $algepsilon is too large you may lose some roots.
#+cl
(defmvar $algdelta 1.0e-4 )


(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)
  #-NIL `(let ((errset 'errbreak1) (unbind (cons bindlist loclist)) val)
	   (setq val (errset ,l nil))
	   (cond ((null val) (errlfun1 unbind)))
	   val)
  ;I guess be a bit dirty now...
  #+NIL (let ((name (gentemp)))
	  `(let ((unbind (cons bindlist loclist)))
	     (let ((val (block ,name
			  (catch 'si:errset-catch
			    (let ((si:errset-status t)
				  (si:errset-print-message t))
			      (return-from ,name (list ,l))))
			  (errbreak1 nil)
			  nil)))
	       (unless val (errlfun1 unbind))
	       val))))

;; Make sure that $SOLVE is in core.  Only needed for small address
;; space systems.

#+PDP10
(find-function '$solve)

(defmfun $algsys (lhslist varxlist &aux varlist genvar)
;  (declare (special varxlist)) ;;??
  (setq $%rnum_list (list '(mlist)))
  (cond ((not ($listp lhslist))
	 (merror  "Wrong type arg to ALGSYS:~%~M" lhslist))
	((not ($listp varxlist))
	 (merror "Wrong type arg to ALGSYS:~%~M" varxlist)))
  ((lambda (tlhslist *tvarxlist* solnlist $ratprint $ratepsilon
		     $keepfloat varlist genvar $ratfac $breakup
		     $solvefactors *roots *failures *ivar* $polyfactor 
		     varxl $infeval $numer $float numerflg)
     (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 (function (lambda (q) (cadr (ratf (meqhk q)))))
		   (cdr lhslist)))
     (setq *ivar* (caadr (ratf '$%I)))
     (setq *tvarxlist*
	   (mapcar #'(lambda (q)
		       (cond ((mnump q)
			      (merror "Unacceptable variable to ALGSYS:~%~M"
				      q))
			     (t (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)) (ssimplifya solnlist))
		  solnlist))
   nil nil nil nil 1.0e-7
   nil (reverse (cdr varxlist)) nil nil nil
   nil nil nil nil nil nil nil nil nil $numer))

(defun condensesolnl (tempsolnl)
   (let (solnl)
	(MAPL #'(lambda (q) (or (subsetl (cdr q) (car q))
			       (setq solnl (cons (car q) solnl))))
	     (sort tempsolnl (function (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)
	       (cond ((m-subset (car l) s2) (return t))))))

(defun m-subset (s1 s2)
       (do ((s s1 (cdr s)))
	   ((null s) t)
	   (cond ((not (memalike (car s) s2)) (return nil)))))

(defun algsys (tlhslist &aux answ)
   (setq answ    (condensesolnl (apply (function append)
			     (mapcar (function algsys0)
				     (distrep (mapcar (function lofactors)
						      tlhslist))))))
;     (displa  (cons '(mlist)  (sloop 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 &aux answ)
       ((lambda (resulteq vartorid nlhslist)
		(setq vartorid (cdr resulteq)
		      resulteq (car resulteq)
		      nlhslist (mapcar (function (lambda (q)
					  (cond ((among vartorid q)
						 (presultant q resulteq
							     vartorid))
						(t q))))
				       (delet resulteq tlhslist)))
		(setq answ (bakalevel (algsys nlhslist) tlhslist vartorid))
		
		answ)
	(findleastvar tlhslist) nil nil))
       




(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)))
		   (lessp (cabs (cadr rr))
			  (times $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 (function roundrhs) lsoln))

(defun bbsorteqns (l) (sort (copy-top-level l) 'ORDERLESSP))

(defun putorder (tempvarl) 
     (do ((n 1 (f1+ n))
	  (tempvarl tempvarl (cdr tempvarl)))
	 ((null tempvarl) nil)
	 (putprop (car tempvarl) n 'VARORDER)))

(defun remorder (gvarl)
       (mapc (function (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 (function (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 (f+ (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 ((lambda (x) (and x
				  (not (> (cdr x) (cadr poly)))
				  (setq *vardegs* (zl-DELETE x *vardegs*))))
		 (assq (car poly) *vardegs*))
		(do ((poly (cdr poly) (cddr poly))
		     (tdeg 0 (max tdeg (f+ (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 (pterm (cdr poly) 0))))
	     (t (getvardegs (pterm (cdr poly) 0)))))

(declare-top(unspecial *vardegs*))

(defun pconstp (poly)
       (or (atom poly) (not (memq (car poly) *tvarxlist*))))

(defun pfreeofmainvarsp (poly)
       (cond ((atom poly) poly)
	     ((null (memq (car poly) *tvarxlist*))
	      ($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 ((memq nil listofl) nil)
	     (t (combiney1 (zl-DELETE '(0) listofl)))))

(defun combiney1 (listofl)
       (cond ((null listofl) (list nil))
	     (t (mapcan #'(lambda (r)
				  (cond ((intersect (car listofl) r) (list r))
					(t (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))
			 (quotient (plus 0.0 (car rr)) (cdr rr)))))

(defun memberroot (a x eps)
       (cond ((null x) nil)
	     ((lessp (abs (difference a (car x)))
		     (quotient (plus 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)
       (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)
	     ((freeof '$%I (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 (delq 'T (mapcar #'(lambda (q)
					     (car (merrset (baksubst1 solnl q))))
				   lhsl)))		;catches arith. ovfl
       (cond ((memq nil lhsl) (list nil))
	     (t 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)
       ((lambda (p)
		(or (numberp p) (eq (pdis (pget (car p))) '$%i)))
	(cadr (ratf ($ratsimp p)))))

(defun bakalevel (solnl lhsl var)
  ;(apply #'append (mapcar #'(lambda (q) (bakalevel1 q lhsl var)) solnl))
  (sloop 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)))
				(zl-REMOVE (car solnl) lhsl) 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)
       ((lambda (poly var varlist genvar *roots *failures $programmode)
		(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 (function (lambda (q)

				  (callapprs (cadr (ratf (meqhk q))))))

					      (deletmult *failures))
				      (mapcar (function list)
					      (cond ($realonly
						     (realonly (deletmult *roots)))
						    (t (deletmult *roots))))))))
	     (t (callapprs poly))))
	(car pv) (cdr pv) varlist genvar nil nil t))

(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 "ALGSYS cannot solve - system too complicated."))
       (let ($rootsquad $dispflag)
	    (cond ($realonly
		   (mapcar (function (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)
			 (merror
			  "ALGSYS cannot solve - system too complicated."))
			((pconstp (car tlhsl)) (return nil))
			((punivarp (car tlhsl))
			 (return (bakalevel (callapprs (car tlhsl))
					    lhsl nil))))))))

(defun tayapprox (p)
       (cons '(MPLUS)
	     (mapcar (function (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)
  ((lambda (result)
    (MAPL #'(lambda (q) (or (memalike (car q) (cdr q))
			  (setq result (cons (car q) result))))
	 l)
    result)
   nil))

(defun condensesublist (lol) (mapcar #'condensey lol))

(defun exclude (l1 l2) 
       (cond ((null l2) nil)
	     ((zl-MEMBER (car l2) l1) (exclude l1 (cdr l2)))
	     (t ;(append (list (car l2)) (exclude l1 (cdr l2)))
		(cons (car l2) (exclude l1 (cdr l2)))
		)))