Here is a little belated Christmas present for Clojure data aficionados:
;; setup
(use 'clojure.core.matrix)
(set-current-implementation :vectorz)
;; create a big sparse matrix with a trillion elements (initially zero)
(def A (new-sparse-array [1000000 1000000]))
;; we are hopefully smart enough to avoid printing the whole array!
A
=> #<SparseRowMatrix Large matrix with shape: [1000000,1000000]>
;; mutable setter operations supported so that you can set individual
sparse elements
(dotimes [i 1000]
(mset! A (rand-int 1000000) (rand-int 1000000) (rand-int 100)))
;; all standard core.matrix operations supported
(esum A)
=> 50479.0
;; efficient addition
(time (add A A))
=> "Elapsed time: 12.849859 msecs"
;; matrix multiplication / inner products actually complete in sensible time
;; (i.e. much faster than than the usual O(n^3) which might take a few
thousand years)
(time (mmul A (transpose A)))
=> "Elapsed time: 2673.085171 msecs"
Some nice things to note about the implementation:
- Everything goes through the core.matrix API, so your code won't have to
change to use sparse matrices :-)
- Sparse matrices are 100% interoperable with non-sparse (dense) matrices
- Sparse arrays are fully mutable. Management of storage / indexing happens
automatically
- It isn't just matrices - you can have sparse vectors, N-dimensional
arrays etc.
- Code is pure JVM - no native dependencies to worry about
This is all still very much alpha - so any comments / patches / more
rigorous testing much appreciated!
--
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
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.