# Sparse matrix-matrix calculation only within specified sparsity?

**URL:** <https://discourse.julialang.org/t/sparse-matrix-matrix-calculation-only-within-specified-sparsity/9007>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 12, 2018, 8:10am UTC](https://discourse.julialang.org/t/sparse-matrix-matrix-calculation-only-within-specified-sparsity/9007 "2018-02-12T08:10:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcovalt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcovalt/32/1722_2.png) [@mcovalt](https://discourse.julialang.org/u/mcovalt)\
**Post date:** [February 12, 2018, 8:10am UTC](https://discourse.julialang.org/t/sparse-matrix-matrix-calculation-only-within-specified-sparsity/9007/1 "2018-02-12T08:10:19Z")

</div>

I need to preform some sparse matrix-matrix calculations, but I only need a few of the non-zeros. Specifically, for `A*B=C` I only need the resulting `C` nonzeros where `A` has nonzeros. Forming all of `C` and then dropping the values I don’t need is prohibitively memory expensive.

It is my understanding that Julia makes use of `cholmod_ssmult` for this computation. What I’m asking is not implemented in `cholmod_ssmult` as far as I can tell. Are there any other libraries that can accomplish this? Is there a Julia way to accomplish this?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 12, 2018, 1:04pm UTC](https://discourse.julialang.org/t/sparse-matrix-matrix-calculation-only-within-specified-sparsity/9007/2 "2018-02-12T13:04:35Z")

</div>

> [@mcovalt](#):
>
> Are there any other libraries that can accomplish this? Is there a Julia way to accomplish this?

You may have to write your own; it shouldn’t be too hard if you understand sparse-matrix data structures.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [February 12, 2018, 1:06pm UTC](https://discourse.julialang.org/t/sparse-matrix-matrix-calculation-only-within-specified-sparsity/9007/3 "2018-02-12T13:06:09Z")

</div>

I don’t know of any standard function that does this. But if you want to implement it in Julia, one way would be to convert `A` to CSR format, then take the old CSC `A` and let that be `C`. You can then traverse the non-zeros of `C`, taking dot products of the i^{th} row of A, and the j^{th} column of B and storing it in the non-zero C[i,j], which can be done efficiently as `A` is CSR and `B` is CSC. For how to convert from CSC to CSR, check this outdated file [https://github.com/JuliaSparse/SparseMatrices.jl/blob/master/src/csr.jl](https://github.com/JuliaSparse/SparseMatrices.jl/blob/master/src/csr.jl) which needs a revamp. And for dot product of sparse vectors, check [https://github.com/JuliaSparse/SparseVectors.jl/blob/30a3c3e80893c7fa4c16464b518a10975a82815d/src/linalg.jl#L103](https://github.com/JuliaSparse/SparseVectors.jl/blob/30a3c3e80893c7fa4c16464b518a10975a82815d/src/linalg.jl#L103).
