Arrays are one of the most commonly used data structures in programming, especially in scientific computing and numerical simulations. In Fortran, arrays are crucial for storing collections of data, whether it’s a series of numbers, a matrix, or higher-dimensional datasets. Understanding the size of an array is often necessary for various operations, such as loops, memory management, or data manipulation.
The size function in Fortran is a powerful tool used to return the total number of elements in an array. This post will delve into the use of the size function in Fortran, explaining its syntax, usage, and real-world applications.
Understanding the size Function in Fortran
In Fortran, the size function is used to return the number of elements in an array along a specified dimension or for the entire array if no dimension is specified.
Syntax:
result = size(array, dimension)
array: The array whose size you want to determine. It can be a one-dimensional, two-dimensional, or multi-dimensional array.dimension(optional): The dimension along which you want to find the size. If not provided, the function returns the total number of elements in the entire array.
Return Value:
- If the dimension argument is provided, the
sizefunction returns the size along that specific dimension. - If the dimension argument is omitted, it returns the total number of elements in the array.
For example:
- If
arrayis a 1D array of sizen,size(array)will returnn. - If
arrayis a 2D array of sizem x n,size(array)will returnm * n, unless you specify one dimension.
Basic Usage of size in Fortran
Example 1: Getting the Size of a One-Dimensional Array
program array_size_example
integer :: arr(5)
integer :: arr_size
! Assign values to the array elements
arr = [1, 2, 3, 4, 5]
! Use the size function to get the total number of elements
arr_size = size(arr)
! Print the result
print *, "Array size: ", arr_size
end program array_size_example
Explanation:
- In this program, an integer array
arrof size 5 is declared and initialized with the values[1, 2, 3, 4, 5]. - The
sizefunction is used to get the total number of elements in the array. Sincearrcontains 5 elements, thesize(arr)function returns5. - The result is printed out, which shows the array’s size.
Output:
Array size: 5
Getting the Size of Multi-Dimensional Arrays
Fortran also supports multi-dimensional arrays. The size function can be used to determine the size along specific dimensions of a multi-dimensional array. By default, if no dimension is specified, size will return the total number of elements in the entire array.
Example 2: Getting the Size of a Two-Dimensional Array
program array_size_2d
integer :: arr(3, 4)
integer :: arr_size
! Assign values to the 3x4 matrix
arr = reshape([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], shape(arr))
! Get the total number of elements in the array
arr_size = size(arr)
! Print the result
print *, "Total number of elements in the 2D array: ", arr_size
end program array_size_2d
Explanation:
- The array
arris a 2D array with dimensions3 x 4(3 rows and 4 columns). We use thereshapefunction to initialize the array with the values from1to12. - The
sizefunction returns the total number of elements in the array. Since the array is3 x 4, the total number of elements is12, andsize(arr)will return12.
Output:
Total number of elements in the 2D array: 12
Getting the Size Along Specific Dimensions
You can also use the size function to determine the size of an array along a specific dimension. For a 2D array, this could be the number of rows (dimension 1) or the number of columns (dimension 2).
Example 3: Getting the Size Along Specific Dimensions
program array_size_dimensions
integer :: arr(3, 4)
integer :: rows, cols
! Initialize the 2D array
arr = reshape([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], shape(arr))
! Get the number of rows (size along the first dimension)
rows = size(arr, 1)
! Get the number of columns (size along the second dimension)
cols = size(arr, 2)
! Print the results
print *, "Number of rows: ", rows
print *, "Number of columns: ", cols
end program array_size_dimensions
Explanation:
- In this example,
arris a 2D array of size3 x 4. - The
size(arr, 1)function returns the number of rows, which is3. - The
size(arr, 2)function returns the number of columns, which is4. - The results are printed out, showing the number of rows and columns separately.
Output:
Number of rows: 3
Number of columns: 4
Using size with Multi-Dimensional Arrays
The size function can be applied to arrays with more than two dimensions as well. The same logic applies: you can get the size along a specific dimension or the total number of elements in the entire array.
Example 4: Using size with a Three-Dimensional Array
program array_size_3d
integer :: arr(2, 3, 4)
integer :: arr_size, dim1, dim2, dim3
! Initialize a 3D array
arr = reshape([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28], shape(arr))
! Get the total number of elements in the 3D array
arr_size = size(arr)
! Get the size along each dimension
dim1 = size(arr, 1)
dim2 = size(arr, 2)
dim3 = size(arr, 3)
! Print the results
print *, "Total number of elements: ", arr_size
print *, "Size along dimension 1: ", dim1
print *, "Size along dimension 2: ", dim2
print *, "Size along dimension 3: ", dim3
end program array_size_3d
Explanation:
arris a 3D array with dimensions2 x 3 x 4, which has a total of2 * 3 * 4 = 24elements.- The
size(arr)function returns the total number of elements in the array. - The
size(arr, 1)function returns2, the size along the first dimension (the number of “layers”). - The
size(arr, 2)function returns3, the size along the second dimension (the number of rows per layer). - The
size(arr, 3)function returns4, the size along the third dimension (the number of columns per row).
Output:
Total number of elements: 24
Size along dimension 1: 2
Size along dimension 2: 3
Size along dimension 3: 4
Real-World Applications of size
The size function is commonly used in many real-world scenarios, such as:
1. Matrix Operations:
In matrix operations, you often need to know the size of matrices to perform tasks like matrix multiplication or solving systems of linear equations. The size function can be used to check if the matrices have compatible dimensions before performing the operation.
2. Memory Allocation:
In large-scale scientific simulations, you might need to dynamically allocate memory for arrays. The size function can help check the dimensions of arrays and ensure that sufficient memory is allocated.
3. Iterative Algorithms:
In algorithms that involve looping through array elements, such as numerical methods or data processing, the size function helps in determining the bounds of loops based on the array size.
Leave a Reply