| Jens Axel Søgaard 2007-06-08, 4:19 am |
| narutocanada@yahoo.ca skrev:
> Is there a faster way for calculating coefficients for Maclaurin
> series?
> I've coded a polynomial library in scheme and a Maclaurin series
> function,
> but it's slow for high degree (six or higher is slow).
> Is there a faster way?
Since how you have implemented the various helpers, it is hard
to say where the culprit is.
I notice that you subsitute 0 for x in P in order to get
the constant term:
(rat-poly-subs p 0)
Unless you have special cased 0 in rat-poly-subs this
might be slow. Consider having a constant-term operation.
In the loop you repeatedly calculate (factorial d).
Is your factorial memoizing?
Have you tried a loop (It's not a pretty, but it gives
you a chance to reuse the results from one term when
calculating the next term)?
Let i run from 0 to n, where n is the last exponent
(let loop ([i 0]
[m 0] ; Maclaurin series from term 0 to term i
[f 1] ; i!
[d P]) ; coeffecients of P differentiated i times
(if (> i n)
m
(loop (+ i 1)
(poly-add m (make-term (/ (constant-term d) i) 'X))
(* f i) ; or (f (+ i 1))
(differentiate d)))
Instead of using an expensive differentiate each time,
one could keep the list of coeffecients of d and simply
cdr through them.
/Jens Axel
|