Sections, strides and triplets

A section is a part of an array named by three numbers: the first index, the last index and the step. It is the array equivalent of a substring, and it is a value: assigning one to another variable copies it, and assigning one onto another section of the same array behaves as if the right side had been copied first. This lesson names the three numbers, leaves the ends out, walks backwards, and shifts an array onto itself.

The triplet

a(2:4) is the element range from 2 to 4 inclusive, a section of three elements. a(2:5:2) adds the stride and takes 2 and 4, so its length is two. The stride defaults to one and may be negative, which walks the section backwards.

The file sections.f90:

program sections implicit none integer, parameter :: n = 6 integer :: a(n), first3(3), last3(3) a = [10, 20, 30, 40, 50, 60] print *, "a :", a print *, "a(1:3) :", a(1:3) print *, "a(4:) :", a(4:) print *, "a(:3) :", a(:3) first3 = a(1:3) last3 = a(4:6) print *, "first3 :", first3 print *, "last3 :", last3 print *, "sum(a(2:4)):", sum(a(2:4)) end program sections

gfortran -Wall -Wextra -std=f2018 sections.f90 -o sections
./sections

a : 10 20 30 40 50 60
a(1:3) : 10 20 30
a(4:) : 40 50 60
a(:3) : 10 20 30
first3 : 10 20 30
last3 : 40 50 60
sum(a(2:4)): 90

Open ends

Either end of a triplet may be left out, and the missing one is the bound of the array: a(4:) runs from 4 to ubound(a) and a(:3) from lbound(a) to 3. The bare a(:) is the whole array as a section, which is the form a procedure argument uses when it wants the value and not a new shape.

An open end and an empty one are different things. a(4:) is a section of three elements and a(7:) would be out of bounds; there is no syntax for "the part after the end", because a section of a fixed-shape array cannot grow.

Strides and reverse order

The third number makes a section out of every other element, or out of a reversed range. A negative stride requires the first index to be larger than the last, and a positive stride requires the opposite: the other combination is an empty section, and gfortran warns when it can see it.

The file strides.f90:

program strides implicit none integer :: a(6) a = [1, 2, 3, 4, 5, 6] print *, "a :", a print *, "a(1:6:2) :", a(1:6:2) print *, "a(2:6:2) :", a(2:6:2) print *, "a(6:1:-1) :", a(6:1:-1) print *, "a(6:1:-2) :", a(6:1:-2) a(1:5) = a(2:6) print *, "shifted :", a end program strides

gfortran -Wall -Wextra -std=f2018 strides.f90 -o strides
./strides

a : 1 2 3 4 5 6
a(1:6:2) : 1 3 5
a(2:6:2) : 2 4 6
a(6:1:-1) : 6 5 4 3 2 1
a(6:1:-2) : 6 4 2
shifted : 2 3 4 5 6 6

A section is a value

The last line of the output is the one to read twice. a(1:5) = a(2:6) shifts the array one place to the left, and the sixth element keeps its old value: 1 2 3 4 5 6 becomes 2 3 4 5 6 6. The two sections overlap, and the standard says the right side is evaluated completely before the assignment begins, so the overlap costs nothing and the result is the one a reader expects.

That rule is why a shift is one statement and not a loop with a temporary. The same rule makes a(2:6) = a(1:5) a shift to the right, and both are correct whatever the compiler does with the instruction order, because the semantics are defined on copies.

Section assignment also checks the shape. a(1:3) = a(2:6) is an error, because the left side has three elements and the right five, and the compiler reports both lengths the way the array assignment error did.

Exercises

  1. Print a(2:6:3) for the six-element array. How many elements does it have?
  2. Reverse the array with a = a(6:1:-1) and print it. Does it work with the assignment on one line?
  3. Write the shift as a(2:6) = a(1:5) and print the array. Which element is duplicated now?
  4. Print size(a(2:6:2)) and count the values by hand. Do they agree?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does a(2:4) contain?

    Answer

    The elements at 2, 3 and 4

    A section includes both ends, so its length is 4 - 2 + 1.

  2. What does a(1:6:2) take?

    Answer

    Elements 1, 3 and 5

    The stride of two skips every other element from the first.

  3. What does a(6:1:-1) give?

    Answer

    The array in reverse order

    A negative stride walks from the first index down to the last.

  4. What is a(4:) for a six-element array?

    Answer

    Elements 4, 5 and 6

    A missing last index means the upper bound.

  5. What does this program print? program p implicit none integer :: a(4) a = [1, 2, 3, 4] print *, sum(a(2:3)) end program p

    Answer

    5

    The section holds 2 and 3, and their sum is 5.

  6. What happens in a(1:5) = a(2:6) when the sections overlap?

    Answer

    The right side is evaluated first, so the shift is correct

    The assignment defines the right side as a value, so overlap is harmless.

  7. What is size(a(1:6:2)) for a six-element array?

    Answer

    3

    The section holds indices 1, 3 and 5.

  8. What does a(:) mean?

    Answer

    The whole array as a section

    Both ends are open, so the section is the whole array.

  9. What does a section assignment check?

    Answer

    That the two sections have the same shape

    Element by element assignment requires equal shapes.

  10. Which combination is an empty section?

    Answer

    A positive stride with the first index above the last

    The stride determines which direction the range must run.

The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev