TExcelTAD
Home › Fortran › Fortran Array Operations & Slicing

Fortran Array Operations & Slicing

In most languages you process an array with a loop. In Fortran you can usually write the operation on the whole array and let the compiler generate the loop. This is shorter, clearer and often faster.

Whole-array arithmetic

Arithmetic operators and most intrinsic functions work element by element on arrays of the same shape. A scalar combined with an array is applied to every element.

fortran · try it yourself
program whole_array
  implicit none
  real :: celsius(5) = [-10.0, 0.0, 21.5, 37.0, 100.0]
  real :: fahrenheit(5)
  real :: a(3) = [1.0, 2.0, 3.0], b(3) = [10.0, 20.0, 30.0]

  fahrenheit = celsius * 9.0 / 5.0 + 32.0
  print '(5f8.1)', fahrenheit

  print '(3f8.1)', a + b
  print '(3f8.1)', a * b          ! element-wise, NOT a dot product
  print '(3f8.3)', sqrt(a)        ! intrinsic applied to each element
  print '(3f8.1)', max(a, 2.0)    ! element-wise max with a scalar
end program whole_array
    14.0    32.0    70.7    98.6   212.0
    11.0    22.0    33.0
    10.0    40.0    90.0
   1.000   1.414   1.732
     2.0     2.0     3.0

Arrays in one operation must be conformable: the same shape (same number of elements in each dimension), or one of them a scalar. The bounds don't have to match, only the extents.

Slicing: array sections

An array section selects part of an array with start:end:stride. Any part can be left out: a(:3) is from the beginning to 3, a(4:) from 4 to the end, a(:) the whole array, and a(::2) every second element.

fortran · try it yourself
program slicing
  implicit none
  integer :: i
  integer :: a(10) = [(i, i = 1, 10)]

  print '(a, *(i3))', 'a(3:6)   =', a(3:6)
  print '(a, *(i3))', 'a(:4)    =', a(:4)
  print '(a, *(i3))', 'a(8:)    =', a(8:)
  print '(a, *(i3))', 'a(1:9:2) =', a(1:9:2)
  print '(a, *(i3))', 'a(10:1:-3)=', a(10:1:-3)
  print '(a, *(i3))', 'a(::-1)  =', a(10:1:-1)
end program slicing
a(3:6)   =  3  4  5  6
a(:4)    =  1  2  3  4
a(8:)    =  8  9 10
a(1:9:2) =  1  3  5  7  9
a(10:1:-3)= 10  7  4  1
a(::-1)  = 10  9  8  7  6  5  4  3  2  1

Unlike Python, the end index is included: a(3:6) has four elements.

Assigning to sections

Sections can appear on the left of an assignment, which makes many updates one-liners:

fortran · try it yourself
program section_assign
  implicit none
  integer :: i
  real :: signal(8) = [(real(i), i = 1, 8)]
  integer :: v(6) = [1, 2, 3, 4, 5, 6]

  signal(1:3) = 0.0                ! zero the first three
  signal(6:) = signal(6:) * 10.0   ! scale the tail
  print '(8f6.1)', signal

  v(2:6) = v(1:5)                  ! shift right by one
  print '(6i3)', v
end program section_assign
   0.0   0.0   0.0   4.0   5.0  60.0  70.0  80.0
  1  1  2  3  4  5

The shift works correctly because Fortran evaluates the entire right-hand side before assigning, as if it made a temporary copy. A naive element-by-element loop going forwards would instead copy v(1) into every element.

Vector subscripts

An integer array can be used as a subscript to pick arbitrary elements in any order:

fortran · try it yourself
program vector_subscripts
  implicit none
  character(len=5) :: city(5) = [character(len=5) :: 'Oslo', 'Rome', 'Paris', 'Lima', 'Cairo']
  integer :: picks(3) = [5, 1, 3]

  print '(3(a, 1x))', city(picks)
  print '(5(a, 1x))', city([2, 2, 4, 4, 1])
end program vector_subscripts
Cairo Oslo  Paris
Rome  Rome  Lima  Lima  Oslo 

Note: A vector subscript may contain repeated indices when you read elements, but not when you assign to them: a([1, 1]) = … is invalid because the same element would be assigned twice.

Differences, running values and other array tricks

Many classic loops become one expression with sections:

fortran · try it yourself
program array_tricks
  implicit none
  real :: position(6) = [0.0, 1.5, 4.0, 7.5, 12.0, 17.5]   ! metres, one per second
  real :: velocity(5), mid(5)

  velocity = position(2:) - position(:5)          ! forward differences
  mid = 0.5 * (position(2:) + position(:5))        ! midpoints
  print '(a, 5f6.2)', 'velocity: ', velocity
  print '(a, 5f6.2)', 'midpoint: ', mid
  print '(a, f6.2)',  'average speed: ', sum(velocity) / size(velocity)
end program array_tricks
velocity:   1.50  2.50  3.50  4.50  5.50
midpoint:   0.75  2.75  5.75  9.75 14.75
average speed:   3.50

Test yourself

How many elements does a(2:8:3) contain?

  1. 2
  2. 3
  3. 6
  4. 7

What does a * b compute for two real arrays of the same shape?

  1. The dot product
  2. The matrix product
  3. The element-wise product
  4. A compile error

Why does v(2:6) = v(1:5) shift the array correctly?

  1. Fortran copies from the end backwards
  2. The whole right-hand side is evaluated before any element is assigned
  3. It doesn't; it fills with v(1)
  4. Sections are always copied twice