Arrays
Definitions
Note: an array may have a null size (for any rank ≥ 1), it is no problem (if a null-size array appears in an expression, no operation is done).
Declaration of an array with constant shape
- Constant explicit shape: the (lower bounds and) extents are known at compile-time. Example :
real a(10), b(10), c(10)
-
The default lower bound is 1. But you can choose another lower bound. Example :
real x(-2:3, 5)
-
You can use named constants for the extents. Example :
integer, parameter:: m = 10, n = 5 real a(m,n)
Array constructor
-
Write expressions, separated by commas, between square brackets:
[expression_1, expression_2, ...]
-
All the expressions must have the same type. Examples :
real v(3)
v = [3.2, 4., 6.51]
character(len=3), parameter:: currency(2) = ["EUR", "FRA"]
Selection of an array element
-
Through a list of subscripts. Number of subscripts: rank of the array. Each subscript must be an integer expression.
-
Example :
t3(i * j, 1, k / i + 2)
Array section
-
Choose an arithmetic progression for one or more dimensions.
a:b:r
where :a
is first subscript, defaults to lower boundb
: is limiting subscript, defaults to upper boundr
: is stride, defaults to 1, may be < 0
-
Examples, assuming
t1
is a vector andmat
a rank-2 array:t1(m:n)
,t1(m + n:n:-1) mat(i, :)
,mat(:i, j:)
Array expression
-
All the intrinsic operators (arithmetic, logical, relational, character) apply to array operands.
-
Array operands must be conformable: they must have the same shape.
-
A scalar and an array are also considered as conformable.
-
The result is an array with the same shape as the operands.
-
Examples :
-
Scalar and array, assuming
mat
is a rank-2 array:mat / 2
-
Several arrays:
mat(:, 0) * t3(:, 1, 1)
real a(5), b(5)
a == b
: logical array -
Array assignment
You can assign an array expression to a conformable array. Examples,
assuming a
is a vector of size 10 and mat
a rank-2 array :
a = a(10:1:-1)
æ
.
mat = 0
zeroes all elements of mat
.
a(::2) = 0.
a
which have an odd subscript.
Reading or printing an array
In a read or print statement, you can use not only array elements but also whole arrays (or array sections). Examples :
integer a(10, 2, 3), b(8)
Not only:
read *, a(3, 2, 1)
print *, a(4, 1, 2)
but also possible:
read *, b
print *, a(5:, :, 2)
Array element order
-
Reading or printing an array is done in “array element order”.
-
Order: the subscripts along the first dimension vary most rapidly. Exemple :
real a(2, 2)
a(1, 1)
,a(2, 1)
,a(1, 2)
,a(2, 2)
-
Nota bene : array element order normally corresponds to storage order. So, if you need to loop over the elements of an array, do it in array element order.
-
Example, which programming is more efficient?
do j = 1, n
do i = 1, m
a(i,j)=i+j
end do
end do
do i = 1, m
do j = 1, n
a(i,j)=i+j
end do
end do
Example of printing an array
integer a(10, 2, 3)
print *, a(5:, :, :)
a(5, 1, 1)
, a(6, 1, 1)
, …, a(10, 1, 1)
, a(5, 2, 1)
, …
on as many lines as necessary (newlines may be inserted, depending on
the compiler).
Reading an array
READ *, my_array
Boucle implicite dans un constructeur de vecteur anonyme
[(une expression qui dépend éventuellement de l'indice, variable indice = valeur initiale, valeur finale)]
Exemples :
[(i, i = 1, 10)]
au lieu de :
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Un peu plus élaboré :
[(- pi / 2 + i * delta, i = 0, n)]
crée un tableau réel contenant une suite arithmétique de raison delta
.
Allocatable arrays
-
When you do not know the extents at compile-time.
-
Declare only the rank of the array, using a colon character
:
for each dimension -
Add the attribute
allocatable
real, allocatable:: a(:), b(:, :)
-
Set the shape of the array at execution-time, either implicitly by assigning an array expression:
a = [3, 5, 1]
or explicitly with an allocate statement:
allocate(a(n:m), b(p, q))
The explicit allocation is useful if, afterwards, you are not going to define the whole array in a single statement.
- The intrinsic function
size
gives you the total number of elements in an array. Example:
allocate(a(n:m), b(p, q))
size(a)
returns m - n + 1, size(b)
returns p × q. The
function size
can also give the number of indices in a given
dimension: size(b, 1)
returns p, size(b, 2)
returns q. Note that
the function size
also works with non-allocatable arrays.
The things to remember about arrays
-
Fortran allows you to use not only array elements but also whole arrays or array sections in expressions, assignments, input and ouput statements.
-
You improve concision and clarity by using this feature instead of programming loops on array elements.