For Programmers: Free Programming Magazines  


Home > Archive > Scheme > June 2007 > Re: Is there a faster way for calculating coefficients for Maclaurin









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Is there a faster way for calculating coefficients for Maclaurin
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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com