# Package to concatenate sparse matrices according to a plan?

**URL:** <https://discourse.julialang.org/t/package-to-concatenate-sparse-matrices-according-to-a-plan/5438>\
**Category:** General Usage\
**Created:** [August 17, 2017, 10:45pm UTC](https://discourse.julialang.org/t/package-to-concatenate-sparse-matrices-according-to-a-plan/5438 "2017-08-17T22:45:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [August 17, 2017, 10:45pm UTC](https://discourse.julialang.org/t/package-to-concatenate-sparse-matrices-according-to-a-plan/5438/1 "2017-08-17T22:45:23Z")

</div>

I discovered that my application is spending quite a bit of time concatenating sparse matrices with statements occurring inside of loops of the form

```julia
A = [B C
     C' D]

```

and the like. In my application, although the numerical entries change, the positions are usually unchanged. So it would be great if there were a function or package so that I could say:

```julia
A = concat_via_schema(schema, [[B, C], [transp(C), D]])

```

where the `schema` is precomputed, and the entries are immediately scattered to the correct locations in memory according to the plan. Actually, such a system of preallocating and precomputing destinations would be helpful for many sparse matrix operations (mat-mat-mult etc) that appear inside of loops. Are there any packages or functions like this?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [August 18, 2017, 6:55am UTC](https://discourse.julialang.org/t/package-to-concatenate-sparse-matrices-according-to-a-plan/5438/2 "2017-08-18T06:55:27Z")

</div>

You could perhaps, allocate `A` once and then update it with ranges, so `A[1:nB, 1:nB] = B`?

---

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [August 20, 2017, 3:32am UTC](https://discourse.julialang.org/t/package-to-concatenate-sparse-matrices-according-to-a-plan/5438/3 "2017-08-20T03:32:22Z")

</div>

According to further testing in 0.6.0:

My original code (concatenating sparse matrices by listing the submatrices in square brackets) requires 5 sec; the approach proposed by Kristoffer Carlsson of using range subarray assignment requires 2.3 sec, and computing a “schema” in advance requires 1.3 sec. (These times are the total times for the preprocessing followed by 15 concatenation operations with the same matrix structure.)
