sicmutils
sicmutils copied to clipboard
Implement compose for rational functions, polynomials
rcf.scm has a compose function that converts to, roughly, this:
;; The following function still reads slightly mystical; this comes from rcf.scm
;; in the scmutils library. I'm sure with some staring and understanding we
;; could build a version that allows composition with any variable in the
;; left [[RationalFunction]] instance.
(defn compose
"only plugs r2 in for the principal indeterminate.
TODO we COULD do a version that composes with a different one??"
[r1 r2]
{:pre [(rational-function? r1)]}
(if-not (rational-function? r2)
(g/div (p/evaluate (bare-u r1) [r2])
(p/evaluate (bare-v r1) [r2]))
(let [nr1 (bare-u r1)
nr2 (bare-u r2)
dr1 (bare-v r1)
dr2 (bare-v r2)
dn (p/degree nr1)
dd (p/degree dr1)
narity (+ (p/arity dr1) 1)
nnr1 (p/extend 1 (p/reciprocal nr1))
ndr1 (p/extend 1 (p/reciprocal dr1))
scales [(second (p/new-variables narity)) 1]
pn (p/evaluate (p/reciprocal
(p/arg-scale nnr1 scales))
[nr2 dr2])
pd (p/evaluate (p/reciprocal
(p/arg-scale ndr1 scales))
[nr2 dr2])]
(cond (> dn dd) (g/div pn (p/mul (p/expt dr2 (- dn dd)) pd))
(< dn dd) (g/div (p/mul (p/expt dr2 (- dd dn)) pn) pd)
:else (g/div pn pd)))))
But it's a bit mystical and I don't see how it works yet. The task is to get this working, and get a similar version working for polynomials.