Advertisement
Guest User

Untitled

a guest
May 29th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns clojure.linear)
  2.  
  3. (defn checkVV
  4.       [a b]
  5.       {:pre [(and (vector? a) (vector? b) (= (count a) (count b)))]}
  6.       true)
  7.  
  8. (defn checkMMLoop
  9.       [a b ind]
  10.       {:pre [checkVV a b]}
  11.       (if (>= ind (count a))
  12.         true
  13.         (and (= (checkVV (nth a ind) (nth b ind)) (= (count (nth a ind)) (count (nth a 0)))) (checkMMLoop a b (+ 1 ind)))))
  14.  
  15. (defn checkMM
  16.       [a b]
  17.       {:pre [checkVV a b]}
  18.       (checkMMLoop a b 0))
  19.  
  20. (defn  v+
  21.        [a b]
  22.        {:pre [checkVV a b], :post [checkVV % a]}
  23.        (mapv + a b))
  24. (defn  v-
  25.        [a b]
  26.        {:pre [checkVV a b], :post [checkVV % a]}
  27.        (mapv - a b))
  28. (defn  v*
  29.        [a b]
  30.        {:pre [checkVV a b], :post [checkVV % a]}
  31.        (mapv * a b))
  32.  
  33. (defn foldLeft
  34.       [zero f col]
  35.       {:pre [vector? zero], :post [= (count %) (count col)]}
  36.       (if (empty? col)
  37.         zero
  38.         (foldLeft (f zero (first col)) f (rest col))))
  39.  
  40. (defn scalar
  41.       [a, b]
  42.       {:pre [checkVV a b], :post [number? %]}
  43.       (foldLeft 0 + (v* a b)))
  44.  
  45. (defn vect
  46.       [a, b]
  47.       {:pre [checkVV a b], :post [(and (vector? %) (= 3 (count %)))]}
  48.       (vector
  49.         (- (* (nth a 1) (nth b 2)) (* (nth a 2) (nth b 1)))
  50.         (- (* (nth a 2) (nth b 0)) (* (nth a 0) (nth b 2)))
  51.         (- (* (nth a 0) (nth b 1)) (* (nth a 1) (nth b 0)))))
  52.  
  53. (defn v*s
  54.       [a, b]
  55.       {:pre [(and (vector? a) (number? b))], :post [(and (vector? %) (= (count a) (count b)))]}
  56.       (mapv (fn [x] (* x b)) a))
  57.  
  58. (defn m+
  59.       [a b]
  60.       {:pre [checkMM a b], :post [checkMM % a]}
  61.       (mapv v+ a b))
  62. (defn m-
  63.       [a b]
  64.       {:pre [checkMM a b], :post [checkMM % a]}
  65.       (mapv v- a b))
  66. (defn m*
  67.       [a b]
  68.       {:pre [checkMM a b], :post [checkMM % a]}
  69.       (mapv v* a b))
  70.  
  71. (defn m*s
  72.       [a b]
  73.       {:pre [(and (checkMM a a) (number? b))], :post [checkMM a %]}
  74.       (mapv (fn [x] (v*s x b)) a))
  75.  
  76. (defn collectNth
  77.       [n v]
  78.       {:pre [(and (integer? n) (checkMM v v) (>= n (count (nth v 0))))], :post [(and (vector? %) (= (count %) (count v)))]}
  79.       (foldLeft [] (fn [a add] (conj a (nth add n))) v))
  80.  
  81. (defn m*vImp
  82.       [zero a b ind]
  83.       {:pre [(and (checkMM a a) (checkVV a b) (vector? zero) (= 0 (count zero)))], :post [(and (vector? %) (= (count %) (count b)))]}
  84.       (if (>= ind (count b))
  85.         zero
  86.         (m*vImp (conj zero (scalar (collectNth ind a) b)) a b (+ ind 1))))
  87.  
  88. (defn m*v
  89.       [a b]
  90.       {:pre [(and (checkMM a a) (checkVV a b))], :post [(and (vector? %) (= (count %) (count b)))]}
  91.       (m*vImp [] a b 0))
  92.  
  93. (defn m*mImp
  94.       [zero a b ind]
  95.       {:pre [(and (checkMM a a) (checkMM b b) (= (count (nth b 0)) (count a)) (vector? zero) (= 0 (count zero)))],
  96.        :post [(and (checkMM % %) (= (count %) (count b)) (= (count (nth % 0)) (count (nth a 0))))]}
  97.       (if (>= ind (count b))
  98.         zero
  99.         (m*mImp (conj zero (m*v a (nth b ind))) a b (+ ind 1))))
  100.  
  101. (defn m*m
  102.       [a b]
  103.       {:pre [(and (checkMM a a) (checkMM b b) (= (count (nth b 0)) (count a)))],
  104.        :post [(and (checkMM % %) (= (count %) (count b)) (= (count (nth % 0)) (count (nth a 0))))]}
  105.       (m*mImp [] a b 0))
  106.  
  107. (defn transpose
  108.       [a]
  109.       {:pre [checkMM a a], :post [(and (checkMM % %) (= (count a) (count (nth % 0))) (= (count (nth a 0)) (count %)))]}
  110.       (apply mapv vector a))
  111.  
  112. (println (transpose [[1 2 3] [4 5 6] [7 8 9]]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement