Avoiding memory allocation for solves with multiple right-hand sides

It allocates because the operation Kc\b allocates, on the right-hand side. You either have to use an in-place version, or if the matrices are small, use static arrays:

julia> M = rand(3,3);  b = rand(3);

julia> @btime x = $M \ $b;
  346.107 ns (3 allocations: 384 bytes)

julia> using StaticArrays

julia> M = @SMatrix(rand(3,3));  b = @SVector(rand(3));

julia> @btime x = $M \ $b;
  5.228 ns (0 allocations: 0 bytes)

Probably this is what you are looking for: Non-allocating matrix inversion - #7 by stevengj

1 Like