# Create Sparse Array without Copying

**URL:** <https://discourse.julialang.org/t/create-sparse-array-without-copying/31984>\
**Category:** General Usage\
**Created:** [December 7, 2019, 6:53pm UTC](https://discourse.julialang.org/t/create-sparse-array-without-copying/31984 "2019-12-07T18:53:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_McVay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_mcvay/32/4307_2.png) [@Paul\_McVay](https://discourse.julialang.org/u/Paul_McVay)\
**Post date:** [December 7, 2019, 6:53pm UTC](https://discourse.julialang.org/t/create-sparse-array-without-copying/31984/1 "2019-12-07T18:53:01Z")

</div>

I have the row vector, column vector and the value vectors of a sparse matrix and would like to create a sparse matrix with them without copying the vectors because the vectors already take up 59% of my systems memory. Is this possible? Basically, I have `indexI`, `indexJ`, and `val` as regular arrays and would like something that accomplishes `sparse(indexI, indexJ, val)` without copying the vectors as that would require too much memory

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [December 7, 2019, 8:22pm UTC](https://discourse.julialang.org/t/create-sparse-array-without-copying/31984/2 "2019-12-07T20:22:41Z")

</div>

Depending on what it is you need the sparse matrix for, there may be a solution that does not involve construction of the sparse matrix: simply work with the representation of the matrix provided by the three vectors.

---

<div class="post-metadata">

**Author:** ![Paul\_McVay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_mcvay/32/4307_2.png) [@Paul\_McVay](https://discourse.julialang.org/u/Paul_McVay)\
**Post date:** [December 7, 2019, 8:33pm UTC](https://discourse.julialang.org/t/create-sparse-array-without-copying/31984/3 "2019-12-07T20:33:56Z")

</div>

Gotcha - yeah, I wanted the BLAS library functions for sparse vectors and matrices

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [December 7, 2019, 8:39pm UTC](https://discourse.julialang.org/t/create-sparse-array-without-copying/31984/4 "2019-12-07T20:39:46Z")

</div>

Sorry, say again? BLAS is dense only AFAIK.

---

<div class="post-metadata">

**Author:** ![Paul\_McVay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_mcvay/32/4307_2.png) [@Paul\_McVay](https://discourse.julialang.org/u/Paul_McVay)\
**Post date:** [December 7, 2019, 8:47pm UTC](https://discourse.julialang.org/t/create-sparse-array-without-copying/31984/5 "2019-12-07T20:47:59Z")

</div>

It may not be called BLAS in this particular case with sparse matrices and vectors, but the common linear algebra functions defined for matrices and vectors

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 8, 2019, 7:40am UTC](https://discourse.julialang.org/t/create-sparse-array-without-copying/31984/7 "2019-12-08T07:40:02Z")

</div>

If you are short on memory, you could try constructing the `colptr` (relatively small), `rowval`, and `nzval` vectors, then just using the inner constructor for `SparseMatrixCSC`. This does not copy.

See this explanation for the storage format:

[https://docs.julialang.org/en/v1/stdlib/SparseArrays/#man-csc-1](https://docs.julialang.org/en/v1/stdlib/SparseArrays/#man-csc-1)
