Symmetric vs symmetric

At present we have the Symmetric type in LinearAlgebra which wraps an AbstractMatrix. This seems like a sensible strategy when dealing with dense Matrixs, and things like that. However, (to pick a single example) wrapping a Diagonal matrix in a Symmetric seems somewhat redundant and, at present, we don’t have functionality that says “give me a symmetric version of a matrix x, and do it in whatever way the author of the type of x deems best”.

Is there a particular reason that we don’t have a function like this, that mirrors e.g. permutedims vs PermutedDimsArray? There appears to be a symmetric function in LinearAlgebra, but it’s not exported, so presumably it’s not intended for use in general.

n.b. I’ve assumed here that if one writes Type(args...) that you should always expect to in fact receive an output of type Type, so it would be incorrect for Symmetric(x) to return anything that isn’t a Symmetric.

edit: I’ve also assumed that array authors are free to implement permutedims in whatever way they deem most suitable for their and, in particular, that they needn’t return a PermutedDimsArray if they choose not to. If this assumption is incorrect, I would appreciate being told about it!

1 Like

Perhaps a better analogy is transpose, where transpose(Diagonal(1:3)) doesn’t do anything.

I’m not sure permutedims ever returns a PermutedDimsArray, although that’s another similar question – why isn’t there lower-case counterpart to PermutedDimsArray, too? Which could, for instance, unwrap this wrapper and return an Array, without violating the T(a) isa T rule.

If this symmetric existed, what would symmetric(reshape(1:9,3,3)) return? Would it symmetrise, or merely ignore the lower half like the Symmetric wrapper?

1 Like

:man_facepalming: you’re completely correct about permutedims – I just assumed that was what it did haha. I agree that transpose is a better analogy.

If this symmetric existed, what would symmetric(reshape(1:9,3,3)) return? Would it symmetrise, or merely ignore the lower half like the Symmetric wrapper?

Unclear to me, but my first inclination would be to say that the Symmetric wrapper seems like a good choice here because you continue to defer instantiating anything, which seems like a desirable outcome but as I say. Maybe a good fallback implementation would be to return a Symmetric?

edit: I think you probably do want to stick with Symmetric the vast majority of the time, because knowing that a matrix is Symmetric based on the type is quite helpful information generally. I could certainly be persuaded that you would really only want to return something other than a Symmetric if the matrix type in question only admits symmetric matrices anyway, so wrapping it in a Symmetric tells you nothing new.

edit 2: Diagonal and UniformScaling seem like prime candidates for this in LinearAlgebra (symmetric could be made to work for UniformScaling where Symmetric fails), while things in PDMats.jl seem like good candidates in the wild.