Great, thanks people.
So to be surfe I understand, let me summarize as follows: There are
two ways to go:
(1) import the existing sparse matrix class in my clojure project and
wrap a new type around it that implements all required java interfaces
(using them as protocols)
(2) code a new java class that implements the java interfaces directly
and import it in clojure
Correct?
Then I think I would go for option (1), since I am not sure what the
added value is of doing (2). I'm not also sure how to deal with
persitency/thread safety. So far I came up with this:
(ns utils.sparse
(:import (org.apache.mahout.math RandomAccessSparseVector)))
(deftype SparseVector [v]
clojure.lang.Counted
(count [this] (.size v))
clojure.lang.Indexed
(nth [this index] (if (< index (.size v))
(.getQuick v index)
...
clojure.lang.IPersistentMap
(assoc [this idx val]
(SparseVector. (RandomAccessSparseVector. ...)))) ;;; use a fresh
RASP (further initialization code not shown)
(defn sparse-vector
([size]
(sparse-vector size {}))
([size & idxvals]
(SparseVector. (doto (RandomAccessSparseVector. size)
((fn [vec] (doseq [[idx val] idxvals]
(.setQuick vec idx
val))))))))
Is this the best way to go? Another possibility is to wrap the RASP in
an atom:
(ns utils.sparse
(:import (org.apache.mahout.math RandomAccessSparseVector)))
(deftype SparseVector [v]
...
clojure.lang.IPersistentMap
(assoc [this idx val]
(swap! v (fn [rasv] (.setQuick rasv idx val))) ;;; side effects
this))
(defn sparse-vector
([size]
(sparse-vector size {}))
([size & idxvals]
(SparseVector. (atom (doto (RandomAccessSparseVector. size) ;;;
make atomic to cope with side effects
((fn [vec] (doseq [[idx val]
idxvals]
(.setQuick
vec idx val)))))))))
I have a feeling that the first approach is preferred, but I'm not
sure why, and also not when to use atoms in general?
Anyway, thanks again, and I would be happy to hear any further
comments and suggestions!
Joachim.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en