Using MAXVAL and MINVAL Functions in Fortran

In Fortran, working with arrays and datasets often requires identifying the largest or smallest values. The MAXVAL and MINVAL intrinsic functions provide a simple and efficient way to accomplish this. These functions are widely used in scientific computing, data analysis, and engineering applications to extract key values from arrays.

This post explores the MAXVAL and MINVAL functions in detail, including syntax, examples, multidimensional arrays, optional arguments, practical applications, and best practices.

1. Introduction to MAXVAL and MINVAL

  • MAXVAL(array): Returns the largest element in an array
  • MINVAL(array): Returns the smallest element in an array

Both functions can handle:

  • 1D arrays
  • Multidimensional arrays
  • Optional dimension argument to operate along specific dimensions

Using these functions simplifies the process of finding extreme values without writing loops manually.


2. Syntax of MAXVAL and MINVAL

result = maxval(array)
result = minval(array)
  • array: Array of numeric values (integer, real, or double precision)
  • result: The largest (MAXVAL) or smallest (MINVAL) element

Optional Arguments:

  • dim: Dimension along which to find the maximum or minimum
  • mask: Logical array to include only certain elements

3. Example: Finding Maximum and Minimum in a 1D Array

program maxmin_example
  real :: arr(5)
  arr = (/1, 3, 5, 2, 4/)
  print *, "Maximum value:", maxval(arr)
  print *, "Minimum value:", minval(arr)
end program maxmin_example

Output:

Maximum value: 5.0
Minimum value: 1.0

Explanation: MAXVAL finds 5 as the largest, MINVAL finds 1 as the smallest in the array.


4. Using MAXVAL and MINVAL with Integer Arrays

program maxmin_integer
  integer :: nums(6)
  nums = (/10, 25, 5, 30, 15, 20/)
  print *, "Maximum value:", maxval(nums)
  print *, "Minimum value:", minval(nums)
end program maxmin_integer

Output:

Maximum value: 30
Minimum value: 5

5. Using MAXVAL and MINVAL in Conditional Statements

These functions can be combined with IF statements to make decisions.

5.1 Example: Check if a Value is Maximum

program check_max
  real :: arr(5), value
  arr = (/2.5, 4.0, 1.5, 3.0, 5.0/)
  value = 4.0

  if (value == maxval(arr)) then
  print *, value, "is the maximum value in the array"
else
  print *, value, "is not the maximum value"
end if end program check_max

Output:

4.0 is not the maximum value

6. MAXVAL and MINVAL with Multi-Dimensional Arrays

Fortran allows MAXVAL and MINVAL to operate along specific dimensions.

6.1 Example: 2D Array

program maxmin_2d
  real :: matrix(2,3)
  matrix = reshape((/1,4,2,5,3,6/), (/2,3/))
  print *, "Maximum value in the matrix:", maxval(matrix)
  print *, "Minimum value in the matrix:", minval(matrix)
end program maxmin_2d

Output:

Maximum value in the matrix: 6.0
Minimum value in the matrix: 1.0

6.2 Example: Along a Dimension

program maxmin_dimension
  real :: matrix(2,3)
  matrix = reshape((/1,4,2,5,3,6/), (/2,3/))
  print *, "Maximum along rows:", maxval(matrix, dim=1)
  print *, "Maximum along columns:", maxval(matrix, dim=2)
end program maxmin_dimension

Output:

Maximum along rows: 4.0 5.0 6.0
Maximum along columns: 2.0 5.0

Explanation:

  • dim=1 → operate along rows
  • dim=2 → operate along columns

7. Using MASK with MAXVAL and MINVAL

The mask argument allows selective consideration of array elements.

7.1 Example: Masked Maximum

program maxval_mask
  real :: arr(5)
  logical :: mask(5)
  arr = (/1, 3, 5, 2, 4/)
  mask = (/ .true., .false., .true., .false., .true. /)

  print *, "Maximum with mask:", maxval(arr, mask=mask)
  print *, "Minimum with mask:", minval(arr, mask=mask)
end program maxval_mask

Output:

Maximum with mask: 5.0
Minimum with mask: 1.0

Explanation: Only elements with .true. in the mask are considered.


8. Practical Applications

8.1 Data Analysis

  • Find maximum and minimum values in experimental datasets
  • Identify peaks or troughs in time-series data

8.2 Engineering Simulations

  • Determine highest and lowest stress values in finite element models
  • Identify maximum temperatures in thermal simulations

8.3 Image Processing

  • MAXVAL for brightest pixel
  • MINVAL for darkest pixel

9. Combining MAXVAL and MINVAL with Loops

Loops can be used to compute MAXVAL and MINVAL across multiple arrays or datasets.

program maxval_loop
  real :: datasets(3,5)
  integer :: i

  datasets = reshape((/1,2,3,4,5,6,7,8,9,10,11,12,13,14,15/), (/3,5/))

  do i = 1, 3
  print *, "Maximum in dataset", i, ":", maxval(datasets(i,:))
  print *, "Minimum in dataset", i, ":", minval(datasets(i,:))
end do end program maxval_loop

Output:

Maximum in dataset 1 : 5.0
Minimum in dataset 1 : 1.0
Maximum in dataset 2 : 10.0
Minimum in dataset 2 : 6.0
Maximum in dataset 3 : 15.0
Minimum in dataset 3 : 11.0

10. MAXVAL and MINVAL with Logical Conditions

You can combine MAXVAL and MINVAL with logical operations to find extreme values satisfying certain conditions.

program maxval_condition
  real :: arr(6)
  logical :: mask(6)
  arr = (/5, 12, 7, 20, 3, 15/)
  mask = arr > 10

  print *, "Maximum value greater than 10:", maxval(arr, mask=mask)
end program maxval_condition

Output:

Maximum value greater than 10: 20.0

11. Best Practices

  1. Use MAXVAL and MINVAL instead of manual loops for simplicity and efficiency
  2. Leverage dim argument for operations along rows or columns in multidimensional arrays
  3. Use mask for conditional maximum/minimum selection
  4. Document array dimensions clearly to avoid confusion
  5. Combine with other functions like sum, average, or where for data analysis

12. Advanced Applications

  • Scientific computing: Find the maximum stress, temperature, or velocity in simulations
  • Finance: Identify peak stock prices or lowest trading values
  • Machine learning: Determine extreme feature values for normalization
  • Optimization: Quickly find maximum or minimum values in optimization arrays

Comments

Leave a Reply

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