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)
50 ; |__>-------| >o--------+
57 ; [0 in] [1 mid] [2 mid] [3 mid] [4 mid] [5 out]
59 ; [hor 0 1] [not1 1 2] [hor 2 3] [ver 3 4] [hor 4 5]
61 ; [x0 y0 x1 y1 x2 y2 x3 y3 x4 y4 x5 y5 -const]
62 ; [ 1 0 0 0 0 0 0 0 0 0 0 0 (- inport-x)] ; [0 in]
63 ; [ 0 0 0 0 0 0 0 0 0 0 1 0 (- outport-x)] ; [5 out]
64 ; [ 0 1 0 -1 0 0 0 0 0 0 0 0 0] ; [hor 0 1]
65 ; [ 0 0 -1 0 1 0 0 0 0 0 0 0 not1-width] ; [not1 1 2]
66 ; [ 0 0 0 1 0 -1 0 0 0 0 0 0 0] ; [not1 1 2]
67 ; [ 0 0 0 0 0 1 0 -1 0 0 0 0 0] ; [hor 2 3]
68 ; [ 0 0 0 0 0 0 1 0 -1 0 0 0 0] ; [ver 3 4]
69 ; [ 0 0 0 0 0 0 0 0 0 1 0 -1 0] ; [hor 4 5]
72 {'+ +, '- -, '* *, '/ /,
77 (defn neval [expr arg env]
78 (cond (isa? (class expr) Number) expr
80 (symbol? expr) (env expr)
81 :else (apply (f-table (first expr))
82 (map #(neval % arg env) (rest expr))
86 (map #(apply + (map * % v)) m))
89 (map (fn [v] (map #(* s %) v))
101 (cons (cons 1 (take (count acc) (repeat 0)))
102 (map #(cons 0 %) acc)
107 (defn jaco [phys xis env]
109 xis+ (map #(assoc xis %1 (+ (nth xis %1) (/ *dxi* 2)))
111 xis- (map #(assoc xis %1 (- (nth xis %1) (/ *dxi* 2)))
112 (range (count xis)))]
113 (map (fn [phy] (map #(/ (- (neval phy (cons '_ %1) env)
114 (neval phy (cons '_ %2) env))
120 ; x^i(t+h) = x^i(t) + f^i(x(t+h))h ; implicit Eular method
121 ; x^i(t+h) = x^i(t) + f^i(x(t) + x(t+h) - x(t))h
122 ; 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
123 ; x~i(t+h) - h*df^i(x(t))/dx^j*x^j(t+h)
124 ; = x^i(t) + f^i(x(t))h - h*df^i(x(t))/dx^j*x^j(t)
127 (defn next-iter [phys xis env]
128 (let [j (jaco phys xis env)
129 lhs (m-m (i-mat (count xis))
131 rhs (map #(+ %1 %2 (- %3))
133 (map #(* *h* (neval % (cons '_ xis) env)) phys)
134 (m*v (s*m *h* j) xis))
135 [solved varswap] (lin-solve (map #(conj %1 %2)
137 (reduce #(swap (nth %2 0) (nth %2 1) %1)
138 (map #(nth % (count xis)) solved)
143 (cond (isa? (class x) Number) 'num
148 (defn stype-order [x]
154 (get order (stype x))
157 ; symbolic addtion 2 terms
159 (let [[t1 t2] [(stype x0) (stype x1)]]
160 (cond (= [t1 t2] '[+ + ]) (concat ['+] (rest x0) (rest x1))
161 (= [t1 t2] '[symb + ]) (concat ['+ x0] (rest x1))
162 (= t2 + ) ['+ (s+2 x0 (first x1)) (rest x1)]
163 (= [t1 t2] '[num num ]) (+ x0 x1)
164 (= [t1 t2] '[up up ]) (cons 'up (map + (rest x0) (rest x1)))
165 (= [t1 t2] '[down down]) (cons 'down (map + (rest x0) (rest x1)))
170 (letfn [(rec [x & xs]
173 (s+2 x (apply rec xs))
175 (apply rec (sort-by stype-order args))