# Why get(A) involves allocation?

**URL:** <https://discourse.julialang.org/t/why-get-a-involves-allocation/39145>\
**Category:** General Usage\
**Created:** [May 8, 2020, 11:26pm UTC](https://discourse.julialang.org/t/why-get-a-involves-allocation/39145 "2020-05-08T23:26:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinhphunguyen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinhphunguyen/32/16316_2.png) [@vinhphunguyen](https://discourse.julialang.org/u/vinhphunguyen)\
**Post date:** [May 8, 2020, 11:26pm UTC](https://discourse.julialang.org/t/why-get-a-involves-allocation/39145/1 "2020-05-08T23:26:41Z")

</div>

Hi all,

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

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

```

---

<div class="post-metadata">

**Author:** ![vinhphunguyen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinhphunguyen/32/16316_2.png) [@vinhphunguyen](https://discourse.julialang.org/u/vinhphunguyen)\
**Post date:** [May 9, 2020, 12:48am UTC](https://discourse.julialang.org/t/why-get-a-involves-allocation/39145/3 "2020-05-09T00:48:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 9, 2020, 12:52am UTC](https://discourse.julialang.org/t/why-get-a-involves-allocation/39145/4 "2020-05-09T00:52:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vinhphunguyen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinhphunguyen/32/16316_2.png) [@vinhphunguyen](https://discourse.julialang.org/u/vinhphunguyen)\
**Post date:** [May 11, 2020, 12:33pm UTC](https://discourse.julialang.org/t/why-get-a-involves-allocation/39145/5 "2020-05-11T12:33:34Z")

</div>

Thanks Oscar.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [May 11, 2020, 2:43pm UTC](https://discourse.julialang.org/t/why-get-a-involves-allocation/39145/6 "2020-05-11T14:43:52Z")

</div>

It is probably [this code](https://github.com/JuliaLang/julia/blob/381693d3dfc9b7072707f6d544f82f6637fc5e7c/stdlib/LinearAlgebra/src/generic.jl#L1470-L1492) that is called, no?

---

<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 11, 2020, 3:07pm UTC](https://discourse.julialang.org/t/why-get-a-involves-allocation/39145/7 "2020-05-11T15:07:04Z")

</div>

If your matrices are 3x3, I’d recomend using [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl).

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

```
