# Circshift() shifts array in opposite direction to circshift!()

**URL:** <https://discourse.julialang.org/t/circshift-shifts-array-in-opposite-direction-to-circshift/107584>\
**Category:** New to Julia\
**Tags:** question, function\
**Created:** [December 13, 2023, 9:18pm UTC](https://discourse.julialang.org/t/circshift-shifts-array-in-opposite-direction-to-circshift/107584 "2023-12-13T21:18:15Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![LucasMontoya4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucasmontoya4/32/205442_2.png) [@LucasMontoya4](https://discourse.julialang.org/u/LucasMontoya4)\
**Post date:** [December 13, 2023, 9:18pm UTC](https://discourse.julialang.org/t/circshift-shifts-array-in-opposite-direction-to-circshift/107584/1 "2023-12-13T21:18:15Z")

</div>

Hi there,

I have written the following little code in Julia 1.8 to try to figure out a bug. I’m trying to simulate periodic boundary conditions, and hence using the circshift function. But for some reason, it seems like the circshift(a,1) of a vector a will shift in the opposite direction to circshift!(a,1), even though I thought (I’m pretty new to Julia) that the only difference between these functions should be that the former creates a copy of its output and leaves a unchanged, and the latter updates the array.

For example,

```julia
vector = Float64[10,20,30,40,50]
println(circshift!(vector,1))
vector = Float64[10,20,30,40,50]
println(circshift(vector,1))

```

Gives me the output

```julia
[20.0, 30.0, 40.0, 50.0, 10.0]
[50.0, 10.0, 20.0, 30.0, 40.0]

```

Why are they shifting in opposite directions?

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [December 13, 2023, 9:24pm UTC](https://discourse.julialang.org/t/circshift-shifts-array-in-opposite-direction-to-circshift/107584/2 "2023-12-13T21:24:14Z")

</div>

I can’t reproduce that on Julia 1.9

```julia
julia> vector = Float64[10,20,30,40,50]
5-element Vector{Float64}:
 10.0
 20.0
 30.0
 40.0
 50.0

julia> println(circshift!(vector,1))
[50.0, 10.0, 20.0, 30.0, 40.0]

julia> vector = Float64[10,20,30,40,50]
5-element Vector{Float64}:
 10.0
 20.0
 30.0
 40.0
 50.0

julia> println(circshift(vector,1))
[50.0, 10.0, 20.0, 30.0, 40.0]

```

---

<div class="post-metadata">

**Author:** ![LucasMontoya4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucasmontoya4/32/205442_2.png) [@LucasMontoya4](https://discourse.julialang.org/u/LucasMontoya4)\
**Post date:** [December 13, 2023, 9:27pm UTC](https://discourse.julialang.org/t/circshift-shifts-array-in-opposite-direction-to-circshift/107584/3 "2023-12-13T21:27:37Z")

</div>

This must be the issue - it must have been changed in 1.9. On my Julia 1.8 REPL, I get

```julia
julia> vector = Float64[10,20,30,40,50]
5-element Vector{Float64}:
 10.0
 20.0
 30.0
 40.0
 50.0

julia> println(circshift!(vector,1))
[20.0, 30.0, 40.0, 50.0, 10.0]

```

which is the opposite of your result. I guess I need an update.

Thank you!

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [December 13, 2023, 9:28pm UTC](https://discourse.julialang.org/t/circshift-shifts-array-in-opposite-direction-to-circshift/107584/4 "2023-12-13T21:28:37Z")

</div>

are you sure there’s not some other hidden state in your REPL? these things sometimes happen during interactive use

I ask because I also do not reproduce even on 1.8, so an update should not be necessary (although I might recommend you update anyway 🙂 for new features and performance improvements)

---

<div class="post-metadata">

**Author:** ![LucasMontoya4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucasmontoya4/32/205442_2.png) [@LucasMontoya4](https://discourse.julialang.org/u/LucasMontoya4)\
**Post date:** [December 13, 2023, 9:29pm UTC](https://discourse.julialang.org/t/circshift-shifts-array-in-opposite-direction-to-circshift/107584/5 "2023-12-13T21:29:23Z")

</div>

I just started the REPL in a terminal specifically for this task, so I assume that that cannot be the case…?

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [December 13, 2023, 9:31pm UTC](https://discourse.julialang.org/t/circshift-shifts-array-in-opposite-direction-to-circshift/107584/6 "2023-12-13T21:31:02Z")

</div>

just to triple check, so in a completely fresh REPL, pasting

```julia
julia> vector = Float64[10,20,30,40,50];
julia> circshift!(vector,1)

```

outputs `[20.0, 30.0, 40.0, 50.0, 10.0]` ? if so that is pretty strange and does not match the behavior I am seeing. do you have any packages loaded in your `startup.jl` ? what is the exact output of `versioninfo()` ?

one more: how about the output of `@less circshift!(vector,1)` ?

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 13, 2023, 9:37pm UTC](https://discourse.julialang.org/t/circshift-shifts-array-in-opposite-direction-to-circshift/107584/7 "2023-12-13T21:37:08Z")

</div>

I also just checked on version 1.8.5 and v1.10.0-rc2 and on both of them `circshift` agrees with `circshift!` for me.

---

<div class="post-metadata">

**Author:** ![LucasMontoya4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucasmontoya4/32/205442_2.png) [@LucasMontoya4](https://discourse.julialang.org/u/LucasMontoya4)\
**Post date:** [December 13, 2023, 9:37pm UTC](https://discourse.julialang.org/t/circshift-shifts-array-in-opposite-direction-to-circshift/107584/8 "2023-12-13T21:37:44Z")

</div>

Ok, so I have VS code running and its own REPL, but I opened a new terminal window and typed that in and I got this:

```julia
julia> vector = Float64[10,20,30,40,50];

julia> circshift!(vector,1)
5-element Vector{Float64}:
 20.0
 30.0
 40.0
 50.0
 10.0

```

If the VSCode is not affecting it or anything like that, then this should be a fresh REPL.

I believe I have imported many packages, but I’m afraid I don’t really know how to look at my startup.jl file.

The output of `versioninfo` is

```julia
Julia Version 1.8.1
Commit afb6c60d69a (2022-09-06 15:09 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 12 × Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, skylake)
  Threads: 1 on 12 virtual cores

```

I believe the output of the last command you mentioned, `@less circshift!(vector,1)`, is far too long for me to add here. What am I looking for there exactly?

Sorry about this, I really didn’t expect this to take so long to figure out! Thank you very much for your help.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [December 13, 2023, 9:39pm UTC](https://discourse.julialang.org/t/circshift-shifts-array-in-opposite-direction-to-circshift/107584/9 "2023-12-13T21:39:33Z")

</div>

how about ` @which circshift!(vector,1)`?

---

<div class="post-metadata">

**Author:** ![LucasMontoya4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucasmontoya4/32/205442_2.png) [@LucasMontoya4](https://discourse.julialang.org/u/LucasMontoya4)\
**Post date:** [December 13, 2023, 9:40pm UTC](https://discourse.julialang.org/t/circshift-shifts-array-in-opposite-direction-to-circshift/107584/10 "2023-12-13T21:40:20Z")

</div>

That gives

```julia
circshift!(a::AbstractVector, shift::Integer) in Base at abstractarray.jl:3185

```

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [December 13, 2023, 9:40pm UTC](https://discourse.julialang.org/t/circshift-shifts-array-in-opposite-direction-to-circshift/107584/11 "2023-12-13T21:40:32Z")

</div>

this is the output of `@less circshift!(vector, 1)` for me:

```julia
function circshift!(a::AbstractVector, shift::Integer)
    n = length(a)
    n == 0 && return
    shift = mod(shift, n)
    shift == 0 && return
    l = lastindex(a)
    reverse!(a, firstindex(a), l-shift)
    reverse!(a, l-shift+1, lastindex(a))
    reverse!(a)
    return a
end

```

where it will show you the definition of the function being called, was just checking if we have the same definition

it does look like there is an issue specifically only on 1.8.1, that is not present in 1.8.0 or 1.8.5 or any other version I can find, and I can now recreate it

---

<div class="post-metadata">

**Author:** ![raminammour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raminammour/32/13572_2.png) [@raminammour](https://discourse.julialang.org/u/raminammour)\
**Post date:** [December 13, 2023, 9:40pm UTC](https://discourse.julialang.org/t/circshift-shifts-array-in-opposite-direction-to-circshift/107584/12 "2023-12-13T21:40:38Z")

</div>

This issue reproduces in version 1.8.1 (sep 2022) and was fixed here:

> <https://github.com/JuliaLang/julia/pull/46759>
>
> This patch fixes the shifting direction for
> circshift!(x::AbstractVector, n::In…teger), which was opposite the
> direction of circshift(x, n) and circshift!(y, x, n). In addition, this
> patch fixes the method to also work correctly with offset arrays.
> Fixes #46533.

---

<div class="post-metadata">

**Author:** ![LucasMontoya4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucasmontoya4/32/205442_2.png) [@LucasMontoya4](https://discourse.julialang.org/u/LucasMontoya4)\
**Post date:** [December 13, 2023, 9:41pm UTC](https://discourse.julialang.org/t/circshift-shifts-array-in-opposite-direction-to-circshift/107584/13 "2023-12-13T21:41:37Z")

</div>

It looks like you’ve found it! Thank you so much for this - I suppose I will have to update to literally any other version…

---

<div class="post-metadata">

**Author:** ![LucasMontoya4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucasmontoya4/32/205442_2.png) [@LucasMontoya4](https://discourse.julialang.org/u/LucasMontoya4)\
**Post date:** [December 13, 2023, 9:42pm UTC](https://discourse.julialang.org/t/circshift-shifts-array-in-opposite-direction-to-circshift/107584/14 "2023-12-13T21:42:11Z")

</div>

Thank you so much! I should have found that sooner before submitting my post, sorry…

---

<div class="post-metadata">

**Author:** ![raminammour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raminammour/32/13572_2.png) [@raminammour](https://discourse.julialang.org/u/raminammour)\
**Post date:** [December 13, 2023, 9:46pm UTC](https://discourse.julialang.org/t/circshift-shifts-array-in-opposite-direction-to-circshift/107584/15 "2023-12-13T21:46:13Z")

</div>

No apologies needed, this is what this forum is for 🙂

The next person who has this problem would find it and the “solution” faster!
