Advertisement
Guest User

Untitled

a guest
May 30th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defn my-count [obj]
  2.   (cond
  3.     (number? obj) 0
  4.     (vector? obj) (count obj)))
  5.  
  6.  
  7. (defn correct-tensor? [t]
  8.   (cond
  9.     (number? t) true
  10.     :else (and
  11.             (vector? t)
  12.             (apply = (map my-count t))
  13.             (every? correct-tensor? t))))
  14.  
  15.  
  16. (defn correct-obj? [t]
  17.   (cond
  18.     (number? t) true
  19.     :else (and
  20.             (vector? t)
  21.             (every? correct-obj? t))))
  22.  
  23.  
  24. (defn shape
  25.   ([obj]
  26.    {:pre (correct-tensor? obj)}
  27.    (shape obj []))
  28.   ([obj cur]
  29.    (cond
  30.      (number? obj) cur
  31.      (vector? obj) (shape (first obj) (conj cur (count obj))))))
  32.  
  33.  
  34. (defn dim [obj]
  35.   (count (shape obj)))
  36.  
  37.  
  38. (defn correct-vector? [v]
  39.   (= (dim v) 1))
  40.  
  41.  
  42. (defn correct-matrix? [m]
  43.   (= (dim m) 2))
  44.  
  45.  
  46. (defn cond-operation [op condition]
  47.   (fn [a & args]
  48.     {:pre [(every? condition (conj args a))]}
  49.     (apply op a args)))
  50.  
  51.  
  52. (defn can-coordinate-wise [& args]
  53.   (cond
  54.     (every? number? args) true
  55.     :else (and
  56.             (apply = (map my-count args))
  57.             (every? true? (apply map can-coordinate-wise args)))))
  58.  
  59.  
  60. (defn postfix? [a b]
  61.   (cond
  62.     (<= (count a) (count b))
  63.     (every? zero? (map - a (subvec b (- (count b) (count a)))))
  64.     :else false))
  65.  
  66. (defn cw-operation [f]
  67.   (fn res [a & args]
  68.     {:pre [(apply can-coordinate-wise a args)]}
  69.     (cond
  70.       (vector? a) (apply mapv res a args)
  71.       (number? a) (apply f a args))))
  72.  
  73.  
  74. (defn transpose [m]
  75.   {:pre [(correct-matrix? m)]}
  76.   (apply mapv vector m))
  77.  
  78.  
  79. (def scalar)
  80. (defn matrix-by-vector [m v]
  81.   {:pre [(= (dim m) 2)
  82.          (= (dim v) 1)
  83.          (= (last (shape m)) (first (shape v)))]}
  84.   (mapv (fn [x] (scalar x v)) m))
  85.  
  86.  
  87. (defn matrix-by-matrix [m1 m2]
  88.   {:pre [(= (dim m1) 2)
  89.          (= (dim m2) 2)
  90.          (= (last (shape m1)) (first (shape m2)))]}
  91.   (transpose
  92.     (mapv (fn [v] (matrix-by-vector m1 v))
  93.           (transpose m2))))
  94.  
  95.  
  96. (defn make-multiarg [op]
  97.   (fn [x & args]
  98.     (reduce op x args)))
  99.  
  100.  
  101. (defn broadcast [t1 res-shape]
  102.   {:pre (postfix? (shape t1) res-shape)}
  103.   (cond
  104.     (= (shape t1) res-shape) t1
  105.     :else (vec (repeat
  106.                  (first res-shape)
  107.                  (broadcast t1 (rest res-shape))))))
  108.  
  109.  
  110. (defn broadcast-all [t & args]
  111.   {:pre (every? correct-tensor? (conj args t))}
  112.   (let [max-shape (apply max-key count (map shape (conj args t)))]
  113.     (map (fn [t] (broadcast t max-shape)) (conj args t))))
  114.  
  115.  
  116. (defn b-operation [f]
  117.   (let [cw (cw-operation f)]
  118.     (fn [t & args]
  119.       {:pre [(every? correct-tensor? (conj args t))]}
  120.       (apply cw (apply broadcast-all t args)))))
  121.  
  122.  
  123. (def s+ (cw-operation +))
  124. (def s* (cw-operation *))
  125. (def s- (cw-operation -))
  126.  
  127.  
  128. (def v+ (cond-operation s+ correct-vector?))
  129. (def v* (cond-operation s* correct-vector?))
  130. (def v- (cond-operation s- correct-vector?))
  131.  
  132. (defn scalar [v & args]
  133.   {:pre [(every? correct-vector? (conj args v))]}
  134.   (reduce + (apply v* v args)))
  135.  
  136. (def vect (make-multiarg
  137.             (fn [v1 v2]
  138.               {:pre [(correct-vector? v1)
  139.                      (correct-vector? v2)
  140.                      (= 3 (first (shape v1)))
  141.                      (= 3 (first (shape v2)))]}
  142.  
  143.               [(- (* (nth v1 1) (nth v2 2)) (* (nth v1 2) (nth v2 1)))
  144.                (- (* (nth v1 2) (nth v2 0)) (* (nth v1 0) (nth v2 2)))
  145.                (- (* (nth v1 0) (nth v2 1)) (* (nth v1 1) (nth v2 0)))])))
  146.  
  147. (def v*s (fn [v & args]
  148.            {:pre [(correct-vector? v)
  149.                   (every? number? args)]}
  150.            (mapv (partial * (apply * args)) v)))
  151.  
  152.  
  153. (def m+ (cond-operation s+ correct-matrix?))
  154. (def m* (cond-operation s* correct-matrix?))
  155. (def m- (cond-operation s- correct-matrix?))
  156.  
  157. (def m*s (fn [m & args]
  158.            {:pre [(correct-matrix? m)]}
  159.            (mapv (fn [x] (v*s x (apply * args))) m)))
  160.  
  161. (def m*v (make-multiarg matrix-by-vector))
  162. (def m*m (make-multiarg matrix-by-matrix))
  163.  
  164.  
  165. (def b+ (b-operation +))
  166. (def b* (b-operation *))
  167. (def b- (b-operation -))
  168.  
  169.  
  170. ;(println (b+ [[1.1 2.1] [1.2 3.4]]))
  171. ;(println (b+ 1))
  172. ;(println (v+ [1.1 2.1] [1.2 3.4]))
  173. ;(println (v*s [1.1 2.1]))
  174. ;(println (m*v [[1 2] [3 4] [5 6]] [10 20]))
  175. ;(println (vect [1 2 3] [4 5 6]))
  176. ;(println (v+ [1 2 3] [4 5 6]))
  177. ;(println (s* [1 2] [3 4] [3 4]))
  178. ;(println (transpose [[1 2] [3 4]]))
  179. ;(println (transpose [[1 2 3 4] [5 6 7 8] [0 1 0 1] [1 0 1 0]]))
  180. ;(println (correct-tensor? [[1 2] [3 4]]))
  181. ;(println (m*m [[1 2] [3 4]] [[1 2] [3 4]]))
  182. ;(println (shape [[1 2 3] [3 4 3]]))
  183. ;(println (broadcast-all 1 [[[10 20 30] [40 50 60]] [[10 20 30] [40 50 60]]]))
  184. ;(println (b+ 1 [[10 20 30] [40 50 60]] [100 200 300]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement