Fortran 简明教程
Fortran - Arrays
数组可以存储同一类型的固定大小的连续元素集合。数组用于存储数据集合,但将数组视为同类型变量的集合通常更有用。
Arrays can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
所有阵列都包含连续的内存位置。最低地址对应于第一个元素,而最高地址对应于最后一个元素。
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
Numbers(1) |
Numbers(2) |
Numbers(3) |
Numbers(4) |
… |
数组可以是一维的(如向量)、二维的(如矩阵),Fortran 允许你创建最多 7 维数组。
Arrays can be one- dimensional (like vectors), two-dimensional (like matrices) and Fortran allows you to create up to 7-dimensional arrays.
Declaring Arrays
使用 dimension 特性声明数组。
Arrays are declared with the dimension attribute.
例如,要声明一个名为 number 的一维数组,其中包含 5 个元素的实数,则需要编写:
For example, to declare a one-dimensional array named number, of real numbers containing 5 elements, you write,
real, dimension(5) :: numbers
通过指定下标来引用数组的各个元素。数组的第一个元素的下标为 1。数组 numbers 包含五个实变量:numbers(1)
、numbers(2)
、numbers(3)
、numbers(4)
和 numbers(5)
。
The individual elements of arrays are referenced by specifying their subscripts. The first element of an array has a subscript of one. The array numbers contains five real variables –numbers(1), numbers(2), numbers(3), numbers(4), and numbers(5).
要创建一个名为 matrix 的 5 x 5 二维整数数组,请编写:
To create a 5 x 5 two-dimensional array of integers named matrix, you write −
integer, dimension (5,5) :: matrix
也可以使用一些明确的下限声明一个数组,例如:
You can also declare an array with some explicit lower bound, for example −
real, dimension(2:6) :: numbers
integer, dimension (-3:2,0:4) :: matrix
Assigning Values
可以针对各个成员分配值,例如:
You can either assign values to individual members, like,
numbers(1) = 2.0
或者可以使用循环:
or, you can use a loop,
do i =1,5
numbers(i) = i * 2.0
end do
可以采用称为数组构造器的简写符号直接为一维数组元素分配值,例如:
One-dimensional array elements can be directly assigned values using a short hand symbol, called array constructor, like,
numbers = (/1.5, 3.2,4.5,0.9,7.2 /)
please note that there are no spaces allowed between the brackets ‘( ‘and the back slash ‘/’
please note that there are no spaces allowed between the brackets ‘( ‘and the back slash ‘/’
Example
以下示例展示了上面讨论的概念。
The following example demonstrates the concepts discussed above.
program arrayProg
real :: numbers(5) !one dimensional integer array
integer :: matrix(3,3), i , j !two dimensional real array
!assigning some values to the array numbers
do i=1,5
numbers(i) = i * 2.0
end do
!display the values
do i = 1, 5
Print *, numbers(i)
end do
!assigning some values to the array matrix
do i=1,3
do j = 1, 3
matrix(i, j) = i+j
end do
end do
!display the values
do i=1,3
do j = 1, 3
Print *, matrix(i,j)
end do
end do
!short hand assignment
numbers = (/1.5, 3.2,4.5,0.9,7.2 /)
!display the values
do i = 1, 5
Print *, numbers(i)
end do
end program arrayProg
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
2.00000000
4.00000000
6.00000000
8.00000000
10.0000000
2
3
4
3
4
5
4
5
6
1.50000000
3.20000005
4.50000000
0.899999976
7.19999981
Some Array Related Terms
下表提供了一些与数组相关的术语:
The following table gives some array related terms −
Term |
Meaning |
Rank |
It is the number of dimensions an array has. For example, for the array named matrix, rank is 2, and for the array named numbers, rank is 1. |
Extent |
It is the number of elements along a dimension. For example, the array numbers has extent 5 and the array named matrix has extent 3 in both dimensions. |
Shape |
The shape of an array is a one-dimensional integer array, containing the number of elements (the extent) in each dimension. For example, for the array matrix, shape is (3, 3) and the array numbers it is (5). |
Size |
It is the number of elements an array contains. For the array matrix, it is 9, and for the array numbers, it is 5. |
Passing Arrays to Procedures
可以将数组作为参数传递给过程。以下示例演示了该概念:
You can pass an array to a procedure as an argument. The following example demonstrates the concept −
program arrayToProcedure
implicit none
integer, dimension (5) :: myArray
integer :: i
call fillArray (myArray)
call printArray(myArray)
end program arrayToProcedure
subroutine fillArray (a)
implicit none
integer, dimension (5), intent (out) :: a
! local variables
integer :: i
do i = 1, 5
a(i) = i
end do
end subroutine fillArray
subroutine printArray(a)
integer, dimension (5) :: a
integer::i
do i = 1, 5
Print *, a(i)
end do
end subroutine printArray
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
1
2
3
4
5
在上述示例中,子例程 fillArray 和 printArray 只可使用维度为 5 的数组调用。但是,要编写可用于任何大小数组的子例程,可以使用以下技术对其进行重写:
In the above example, the subroutine fillArray and printArray can only be called with arrays with dimension 5. However, to write subroutines that can be used for arrays of any size, you can rewrite it using the following technique −
program arrayToProcedure
implicit none
integer, dimension (10) :: myArray
integer :: i
interface
subroutine fillArray (a)
integer, dimension(:), intent (out) :: a
integer :: i
end subroutine fillArray
subroutine printArray (a)
integer, dimension(:) :: a
integer :: i
end subroutine printArray
end interface
call fillArray (myArray)
call printArray(myArray)
end program arrayToProcedure
subroutine fillArray (a)
implicit none
integer,dimension (:), intent (out) :: a
! local variables
integer :: i, arraySize
arraySize = size(a)
do i = 1, arraySize
a(i) = i
end do
end subroutine fillArray
subroutine printArray(a)
implicit none
integer,dimension (:) :: a
integer::i, arraySize
arraySize = size(a)
do i = 1, arraySize
Print *, a(i)
end do
end subroutine printArray
请注意,程序正在使用 size 函数获取数组的大小。
Please note that the program is using the size function to get the size of the array.
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
1
2
3
4
5
6
7
8
9
10
Array Sections
到目前为止,我们只参考了整个数组,Fortran 提供了一种简单的方法,可以使用单个语句来引用几个元素或数组的一部分。
So far we have referred to the whole array, Fortran provides an easy way to refer several elements, or a section of an array, using a single statement.
要访问数组部分,需要提供该部分的下限和上限,以及所有维度的步长(增量)。这种表示法称为 subscript triplet:
To access an array section, you need to provide the lower and the upper bound of the section, as well as a stride (increment), for all the dimensions. This notation is called a subscript triplet:
array ([lower]:[upper][:stride], ...)
未提及下限和上限时,它默认为声明的扩展名,步幅值默认为 1。
When no lower and upper bounds are mentioned, it defaults to the extents you declared, and stride value defaults to 1.
以下示例演示了此概念 −
The following example demonstrates the concept −
program arraySubsection
real, dimension(10) :: a, b
integer:: i, asize, bsize
a(1:7) = 5.0 ! a(1) to a(7) assigned 5.0
a(8:) = 0.0 ! rest are 0.0
b(2:10:2) = 3.9
b(1:9:2) = 2.5
!display
asize = size(a)
bsize = size(b)
do i = 1, asize
Print *, a(i)
end do
do i = 1, bsize
Print *, b(i)
end do
end program arraySubsection
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
5.00000000
5.00000000
5.00000000
5.00000000
5.00000000
5.00000000
5.00000000
0.00000000E+00
0.00000000E+00
0.00000000E+00
2.50000000
3.90000010
2.50000000
3.90000010
2.50000000
3.90000010
2.50000000
3.90000010
2.50000000
3.90000010