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

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.

1 Like

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.

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

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

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

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.

1 Like