
( (create-solver-call $odeargs $constraints
                      $res) 
                                            
    (#)
    (preprocess $odeargs $constraints
                ($soln-method $fnames $ivar $endpt $num-steps $eqns)) (!)
    (#) (analyze-iconds $fnames $ivar $eqns
                        $iconds $rest-eqns)        (!)
        (analyze-eqns $fnames $ivar $rest-eqns
                      $derivs $alg) 
;        (validity-check $iconds $derivs $alg)
    (#) (make-deriv-mat $fnames $derivs $alg
                        $dmat $omap $alg-f) (!)
    (#) (perform-substitutions $ivar $omap $dmat
                               $new-dmat) (!)
        (check-no-fail $new-dmat)
        (length $omap $num-func)
        (length $fnames $num-func)
        (length $alg $nalg)
        (make-ipoint $iconds $ipoint) 
        (make-ic-mat $iconds $alg-f $icmat)
        (make-sol-mat $fnames $omap $sol-mat)
        (make-solver-call 
           $soln-method $icmat $endpt $num-steps $ivar $new-dmat $nalg
           $solver-call)
        (postprocess (let* ((-ipoint $ipoint)
                            (-sol $solver-call))
                          $sol-mat)
                     $res)
)


( (preprocess ($meth-num 
                $interp-num 
                (-mkMatrix $r $c | $fnames) $ivar $endpt | $opt) $constraints
              ($meth $fnames $ivar $endpt $num-steps $eqns))
    (lookup-alist $meth-num ((0 rkfixed) (1 Rkadapt) (2 -radau))
                  $meth)
    (preprocess-constraints $constraints $eqns)
    (make-num-steps $endpt $opt
                    $num-steps $rest-opt) )


( (preprocess-constraints ((-lambda (-dummy) (-sub $a $b)) | $r)
                          ((-EQUALS $a $b) | $s))
    (preprocess-constraints $r $s) )

( (preprocess-constraints ((-sub $a $b) | $r)
                          ((-EQUALS $a $b) | $s))
    (preprocess-constraints $r $s) )

( (preprocess-constraints () ()) )


( (analyze-iconds $fnames $ivar ($eq | $eqns)
                  (($f $order $point $val) | $iconds) $rest)
    (analyze-icond $fnames $ivar $eq ($f $order $point $val))
    (analyze-iconds $fnames $ivar $eqns
                    $iconds $rest) )

( (analyze-iconds $fnames $ivar ($eq | $eqns)
                  $iconds ($eq | $rest))
    (analyze-iconds $fnames $ivar $eqns
                    $iconds $rest) )

( (analyze-iconds $fnames $ivar ()
                  () ()) )
    

( (analyze-eqns $fnames $ivar ($eq | $eqns)
                (($f $order $expr) | $derivs) $alg)
    (analyze-eq $fnames $ivar $eq ($f $order $expr))
    (analyze-eqns $fnames $ivar $eqns
                  $derivs $alg) )

; For now, anything which isn't a differential equation
; or an initial condition will be considered an algebraic
; constraint.
( (analyze-eqns $fnames $ivar ($eq | $eqns)
                $derivs ($alg-rhs | $alg))
    (#) (analyze-alg $eq $alg-rhs) (!)
    (analyze-eqns $fnames $ivar $eqns
                  $derivs $alg) )

( (analyze-eqns $fnames $ivar ()
                () ()) )


; This worked, but it turned out to be much more efficient
; to first extract the initial conditions...

; ( (analyze-block $fnames $ivar ($eq | $eqns)
;   $iconds (($f $order $expr) | $derivs) $alg)
;     (analyze-eq $fnames $ivar $eq ($f $order $expr))
;     (analyze-block $fnames $ivar $eqns
;                    $iconds $derivs $alg) )
; 
; 
; ( (analyze-block $fnames $ivar ($eq | $eqns)
;   (($f $order $point $val) | $iconds) $derivs $alg)
;     (#) (analyze-icond $fnames $ivar $eq ($f $order $point $val)) (!)
;     (analyze-block $fnames $ivar $eqns
;                    $iconds $derivs $alg) )
; 
; ; For now, anything which isn't a differential equation
; ; or an initial condition will be considered an algebraic
; ; constraint.
; ( (analyze-block $fnames $ivar ($eq | $eqns)
;   $iconds $derivs ($alg-rhs | $alg))
;     (#) (analyze-alg $eq $alg-rhs) (!)
;     (analyze-block $fnames $ivar $eqns
;                    $iconds $derivs $alg) )
; 
; ( (analyze-block $fnames $ivar ()
;                  () () ()) )
 
( (analyze-icond ($f | $fnames) $ivar $eq ($f $order $point $val))
    (analyze-icond-1 $f $ivar $eq ($f $order $point $val)) )

( (analyze-icond ($f | $fnames) $ivar $eq ($g $order $point $val))
    (analyze-icond $fnames $ivar $eq ($g $order $point $val)) )

( (analyze-icond-1 $fname $ivar (-EQUALS $a $val) ($fname $order $point $val))
    (deriv ($fname $ivar) $order $point $a)
    (valid-icond-point $ivar $point) )

( (valid-icond-point $ivar ($x | $r)) )

( (valid-icond-point $ivar $pt)
    (!= $ivar $pt) )

( (analyze-eq ($f | $fnames) $ivar $eq ($f $order $expr))
    (analyze-eq-1 ($f $ivar) $eq ($f $order $expr)) )

( (analyze-eq ($f | $fnames) $ivar $eq ($g $order $expr))
    (analyze-eq $fnames $ivar $eq ($g $order $expr)) )

( (analyze-eq-1 ($fname $ivar) $eq ($fname $order $expr))
    (#)
    (order ($fname $ivar) $eq $order)
    (> $order 0)
    (deriv ($fname $ivar) $order $ivar $d)
    (solve $d $eq $expr) 
    (!)
)
    
( (analyze-alg (-EQUALS $a $b) (-sub $a $b)) )


( (validity-check $iconds $derivs $alg)
    (length $iconds $n)
    (length $alg $nalg)
    (dsum $derivs $ds)
    (+ $n $nalg $ds) )

( (dsum (($f $o $e) | $derivs) $res)
    (dsum $derivs $s)
    (+ $o $s $res) )

( (dsum () 0) )


( (make-ic-mat $iconds $alg-f (-mkMatrix $r 1 | $icexpr))
    (sort-lists-2 $iconds $ics)
    (move-to-end $alg-f $ics $ics1)
    (lists-nth 3 $ics1 $icexpr)
    (length $icexpr $r) )

( (mov-e-1 $f (($f $o $p $e) | $r)
           $res)
    (append ($r (($f $o $p $e))) $res) )

( (mov-e-1 $f (($g $o $p $e) | $r)
           (($g $o $p $e) | $r1))
    (mov-e-1 $f $r $r1) )

( (mov-e-1 $f ()
           ()) )

( (move-to-end ($f | $r) $ic
               $res)
    (mov-e-1 $f $ic $ic1)
    (move-to-end $r $ic1 $res) )

( (move-to-end () $ic
               $ic) )


( (make-ipoint $iconds 
               (if (and | $tests) 
                   $pt 
                   (-mkError "ODEInconsistentInitCond")))
    (lists-nth 2 $iconds ($pt | $pts))
    (make-eq-tests $pt $pts $tests) )

( (make-eq-tests $pt ($x | $r) ((-EQUALS $pt $x) | $s))
    (make-eq-tests $pt $r $s) )
    
( (make-eq-tests $pt () ()) )

( (make-num-steps $endpt () 
                  (trunc (-mult 10 (-abs (-sub $endpt -ipoint)))) ()) )

( (make-num-steps $endpt ($num-steps | $rest)
                  $num-steps $rest) )


; Make the matrix of derivatives and algebraic constraints.
; Also return an assoc-list mapping each function to
; its position in the solution vector.
; The function names are needed because there may be
; algebraic constraints.
( (make-deriv-mat $fnames $derivs $alg
                  (-mkMatrix $r 1 | $all-expr) $omap $alg-f)
    (#) (sort-lists-2 $derivs $ds) (!)
    (#) (divide-nth 0 $ds $blocks) (!)
    (#) (fill-blocks 0 $blocks $blocks-filled) (!)
    (sort $fnames $fnames-sorted) 
    (make-omap 0 $fnames-sorted $blocks-filled $omap () $alg-f)
    (append $blocks-filled $dsf)
    (lists-nth 2 $dsf $dexpr)
    (length $dexpr $ld)
    (append ($dexpr $alg) $all-expr)
    (length $all-expr $r) 
)


( (fill-blocks $offset ($b | $r) ($b1 | $r1))
    (fill-block $offset 1 $b $b1)
    (length $b1 $len)
    (+ $offset $len $new-offset)
    (fill-blocks $new-offset $r $r1) )

( (fill-blocks $offset () ()) )


( (fill-block $offset $n ()
              ()) )

( (fill-block $offset $n (($f $n $expr) | $derivs)
              (($f $n $expr) | $res))
    (+ 1 $n $m)
    (fill-block $offset $m $derivs $res) )

( (fill-block $offset $n (($f $n1 $e1) | $derivs)
              (($f $n $e) | $res))
    (> $n1 $n)
    (+ $offset $n $noff)
    (make-dmat-entry $f $noff $e)
    (+ 1 $n $m)
    (fill-block $offset $m (($f $n1 $e1) | $derivs) $res) )


( (make-omap $n ($f | $fnames) ($b | $blocks)
                (($f ($n $len)) | $res) () $alg-f)
    (length $b $len)
    (+ $n $len $m)
    (bfunc $b $f)
    (make-omap $m $fnames $blocks
               $res () $alg-f) )

( (make-omap $n ($g | $fnames) ($b | $blocks)
                $res () $alg-f)
    (append ($fnames ($g)) $nfnames)
    (make-omap $n $nfnames ($b | $blocks)
               $res () $alg-f) )

( (make-omap $n () ()
             () () ()) )

( (make-omap $n ($f | $fnames) ()
             $res () ($f | $fnames))
    (make-omap $n () ()
               $res ($f | $fnames) ()) )

; This clause handles algebraic constraints... they're
; all that should be left at this point
( (make-omap $n () ()
             (($f ($n 1)) | $res) ($f | $alg) ())
    (+ $n 1 $m)
    (make-omap $m () ()
               $res $alg ()) )



( (bfunc (($f | $r) | $r1) $f) )

( (make-dmat-entry $f $n (-SUBSCRIPT y ($n))) )


( (perform-substitutions $ivar $omap $d
                         (-SUBSCRIPT y ($m)))
    (#) (deriv ($fname $ivar) $n $ivar $d)
    (lookup-alist $fname $omap ($o $bound)) (!)
    (> $bound $n)
    (+ $o $n $m) )

( (perform-substitutions $ivar $omap $d
                         -FAIL)
    (deriv ($fname $ivar) $n $ivar $d)
    (lookup-alist $fname $omap ($o $bound)) )

( (perform-substitutions $ivar $omap ($x | $r)
                         ($x1 | $r1))
    (#) (perform-substitutions $ivar $omap $x $x1) (!)
    (#) (perform-substitutions $ivar $omap $r $r1) (!) )
    

( (perform-substitutions $ivar $omap $x
                         $x) )


( (check-no-fail ($x | $r))
    (#) (check-no-fail $x) (!)
    (#) (check-no-fail $r) (!) )

( (check-no-fail ()) )

( (check-no-fail $x)
    (!= $x -FAIL) )

( (make-sol-mat $fnames $omap (-mkMatrix $r 1 | $s))
    (length $fnames $r)
    (make-sol-entries $fnames $omap $s) )
  

( (make-sol-entries ($f | $fnames) $omap ($s | $r))
    (make-interp $f $omap $s)
    (make-sol-entries $fnames $omap $r) )

( (make-sol-entries () $omap ()) )

( (make-interp $f $omap
               (let* ((c0 (-colref -sol 0))
                      (cn (-colref -sol $n))
                      (-imat (lspline c0 cn)))
                 (-lambda (x) (interp -imat c0 cn x))))
    (lookup-alist $f $omap ($o $_))
    (+ $o 1 $n) )


; For the -radau solver, we ignore the number of steps, but
; do permit the number of algebraic constraints to be nonzero
( (make-solver-call -radau $icmat $endpt $numsteps $ivar $mat $nalg
                    (-radau $icmat
                            -ipoint
                            $endpt
                            0
                            $nalg
                            (-lambda ($ivar y) $mat)
                            TOL
                            (trunc (-mult 10000 (-abs (-sub $endpt -ipoint))))
                            TOL)) )
                            

; For any other solver, the number of algebraic constraints must
; be zero.
( (make-solver-call $soln-method $icmat $endpt $numsteps $ivar $mat 0
                    ($soln-method $icmat
                                  -ipoint
                                  $endpt
                                  $numsteps
                                  (-lambda ($ivar y) $mat))) )

; If the number of algebraic constraints isn't zero, and we're
; not using the -radau method, report an error
( (make-solver-call $soln-method $icmat $endpt $numsteps $ivar $mat $nalg
                    (-mkError "ODETooManyAlg")) )


( (deriv ($fname $ivar) 0 $pt
  ($fname $pt)) )

( (deriv ($fname $ivar) 1 $pt
  (-DERIVATIVE (-lambda ($ivar) ($fname $ivar)) $pt)) )

( (deriv ($fname $ivar) $n $pt
  (NthDerivative (-lambda ($ivar) ($fname $ivar)) $n $pt))
    (> $n 1) )

( (deriv ($fname $ivar) $n $pt 
  ($fprime $pt))
    (count-primes $fprime $n) 
    (strip-primes $fprime $fname) )

( (count-primes $fprime $n)
    (strcat $f1 "'" $fprime)
    (count-primes $f1 $m)
    (+ $m 1 $n) )

( (count-primes $f 0) )

( (strip-primes $fprime $fname)
    (strcat $f1 "'" $fprime)
    (strip-primes $f1 $fname) )

( (strip-primes $f $f) )


( (order ($fname $ivar) $d $n)
    (#)
    (order-i ($fname $ivar) $d $n)
    (!) )

( (order-i ($fname $ivar) $d $n)
    (deriv ($fname $ivar) $n $ivar $d) )

( (order-i ($fname $ivar) ($a | $b) $n)
    (order ($fname $ivar) $a $oa)
    (order ($fname $ivar) $b $ob)
    (max ($oa $ob) $n) )

( (order-i $e $x -1) )


( (max ($a $b) $a)
    (> $a $b) )

( (max ($a $b) $b) )

; Solve for a subexpression, given an equality

( (solve $e (-EQUALS $a $b) $res)
    (solve $e $a $b $res) )

; Given that two expressions are equal, solve
; for a subexpression of one of them.

( (solve $v $v $rhs $rhs) )

( (solve $v $w $rhs $rhs)
    (deriv ($f $ivar) $n $pt $v)
    (deriv ($f $ivar) $n $pt $w) )

( (solve $v (-plus $a $b) $rhs $res)
    (solve $v $a (-sub $rhs $b) $res) )
( (solve $v (-plus $a $b) $rhs $res)
    (solve $v $b (-sub $rhs $a) $res) )

( (solve $v (-sub $a $b) $rhs $res)
    (solve $v $a (-plus $rhs $b) $res) )
( (solve $v (-sub $a $b) $rhs $res)
    (solve $v $b (-sub $a $rhs) $res) )

( (solve $v (-mult $a $b) $rhs $res)
    (solve $v $a (-div $rhs $b) $res) )
( (solve $v (-mult $a $b) $rhs $res)
    (solve $v $b (-div $rhs $a) $res) )

( (solve $v (-div $a $b) $rhs $res)
    (solve $v $a (-mult $rhs $b) $res) )
( (solve $v (-div $a $b) $rhs $res)
    (solve $v $b (-div $a $rhs) $res) )

( (solve $v (-NEG $a) $rhs $res)
    (solve $v $a (-NEG $rhs) $res) )

; Sorting

( (sort ($x | $r) $res)
    (#) 
    (split $x ($x | $r) $less $equal $greater)
    (sort $less $a)
    (sort $greater $b)
    (append ($a $equal $b) $res) 
    (!) )

( (sort () ()) )


( (split $x ($y | $r) ($y | $less) $equal $greater)
    (> $x $y)
    (split $x $r $less $equal $greater) )

( (split $x ($y | $r) $less $equal ($y | $greater))
    (> $y $x)
    (split $x $r $less $equal $greater) )

( (split $x ($y | $r) $less ($y | $equal) $greater)
    (split $x $r $less $equal $greater) )

( (split $x () () () ()) )


; Sort a list of lists lexicographically based on
; the first two elements of each list

( (sort-lists-2 ($x | $r) $res)
    (split-lists-2 $x ($x | $r) $less $equal $greater)
    (sort-lists-2 $less $a)
    (sort-lists-2 $greater $b)
    (append ($a $equal $b) $res) )

( (sort-lists-2 () ()) )


( (split-lists-2 $x ($y | $r) ($y | $less) $equal $greater)
    (greater-lists-2 $x $y)
    (split-lists-2 $x $r $less $equal $greater) )

( (split-lists-2 $x ($y | $r) $less $equal ($y | $greater))
    (greater-lists-2 $y $x)
    (split-lists-2 $x $r $less $equal $greater) )

( (split-lists-2 $x ($y | $r) $less ($y | $equal) $greater)
    (split-lists-2 $x $r $less $equal $greater) )

( (split-lists-2 $x () () () ()) )

( (greater-lists-2 ($x $y1 | $r1) ($x $y2 | $r2) )
    (> $y1 $y2) )

( (greater-lists-2 ($x1 $y1 | $r1) ($x2 $y2 | $r2) )
    (> $x1 $x2) )



( (append2 ($x | $r) $l ($x | $s))
    (append2 $r $l $s) )

( (append2 () $l $l) )

( (append ($l | $r) $res)
    (append $r $r1)
    (append2 $l $r1 $res) )

( (append () ()) )


( (nth 0 ($x | $r) $x) )

( (nth $n ($x | $r) $y) 
    (+ $m 1 $n)
    (nth $m $r $y) )
    
; Extract the $n-th element from each list in a list of lists
; and return the result
( (lists-nth $n ($l | $lists) ($x | $r))
    (nth $n $l $x)
    (lists-nth $n $lists $r) )
    
( (lists-nth $n () ()) )

( (length () 0) )
( (length ($x | $r) $n)
    (length $r $m)
    (+ $m 1 $n) )


( (lookup-alist $f (($f $o) | $r) $o) )

( (lookup-alist $f ($pair | $r) $o)
    (lookup-alist $f $r $o) ) 

( (zip ($x | $r) ($y | $s) (($x $y) | $rs))
    (zip $r $s $rs) )

( (zip () () ()) )


; Reverse a list
( (reverse $x $y)
    (r-help $x $x () $y $y ()) )

( (r-help $a ($x | $r) $t1 $b ($y | $s) $t2) 
     (r-help $a $r ($x | $t1) $b $s ($y | $t2)) )
( (r-help $a () $b $b () $a) )
    

; Divide a list of lists according to the nth
; element of each list
( (divide-nth $n ($l | $s)
              $res)
    (nth $n $l $x)
    (divide-nth-i $x $n ($l | $s)
                  $res) )

( (divide-nth-i $x $n ($l | $s) 
                $res1)
    (nth $n $l $x)
    (divide-nth-i $x $n $s
                  $res)
    (push-into-1st-block $l $res
                         $res1) )

( (divide-nth-i $x $n ($l | $s) 
                $res1)
    (nth $n $l $y)
    (divide-nth-i $y $n ($l | $s)
                  $res)
    (push-empty-block $res
                      $res1) )

( (divide-nth-i $x $n ()
                (())) )                    

( (push-into-1st-block $l ($ll | $s)
                       (($l | $ll) | $s)) )

( (push-empty-block $s
                    (() | $s)) )

; Postprocessing transforms standard Scheme constructs
; like let*, if, and, or, and so forth, into the
; idiosyncratic syntax required by the Mathcad compute
; engine.
;

; ( (postprocess $x $x) )

( (postprocess (if $cond $then $else) (-ifblock (($then1 $cond1)) $else1))
    (postprocess $cond $cond1)
    (postprocess $then $then1)
    (postprocess $else $else1) )

( (postprocess (or | $exprs) (-ifblock $pairs 0))
    (postprocess $exprs $exprs1)
    (zip $exprs1 $exprs1 $pairs) )

( (postprocess (and $a) $a1)
    (postprocess $a $a1) )

( (postprocess (and $a | $exprs) $res)
    (postprocess (if $a (and | $exprs) 0) $res) )

( (postprocess (let* ($b | $bindings) $body)
               (-let ($b1) $expr) )
    (postprocess $b $b1)
    (postprocess (let* $bindings $body) $expr) )

( (postprocess (let* () $body)
               $expr)
    (postprocess $body $expr) )
               
( (postprocess (-mkMatrix | $r) $res)
    (postprocess $r $s)
    (unit-protect $s $s1)
    (unit-protect-1 (-mkMatrix | $s1) $res) )

( (postprocess ($x | $r) ($x1 | $r1))
    (postprocess $x $x1)
    (postprocess $r $r1) )

( (postprocess $x $x) )

( (unit-protect ($x | $r) ($y | $s))
    (unit-protect-1 $x $y)
    (unit-protect $r $s) )

( (unit-protect () ()) )

( (unit-protect-1 $x (-GetNth (-mkListOfUV $x) 0)) )


( (id $x $x) )


; ( (test $fnames $ivar $eqns
;         $omap $dmat $new-dmat)
;     (analyze-block $fnames $ivar $eqns
;                    $iconds $derivs $alg) 
;     (#) (make-deriv-mat $fnames $derivs $alg
;                         $dmat $omap $alg-f) (!)
;     (perform-substitutions $ivar $omap $dmat
;                            $new-dmat) )
; 
( (test2 $fnames $ivar $eqns
         $blocks $blocks-filled $omap $alg-f $new-dmat)
    (#) (analyze-iconds $fnames $ivar $eqns
                        $iconds $rest-eqns)        (!)
        (analyze-eqns $fnames $ivar $rest-eqns
                      $derivs $alg) 

    (validity-check $iconds $derivs $alg)

    (#) (sort-lists-2 $derivs $ds) (!)
    (#) (divide-nth 0 $ds $blocks) (!)
    (#) (fill-blocks 0 $blocks $blocks-filled) (!)
    (#) (sort $fnames $fnames-sorted) (!)
    (#) (make-omap 0 $fnames-sorted $blocks-filled $omap () $alg-f) (!)
    (#) (append $blocks-filled $dsf)
    (lists-nth 2 $dsf $dexpr)
    (length $dexpr $ld)
    (append ($dexpr $alg) $all-expr)
    (length $all-expr $r) (!)

    (#) (perform-substitutions $ivar $omap (-mkMatrix $r 1 | $all-expr)
                               $new-dmat) (!)
    (check-no-fail $new-dmat) )


--
