FillArrays.jl icon indicating copy to clipboard operation
FillArrays.jl copied to clipboard

Unify `Ones` and `Zeros` as `SFill{0}` and `SFill{k}`?

Open dlfivefifty opened this issue 5 years ago • 9 comments

It might make the code cleaner to do:

struct SFill{k, T, N, Axes} <: AbstractFill{T,N}
   axes::Axes
end

getindex_value(F::SFill{k,T}) where {k,T} = convert(T,k)

const Ones{T,N,Axes} = SFill{true,T,N,Axes}
const Zeros{T,N,Axes} = SFill{false,T,N,Axes}

It would then be possible to support other special cases. Though whether this machinery is worth it is not clear.

dlfivefifty avatar Jul 06 '20 10:07 dlfivefifty

Coming here from https://github.com/JuliaArrays/StaticArrays.jl/issues/815.

I'll let others debate about whether the machinery is worth it :) A small note - I'm not sure you want the value k in the type if it can be a non bits type? Though it seems fine for the Ones and Zeros you've defined here.

c42f avatar Sep 16 '20 03:09 c42f

We already have Fill when the value is not in the type...

A more pressing case is where the length is static. But it seems this might be better done via Axes? If only there were a SOneTo.

Edit: Errr, there is:

julia> axes(SVector(1,2))
(SOneTo(2),)

So Actually we can already support static lengthed Fill:

julia> Fill(1, (SOneTo{2}(),))
2-element Fill{Int64,1,Tuple{SOneTo{2}}} with indices SOneTo(2): entries equal to 1

dlfivefifty avatar Sep 16 '20 09:09 dlfivefifty

Coming from #176 (compatibility with Static.jl), might

const Ones{T,N,Axes} = Fill{Static.True,N,Axes}
const Zeros{T,N,Axes} = Fill{Static.False,N,Axes}

not be an elegant alternative?

oschulz avatar Apr 06 '22 10:04 oschulz

Where did T go?

dlfivefifty avatar Apr 06 '22 10:04 dlfivefifty

If FillArrays would take on Static as a dependency, also #140 could be extended to enable Fill-to-Fill vcat for arbitrary static values, not just one and zero, we could have

vcat(Fill(static(4.2), 5), Fill(static(4.2), 5)) === Fill(static(4.2), 10)

oschulz avatar Apr 06 '22 10:04 oschulz

Where did T go?

Ooops, indeed! :-) That's a problem, because there's no common static supertype across all value types in Static.jl. But we could do

~~Struct SFill ...~~

(Update: As @dlfivefifty pointed out, no we can't)

oschulz avatar Apr 06 '22 10:04 oschulz

Except then you lose the propagation of static values so there's no point...

dlfivefifty avatar Apr 06 '22 10:04 dlfivefifty

I would say a better solution may be to just support Fill{StaticInt}, etc.

dlfivefifty avatar Apr 06 '22 10:04 dlfivefifty

Except then you lose the propagation of static values so there's no point...

Ah, silly me convert(T,static(k)) doesn't return a static number, of course, that approach won't work. Sorry, I think I misunderstood the semantics of Ones and Zeros a bit.

I would say a better solution may be to just support Fill{StaticInt}, etc.

General support for Static would be awesome (e.g. for the possibilities with vcat mentioned above).

And I guess one you just use Ones{StaticInt}(5) then, right?

oschulz avatar Apr 06 '22 11:04 oschulz