In Fortran programming, an array is a collection of elements, all of the same data type, stored under a single variable name. Arrays are fundamental to scientific computing, engineering simulations, and numerical analysis because they allow efficient storage, manipulation, and processing of large datasets. By using arrays, programmers can perform repetitive operations, implement algorithms for matrices, vectors, and multidimensional data, and simplify code readability.
This post will provide a thorough introduction to arrays in Fortran, covering declaration, initialization, element access, array operations, practical examples, intrinsic functions, and best practices. Mastery of arrays is essential for writing high-performance Fortran programs for scientific and engineering applications.
What is an Array?
An array is a structured collection of variables of the same type. Each element in an array can be accessed using an index, which specifies the element’s position. The main advantages of arrays include:
- Efficiency: Arrays allow operations on multiple elements without writing separate variables for each.
- Scalability: Ideal for large datasets, matrices, and multidimensional data.
- Clarity: Using arrays improves code readability compared to multiple individual variables.
Arrays can be one-dimensional, two-dimensional, or multi-dimensional, depending on the application.
Declaring Arrays in Fortran
Before using an array, it must be declared with its type and size. The syntax is:
type :: array_name(size)
typecan beinteger,real,double precision,logical, orcharacter.sizespecifies the number of elements in the array.
Example: Declaring a One-Dimensional Array
real :: arr(5)
- This declares an array
arrwith 5 elements of typereal. - The elements are accessed as
arr(1),arr(2), …,arr(5).
Example: Declaring a Two-Dimensional Array
real :: matrix(3,3)
- This declares a 3×3 matrix with 9 real elements.
- Elements are accessed using two indices:
matrix(row, column).
Initializing Arrays
Arrays can be initialized at the time of declaration using array constructors (/ ... /) or the reshape function for multi-dimensional arrays.
Initializing One-Dimensional Arrays
real :: arr(5)
arr = (/1.0, 2.0, 3.0, 4.0, 5.0/)
print *, "Array elements:", arr
Output:
Array elements: 1.0 2.0 3.0 4.0 5.0
- Each value corresponds to an element in the array.
- Indexing starts at 1 by default in Fortran.
Initializing Multi-Dimensional Arrays
real :: matrix(3,3)
matrix = reshape((/1,2,3,4,5,6,7,8,9/), (/3,3/))
print *, "Matrix elements:"
do i = 1, 3
print *, matrix(i,1:3)
end do
Output:
Matrix elements:
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0
reshapefills the matrix row by row (default Fortran ordering).
Accessing Array Elements
Array elements are accessed using parentheses and an index.
One-Dimensional Array Access
print *, "First element:", arr(1)
print *, "Third element:", arr(3)
Multi-Dimensional Array Access
print *, "Element at row 2, column 3:", matrix(2,3)
- Proper indexing is critical to avoid runtime errors.
- Indexing must stay within the declared array bounds.
Modifying Array Elements
Individual elements can be modified directly by assigning new values to their indices.
Example:
arr(2) = 10.0
print *, "Updated array:", arr
Output:
Updated array: 1.0 10.0 3.0 4.0 5.0
- Only the element at index 2 is updated; other elements remain unchanged.
Array Operations
Fortran allows element-wise operations on arrays of the same size:
- Addition:
result = arr1 + arr2 - Subtraction:
result = arr1 - arr2 - Multiplication:
result = arr1 * arr2 - Division:
result = arr1 / arr2
Example: Element-wise Addition
real :: arr1(5), arr2(5), result(5)
arr1 = (/1,2,3,4,5/)
arr2 = (/5,4,3,2,1/)
result = arr1 + arr2
print *, "Result of addition:", result
Output:
Result of addition: 6 6 6 6 6
- Operations are performed element by element.
Looping Through Arrays
DO loops are commonly used to iterate through array elements for calculations, modifications, or conditional checks.
Example: Doubling Array Elements
integer :: i
do i = 1, 5
arr(i) = arr(i) * 2
end do
print *, "Doubled array:", arr
Output:
Doubled array: 2.0 20.0 6.0 8.0 10.0
- The loop accesses each element by index and modifies it.
Intrinsic Array Functions
Fortran provides several intrinsic functions to perform operations on arrays without explicit loops:
- sum(array) – Returns the sum of all elements.
- maxval(array) – Returns the largest element.
- minval(array) – Returns the smallest element.
- product(array) – Returns the product of all elements.
- size(array) – Returns the number of elements.
Example: Using sum, maxval, and minval
print *, "Sum of array:", sum(arr)
print *, "Maximum value:", maxval(arr)
print *, "Minimum value:", minval(arr)
Output:
Sum of array: 48.0
Maximum value: 20.0
Minimum value: 2.0
- Intrinsic functions simplify array calculations and improve code readability.
Arrays in Mathematical and Scientific Computations
Arrays are widely used in scientific computing for:
- Vector operations: Representing physical quantities like velocity or force.
- Matrices: Storing coefficients for linear equations or transformations.
- Numerical simulations: Solving differential equations, grids for finite element methods.
- Data analysis: Storing and processing measurement datasets.
Example: Simple Matrix Multiplication Using Loops
real :: A(2,2), B(2,2), C(2,2)
integer :: i, j, k
A = reshape((/1,2,3,4/), (/2,2/))
B = reshape((/5,6,7,8/), (/2,2/))
C = 0.0
do i = 1, 2
do j = 1, 2
do k = 1, 2
C(i,j) = C(i,j) + A(i,k) * B(k,j)
end do
end do
end do
print *, "Matrix multiplication result:"
do i = 1, 2
print *, C(i,1:2)
end do
Output:
Matrix multiplication result:
19.0 22.0
43.0 50.0
- Arrays allow concise representation and manipulation of matrices.
Best Practices for Using Arrays
- Declare correct sizes: Avoid exceeding array bounds.
- Use descriptive names: Improves code readability for complex programs.
- Use loops efficiently: Iterate through large arrays with DO loops.
- Leverage intrinsic functions: Simplifies code and reduces errors.
- Comment array operations: Especially for multi-dimensional arrays.
Summary
Arrays in Fortran are a powerful tool for storing and manipulating multiple values efficiently. This post covered:
- Definition and importance: Arrays store multiple elements of the same type.
- Declaration and initialization: One-dimensional and multi-dimensional arrays.
- Accessing and modifying elements: Using indices for direct operations.
- Element-wise operations: Addition, subtraction, multiplication, division.
- Intrinsic functions: sum, maxval, minval, product, size.
- Loops and iterations: Modifying arrays and performing calculations.
- Applications: Vectors, matrices, simulations, and data processing.
- Best practices: Efficient, readable, and maintainable array usage.
Leave a Reply