From 4c84fd46dad19df1c14b6face94d64302f6f3ff4 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Sun, 17 Jan 2016 23:27:19 +0200 Subject: [PATCH] Add matrix library abstraction. --- src/uxbox/matrix.cljs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/uxbox/matrix.cljs diff --git a/src/uxbox/matrix.cljs b/src/uxbox/matrix.cljs new file mode 100644 index 0000000000..4e9159aed5 --- /dev/null +++ b/src/uxbox/matrix.cljs @@ -0,0 +1,40 @@ +(ns uxbox.matrix + "A lightweight abstraction over Matrix library + of the Google Closure Library." + (:import goog.math.Matrix + goog.math.Size)) + +(extend-type Matrix + cljs.core/ICounted + (-count [v] + (let [^Size size (.getSize v)] + (* (.-width size) + (.-height size)))) + + cljs.core/IDeref + (-deref [v] + (js->clj (.toArray v))) + + IPrintWithWriter + (-pr-writer [v writer _] + (->> (str "#goog.math.Matrix " (js->clj (.toArray v))) + (cljs.core/-write writer)))) + +(defn matrix + "Create a matrix instance from coll. + The size is determined by the number + of elements of the collection." + [coll] + {:pre [(coll? coll) + (coll? (first coll)) + (= (count coll) + (count (first coll)))]} + (Matrix. (clj->js coll))) + +(defn multiply + ([n] n) + ([n m] + (.multiply n m)) + ([n m & more] + (reduce multiply (.multiply n m) more))) +