# Is there a better name for «moving array elements»?

**URL:** <https://fortran-lang.discourse.group/t/is-there-a-better-name-for-moving-array-elements/3701>\
**Category:** Homework\
**Created:** [June 8, 2022, 5:57pm UTC](https://fortran-lang.discourse.group/t/is-there-a-better-name-for-moving-array-elements/3701 "2022-06-08T17:57:41Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![nbehrnd](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@nbehrnd](https://fortran-lang.discourse.group/u/nbehrnd)\
**Post date:** [June 8, 2022, 5:57pm UTC](https://fortran-lang.discourse.group/t/is-there-a-better-name-for-moving-array-elements/3701/1 "2022-06-08T17:57:41Z")

</div>

Brad Richardson’s most recent [Fortran beginners class](https://fortran-lang.discourse.group/t/new-course-available/3620) includes an array operation which I was not aware, nor found a better name than «moving array elements». Is there a better, i.e. well-recognized name for this?

There is an array like one in depicted in the top line with seven elements:

 ![element_move](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/7/793f0e382256383831c533b45b0d750a2dd69f2c.png)

The operation defines a slice (blue box) which overwrite _en bloc_ the elements prior to this. As a result (second line), the particular block starts earlier. On the other hand, I speculate it might not be called a move _per se_ because it isn’t a shift leaving a gap as the slot on `array(5)` isn’t vacated (elements `array(6:7)` aren’t affected anyway).

The underlying minimal working example used here:

```fortran
program update
   implicit none

   integer :: i, start_point, end_point
   integer :: testarray(7)

   start_point = 1
   end_point = 4

! explicitly populate array with non-zero data
   do i = 1, 7
      testarray(i) = 2*i
   end do
   print *, testarray

! the operation
   do i = start_point, end_point
      testarray(i) = testarray(i + 1)
   end do
   print *, testarray

end program update

```

---

<div class="post-metadata">

**Author:** ![Abhilash](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/abhilash/32/1635_2.png) [@Abhilash](https://fortran-lang.discourse.group/u/Abhilash)\
**Post date:** [June 8, 2022, 7:36pm UTC](https://fortran-lang.discourse.group/t/is-there-a-better-name-for-moving-array-elements/3701/2 "2022-06-08T19:36:00Z")

</div>

The operation you describe is a bit specific. It seems similar to [Circular Shift](https://en.wikipedia.org/wiki/Circular_shift) which applies to the whole array.  
In NumPy, there is a function named `numpy.roll()` function that does this.

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [June 8, 2022, 9:26pm UTC](https://fortran-lang.discourse.group/t/is-there-a-better-name-for-moving-array-elements/3701/3 "2022-06-08T21:26:35Z")

</div>

I hadn’t thought about this as a common pattern, let alone whether it had a name. Interesting. I’ll note though that you can actually accomplish it in a single line like

```fortran
testarray(start_point:end_point) = testarray(start_point+1:end_point+1)

```

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [June 8, 2022, 10:36pm UTC](https://fortran-lang.discourse.group/t/is-there-a-better-name-for-moving-array-elements/3701/4 "2022-06-08T22:36:34Z")

</div>

> [@everythingfunctional](#):
>
> accomplish it in a single line like
> 
> ```auto
> testarray(start_point:end_point) = testarray(start_point+1:end_point+1)
> 
> ```

Where `testarray` is an object of intrinsic type, the above generally works well with current processors.

But with `testarray` as derived types with various levels of object composition and/or inheritance, the one-liner can prove extremely expensive unless the author(s) have taken tremendous care to achieve the equivalent of what other languages such as C++ offer conveniently as **move semantics**.

---

<div class="post-metadata">

**Author:** ![nshaffer](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/nshaffer/32/161_2.png) [@nshaffer](https://fortran-lang.discourse.group/u/nshaffer)\
**Post date:** [June 9, 2022, 2:52am UTC](https://fortran-lang.discourse.group/t/is-there-a-better-name-for-moving-array-elements/3701/5 "2022-06-09T02:52:40Z")

</div>

If I understand correctly, `eoshift` is another way to do this.

```fortran
x(i:j) = eoshift(x(i:j), 1, x(j))

```

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [June 9, 2022, 3:23am UTC](https://fortran-lang.discourse.group/t/is-there-a-better-name-for-moving-array-elements/3701/6 "2022-06-09T03:23:25Z")

</div>

> [@nshaffer](#):
>
> `x(i:j) = eoshift(x(i:j), 1, x(j))`

You mean the following with respect to the depiction in the original post?

```fortran
x(i:j) = eoshift(x(i:j), 1, x(j+1))

```

---

<div class="post-metadata">

**Author:** ![Pap](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pap/32/1305_2.png) [@Pap](https://fortran-lang.discourse.group/u/Pap)\
**Post date:** [June 9, 2022, 7:52am UTC](https://fortran-lang.discourse.group/t/is-there-a-better-name-for-moving-array-elements/3701/7 "2022-06-09T07:52:26Z")

</div>

I would avoid terms such as _circular shift_ or _rotate left_ because they both imply that the numbers are preserved, which is clearly not the case here (first element is destroyed in this example, and fifth element is duplicated.) I also think the term _rolling left_ is misleading.  
Such an operation reminds Assembly but, at least for the CPUs I know Assembly, there is no equivalent instruction. All “shift” and “rotate” operations are non-destructive, in the sense elements are somehow preserved - either by cycling all the elements or by using an external register to store the element that is lost. There is no instruction such as this, destroying one element and duplicating another one.  
So I am not aware of a proper name for this, but if I had to pick one, I would call it _partial left shift_. As others already pointed out, this is a one-liner for Fortran 90 or above; there is no need for a do-loop.

---

<div class="post-metadata">

**Author:** ![nbehrnd](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@nbehrnd](https://fortran-lang.discourse.group/u/nbehrnd)\
**Post date:** [June 9, 2022, 9:20am UTC](https://fortran-lang.discourse.group/t/is-there-a-better-name-for-moving-array-elements/3701/8 "2022-06-09T09:20:33Z")

</div>

The example Wikipedia’s article uses to illustrate the _circular shift_ on an array `(A, B, C, D)` is the sequence

```nohighlight
A, B, C, D
D, A, B, C
C, D, A, B
B, C, D, A

```

Processing this line by line, the result is like the truncation of the last element of the array so that it is prepended to the remainder of the array (in a way, _recycled_).

This pattern however does not match my observation applying Brad’s instruction. Thus (including the input provided by subsequent answers and their ramifications) _move_ now appears to me as _acceptable_ description about the result shown in the figure. On the other hand, _cyclic shift_ (because of _cycle_) does not.

---

<div class="post-metadata">

**Author:** ![nbehrnd](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@nbehrnd](https://fortran-lang.discourse.group/u/nbehrnd)\
**Post date:** [June 9, 2022, 9:25am UTC](https://fortran-lang.discourse.group/t/is-there-a-better-name-for-moving-array-elements/3701/9 "2022-06-09T09:25:22Z")

</div>

I speculate the bit derived from your class (and now this one) are examples of an elegant transformation for what the execution requires only few instructions in the source code (e.g., no `do` loop, as @Pap hints). And without background in computer science, past the _aha moment_ of first encounter, a pattern one likely does not forget / “unsee” soon.

---

<div class="post-metadata">

**Author:** ![nbehrnd](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@nbehrnd](https://fortran-lang.discourse.group/u/nbehrnd)\
**Post date:** [June 9, 2022, 9:32am UTC](https://fortran-lang.discourse.group/t/is-there-a-better-name-for-moving-array-elements/3701/10 "2022-06-09T09:32:27Z")

</div>

There are (much deeper than I anticipated) insights about [move semantics](https://stackoverflow.com/questions/3106110/what-is-move-semantics) in dedicated answers on stackoverflow. Though addressing C++, one of the take home messages for me is _move_ in _move elements_ of the original question likely is an acceptable description.

---

<div class="post-metadata">

**Author:** ![nbehrnd](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@nbehrnd](https://fortran-lang.discourse.group/u/nbehrnd)\
**Post date:** [June 9, 2022, 9:36am UTC](https://fortran-lang.discourse.group/t/is-there-a-better-name-for-moving-array-elements/3701/11 "2022-06-09T09:36:35Z")

</div>

Based on the example on [Fortran wiki](https://fortranwiki.org/fortran/show/eoshift), I modified my MWE to include your suggestion:

```fortran
program update
   implicit none

   integer :: i, start_point, end_point
   integer :: testarray(7)

   start_point = 1
   end_point = 4

! explicitly populate array with non-zero data
   do i = 1, 7
      testarray(i) = 2*i
   end do
   print *, testarray

!! the operation
! do i = start_point, end_point
! testarray(i) = testarray(i + 1)
! end do

! suggestion nshaffer:
! x(i:j) = eoshift(x(i:j), 1, x(j))
   testarray(start_point:end_point) = eoshift(testarray(start_point:end_point), 1, testarray(end_point))
   print *, testarray

end program update

```

which indeed, as correctly spot by @FortranFan, is off by one compared to the result shown in the original question.

---

<div class="post-metadata">

**Author:** ![nbehrnd](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@nbehrnd](https://fortran-lang.discourse.group/u/nbehrnd)\
**Post date:** [June 9, 2022, 9:50am UTC](https://fortran-lang.discourse.group/t/is-there-a-better-name-for-moving-array-elements/3701/12 "2022-06-09T09:50:55Z")

</div>

Here it becomes puzzling for me. Adjusting my initial MWE to accommodate your suggestion, i.e.

```fortran
program update
   implicit none

   integer :: i, start_point, end_point
   integer :: testarray(7)

   start_point = 1
   end_point = 4

! explicitly populate array with non-zero data
   do i = 1, 7
      testarray(i) = 2*i
   end do
   print *, testarray

!! the operation
! do i = start_point, end_point
! testarray(i) = testarray(i + 1)
! end do

!! suggestion natshaffer:
!! x(i:j) = eoshift(x(i:j), 1, x(j))
! testarray(start_point:end_point) = eoshift(testarray(start_point:end_point), 1, testarray(end_point))
! print *, testarray

! suggestion fortranfan
! x(i:j) = eoshift(x(i:j), 1, x(j + 1))
   testarray(start_point:end_point) = eoshift(testarray(start_point:end_point), 1, testarray(end_point + 1))
   print *, testarray

end program update

```

yields a result matching my observation in the original post.

---

<div class="post-metadata">

**Author:** ![nbehrnd](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@nbehrnd](https://fortran-lang.discourse.group/u/nbehrnd)\
**Post date:** [June 9, 2022, 9:55am UTC](https://fortran-lang.discourse.group/t/is-there-a-better-name-for-moving-array-elements/3701/13 "2022-06-09T09:55:13Z")

</div>

With what I learned by now, I agree _shift_ is succinct, brief, acceptable.

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [June 10, 2022, 1:50pm UTC](https://fortran-lang.discourse.group/t/is-there-a-better-name-for-moving-array-elements/3701/14 "2022-06-10T13:50:33Z")

</div>

> [@nbehrnd](#):
>
> It is puzzling because the result matches the one based on @nshaffer suggestion. Is it possible that I misinterpret your suggestion / omit a crucial detail?

@nbehrnd, are you sure about “the result matches the one based on @nshaffer suggestion”?

A rather terse case with Intel oneAPI behaves as I implied above:

```fortran
   integer :: x(7), y(7)
   x = [( i, integer :: i=2,14,2 )] ; y = x
   print *, "Initially, x = ", x
   print *, "Initially, y = ", y
   x(1:4) = eoshift( x(1:4), 1, x(5) )
   y(1:4) = eoshift( y(1:4), 1, y(4) )
   print *, 'Following EOSHIFT with "x(j+1)" as boundary, x = ', x
   print *, 'Following EOSHIFT with x(j) as boundary, y = ', y
end

```

```auto
C:\temp>ifort /standard-semantics p.f90
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.6.0 Build 20220226_000000
Copyright (C) 1985-2022 Intel Corporation. All rights reserved.

Microsoft (R) Incremental Linker Version 14.30.30706.0
Copyright (C) Microsoft Corporation. All rights reserved.

-out:p.exe
-subsystem:console
p.obj

C:\temp>p.exe
 Initially, x = 2 4 6 8 10 12 14
 Initially, y = 2 4 6 8 10 12 14
 Following EOSHIFT with "x(j+1)" as boundary, x = 4 6 8 10 10 12 14
 Following EOSHIFT with "x(j)" as boundary, y = 4 6 8 8 10 12 14

```

---

<div class="post-metadata">

**Author:** ![nbehrnd](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@nbehrnd](https://fortran-lang.discourse.group/u/nbehrnd)\
**Post date:** [June 11, 2022, 9:03pm UTC](https://fortran-lang.discourse.group/t/is-there-a-better-name-for-moving-array-elements/3701/15 "2022-06-11T21:03:38Z")

</div>

Thank you very much @FortranFan for pointing this out; I re-run each individually and compared the results. gfortran can only assist in spotting some, but can’t warn against all errors possible.
