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

Support setindex

Open dlfivefifty opened this issue 4 years ago • 2 comments

The following should be a fast way of creating e_k (the vector of all zeros but with 1 in the k-th entry):

julia> using FillArrays

julia> Base.setindex(Zeros(5), 2, 3)
ERROR: MethodError: no method matching setindex(::Zeros{Float64, 1, Tuple{Base.OneTo{Int64}}}, ::Int64, ::Int64)
Closest candidates are:
  setindex(::Tuple, ::Any, ::Integer) at tuple.jl:49
  setindex(::CartesianIndex, ::Any, ::Any) at multidimensional.jl:100
  setindex(::NamedTuple, ::Any, ::Symbol) at namedtuple.jl:355
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

dlfivefifty avatar Sep 27 '21 20:09 dlfivefifty

What are you picturing this returning?

Zygote's OneElement is something very close to this, which could perhaps be moved upstream if it has other uses. Allows any ndims, any axes.

Flux also has a OneHotVector which is more restricted, stores just a UInt32. A vector of these can be re-interpreted as integers. There was talk of moving that out, too, but perhaps not a good fit here.

mcabbott avatar Oct 29 '21 23:10 mcabbott

Yeah, I guess the proposal would be to move OneElement here

dlfivefifty avatar Oct 30 '21 07:10 dlfivefifty