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]
71 (defn one-hot [tuple idx]
73 (assoc (vec (take (count (rest tuple)) (repeat 0)))
78 (map (fn [x] (if (seq? x) (recur x) 1))
81 (defn trans-t [tuple] ; transposes tuple
82 (cons ({'up 'down, 'down 'up} (first tuple))
86 {'+ +, '- -, '* *, '/ /,
88 (nth tuple (inc idx)))
94 (defn neval [expr arg env]
95 (cond (isa? (class expr) Number) expr
97 (symbol? expr) (env expr)
98 :else (apply (f-table (first expr))
99 (map #(neval % arg env) (rest expr))
103 (map #(apply + (map * % v)) m))
106 (map (fn [v] (map #(* s %) v))
114 (loop [i size acc []]
118 (cons (cons 1 (take (count acc) (repeat 0)))
119 (map #(cons 0 %) acc)
124 (defn jaco [phys xis env]
126 xis+ (map #(assoc xis %1 (+ (nth xis %1) (/ *dxi* 2)))
128 xis- (map #(assoc xis %1 (- (nth xis %1) (/ *dxi* 2)))
129 (range (count xis)))]
130 (map (fn [phy] (map #(/ (- (neval phy (cons '_ %1) env)
131 (neval phy (cons '_ %2) env))
137 ; x^i(t+h) = x^i(t) + f^i(x(t+h))h ; implicit Eular method
138 ; x^i(t+h) = x^i(t) + f^i(x(t) + x(t+h) - x(t))h
139 ; 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
140 ; x~i(t+h) - h*df^i(x(t))/dx^j*x^j(t+h)
141 ; = x^i(t) + f^i(x(t))h - h*df^i(x(t))/dx^j*x^j(t)
144 (defn next-iter [phys xis env]
145 (let [j (jaco phys xis env)
146 lhs (m-m (i-mat (count xis))
148 rhs (map #(+ %1 %2 (- %3))
150 (map #(* *h* (neval % (cons '_ xis) env)) phys)
151 (m*v (s*m *h* j) xis))
152 [solved varswap] (lin-solve (map #(conj %1 %2)
154 (reduce #(swap (nth %2 0) (nth %2 1) %1)
155 (map #(nth % (count xis)) solved)
160 (cond (isa? (class x) Number) 'num
165 (defn stype-order [x]
171 (get order (stype x))
174 ; symbolic addtion 2 terms
176 (let [[t1 t2] [(stype x0) (stype x1)]]
177 (cond (= [t1 t2] '[+ + ]) (concat ['+] (rest x0) (rest x1))
178 (= [t1 t2] '[symb + ]) (concat ['+ x0] (rest x1))
179 (= t2 + ) ['+ (s+2 x0 (first x1)) (rest x1)]
180 (= [t1 t2] '[num num ]) (+ x0 x1)
181 (= [t1 t2] '[up up ]) (cons 'up (map + (rest x0) (rest x1)))
182 (= [t1 t2] '[down down]) (cons 'down (map + (rest x0) (rest x1)))
187 (letfn [(rec [x & xs]
190 (s+2 x (apply rec xs))
192 (apply rec (sort-by stype-order args))
196 (let [[t0 t1] (map stype [x0 x1])]
197 (cond (= [t0 t1] '[num num]) (* x0 x1)
199 (or (= [t0 t1] '[up down])
200 (= [t0 t1] '[down up]))
201 (apply + (map * (rest x0) (rest x1)))
203 (= t1 'down) (cons 'down (map #(n*2 x0 %) (rest x1)))
204 (= t1 'up) (cons 'up (map #(n*2 x0 %) (rest x1)))
207 (defn onehot [idxs tuple]
208 (letfn [(rec [tuple pos]
211 (map #(rec %1 (cons %2 pos))
214 (if (= pos idxs) 1 0)
218 (defn diff-tuple [tuple & [sel]]
219 (letfn [(rec [pos sub-tuple]
220 (if (coll? sub-tuple)
221 (cons ({'up 'down, 'down 'up} (first sub-tuple))
222 (map #(rec (cons %2 pos) %1)
223 (rest sub-tuple) (iterate inc 0)
225 (reduce (fn [acc x] (nth acc (inc x)))
230 ; symbolic differentiation
232 (cond (= expr 'arg) '[D arg]
234 (= (first expr) 'arg) (cons 'D expr)
236 (= (first expr) 'expt)
237 ['* (if (= (nth expr 2) 2)
239 ['expt (nth expr 1) (dec (nth expr 2))])