Change broadcast form

I would like to make the following broadcast expression:

J = rand(4,4) fx1 = rand(2,2) fx2 = rand(2,2) @. J[:,1] = fx1 + fx2 

I want it very much:

 @. J[:,1] = vec(fx1 + fx2) 

where this vec says it should change to 4x1, but I do not want this selection. How could this be handled as a whole (i.e. do not index on fx)?

+7
arrays julia-lang
source share
3 answers

Another possibility, instead of vec is for fx1 and fx2 to change fragment J :

 Jcol = reshape(view(J,:,1),(2,2)) @. Jcol = fx1 + fx2 

Not sure about the efficiency, but it can give a clearer perspective depending on the surrounding algorithm. The LLVM code seems short enough, and the assignment statement is clear.

+3
source share

You can free vec from the @. macro @. protecting it with $ :

 julia> expand(:(@. J[:,1] = $vec(fx1) + $vec(fx2))) :((Base.broadcast!)(+, (Base.dotview)(J, :, 1), (vec)(fx1), (vec)(fx2))) 
+3
source share

Since vecs are views, the following works:

 J = rand(4,4) fx1 = rand(2,2) fx2 = rand(2,2) vfx1,vfx2 = vec(fx1),vec(fx2) @. J[:,1] = vfx1 + vfx2 

I donโ€™t think there is a way to do this on the same line as I wanted, but thatโ€™s fine.

+2
source share

All Articles