# Should Diagonal(A) give an InexactError if not isdiag(A)?

**URL:** <https://discourse.julialang.org/t/should-diagonal-a-give-an-inexacterror-if-not-isdiag-a/93464>\
**Category:** General Usage\
**Tags:** linearalgebra\
**Created:** [January 24, 2023, 3:16pm UTC](https://discourse.julialang.org/t/should-diagonal-a-give-an-inexacterror-if-not-isdiag-a/93464 "2023-01-24T15:16:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [January 24, 2023, 3:16pm UTC](https://discourse.julialang.org/t/should-diagonal-a-give-an-inexacterror-if-not-isdiag-a/93464/1 "2023-01-24T15:16:19Z")

</div>

I understand the convenience of not having to do `Diagonal(diag(A))`, but with the same logic `Int(1.2)` should give `1`. I would expect an `InexactError`, if `isdiag(A) == false`.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 24, 2023, 3:22pm UTC](https://discourse.julialang.org/t/should-diagonal-a-give-an-inexacterror-if-not-isdiag-a/93464/2 "2023-01-24T15:22:03Z")

</div>

I think it’s analogous to Triangular(A), which also doesn’t look at the other triangle? You should think of these as being like matrix subarrays, not as synonyms for the whole matrix.

Many linear algebra routines exploit this to store other data in other parts of A. For example, LU factorization can store U in the upper half of A and L in the lower half, offering triangular views of both, without storing arrays half full of zeros.

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [January 24, 2023, 3:27pm UTC](https://discourse.julialang.org/t/should-diagonal-a-give-an-inexacterror-if-not-isdiag-a/93464/3 "2023-01-24T15:27:06Z")

</div>

I see your point, but then I would expect `Diagonal` to create a view, as `UpperTriangular` does for example:

```julia
julia> @btime Diagonal($A);
  92.248 μs (16 allocations: 365.41 KiB)

julia> @btime UpperTriangular($A);
  5.387 ns (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [January 25, 2023, 11:33am UTC](https://discourse.julialang.org/t/should-diagonal-a-give-an-inexacterror-if-not-isdiag-a/93464/4 "2023-01-25T11:33:53Z")

</div>

In my opinion, there should be methods `uppertriagonal(A::AbstractMatrix)`, `diagonal(A::AbstractMatrix)`, etc. that return a `UpperTriangular` or `Diagonal`, regardless of the remainder of the matrix. And if I where to call the constructor `UpperTriangular(A::AbstractMatrix)` or `Diagonal(A::AbstractMatrix)` it would check if the conversion is exact or not.

I guess it’s a small detail that no one really cares about, and neither should I.
