Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defn common-op [_ op]
  2.   (fn [& vs]
  3.     (apply mapv op vs)))
  4.  
  5. (def v+
  6.   (common-op vector? +))
  7. (def v-
  8.   (common-op vector? -))
  9. (def v*
  10.   (common-op vector? *))
  11.  
  12. (defn matrix? [m]
  13.   (and (vector? m) (every? vector? m)))
  14.  
  15. (def m+
  16.   (common-op matrix? v+))
  17. (def m-
  18.   (common-op matrix? v-))
  19. (def m*
  20.   (common-op matrix? v*))
  21.  
  22. (defn scalar [a, b]
  23.   (apply + (v* a b)))
  24.  
  25. (defn vect [a b]
  26.   [(- (* (nth a 1) (nth b 2)) (* (nth a 2) (nth b 1)))
  27.    (- (* (nth a 2) (nth b 0)) (* (nth a 0) (nth b 2)))
  28.    (- (* (nth a 0) (nth b 1)) (* (nth a 1) (nth b 0)))
  29.    ])
  30.  
  31. (defn v*s [a, b]
  32.   (mapv (fn [n] (* n b)) a))
  33. (defn m*s [a, b]
  34.   (mapv (fn [n] (v*s n b)) a))
  35. (defn m*v [a b]
  36.   (mapv (fn [n] (scalar n b)) a))
  37. (defn transpose [m]
  38.   (apply mapv vector m))
  39. (defn m*m [a b]
  40.   (mapv (fn [n] (mapv (fn [m] (scalar n m)) (transpose b))) a))
  41.  
  42. (defn common-deep-op [op]
  43.   (fn calc [a b]
  44.     (if (vector? a)
  45.       (mapv calc a b)
  46.       (op a b))))
  47.  
  48. (def s+
  49.   (common-deep-op +))
  50. (def s*
  51.   (common-deep-op *))
  52. (def s-
  53.   (common-deep-op -))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement