047. reshape with the optional order and pad arguments#
topic: Arrays
reshape
returns an array with a different shape than the source array.
Optional order argument changes the order of elements of source from the usual column major.
Optional pad argument fills the result, with recycling, after the source is exhausted.
program test_reshape
implicit none
integer, allocatable :: v(:), m(:, :)
v = [1, 2, 3, 4, 5, 6]
print *, "Fill column-wise (default):"
m = reshape(source=v, shape=[2, 3])
print *, m(1, :) ! 1 3 5
print *, m(2, :) ! 2 4 6
print *, "Fill row-wise:"
m = reshape(source=v, shape=[2, 3], order=[2, 1])
print *, m(1, :) ! 1 2 3
print *, m(2, :) ! 4 5 6
print *, "Pad with [1, -1] after exhausting source [0, 0]:"
m = reshape(source=[0, 0], shape=[2, 3], order=[2, 1], pad=[1, -1])
print *, m(1, :) ! 0 0 1
print *, m(2, :) ! -1 1 -1
print *, "Repeat row-wise example supplying `pad` (unused):"
! Here `pad` is unused because `size(source)` equals `product(shape)`
m = reshape(source=v, shape=[2, 3], order=[2, 1], pad=[1, -1])
print *, m(1, :) ! 1 2 3
print *, m(2, :) ! 4 5 6
end program test_reshape
Fill column-wise (default):
1 3 5
2 4 6
Fill row-wise:
1 2 3
4 5 6
Pad with [1, -1] after exhausting source [0, 0]:
0 0 1
-1 1 -1
Repeat row-wise example supplying `pad` (unused):
1 2 3
4 5 6
RESHAPE returns an array with a different shape than the SOURCE array. Optional ORDER argument changes the order of elements of SOURCE from the usual column major. Optional PAD argument fills the result, with recycling, after source is exhausted. pic.twitter.com/Xb3RCZ6LcW
— FortranTip (@fortrantip) December 24, 2021
- 1
Compiled using
GNU Fortran (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0with no flags
