Package to concatenate sparse matrices according to a plan?

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

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:

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?

1 Like

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

1 Like

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.)