# How to obtain the diagonal vector of a Diagonal matrix without allocations?

**URL:** <https://discourse.julialang.org/t/how-to-obtain-the-diagonal-vector-of-a-diagonal-matrix-without-allocations/69773>\
**Category:** General Usage\
**Tags:** question, linearalgebra\
**Created:** [October 14, 2021, 6:33pm UTC](https://discourse.julialang.org/t/how-to-obtain-the-diagonal-vector-of-a-diagonal-matrix-without-allocations/69773 "2021-10-14T18:33:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [October 14, 2021, 6:33pm UTC](https://discourse.julialang.org/t/how-to-obtain-the-diagonal-vector-of-a-diagonal-matrix-without-allocations/69773/1 "2021-10-14T18:33:30Z")

</div>

I want to obtain `D.diag` for a `Diagonal` matrix `D`, except I’m not sure if this is public. Using `diag(D)` allocates unnecessarily in certain cases (as it uses `similar` to ensure type-stability).

```julia
julia> D = Diagonal(3:4)
2×2 Diagonal{Int64, UnitRange{Int64}}:
 3 ⋅
 ⋅ 4

julia> diag(D)
2-element Vector{Int64}:
 3
 4

julia> D.diag
3:4

```

How to I obtain the range back, given `D`, in a manner that is considered future-proof to include in a package?

Edit: I’ve found this hack that works, but I’m not sure if this is future-proof:

```julia
julia> eigvals(D)
3:4

```

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [October 14, 2021, 6:37pm UTC](https://discourse.julialang.org/t/how-to-obtain-the-diagonal-vector-of-a-diagonal-matrix-without-allocations/69773/2 "2021-10-14T18:37:09Z")

</div>

Evidently `parent` returns this

```julia
julia> parent(D)
3:4

```
