You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For zeros (and ones), the reason this happens is that it falls back to a Base implementation of the kind
function$fs(::Type{T}, dims) where T
A =Array{T}(undef, dims)
fill!(A, $f(T))
end
where e.g., fs = :zeros and f = :zero. StaticArrays' zero implementation correctly figures out the omitted type parameters in zero, but the pre-allocated array doesn't figure it out. You could argue the implementation should be:
$fs(::Type{T}, dims) where T =fill($f(T), dims)
I don't know why it isn't: but it should probably be a fix in Julia rather than StaticArrays. It's a somewhat analogous thing for randn and randexp, but the methods are inherited from the Random stdlib (and a fill approach wouldn't work).
For rand, the methods are actually defined in StaticArrays - but those methods should probably be removed since they do the same thing as the fallback definition in Random.
Implementing zeros in terms of fill may not be possible in general, if the zero element isn't of the same type. To be precise, the result may not have the same eltype.
From https://discourse.julialang.org/t/why-does-smatrix-allocate-in-this-case/127808.
The same behavior happens for
rand
,randn
, and probably any others in this family. And also for theMArray
type.It seems that it should be possible for the function to infer a missing length parameter and use it for the container's type.
The text was updated successfully, but these errors were encountered: