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

Inverse of `circshift(_, shifts)`

Open jariji opened this issue 1 year ago • 7 comments

I think the inverse of circshift(_, shifts) is circshift(_, map(-, shifts)). Would that fit here?

jariji avatar Jun 20 '24 19:06 jariji

Hm, that's quite specialized - what do you think @devmotion ?

oschulz avatar Jun 21 '24 07:06 oschulz

circshift is underappreciated but has many uses when you want to move around items in an array.

For example, suppose you have a list of items [a,b,c,d,e] in your list and you want to slide d up a couple places.

using Accessors, AccessorsExtra
julia> x = collect(1:5)
5-element Vector{Int64}:
 1
 2
 3
 4
 5

julia> @modify(x |> view(_, 2:4)) do y
           circshift(y, -2)
       end
5-element Vector{Int64}:
 1
 4
 2
 3
 5

Suppose you want to put 10,20 in the middle of a list. Rotate the list so the middle is at the end, append 10,20, rotate back.

julia> using Accessors, InverseFunctions

julia> InverseFunctions.inverse(f::Base.Fix2{typeof(circshift)}) = Base.Fix2(circshift, -f.x);

julia> x = [5,6,7,8,9];

julia> modify(x, @o circshift(_, 3)) do y
         append!(y, 10:10:20)
       end
7-element Vector{Int64}:
  5
  6
 10
 20
  7
  8
  9

jariji avatar Jun 21 '24 20:06 jariji

Sure, it's definitely useful. The only thing is, we don't really have circshift(_, shifts) in Julia yet (https://github.com/JuliaLang/julia/pull/24990), except with Base.Fix2(circshift, shifts). Is that a common use case? If so, I'm not against adding it.

oschulz avatar Jun 22 '24 18:06 oschulz

Nice. Is there one for

slide d up a couple places

too?

jariji avatar Jun 25 '24 18:06 jariji

@jariji, do have have any example use cases in mind?

oschulz avatar Jun 25 '24 20:06 oschulz

Those were the examples I had, but there might be better ones. I'll see what else I can come up with.

jariji avatar Jun 26 '24 18:06 jariji

Thanks - it's just that if we defines inverses also for functions with fixed arguments (beyond some basic math functions like we do now) then there might be a lot of functions in Base that could qualify, but most of them will not really come up in use cases for inverse. We do have setinverse now to quickly set inverses for more rare cases. So with "less obvious" functions that can be invertible with a fixed argument I'd suggest to wait for actual use cases.

oschulz avatar Jun 26 '24 20:06 oschulz