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

Support diff

Open fredcallaway opened this issue 3 years ago • 1 comments

e.g.

x = KeyedArray(rand(10, 20), x=1:10, y=1:20)
diff(x; dims=:x)

Currently, diff(x; dims=1) errors because diff changes the size of the array. There is some ambiguity about the correct way to handle keys in the result. In some strict sense, the keys should be something like "Δ(1, 2)". But I think the most natural/useful thing would probably be to just use the lower label (so, drop 10).

fredcallaway avatar Mar 03 '22 13:03 fredcallaway

I use this:

function Base.diff(K::KeyedArray; dims, removefirst::Bool=true)

    range = removefirst ? (2:size(K, dims)) : (1:size(K,dims)-1)
    out = similar(selectdim(K, dims, range) )
    out[:] = Base.diff(parent(parent(K)); dims=NamedDims.dim(parent(K),dims))
    return out
end

takbal avatar Mar 06 '22 10:03 takbal