# Adding an SMatrix

**URL:** https://discourse.julialang.org/t/adding-an-smatrix/130202
**Category:** New to Julia
**Tags:** question, staticarrays
**Created:** [June 25, 2025, 2:49pm UTC](https://discourse.julialang.org/t/adding-an-smatrix/130202 "2025-06-25T14:49:27Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![matthiasbaitsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matthiasbaitsch/32/214952_2.png) [@matthiasbaitsch](https://discourse.julialang.org/u/matthiasbaitsch)
#### Post date: [June 25, 2025, 2:49pm UTC](https://discourse.julialang.org/t/adding-an-smatrix/130202/1 "2025-06-25T14:49:28Z")

</div>

Hi,

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

```julia
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.

---

<div class="post-metadata">

### Author: ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)
#### Post date: [June 25, 2025, 2:56pm UTC](https://discourse.julialang.org/t/adding-an-smatrix/130202/2 "2025-06-25T14:56:36Z")

</div>

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:

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

```

---

<div class="post-metadata">

### Author: ![matthiasbaitsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matthiasbaitsch/32/214952_2.png) [@matthiasbaitsch](https://discourse.julialang.org/u/matthiasbaitsch)
#### Post date: [June 25, 2025, 3:26pm UTC](https://discourse.julialang.org/t/adding-an-smatrix/130202/3 "2025-06-25T15:26:28Z")

</div>

Thanks a lot for the clarification!

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [June 25, 2025, 4:23pm UTC](https://discourse.julialang.org/t/adding-an-smatrix/130202/4 "2025-06-25T16:23:10Z")

</div>

> [@barucden](#):
>
> It is intended: adding an `AbstractMatrix` to a `SMatrix` results in a `SMatrix`.

Weird. The implementation is here:

> <https://github.com/JuliaArrays/StaticArrays.jl/blob/563adebc264a23d38da97bf5d9e5d3c6333bdf26/src/linalg.jl#L6-L25>

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](https://github.com/JuliaLang/julia/pull/58005)).

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.
