# Static matrix with size 400

**URL:** <https://discourse.julialang.org/t/static-matrix-with-size-400/113843>\
**Category:** New to Julia\
**Tags:** staticarrays\
**Created:** [May 4, 2024, 6:05pm UTC](https://discourse.julialang.org/t/static-matrix-with-size-400/113843 "2024-05-04T18:05:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiyashxi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiyashxi/32/208173_2.png) [@jiyashxi](https://discourse.julialang.org/u/jiyashxi)\
**Post date:** [May 4, 2024, 6:05pm UTC](https://discourse.julialang.org/t/static-matrix-with-size-400/113843/1 "2024-05-04T18:05:26Z")

</div>

Hi,

I want to write a function to generate random matrices with size 20x20. I need to use this function frequently but I don’t need to change the matrices generated by it. So the `StaticArrays` seem to be a good choice. But I further notice that it only works for arrays less than 100. Is there any way to overcome this?

Thanks

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 4, 2024, 6:18pm UTC](https://discourse.julialang.org/t/static-matrix-with-size-400/113843/2 "2024-05-04T18:18:56Z")

</div>

> [@jiyashxi](#):
>
> But I further notice that it only works for arrays less than 100. I

why?

```julia
julia> SMatrix{20,20}(rand(20,20))
20×20 SMatrix{20, 20, Float64, 400} with indices SOneTo(20)×SOneTo(20):

#or

julia> @SMatrix(rand(20,20))
20×20 SMatrix{20, 20, Float64, 400} with indices SOneTo(20)×SOneTo(20):

```

It is true that the performance benefit degrades as matrix gets larger but you should try it for your workload to determine if it actually speeds things up or not, I think 20x20 is still maybe possible to still see a benefit

---

<div class="post-metadata">

**Author:** ![jiyashxi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiyashxi/32/208173_2.png) [@jiyashxi](https://discourse.julialang.org/u/jiyashxi)\
**Post date:** [May 4, 2024, 6:21pm UTC](https://discourse.julialang.org/t/static-matrix-with-size-400/113843/3 "2024-05-04T18:21:37Z")

</div>

I found it in this discussion. [Large matrix crashes StaticArrays](https://discourse.julialang.org/t/large-matrix-crashes-staticarrays/58689). Maybe things changed. Thanks for this check. I will try it myself

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 4, 2024, 9:28pm UTC](https://discourse.julialang.org/t/static-matrix-with-size-400/113843/4 "2024-05-04T21:28:37Z")

</div>

1000x1000 is quite different from 20x20

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [May 4, 2024, 11:24pm UTC](https://discourse.julialang.org/t/static-matrix-with-size-400/113843/5 "2024-05-04T23:24:39Z")

</div>

Compile and runtime performance may be bad.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/224680_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [May 5, 2024, 5:17am UTC](https://discourse.julialang.org/t/static-matrix-with-size-400/113843/6 "2024-05-05T05:17:46Z")

</div>

> [@jiyashxi](#):
>
> I want to write a function to generate random matrices with size 20x20. I need to use this function frequently but I don’t need to change the matrices generated by it.

What do you do with the matrices later? Allocating a single (standard) `Matrix` at the start of the procedure and then using `rand!` instead of `rand` might be efficient too

---

<div class="post-metadata">

**Author:** ![jiyashxi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiyashxi/32/208173_2.png) [@jiyashxi](https://discourse.julialang.org/u/jiyashxi)\
**Post date:** [May 6, 2024, 2:50am UTC](https://discourse.julialang.org/t/static-matrix-with-size-400/113843/7 "2024-05-06T02:50:00Z")

</div>

Hi

I basically want to generate some haar random unitary matrices. The code I used before is like

```julia
using StaticArrays
using LinearAlgebra
using StatsBase
using Random

function rand_haar2(::Val{n}) where n
    M = @SMatrix randn(ComplexF64,n,n) 
    q = qr(M).Q
    L = cispi.(2 .* @SVector(rand(Float64,n)))
    return q*diagm(L)
end

```

Then I want to frequently use the function for instance `u = rand_haar2(Val{4}())` if I want random unitary matrices with size 4. This worked well before. But now I want to generate random unitary matrices with size 20. This seems to be not so efficient based on the discussion in the link I posted. I am using the version without static arrays.

```julia
function rand_haar2_new(::Val{n}) where n
    M = randn(ComplexF64,n,n) 
    q = qr(M).Q
    L = cispi.(2 .* (rand(Float64,n)))
    return q*diagm(L)
end

```

It makes my code a little faster. But I am thinking if there can be further improvement.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/224680_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [May 6, 2024, 6:07am UTC](https://discourse.julialang.org/t/static-matrix-with-size-400/113843/8 "2024-05-06T06:07:33Z")

</div>

> [@jiyashxi](#):
>
> I am using the version without static arrays.

In the version without StaticArrays, you don’t need the `n` to be known statically, i.e. enclosed in a `Val`. This will only slow down compilation. Moreover, you will be better off reusing the memory you create, like so:

```julia
using LinearAlgebra
using Random: rand!, randn!

function rand_haar2!(A::AbstractMatrix, M::AbstractMatrix, L::AbstractVector)
    randn!(M)
    Q = qr!(M).Q
    rand!(L)
    copyto!(A, Q)
    transpose(A) .*= cispi.(2 .* L) # double check that this does indeed multiply the columns of `A`
    return A
end

n = 20
M = randn(ComplexF64, n, n);
L = rand(Float64, n);
A = similar(M);

rand_haar2!(A, M, L)

```
