Is there a generic way, given a vector, to produce a square matrix of the “best matching” type? By “best matching” I mean here Matrix
for Base
vectors and views, MMatrix
for static arrays and SizedMatrix
for SizedVector
s from StaticArrays.jl.
For arrays in Base
, there’s of course similar(vec, (length(vec), length(vec)))
. However, it does not produce the desired result with static vectors (returns an ordinary matrix for the sake of type stability).
I have come to the following:
function sqmatr(vec::AbstractVector{T}, F::DataType = T) where {T}
cinds = CartesianIndices((eachindex(vec), eachindex(vec)))
return similar(cinds, F)
end
which handles Base.Vector
and SVector
/MVector
but not SizedVector
(returns MMatrix
when SizedMatrix
would be desired).
The ideal option would be similar(vec * vec')
but without the actual computation of the outer product.
Is there a way to do that without introducing an explicit dependency on StaticArrays.jl?