Adding an SMatrix

Hi,

the following code fails because Ke is immutable after adding the SMatrix.

using StaticArrays
Ke = zeros(3, 3)
Ke += SA[1 2 3; 4 3 2; 4 3 2]
Ke[1, 1] += 3

I wonder whether this is intended behaviour.

It is intended: adding an AbstractMatrix to a SMatrix results in a SMatrix. If you want to keep the type of Ke unchanged, you could use broadcasting:

using StaticArrays
Ke = zeros(3, 3)
Ke .+= SA[1 2 3; 4 3 2; 4 3 2]
Ke[1, 1] += 3
2 Likes

Thanks a lot for the clarification!

Weird. The implementation is here:

Possibly this is intentional, but it seems like a very bad choice on part of StaticArrays.jl, considering the lack of type stability, and considering how the methods cause method ambiguity for other packages (relevant doc PR of mine: manual: methods: new section on avoiding ambiguity: "playing nicely" by nsajko · Pull Request #58005 · JuliaLang/julia · GitHub).

Furthermore I see StaticArrays.jl doesn’t have promotion with Array defined, which seems like a related bad choice.

I suppose all of this is just technical debt from the early days of Julia.

2 Likes