# Why get(A) involves allocation?

Hi all,

Could u please explain why get(A) where A is a matrix involves allocation? The code is:

``````julia> a=ones(3,3)
3×3 Array{Float64,2}:
1.0  1.0  1.0
1.0  1.0  1.0
1.0  1.0  1.0

julia> function f(a)
return det(a)
end
f (generic function with 1 method)

julia> time_f(a) = @time f(a)
time_f (generic function with 1 method)

julia> time_f
time_f (generic function with 1 method)

julia> time_f(a)
0.000069 seconds (3 allocations: 304 bytes)
0.0
``````

Thanks Oscar. Even though I do not understand why determinant is a scalar, but it needs a matrix for output. Anyway, there is any way to remove this allocation as I need to compute det(A) in inner loops.

Sorry, I didn’t mean for computing the output. I meant for workspace. My guess (I’m double checking) is that `det` is probably found using a LU decomposition (confirmed) or similar, which requires a matrix of workspace. This means that if you can store the matrix in LU form, the det computation would be very fast and non-allocating.

2 Likes

Thanks Oscar.

It is probably this code that is called, no?

If your matrices are 3x3, I’d recomend using StaticArrays.jl.

``````julia> using StaticArrays, LinearAlgebra, BenchmarkTools

julia> A = @SMatrix ones(3,3);

julia> B = @SMatrix rand(3,3);

julia> @btime det(\$(Ref(A))[])
1.575 ns (0 allocations: 0 bytes)
0.0

julia> @btime det(\$(Ref(B))[])
1.577 ns (0 allocations: 0 bytes)
-0.012987097768484175
``````