Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn abstractOp [action]
- (fn [& args]
- {:pre [(= (count args) (count (filter #(or (number? %) ( = (count (first args)) (count %))) args)))]}
- (apply mapv action args)))
- (def v+ (abstractOp +))
- (def v- (abstractOp -))
- (def v* (abstractOp *))
- (defn scalar [& args] (reduce + (reduce v* args)))
- (defn vectHelp [a, b, i, j] (- (* (a i) (b j)) (* (a j) (b i))))
- (defn vect [& args]
- {:pre [(= (count args) (count (filter #(= (count (first args)) (count %)) args)))]}
- (reduce (fn [a, b] [(vectHelp a b 1 2), (vectHelp a b 2 0), (vectHelp a b 0 1)]) args))
- (defn v*s [a & s] (mapv #(* % (reduce * s)) a))
- (def m+ (abstractOp v+))
- (def m- (abstractOp v-))
- (def m* (abstractOp v*))
- (defn m*s [a & s] (mapv #(v*s % (reduce * s)) a))
- (defn m*v [m, v]
- (mapv (fn [x] (scalar x v)) m))
- (defn transpose [m]
- (apply mapv vector m))
- (defn m*m [& args] (reduce (fn [m, p]
- (let [tr_p (transpose p)]
- (mapv (partial m*v tr_p) m))) args))
- (defn myF [action actionS args]
- (if (number? (first args))
- (apply action args)
- (apply (abstractOp actionS) args)))
- (defn s+ [& args] (myF + s+ args))
- (defn s- [& args] (myF - s- args))
- (defn s* [& args] (myF * s* args))
- ;correct
- ;(defn myF [action actionS]
- ; (fn [args]
- ; (if (number? (first args))
- ; (apply action args)
- ; (apply (abstractOp actionS) args))))
- ;
- ;(defn s+ [& args] ((myF + s+) args))
- ;
- ;incorrect
- ;(defn myF2 [action actionS]
- ; (fn [& args]
- ; (if (number? (first args))
- ; (apply action args)
- ; (apply (abstractOp actionS) args))))
- ;
- ;(def s+ (myF2 + s+))
- (defn getForm [x]
- (loop [x x ans []]
- (if (number? x)
- ans
- (recur (first x) (conj ans (count x))))))
- (defn broadcast [x y action]
- (let [fx (getForm x) fy (getForm y)]
- (if (< (count fx) (count fy))
- (loop [i (- (count fy) (count fx) 1) x x]
- (if (< i 0)
- (action x y)
- (recur (dec i) (vec (repeat (nth fy i) x)))))
- (loop [i (- (count fx) (count fy) 1) y y]
- (if (< i 0)
- (action x y)
- (recur (dec i) (vec (repeat (nth fx i) y))))))))
- (defn b+ [& args] (reduce #(broadcast %1 %2 s+) args))
- (defn b- [& args]
- (if (= (count args) 1)
- (s- (first args))
- (reduce #(broadcast %1 %2 s-) args)))
- (defn b* [& args] (reduce #(broadcast %1 %2 s*) args))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement