I'm trying to migrate from core.matrix to neanderthal, and I'm struggling with sparse matrixes.
Is it possible to convert a dense matrix created with dge to a sparse matrix?
Why would you create a dense matrix, just to convert it to a sparse? Isn't it more straightforward to create the sparse matrix right away?
Algorithms for sparse matrices are available only when they make sense. For available algorithms, please consult MKL documentation. Not all are integrated in Neanderthal yet, but if they exist in MKL, and you need them, we can include it. But if they don't exist in MKL, in most probability it's a not a good candidate for sparseness.
Are all operations supported between sparse matrix and dense matrix/vector?No > will the result of any operation containing a sparse matrix be sparse? It depends > Are there any helpers to build a sparse matrix/vector from a clojure sequence? How are you going to specify indexes of these elements? Where is the matrix/vector sparse, and where are the non-zero entries?
I see that some functions are not supported for sparse matrixes, like trf tri! entry and I assume more. Are there equivalent function?
In my case is because I already have an existing codebase and it would be easier to compare results and to benchmark, also I'm struggling a bit with the syntax of csr. But you are of course right. I have some questions • Are all operations supported between sparse matrix and dense matrix/vector? • will the result of any operation containing a sparse matrix be sparse? • Are there any helpers to build a sparse matrix/vector from a clojure sequence? I've built my own but I dont to re-invent the wheel if something already exists.
(defn seq->csr
"Converts a Clojure sequence of sequences (nested vectors) into a sparse CSR matrix."
[factory nested-seq]
(let [rows (count nested-seq)
cols (count (first nested-seq))
values []
col-idx []
row-ptr [0]]
(loop [r 0 values values col-idx col-idx row-ptr row-ptr nnz 0]
(if (= r rows)
(sp/csr factory rows cols col-idx row-ptr values {:layout :row})
(let [row-values (for [c (range cols)
:let [val (get-in nested-seq [r c])]
:when (not (zero? val))]
[val c])]
(recur (inc r)
(into values (map first row-values))
(into col-idx (map second row-values))
(conj row-ptr (+ nnz (count row-values)))
(+ nnz (count row-values))))))))
(seq->csr factory
[[0 10 0 0 20]
[0 0 30 0 0]
[40 0 0 50 0]
[0 0 0 0 60]])