Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions src/arraymath.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
@inline zeros(::Type{SA}) where {SA <: StaticArray} = _zeros(Size(SA), SA)
@inline zeros(::Type{SA}) where {SA <: StaticArray{<:Tuple}} = zeros(SA{Float64})
@inline zeros(::Type{SA}) where {SA <: StaticArray{<:Tuple, T}} where T = _zeros(Size(SA), SA)
@generated function _zeros(::Size{s}, ::Type{SA}) where {s, SA <: StaticArray}
T = eltype(SA)
if T == Any
T = Float64
end
v = [:(zero($T)) for i = 1:prod(s)]
if SA <: SArray
SA = SArray{Tuple{s...}, T, length(s), prod(s)}
Expand All @@ -18,12 +16,10 @@
end
end

@inline ones(::Type{SA}) where {SA <: StaticArray} = _ones(Size(SA), SA)
@inline ones(::Type{SA}) where {SA <: StaticArray{<:Tuple}} = ones(SA{Float64})
@inline ones(::Type{SA}) where {SA <: StaticArray{<:Tuple, T}} where T = _ones(Size(SA), SA)
@generated function _ones(::Size{s}, ::Type{SA}) where {s, SA <: StaticArray}
T = eltype(SA)
if T == Any
T = Float64
end
v = [:(one($T)) for i = 1:prod(s)]
if SA <: SArray
SA = SArray{Tuple{s...}, T, length(s), prod(s)}
Expand All @@ -38,13 +34,11 @@ end
end
end

@inline fill(val, ::SA) where {SA <: StaticArray} = _fill(val, Size(SA), SA)
@inline fill(val, ::Type{SA}) where {SA <: StaticArray} = _fill(val, Size(SA), SA)
@generated function _fill(val::U, ::Size{s}, ::Type{SA}) where {U, s, SA <: StaticArray}
@inline fill(val, ::SA) where {SA <: StaticArray{<:Tuple}} = _fill(val, Size(SA), SA)
@inline fill(val::U, ::Type{SA}) where {SA <: StaticArray} where U = fill(val, SA{U})
Copy link

@Jollywatt Jollywatt Feb 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't pretend to know much about this code, but is it safe to add the type parameter U to SA{U} like that? (Line 38). What if SA = SVector{N,T} where {N,T} so that SA{U} = SVector{U,T} where T? Then the eltype U would be in the N slot instead of the T slot.

Would something like

@inline fill(val::U, ::Type{SA}) where {SA <: StaticArray{S}} where {U,S} = fill(val, SA{U})

at least ensure that the first type parameter of SA is filled, so that SA{U} fills the second slot? But then what happens when SA = SVector{3,Float64} has no slots to be filled?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, applying U to an arbitrary subtype of StaticArray is not safe.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comments! I have replaced SA{U} with Base.typeintersect(SA, AbstractArray{U}).

@inline fill(val, ::Type{SA}) where {SA <: StaticArray{<:Tuple, T}} where T = _fill(val, Size(SA), SA)
@generated function _fill(val, ::Size{s}, ::Type{SA}) where {s, SA <: StaticArray}
T = eltype(SA)
if T == Any
T = U
end
v = [:val for i = 1:prod(s)]
if SA <: SArray
SA = SArray{Tuple{s...}, T, length(s), prod(s)}
Expand Down