I'm writing a numerical methods library, and I already found a use for
you macro. Here's the old way:
(defn solve
[f & params]
(let [param-map (merge
{:start 1
:target 0
:diff-method :forward
:iter-method :newton
:epsilon 0.00000001
:max-iters 100}
(apply hash-map params))
diff-method (param-map :diff-method)
iter-method (param-map :iter-method)
start (param-map :start)
target (param-map :target)
max-iters (param-map :max-iters)
epsilon (param-map :epsilon)
f-point #(- (f %) target)
iter-methods {:newton (fn[x]
(- x (/ (f-point x) ((f-prime f-
point :diff-method diff-method) x))))}
iterator (iter-methods iter-method)]
(loop [x start
iter-count 0]
(let [next-x (iterator x)]
(cond
(< (java.lang.Math/abs (- (f-point x) (f-point next-x)))
epsilon) next-x
(> iter-count max-iters) (do
(println "Maximum number of
iterations reached")
nil)
true (recur next-x (inc iter-count)))))))
And here's the new way with your macro
(defnk solve-key
[f
:start 1
:target 0
:diff-method :forward
:iter-method :newton
:epsilon 0.00000001
:max-iters 100]
(let [f-point #(- (f %) target)
iter-methods {:newton (fn[x]
(- x
(/ (f-point x)
((f-prime f-point :diff-method
diff-method) x))))}
iterator (iter-methods iter-method)]
(loop [x start
iter-count 0]
(let [next-x (iterator x)]
(cond
(< (java.lang.Math/abs (- (f-point x) (f-point next-x)))
epsilon) next-x
(> iter-count max-iters) (do
(println "Maximum number of iterations
reached")
nil)
true (recur next-x (inc iter-count)))))))
So it's paying off already!
On Jun 4, 7:02 pm, "Stephen C. Gilardi" <[email protected]> wrote:
> On Jun 4, 2009, at 6:54 PM, Sean Devlin wrote:
>
> > Gut gemacht!
>
> > Absolutely amazing Meikel. Now get some well earned sleep.
>
> > Sean
>
> I agree. It's a really beautiful piece of code, packed full of Clojure
> goodness.
>
> Nicely done!
>
> I checked it into clojure.contrib.def:
>
> http://code.google.com/p/clojure-contrib/source/detail?r=889
>
> Thanks!
>
> --Steve
>
> smime.p7s
> 3KViewDownload
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---