2 ;(alias 'cl 'clojure.core)
4 (defn neg-index [cnt x]
5 (if (neg? x) (+ cnt x) x))
8 (let [[i j] (map (partial neg-index (count s)) [i j])]
9 (assoc (assoc (vec s) i (nth s j))
13 (defn search-non-zero [mat pivot]
14 (let [width (count (first mat))
16 (first (for [j (range pivot (dec width))
17 i (range pivot height)
18 :when (not= 0 (nth (nth mat i) j))]
21 (defn sweep1 [mat pivot]
22 (let [denom (nth (nth mat pivot) pivot)
23 p-row (nth mat pivot)]
26 (map #(/ % denom) row)
27 (let [multiplier (/ (nth row pivot) denom)]
28 (map #(- %1 %2) row (map #(* % multiplier) p-row))
30 (iterate inc 0) mat)))
33 (let [width (count (first mat))
35 (loop [pivot 0 m (vec (map vec mat)) swap-acc ()]
36 (if (>= pivot (max (dec width) height))
38 (let [[col row] (search-non-zero mat pivot)]
42 (sweep1 (swap pivot row
43 (map (partial swap pivot col) m))
45 (cons [pivot col] swap-acc)
49 (map #(apply + (map * % v)) m))
52 (map (fn [v] (map #(* s %) v))
64 (cons (cons 1 (take (count acc) (repeat 0)))
69 ; D(d2 L o G[q]) - d1 L o G[q] = 0
70 ; (D d2 L o G[q])D G[q] = d1 L o G[q]
71 ; d0 d2 L o G[q] + (d1 d2 L o G[q])Dq + (d2 d2 L o G[q])D^2q = d1 L o G[q]
73 ; (d2 d2 L o G[q])^(-1) (d1 L o G[q] - d0 d2 L o G[q] - (d1 d2 L o G[q])Dq)
74 ; A = (d2 d2 L)^(-1) (d1 L - d0 d2 L - (d1 d2 L)I2)
76 ; (fn [x] ; x -> [up t q qdot]
77 ; (t* (inverse ((partial (partial L 2) 2) x))
78 ; (t- ((partial L 1) x)
79 ; ((partial (partial L 2) 0) x)
80 ; (t* (partial (partial L 2) 1)
85 ; 'jaco' will be rewritten more shortly by using 'deriv' function.
88 xis+ (map #(assoc xis %1 (+ (nth xis %1) (/ *dxi* 2)))
90 xis- (map #(assoc xis %1 (- (nth xis %1) (/ *dxi* 2)))
92 (apply map vector ; transpose
93 (map (fn [xis+1 xis-1]
103 ; x^i(t+h) = x^i(t) + f^i(x(t+h))h ; implicit Eular method
104 ; x^i(t+h) = x^i(t) + f^i(x(t) + x(t+h) - x(t))h
105 ; x^i(t+h) = x^i(t) + f^i(x(t))h + df^i(x(t))/dx^j * (x^j(t+h) - x^j(t)) * h
106 ; x~i(t+h) - h*df^i(x(t))/dx^j*x^j(t+h)
107 ; = x^i(t) + f^i(x(t))h - h*df^i(x(t))/dx^j*x^j(t)
110 (defn next-iter [phys xis]
111 (let [j (jaco phys xis)
112 lhs (m-m (i-mat (count xis))
114 rhs (map #(+ %1 %2 (- %3))
116 (map #(* *h* %) (phys xis))
117 (m*v (s*m *h* j) xis))
118 [solved varswap] (lin-solve (map #(conj %1 %2)
120 (reduce #(swap (nth %2 0) (nth %2 1) %1)
121 (map #(nth % (count xis)) solved)