Arrays are essential in programming for storing multiple values of the same data type. When working with data arranged in multiple dimensions, such as matrices, grids, or tables, multi-dimensional arrays become invaluable. Fortran, being a language widely used in scientific and engineering applications, provides robust support for multi-dimensional arrays.
This post explores how to declare, initialize, and use multi-dimensional arrays in Fortran, with detailed examples and practical applications.
1. Introduction to Multi-Dimensional Arrays
A multi-dimensional array is an array with more than one index. The most common forms are:
- Two-dimensional arrays: Represent matrices or grids, indexed by row and column
- Three-dimensional arrays: Represent volumetric data or 3D grids
- Higher-dimensional arrays: Useful in simulations and tensor computations
Advantages of multi-dimensional arrays:
- Organized data storage: Store related data in a structured format
- Efficient computations: Access elements using indices instead of separate variables
- Scalability: Handle large datasets without cluttering code
2. Syntax of Multi-Dimensional Arrays
The syntax for declaring multi-dimensional arrays in Fortran is:
type :: array_name(dim1, dim2, ..., dimN)
- type: Data type (
integer,real,double precision,logical,character) - array_name: Name of the array
- dim1, dim2, …, dimN: Sizes of each dimension
2.1 Example: Two-Dimensional Arrays
real :: matrix(3,3)
integer :: grid(4,5)
matrixis a 3×3 real matrixgridis a 4×5 integer grid
3. Initializing Multi-Dimensional Arrays
Arrays can be initialized using the reshape function, which assigns values in a specified shape.
3.1 Example: Initializing a 3×3 Matrix
program matrix_example
real :: matrix(3,3)
matrix = reshape((/1,2,3,4,5,6,7,8,9/), (/3,3/))
print *, "Matrix:"
print *, matrix
end program matrix_example
Output:
Matrix:
1.0000000 4.0000000 7.0000000
2.0000000 5.0000000 8.0000000
3.0000000 6.0000000 9.0000000
Explanation: reshape fills the matrix column-wise by default in Fortran.
4. Accessing Elements in Multi-Dimensional Arrays
Each element is accessed using indices corresponding to its position in each dimension.
4.1 Example: Accessing Elements
program access_matrix
real :: matrix(3,3)
matrix = reshape((/1,2,3,4,5,6,7,8,9/), (/3,3/))
print *, "Element at row 2, column 3:", matrix(2,3)
end program access_matrix
Output:
Element at row 2, column 3: 8.0
5. Modifying Elements in Multi-Dimensional Arrays
Individual elements or entire rows/columns can be modified.
5.1 Example: Modifying Elements
program modify_matrix
real :: matrix(3,3)
matrix = reshape((/1,2,3,4,5,6,7,8,9/), (/3,3/))
matrix(1,1) = 10.0
matrix(3,2) = 20.0
print *, "Modified Matrix:"
print *, matrix
end program modify_matrix
Output:
Modified Matrix:
10.000000 4.000000 7.000000
2.000000 5.000000 8.000000
3.000000 20.000000 9.000000
6. Iterating Over Multi-Dimensional Arrays
Nested loops are used to iterate over rows and columns of a matrix.
6.1 Example: Summing All Elements
program sum_matrix
real :: matrix(3,3)
real :: total
integer :: i, j
matrix = reshape((/1,2,3,4,5,6,7,8,9/), (/3,3/))
total = 0.0
do i = 1, 3
do j = 1, 3
total = total + matrix(i,j)
end do
end do
print *, "Sum of all elements:", total
end program sum_matrix
Output:
Sum of all elements: 45.0
7. Practical Applications of Multi-Dimensional Arrays
Multi-dimensional arrays are widely used in scientific computing and engineering.
7.1 Matrices in Linear Algebra
Matrices are used for:
- Solving systems of equations
- Matrix multiplication
- Eigenvalue computations
7.2 Grids in Physics Simulations
2D and 3D grids are used in:
- Heat distribution simulations
- Wave propagation
- Fluid dynamics
7.3 Data Tables
Multi-dimensional arrays can represent:
- Experimental data
- Financial datasets
- Game boards or maps
8. Multi-Dimensional Arrays with Loops
Nested loops are commonly combined with multi-dimensional arrays for processing.
8.1 Example: Printing Matrix Elements
program print_matrix
real :: matrix(3,3)
integer :: i, j
matrix = reshape((/1,2,3,4,5,6,7,8,9/), (/3,3/))
print *, "Matrix elements:"
do i = 1, 3
do j = 1, 3
print *, "Element (", i, ",", j, ") =", matrix(i,j)
end do
end do
end program print_matrix
9. Multi-Dimensional Arrays with Arithmetic Operations
Arrays can be used for calculations directly using loops.
9.1 Example: Matrix Scaling
program scale_matrix
real :: matrix(3,3)
integer :: i, j
real :: factor
factor = 2.0
matrix = reshape((/1,2,3,4,5,6,7,8,9/), (/3,3/))
do i = 1, 3
do j = 1, 3
matrix(i,j) = matrix(i,j) * factor
end do
end do
print *, "Scaled Matrix:"
print *, matrix
end program scale_matrix
Output:
Scaled Matrix:
2.0 8.0 14.0
4.0 10.0 16.0
6.0 12.0 18.0
10. Multi-Dimensional Arrays in Functions
Arrays can be passed to functions and subroutines for modular programming.
10.1 Example: Function Sum of Matrix
program sum_function
real :: matrix(3,3)
real :: total
matrix = reshape((/1,2,3,4,5,6,7,8,9/), (/3,3/))
total = sum_matrix(matrix)
print *, "Total sum:", total
contains
function sum_matrix(mat) result(total)
real, intent(in) :: mat(3,3)
real :: total
integer :: i, j
total = 0.0
do i = 1, 3
do j = 1, 3
total = total + mat(i,j)
end do
end do
end function sum_matrix
end program sum_function
11. Best Practices
- Use descriptive names for arrays (
matrix,grid,data) - Prefer
reshapefor initialization to avoid manual assignment - Use nested loops for iteration over rows and columns
- Document dimensions clearly to avoid confusion
- Avoid excessive dimensions unless necessary; high-dimensional arrays can reduce readability
- Combine with functions to modularize computations
12. Advanced Applications
- Matrix multiplication: Multiply two matrices using nested loops
- Image processing: 2D arrays for pixel data, 3D for color channels
- Simulation grids: 2D or 3D arrays for spatial simulations
- Tensor computations: Higher-dimensional arrays for advanced physics calculations
Leave a Reply