Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns clojure.linear)
- (defn checkVV
- [a b]
- {:pre [(and (vector? a) (vector? b) (= (count a) (count b)))]}
- true)
- (defn checkMMLoop
- [a b ind]
- {:pre [checkVV a b]}
- (if (>= ind (count a))
- true
- (and (= (checkVV (nth a ind) (nth b ind)) (= (count (nth a ind)) (count (nth a 0)))) (checkMMLoop a b (+ 1 ind)))))
- (defn checkMM
- [a b]
- {:pre [checkVV a b]}
- (checkMMLoop a b 0))
- (defn v+
- [a b]
- {:pre [checkVV a b], :post [checkVV % a]}
- (mapv + a b))
- (defn v-
- [a b]
- {:pre [checkVV a b], :post [checkVV % a]}
- (mapv - a b))
- (defn v*
- [a b]
- {:pre [checkVV a b], :post [checkVV % a]}
- (mapv * a b))
- (defn foldLeft
- [zero f col]
- {:pre [vector? zero], :post [= (count %) (count col)]}
- (if (empty? col)
- zero
- (foldLeft (f zero (first col)) f (rest col))))
- (defn scalar
- [a, b]
- {:pre [checkVV a b], :post [number? %]}
- (foldLeft 0 + (v* a b)))
- (defn vect
- [a, b]
- {:pre [checkVV a b], :post [(and (vector? %) (= 3 (count %)))]}
- (vector
- (- (* (nth a 1) (nth b 2)) (* (nth a 2) (nth b 1)))
- (- (* (nth a 2) (nth b 0)) (* (nth a 0) (nth b 2)))
- (- (* (nth a 0) (nth b 1)) (* (nth a 1) (nth b 0)))))
- (defn v*s
- [a, b]
- {:pre [(and (vector? a) (number? b))], :post [(and (vector? %) (= (count a) (count b)))]}
- (mapv (fn [x] (* x b)) a))
- (defn m+
- [a b]
- {:pre [checkMM a b], :post [checkMM % a]}
- (mapv v+ a b))
- (defn m-
- [a b]
- {:pre [checkMM a b], :post [checkMM % a]}
- (mapv v- a b))
- (defn m*
- [a b]
- {:pre [checkMM a b], :post [checkMM % a]}
- (mapv v* a b))
- (defn m*s
- [a b]
- {:pre [(and (checkMM a a) (number? b))], :post [checkMM a %]}
- (mapv (fn [x] (v*s x b)) a))
- (defn collectNth
- [n v]
- {:pre [(and (integer? n) (checkMM v v) (>= n (count (nth v 0))))], :post [(and (vector? %) (= (count %) (count v)))]}
- (foldLeft [] (fn [a add] (conj a (nth add n))) v))
- (defn m*vImp
- [zero a b ind]
- {:pre [(and (checkMM a a) (checkVV a b) (vector? zero) (= 0 (count zero)))], :post [(and (vector? %) (= (count %) (count b)))]}
- (if (>= ind (count b))
- zero
- (m*vImp (conj zero (scalar (collectNth ind a) b)) a b (+ ind 1))))
- (defn m*v
- [a b]
- {:pre [(and (checkMM a a) (checkVV a b))], :post [(and (vector? %) (= (count %) (count b)))]}
- (m*vImp [] a b 0))
- (defn m*mImp
- [zero a b ind]
- {:pre [(and (checkMM a a) (checkMM b b) (= (count (nth b 0)) (count a)) (vector? zero) (= 0 (count zero)))],
- :post [(and (checkMM % %) (= (count %) (count b)) (= (count (nth % 0)) (count (nth a 0))))]}
- (if (>= ind (count b))
- zero
- (m*mImp (conj zero (m*v a (nth b ind))) a b (+ ind 1))))
- (defn m*m
- [a b]
- {:pre [(and (checkMM a a) (checkMM b b) (= (count (nth b 0)) (count a)))],
- :post [(and (checkMM % %) (= (count %) (count b)) (= (count (nth % 0)) (count (nth a 0))))]}
- (m*mImp [] a b 0))
- (defn transpose
- [a]
- {:pre [checkMM a a], :post [(and (checkMM % %) (= (count a) (count (nth % 0))) (= (count (nth a 0)) (count %)))]}
- (apply mapv vector a))
- (println (transpose [[1 2 3] [4 5 6] [7 8 9]]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement