;;; -*-  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 1980 Massachusetts Institute of Technology         ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(in-package "MAXIMA")
(macsyma-module polyrz)

(declare-top(special errrjfflag $programmode varlist 
		     $ratepsilon $ratprint $factorflag genvar
		     equations $keepfloat $ratfac $rootsepsilon
		     $multiplicities))

(declare-top(genprefix a_5))

(load-macsyma-macros ratmac)


;;	PACKAGE FOR FINDING REAL ZEROS OF UNIVARIATE POLYNOMIALS
;;	WITH INTEGER COEFFICIENTS USING STURM SEQUENCES.

;; Better programming technology.  To be installed with new argument
;; checking scheme.
;; (DEFMFUN $REALROOTS (EXP (EPS $ROOTSEPSILON))
;;        (SETQ EXP (MEQHK EXP))
;;        (IF ($RATP EXP) (SETQ EXP ($RATDISREP EXP)))
;;        (COND ((OR (NOT (MNUMP EPS)) (MNEGP EPS) (EQUAL EPS 0))
;; 	      (DISPLA EPS)
;; 	      (MERROR "Second argument must be a positive number - `realroots'")))
;;        (LET (($KEEPFLOAT NIL)) (STURMSEQ EXP EPS)))

(defmfun $realroots n
  (let ((exp nil) (eps nil)) 
    (cond ((= n 1) (setq eps $rootsepsilon))
	  ((= n 2) (setq eps (arg 2)))
	  (t (merror "Wrong number of arguments - `realroots'")))
    (setq exp (meqhk (arg 1)))
    (if ($ratp exp) (setq exp ($ratdisrep exp)))
    (cond ((or (not (mnump eps)) (mnegp eps) (equal eps 0))
	   (merror "Second argument to `realroots' was not a~
		      positive number: ~M" eps)))
    (let (($keepfloat nil)) (sturmseq exp eps))))

(defun unipoly (exp) 
  (setq exp (cadr (ratf exp)))
  (cond ((and (not (atom exp))
	      (loop for v in (cdr exp)
		     when (not (atom v))

		     do (return nil)
		     finally (return t)))
	 ;;(EVERY #'ATOM (CDR EXP)))
	 exp)
	(t (merror "Argument must be a univariate polynomial"))))

(defun makrat (pt)
  (cond ((floatp pt) (maxima-rationalize pt))
	((numberp pt) (cons pt 1))
	(($bfloatp pt) (bigfloat2rat pt))
	((atom pt) (merror "~M Non-numeric argument" pt))
	((equal (caar pt) 'rat) (cons (cadr pt) (caddr pt)))
	(t (merror "~M Non-numeric argument" pt))))

(declare-top(special equations))

(defun sturmseq (exp eps)
  (let (varlist equations $factorflag $ratprint $ratfac)
    (cond ($programmode
	   (cons '(mlist)
		 (multout (findroots (psqfr (pabs (unipoly exp)))
				     (makrat eps)))))
	  (t (solve2 (findroots (psqfr (pabs (unipoly exp)))
				(makrat eps)))
	     (cons '(mlist) equations)))))

(declare-top(unspecial equations))

(defmfun sturm1 (poly eps &aux b llist)
  (setq b (cons (root-bound (cdr poly)) 1))
  (setq llist (isolat poly (cons (minus (car b)) (cdr b)) b))
  (mapcar #'(lambda (int) (refine poly (car int) (cdr int) eps)) llist))

(defun root-bound (p)
  (prog (n lcf loglcf coef logb)
     (setq n (car p))
     (setq lcf (abs (cadr p)))
     (setq loglcf (f1- (log2 lcf)))
     (setq logb 1)
     loop (cond ((null (setq p (cddr p))) (return (expt 2 logb)))
		((lessp (setq coef (abs (cadr p))) lcf) (go loop)) )
     (setq logb (max logb (f+ 1 (ceil (f- (log2 coef) loglcf)
				      (f- n (car p)) )))) 
     (go loop) ))

(defun ceil (a b) (plus (quotient a b)	;CEILING FOR POS A,B
			(signum (remainder a b))))

(defun sturmapc (fn llist multiplicity)
  (cond ((null llist) nil)
	(t  (cons (funcall fn (car llist))
		  (cons  multiplicity 
			 (sturmapc fn (cdr llist) multiplicity)))) ))

(defun findroots (l eps)
  (cond ((null l) nil)
	((numberp (car l)) (findroots (cddr l) eps))
	(t (append (sturmapc 'sturmout (sturm1 (car l) eps)(cadr l))
		   (findroots (cddr l) eps) )) ))

(defun sturmout (int)
  (list '(mequal simp) (car varlist)
	(midout (rhalf (rplus* (car int) (cadr int)))) ))

(defun midout (pt)
  (cond ((equal (cdr pt) 1) (car pt))
	($float (fpcofrat1 (car pt) (cdr pt)))
	(t (list '(rat simp) (car pt) (cdr pt))) ))

(defun uprimitive (p) (pquotient p (ucontent p))) ;PRIMITIVE UNIVAR. POLY

(defun sturm (p)
  (prog (p1 p2 seq r)
     (setq p1 (uprimitive  p))
     (setq p2 (uprimitive (pderivative p1 (car p1))))
     (setq seq (list p2 p1))
     a    (setq r (prem p1 (pabs p2)))
     (cond ((pzerop r) (return (reverse seq))))
     (setq p1 p2)
     (setq p2 (pminus (uprimitive r)))
     (push p2 seq)
     (go a) ))

(defun signum(x)
  (cond ((zerop x) 0)
	((minusp x) -1)
	(t 1)))

;;	IVAR COUNTS SIGN CHANGES IN A STURM SEQUENCE

(defun ivar (seq pt)
  (prog (v s ls)
     (setq v 0)
     (setq ls 0)
     a    (cond ((null seq)(return v)))
     (setq s (reval (car seq) pt))
     (setq seq (cdr seq))
     (cond ((minusp (times s  ls))(setq v (add1 v)))
	   ((not (zerop ls))(go a)))
     (setq ls s)
     (go a) ))

(defun ivar2 (seq pt)
  (cond ((not (atom pt)) (ivar seq pt))
	(t (setq seq (mapcar (function leadterm) seq))
	   (ivar seq (cons pt 1)) )))

;;	OUTPUT SIGN(P(R)) , R RATIONAL (A.B)

(defun reval (p r)
  (cond ((pcoefp p) (signum p))
	((zerop (car r)) (signum (pterm (cdr p) 0)))
	(t (prog (a b bi v m c)
	      (setq bi 1)
	      (setq v 0)
	      (setq p (cdr p))
	      (setq m (car p))
	      (setq a (car r))
	      (setq b (cdr r))
	      a    (cond ((equal m (car p)) (setq c (cadr p))
			  (setq p (cddr p)))
			 (t (setq c 0)))
	      (cond ((zerop m) (return (signum (plus v (times bi c))))))
	      (setq v (times a (plus v (times bi c))))
	      (setq bi (times bi b))
	      (setq m (sub1 m))
	      (go a) ))))

(defun makpoint (pt)
  (cond ((eq pt '$inf) 1)
	((eq pt '$minf) -1)
	(t (makrat ((lambda ($numer) (meval pt)) t)))))

(defmfun $nroots n
  (prog (varlist $keepfloat $ratfac l r)
     (cond ((= n 1) (setq l '$minf r '$inf))
	   ((= n 3) (setq l (arg 2) r (arg 3)))
	   (t (merror "Wrong number of arguments - `nroots'")))
     (return (nroots (unipoly (meqhk (arg 1))) (makpoint l) (makpoint r)))))

(defun nroots (p l r) (rootaddup (psqfr p) l r))

(defun rootaddup (llist l r)
  (cond ((null llist) 0)
	((numberp (car llist)) (rootaddup (cddr llist) l r))
	(t (plus (rootaddup (cddr llist) l r)
		 (times (cadr llist) (nroot1 (car llist) l r)))) ))

(defun nroot1 (p l r)
  (let ((seq (sturm p)))
    (difference (ivar2 seq l) (ivar2 seq r))))

;;	RETURNS ROOT IN INTERVAL OF FORM (A,B])

(defun isolat (p l r)
  (prog (seq lv rv mid midv tlist islist rts)
     (setq seq (sturm p))
     (setq lv (ivar seq l))
     (setq rv (ivar seq r))
     (setq tlist (setq islist nil))
     (cond ((equal lv rv) (return nil)))
     a	(cond ((greaterp (setq rts (difference lv rv)) 1)(go b))
	      ((equal rts 1)(setq islist (cons (cons l r) islist))))
     (cond ((null tlist) (return islist)))
     (setq lv (car tlist))
     (setq rv (cadr tlist))
     (setq l (caddr tlist))
     (setq r (cadddr tlist))
     (setq tlist (cddddr tlist))
     (go a)
     b	(setq mid (rhalf (rplus* l r)))
     (setq midv (ivar seq mid))
     (cond ((not (equal lv midv))
	    (setq tlist (append (list lv midv l mid) tlist))))
     (setq l mid)
     (setq lv midv)
     (go a) ))

(defun refine (p l r eps)
  (prog (sr mid smid)
     (cond ((zerop (setq sr (reval p r)))
	    (return (list r r))) )
     a	(cond ((rlessp (rdifference* r l) eps)
	       (return (list l r))) )
     (setq mid (rhalf (rplus* l r)))
     (setq smid (reval p mid))
     (cond ((zerop smid)(return (list mid mid)))
	   ((equal smid sr)(setq r mid))
	   (t (setq l mid)) )
     (go a) ))

(defun rhalf (r) (rreduce (car r) (times 2 (cdr r))))

(defun rreduce (a b) 
  (let ((g (abs (gcd a b))))
    (cons (quotient a g) (quotient b g))) )

(defun rplus* (a b)
  (cons (plus (times (car a) (cdr b))(times (car b) (cdr a)))
	(times (cdr a) (cdr b))))

(defun rdifference* (a b)
  (rplus* a (cons (minus (car b)) (cdr b))) )

(defun rlessp (a b)
  (lessp (times (car a) (cdr b))
	 (times (car b) (cdr a)) ))


;;; This next function is to do what SOLVE2 should do in programmode
(defun multout (rootlist)
  (progn
    (setq rootlist (do ((rtlst)
			(multlst)
			(lunch rootlist))
		       ((null lunch) (cons (reverse rtlst)
					   (reverse multlst)))
		     (setq rtlst (cons (car lunch) rtlst))
		     (setq multlst (cons (cadr lunch) multlst))
		     (setq lunch (cddr lunch))))
    (setq $multiplicities (cons '(mlist)  (cdr rootlist)))
    (car rootlist)))

(declare-top(unspecial equations))

