Inconsistent behaviour of Diagonal and Triangular matrix constructors


Let e.g.: R=randn(2,2), (a general square matrix) then U = UpperTriangular(R) or L = LowerTriangular(R) would sucessfully return triangular matrices. Also, D = Diagonal(R) works similarly. In all these cases, some (in general) non-zero elements of the original R have been effectively zeroed, without throwing errors. (You can verify they are zero by doing collect(U) etc.)

However, Diagonal(U), or Diagonal(L) throws ArgumentError: matrix cannot be represented as Diagonal because of the non-zero off-diagonal elements in the triangular inputs.

While the error can be avoided by explicitly doing Diagonal(diag(L)) , I would argue that this behaviour of the Diagonal constructor is inconsistent between full and triangular arguments. It is also inconsistent with the UpperTriangular and LowerTriangular constructors.

My vote would be to resolve the inconsistencies by e.g.:

Diagonal(Tr::AbstractTriangular) = Diagonal(diag(Tr))

or something equivalent.


I think that this is a reasonable suggestion. There is already an open issue,

which seems to be dormant. Perhaps revive it, ideally by making a small PR.

1 Like