# Example use of `sparse!`

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

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> 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
``````

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 of `BlockArrays.jl`.

In my package `PseudoArcLengthContinuation.jl`, I create a Block matrix with some sparse blocks in the function `JacobianPeriodicFD`.

Then, at line 137, 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.