# Example use of \`sparse!\`

**URL:** <https://discourse.julialang.org/t/example-use-of-sparse/21808>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 13, 2019, 7:49am UTC](https://discourse.julialang.org/t/example-use-of-sparse/21808 "2019-03-13T07:49:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [March 13, 2019, 7:49am UTC](https://discourse.julialang.org/t/example-use-of-sparse/21808/1 "2019-03-13T07:49:15Z")

</div>

Hi,

I am building sparse matrices with the same sparsity pattern. The function `sparse(...)` represent 90% of the computation time, the rest being a linear solve.

I am wondering if `sparse!` could help this but I did not find any example of its use.

Does anybody have a pointer please?

Thank you

Best

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [March 13, 2019, 8:05am UTC](https://discourse.julialang.org/t/example-use-of-sparse/21808/2 "2019-03-13T08:05:07Z")

</div>

If the sparsity patterns are guaranteed to always be static and identical, how about reusing the `colptr` and `rowval` vectors, as in this example:

```julia
julia> A = sprandn(10, 10, 0.05)
10×10 SparseMatrixCSC{Float64,Int64} with 5 stored entries:
  [3 , 1] = -0.00762068
  [4 , 3] = 0.24252
  [3 , 5] = -0.357867
  [3 , 9] = 0.451247
  [7 , 9] = 0.810302

julia> B = SparseMatrixCSC(size(A)..., SparseArrays.getcolptr(A), rowvals(A), rand(nnz(A)))
10×10 SparseMatrixCSC{Float64,Int64} with 5 stored entries:
  [3 , 1] = 0.506203
  [4 , 3] = 0.719589
  [3 , 5] = 0.336872
  [3 , 9] = 0.949761
  [7 , 9] = 0.59247

```

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [March 13, 2019, 4:34pm UTC](https://discourse.julialang.org/t/example-use-of-sparse/21808/3 "2019-03-13T16:34:38Z")

</div>

This is really good, thank you!

Let me explain what my goal is so that other might benefit from it.

What I am trying to improve is in fact a bit more sophisticated. I presume it is a sparse version of what is explained in the beginning of the [docs](http://juliaarrays.github.io/BlockArrays.jl/stable/man/pseudoblockarrays.html#PseudoBlockArrays-1) of `BlockArrays.jl`.

In my package `PseudoArcLengthContinuation.jl`, I create a Block matrix with some sparse blocks in the function [`JacobianPeriodicFD`](https://github.com/rveltz/PseudoArcLengthContinuation.jl/blob/master/src/periodicorbit/PeriodicOrbitFD.jl#L95).

Then, at line [137](https://github.com/rveltz/PseudoArcLengthContinuation.jl/blob/master/src/periodicorbit/PeriodicOrbitFD.jl#L137), I create a sparse matrix version of the Block matrix in order to solve a linear problem associated to it. This line 137, makes up for most of the computational cost. I will try to replace it using `sparse!` or the trick by @bennedich.
