RESHAPE, SPREAD, PACK, UNPACK, CSHIFT
Between the arithmetic and the data there is a set of intrinsics that only move values around: they change the shape, add a dimension, select elements, scatter them back, or shift them along. None of them computes a number; all of them save a loop that would be all bookkeeping. This lesson runs the five that a program meets most.
RESHAPE
reshape(source, shape) reads the values of the source in memory order and lays them into a new array of the given shape, again in memory order. The number of values must be the number the shape holds, unless a pad value is given.
The file reshape.f90:
program reshape_demo
implicit none
integer :: a(6), m(2, 3), p(2, 3), o(3, 2)
a = [1, 2, 3, 4, 5, 6]
m = reshape(a, [2, 3])
p = reshape([1, 2, 3, 4], [2, 3], pad=[0])
o = reshape(a, [3, 2], order=[2, 1])
print *, "a :", a
print *, "m :", m
print *, "p :", p
print *, "o :", o
end program reshape_demo
gfortran -Wall -Wextra -std=f2018 reshape.f90 -o reshape
./reshape
a : 1 2 3 4 5 6
m : 1 2 3 4 5 6
p : 1 2 3 4 0 0
o : 1 3 5 2 4 6
The matrix m prints exactly like the vector it came from because a print walks memory order for both: what changed is the shape, not the values. m(1,1) is 1, m(2,1) is 2, m(1,2) is 3, and every later expression indexes it that way.
PAD and ORDER
pad=[0] supplies the values a shape needs when the source is shorter: the four numbers fill the first four places of the two by three result and the last two are the pad value. A shape that needs more values than the source and the pad together is an error: gfortran says there are not enough elements in the source to match the shape, rather than filling the gap with zeros.
order=[2, 1] says which of the source dimensions varies fastest while the values are laid out, so the six numbers fill the three by two result by its second index first: 1 and 2 in the first column, then 3 and 4, then 5 and 6, printed as 1 3 5 2 4 6 in memory order. Without order the result is the plain column-major fill, and both forms have their uses in reading a table that was written row by row.
SPREAD
spread(v, dim=2, ncopies=2) takes a vector and repeats it along a new dimension: the result is a matrix whose every column is the vector. The dimension names the position of the new axis in the result, and the copies are exact copies.
The file spread.f90 also packs and unpacks, because the three are the same idea of moving values between shapes:
program spread_demo
implicit none
integer :: v(3), m(3, 2), grid(3, 2), full(3, 2)
integer, allocatable :: odd(:)
v = [1, 2, 3]
m = spread(v, dim=2, ncopies=2)
odd = pack(v, mod(v, 2) == 1)
grid = 0
grid(1, 1) = 5
grid(3, 2) = 7
full = unpack([5, 7], grid /= 0, -1)
print *, "m :", m
print *, "odd :", odd, " size =", size(odd)
print *, "full :", full
end program spread_demo
gfortran -Wall -Wextra -std=f2018 spread.f90 -o spread
./spread
m : 1 2 3 1 2 3
odd : 1 3 size = 2
full : 5 -1 -1 -1 -1 7
The matrix m is three by two and its two columns are both the vector: the print shows the first column and then the second. spread(v, dim=1, ncopies=2) would give a two by three matrix whose rows are the vector, which is the other axis of the same idea.
PACK and UNPACK
pack(v, mask) returns the selected values as a rank-one array whose length is the number of true elements. That length is not known when the program is written, which is why the result is assigned to an allocatable array here: a fixed-size target of three elements would be right only for this mask, and the third element would hold whatever was there before.
unpack(vector, mask, field) is the other direction: the vector supplies the values in order to the true positions of the mask, and every other position takes the matching element of the field. The program scatters two values into a grid that has two set cells and fills the rest with minus one.
CSHIFT and EOSHIFT
cshift rotates: the values that leave at one end come back at the other. eoshift shifts and fills the vacated places with a boundary value, which is the operation a finite-difference stencil uses.
program shift_demo
implicit none
integer :: a(6)
a = [1, 2, 3, 4, 5, 6]
print *, "a :", a
print *, "cshift 1:", cshift(a, 1)
print *, "cshift-2:", cshift(a, -2)
print *, "eoshift1:", eoshift(a, 1)
print *, "eoshift-1:", eoshift(a, 1, -1)
end program shift_demo
gfortran -Wall -Wextra -std=f2018 shift.f90 -o shift
./shift
a : 1 2 3 4 5 6
cshift 1: 2 3 4 5 6 1
cshift-2: 5 6 1 2 3 4
eoshift1: 2 3 4 5 6 0
eoshift-1: 2 3 4 5 6 -1
A positive shift moves the values to the left and a negative one to the right; cshift(a, -2) put the last two values in front. The two eoshift lines differ only in the boundary: zero is the default for an integer array, and the third argument names any other value.
Exercises
- Reshape the six values to a three by two matrix with the default order and print it. Which positions hold 5 and 6 now?
- Give reshape a shape of eight elements and a pad of two. What does the last line of the matrix hold?
- Pack the even values of the vector and print the size. Then pack with a mask that selects nothing: what size comes out?
- Use eoshift(a, 2, -1) and print the array. Which two elements are the boundary now?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
reshape(a, [2, 3])do with a six-element vector?Answer
Lays the values out in a two by three array in memory order
RESHAPE keeps memory order and changes the shape.
-
What is the PAD argument of RESHAPE for?
Answer
It supplies the values when the source has fewer elements than the shape needs
The pad value fills the places the source does not cover.
-
What does
spread(v, dim=2, ncopies=2)return for a three-element v?Answer
A three by two matrix whose columns are both v
The new axis is the second one, so the copies run across columns.
-
What is the length of
pack(v, mask)?Answer
The number of true elements in the mask
PACK returns exactly the selected values.
-
What does this program print?
program p implicit none integer :: v(4) v = [1, 2, 3, 4] print *, pack(v, mod(v, 2) == 0) end program pAnswer
2 4
The mask selects the even values.
-
What does
unpack([5, 7], grid /= 0, -1)do?Answer
Puts 5 and 7 in the true positions in order and fills the rest with -1
The vector supplies the true positions, the field the others.
-
How do CSHIFT and EOSHIFT differ?
Answer
CSHIFT wraps the values round, EOSHIFT fills the gap with a boundary value
The rotating shift keeps every value, the end-off shift does not.
-
What is
cshift(a, 1)for a = [1, 2, 3, 4]?Answer
2 3 4 1
A positive shift moves values left and wraps the first to the end.
-
Why is a PACK result often assigned to an allocatable array?
Answer
Its length depends on the mask and is not known from the declaration
The result takes the shape of the selected values.
-
What does the ORDER argument of RESHAPE change?
Answer
Which dimension of the source varies fastest while the values are laid out
ORDER permutes the dimensions during the fill.
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