You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2019/03/09 20:59:52 UTC

[GitHub] [incubator-mxnet] Chouffe commented on a change in pull request #14308: [clojure-package][wip] add `->nd-vec` function in `ndarray.clj`

Chouffe commented on a change in pull request #14308: [clojure-package][wip] add `->nd-vec` function in `ndarray.clj`
URL: https://github.com/apache/incubator-mxnet/pull/14308#discussion_r264014368
 
 

 ##########
 File path: contrib/clojure-package/src/org/apache/clojure_mxnet/ndarray.clj
 ##########
 @@ -167,3 +171,36 @@
 
 (defn shape-vec [ndarray]
   (mx-shape/->vec (shape ndarray)))
+
+(s/fdef vec->nd-vec
+        :args (s/cat :v vector? :shape-vec vector?)
+        :ret vector?)
+
+(defn- vec->nd-vec
+  "Convert a vector `v` into a n-dimensional vector given the `shape-vec`
+
+   Ex:
+     (vec->nd-vec [1 2 3] [1 1 3])       ;[[[1 2 3]]]
+     (vec->nd-vec [1 2 3 4 5 6] [2 3 1]) ;[[[1] [2] [3]] [[4] [5] [6]]]
+     (vec->nd-vec [1 2 3 4 5 6] [1 2 3]) ;[[[1 2 3]] [4 5 6]]]
+     (vec->nd-vec [1 2 3 4 5 6] [3 1 2]) ;[[[1 2]] [[3 4]] [[5 6]]]
+     (vec->nd-vec [1 2 3 4 5 6] [3 2])   ;[[1 2] [3 4] [5 6]]"
+  [v [s1 & ss :as shape-vec]]
+  (if-not (seq ss)
+    (vec v)
+    (->> v
+         (partition (/ (count v) s1))
+         vec
+         (mapv #(vec->nd-vec % ss)))))
+
+(s/fdef ->nd-vec
+        :args (s/cat :ndarray #(instance? NDArray %))
+        :ret vector?)
+
+(defn ->nd-vec
+  "Convert an ndarray `nd` into a n-dimensional Clojure vector.
+  Ex:
+    (->nd-vec (array [1] [1 1 1]))             ;[[[1.0]]]
+    (->nd-vec (ndarray/array [1 2 3] [3 1 1])) ;[[[1.0]] [[2.0]] [[3.0]]]"
+  [ndarray]
 
 Review comment:
   Yes lets do that!

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services