# How to create static Matrix from a normal Matrix

**URL:** <https://discourse.julialang.org/t/how-to-create-static-matrix-from-a-normal-matrix/76688>\
**Category:** Specific Domains\
**Created:** [February 18, 2022, 10:58am UTC](https://discourse.julialang.org/t/how-to-create-static-matrix-from-a-normal-matrix/76688 "2022-02-18T10:58:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [February 18, 2022, 10:58am UTC](https://discourse.julialang.org/t/how-to-create-static-matrix-from-a-normal-matrix/76688/1 "2022-02-18T10:58:32Z")

</div>

I find this behaviour very confusing:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/a/2a5ac69a6c8548426ce2e422f0f5a85acb47b8ad.png)

The following works, but I am certain that there must be better ways to do it:  
`SMatrix{size(tester)...}(tester)`

How _should_ I construct a static vector?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 18, 2022, 11:02am UTC](https://discourse.julialang.org/t/how-to-create-static-matrix-from-a-normal-matrix/76688/2 "2022-02-18T11:02:11Z")

</div>

This is explicitly stated in the [docs](https://juliaarrays.github.io/StaticArrays.jl/stable/pages/quickstart/):

```julia
m5 = SMatrix{2,2}([1 3 ; 2 4]) # Array conversions must specify size

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [February 18, 2022, 11:50am UTC](https://discourse.julialang.org/t/how-to-create-static-matrix-from-a-normal-matrix/76688/3 "2022-02-18T11:50:00Z")

</div>

When you’re using a macro, the macro can only see the syntax. In the first case, it can figure out the size of the result, but in the second case it just sees a symbol and has no idea what hides behind it.

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [February 18, 2022, 11:57am UTC](https://discourse.julialang.org/t/how-to-create-static-matrix-from-a-normal-matrix/76688/4 "2022-02-18T11:57:15Z")

</div>

Alright, that makes sense.

I have now defined the following in my script:

```julia
svec(x::AbstractVector) = SVector{length(x)}(x)
svec(some_vector) == SVector{length(some_vector)}(some_vector) # returns true

```

Why is that, and something like it, not defined? It seems awfully redundant to write the proposed

```julia
m5 = SMatrix{2,2}([1 3 ; 2 4])

```

, when the dimensions of `[1 3; 2 4]` can be found from the input.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [February 18, 2022, 12:13pm UTC](https://discourse.julialang.org/t/how-to-create-static-matrix-from-a-normal-matrix/76688/6 "2022-02-18T12:13:12Z")

</div>

In this case `SMatrix{2,2}([1 3 ; 2 4])` the _function_ `SMatrix` does not have access to the size at type level, the _macro_ `@SMatrix` does. If you allow `SMatrix(matrix)` you will end up with a lot of users having type unstable code which would defeat a lot of the purpose of StaticArrays, speed and stack allocation. The function you defined is indeed type unstable for normal arrays.

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [February 18, 2022, 12:31pm UTC](https://discourse.julialang.org/t/how-to-create-static-matrix-from-a-normal-matrix/76688/7 "2022-02-18T12:31:04Z")

</div>

Got it. Thanks!
