basically what I want to do is calculate the sum of a matrix, say u, by column and store it in a pre-allocated diagonal matrix, say du, in a more efficient way than
vi = sum(u, dims=2) for i = 1:length(vi) du[i, i] = vi[i] end
the whole idea is that i need to do an operation:
usg* A1*u + Deg*A2*u
A1 and A2 do the first and second derivation. This is a typical operation in the simulation of the convection-diffusion phenomenon. If
usg is a diagonal matrix or a constant, the equation can be calculated with inplace operation (with an intermediate vector
mul!( lmul!(usg, mul!( du, A1, u)), A2, u, Deg, true)
In my case, Deg can be seen as constant. But usg is no constant and calculated from u by
usg = sum(u, dims=2)*const ./v1.
As v1 is a constant vector, first I can make a diagnoal matrix
rv1 = Diagnoal(1 ./v1)
usg = sum(u, dims=2)*const ./v1 = rv1*sum(u, dims=2)*const
and can be calculated using inplace operator
mul!(usg, rv1, lmul!(const, du) (take
du = sum(u, dims=2) is the intermediate vector)
But now usg is a vector because
sum(u, dims=2) is an vector.
So is there a way to make
sum(u, dims=2) to store in a diagonal matrix? or is there a simpler way to realize the whole operation (
usg* A1*u + Deg*A2*u ) ?
thanks for reading and giving advice.