Hi,
I've got a simple implementation of the Jacobi-method for
approximating the solution of a linear system of equations, maybe your
interested in this.
(defn jacobi-x-step [m b x i]
"Calculate a part of the solution of a jacobi-method step"
;; 1
;; xi = --- (bi - ( sum aij * xij)
;; mij (not= i j)
(* (/ 1 (get-in m [i i])) (- (b i) (reduce + (map #(* (get-in m [i
%]) (x %)) (remove #(= i %) (range (count x))))))))
(defn jacobi-method-step
"Calculate one step of the jacobi-method given a matrix m, a vector
b, and a former solution x."
([m b]
;; first step of the jacobi method, init x with nullvector
(jacobi-method-step m b (vec (repeat (count b) 0))))
([m b x] (vec (map #(jacobi-x-step m b x %) (range (count x))))))
(defn jacobi-method
"Return a lazy sequence of solutions to the equation of
matrix m and vector b and x: M*b = x. To obtain a convergent
sequence,
m should be diagonally dominant."
[m b]
(iterate (partial jacobi-method-step m b) (jacobi-method-step m b)))
;; example:
(last (take 30
(jacobi-method
[[3 1 1]
[1 3 1]
[1 1 3]]
[11.0 10.0 8.0]
)))
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---