Arrays are one of the most important data structures in Fortran, particularly in scientific and numerical computing. An array is a collection of elements, all of the same data type, stored in contiguous memory locations and accessed using indices. Arrays simplify the storage and manipulation of large sets of data, such as measurements, simulation results, or matrices in mathematical computations.

In Fortran, arrays can be one-dimensional, two-dimensional, or even multi-dimensional. The language offers a rich set of features for working with arrays efficiently, including array operations, intrinsic functions, and slicing capabilities. Mastery of arrays is essential for writing optimized and readable Fortran programs.

Declaring Arrays in Fortran

Arrays in Fortran are declared by specifying the type, name, and dimensions. The general syntax is:

type, dimension(size1, size2, ...) :: array_name
  • type specifies the data type, such as integer, real, double precision, or character.
  • dimension(size1, size2, ...) indicates the size of each dimension.
  • array_name is the identifier for the array.

One-Dimensional Arrays

A one-dimensional array is similar to a list of elements. Here’s an example of declaring a one-dimensional integer array with 5 elements:

integer, dimension(5) :: arr

You can assign values to the array elements in two ways: individually or using array constructors.

Assigning Values Individually

arr(1) = 1
arr(2) = 2
arr(3) = 3
arr(4) = 4
arr(5) = 5

Using an Array Constructor

Fortran allows a more concise way to initialize arrays using the (/ ... /) notation:

arr = (/1, 2, 3, 4, 5/)

Here, arr(1) contains 1, arr(2) contains 2, and so on. This notation is especially useful for initializing arrays in a single statement.

Multi-Dimensional Arrays

For scientific computing, two-dimensional arrays (matrices) and higher-dimensional arrays are often required. Fortran makes this simple with its dimension syntax.

Declaring a Two-Dimensional Array

real, dimension(3, 3) :: matrix

This declares a 3×3 matrix of real numbers. The first index represents the row, and the second index represents the column.

Assigning Values to Multi-Dimensional Arrays

Values can be assigned element by element:

matrix(1,1) = 1.0
matrix(1,2) = 2.0
matrix(1,3) = 3.0
matrix(2,1) = 4.0
matrix(2,2) = 5.0
matrix(2,3) = 6.0
matrix(3,1) = 7.0
matrix(3,2) = 8.0
matrix(3,3) = 9.0

Or using the reshape intrinsic function:

matrix = reshape((/1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0/), (/3,3/))

The reshape function converts a one-dimensional array into a multi-dimensional array according to the specified shape. This is especially helpful for initializing large matrices concisely.

Accessing Array Elements

Array elements are accessed using their indices. For one-dimensional arrays:

print *, "Third element:", arr(3)

For two-dimensional arrays:

print *, "Element at row 2, column 3:", matrix(2,3)

For multi-dimensional arrays, Fortran uses column-major order, meaning elements are stored column by column in memory. This is important to understand for performance optimization, especially when dealing with large arrays.

Array Operations

Fortran allows operations on entire arrays or subarrays without explicitly looping through elements. This feature is called array syntax.

Example: Element-wise Operations

integer, dimension(5) :: a, b, c
a = (/1, 2, 3, 4, 5/)
b = (/5, 4, 3, 2, 1/)
c = a + b
print *, "Resulting array:", c

Output:

Resulting array: 6 6 6 6 6

Here, the addition is applied element-wise across arrays a and b. Fortran supports +, -, *, /, and other arithmetic operators in this manner.

Array Slicing

Fortran provides powerful slicing capabilities that allow you to access subarrays without loops.

integer, dimension(5) :: arr
arr = (/1, 2, 3, 4, 5/)
print *, "Elements from 2 to 4:", arr(2:4)

Output:

Elements from 2 to 4: 2 3 4

You can also use strides:

print *, "Every second element:", arr(1:5:2)

Output:

Every second element: 1 3 5

This feature greatly simplifies the manipulation of arrays and reduces the need for verbose loops.

Intrinsic Functions for Arrays

Fortran provides many built-in functions to operate on arrays efficiently. Some commonly used array functions include:

  • size(array): Returns the total number of elements in the array.
  • shape(array): Returns the dimensions of the array.
  • sum(array): Returns the sum of all elements.
  • maxval(array), minval(array): Return the maximum or minimum element.
  • transpose(matrix): Returns the transpose of a two-dimensional array.

Example:

integer, dimension(5) :: arr
arr = (/1, 2, 3, 4, 5/)
print *, "Size of array:", size(arr)
print *, "Sum of elements:", sum(arr)
print *, "Maximum element:", maxval(arr)

Output:

Size of array: 5
Sum of elements: 15
Maximum element: 5

Dynamic Arrays

Fortran also supports allocatable arrays, which allow you to create arrays with sizes determined at runtime. This is useful when the number of elements is not known in advance.

program dynamic_array
integer, allocatable :: arr(:)
integer :: n, i
print *, "Enter the number of elements:"
read *, n
allocate(arr(n))
do i = 1, n
    arr(i) = i * 2
end do
print *, "Array elements:", arr
deallocate(arr)
end program dynamic_array

In this program:

  • allocatable :: arr(:) declares a dynamic array.
  • allocate(arr(n)) creates the array with n elements at runtime.
  • deallocate(arr) releases the memory after use.

Multi-Dimensional Dynamic Arrays

You can also create dynamic matrices:

program dynamic_matrix
real, allocatable :: mat(:,:)
integer :: rows, cols, i, j
print *, "Enter number of rows and columns:"
read *, rows, cols
allocate(mat(rows, cols))
do i = 1, rows
    do j = 1, cols
        mat(i,j) = i * j
    end do
end do
print *, "Matrix elements:"
do i = 1, rows
    print *, mat(i, :)
end do
deallocate(mat)
end program dynamic_matrix

Dynamic arrays provide flexibility while maintaining the performance Fortran is known for.

Practical Examples of Arrays in Fortran

Arrays are essential in many scientific and engineering applications. Some examples include:

  1. Physics Simulations: Representing particle positions, velocities, and forces.
  2. Numerical Methods: Storing values in finite difference grids or matrices for solving differential equations.
  3. Statistics: Holding large datasets for statistical analysis.
  4. Graphics and Image Processing: Representing pixel values in images.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *