JuliaArrays / JuliaArrays/StaticArrays.jl
diagm does not correctly preserve eltype for StaticArrays
Nobody has claimed this yet.
- Dominant language
- Julia
- Stars
- 844
- Forks
- 159
- Avg merge
- 3d 21h
- Merged PRs (30d)
- 3
Description
I think this is a bug, although it may be that for some reason this behavior is expected:
With a regular array, diagm preserves the element type:
julia> using LinearAlgebra, Unitful
julia> v = [ 1.0, 1.0 ]u"nm"
2-element Vector{Quantity{Float64, 𝐋, Unitful.FreeUnits{(nm,), 𝐋, nothing}}}:
1.0 nm
1.0 nm
julia> eltype(v)
Quantity{Float64, 𝐋, Unitful.FreeUnits{(nm,), 𝐋, nothing}}
julia> m = diagm(v)
2×2 Matrix{Quantity{Float64, 𝐋, Unitful.FreeUnits{(nm,), 𝐋, nothing}}}:
1.0 nm 0.0 nm
0.0 nm 1.0 nm
julia> eltype(m)
Quantity{Float64, 𝐋, Unitful.FreeUnits{(nm,), 𝐋, nothing}}
But with static arrays it does not:
julia> using Unitful, LinearAlgebra
julia> using StaticArrays
julia> v = SVector(1.0, 1.0)u"nm"
2-element SVector{2, Quantity{Float64, 𝐋, Unitful.FreeUnits{(nm,), 𝐋, nothing}}} with indices SOneTo(2):
1.0 nm
1.0 nm
julia> eltype(v)
Quantity{Float64, 𝐋, Unitful.FreeUnits{(nm,), 𝐋, nothing}}
julia> m = diagm(v)
2×2 SMatrix{2, 2, Quantity{Float64}, 4} with indices SOneTo(2)×SOneTo(2):
1.0 nm 0.0
0.0 1.0 nm
julia> eltype(m)
Quantity{Float64}
Probably irrelevant, but this is how I had implemented that as a workaround:
julia> function my_diagm(v::StaticVector{N}) where {N}
T = promote_type(typeof.(v)...)
cart_idxs = CartesianIndices((1:N,1:N))
D = SMatrix{N,N,T,N*N}(
ntuple(N*N) do i
c = cart_idxs[i]
if c[1] == c[2]
return v[c[1]]
else
return zero(T)
end
end
)
return D
end
my_diagm (generic function with 1 method)
julia> v = SVector(1.0, 1.0)u"nm"
2-element SVector{2, Quantity{Float64, 𝐋, Unitful.FreeUnits{(nm,), 𝐋, nothing}}} with indices SOneTo(2):
1.0 nm
1.0 nm
julia> my_diagm(v)
2×2 SMatrix{2, 2, Quantity{Float64, 𝐋, Unitful.FreeUnits{(nm,), 𝐋, nothing}}, 4} with indices SOneTo(2)×SOneTo(2):
1.0 nm 0.0 nm
0.0 nm 1.0 nm
This function will fail if the type promoted has not zero defined, but that seems reasonable to me, as it would be ambiguous what to put off the diagonals.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the regular-array and StaticArrays examples in the issue, including the Unitful-valued SVector, then locate the implementation of diagm used for static vectors. Confirm how the off-diagonal zero values determine the result type; done means the static result preserves the input element type and the regression is covered by a test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100