MAX(3) - [NUMERIC] Maximum value of an argument list
result = max(a1, a2, a3, ...)
elemental TYPE(kind=KIND) function max(a1, a2, a3, ... )TYPE(kind=KIND,intent(in),optional :: a1 TYPE(kind=KIND,intent(in),optional :: a2 TYPE(kind=KIND,intent(in),optional :: a3 : :
o A3, A3, A4, ... must be of the same type and kind as A1 o the arguments may (all) be integer, real or character o there must be at least two arguments o the length of a character result is the length of the longest argument o the type and kind of the result is the same as those of the arguments
MAX(3) returns the argument with the largest (most positive) value.
For arguments of character type, the result is as if the arguments had been successively compared with the intrinsic operational operators, taking into account the collating sequence of the character kind.
The returned selected character argument is padded with blanks as needed on the right to the same length of the longest argument.
It is unusual for a Fortran intrinsic to take an arbitrary number of options, and in addition MAX(3) is elemental, meaning any number of arguments may be arrays as long as they are of the same shape.
The examples contain such cases as examples to clarify the resulting behavior for those not familiar with calling a "scalar" function elementally with arrays.
See maxval(3) for simply getting the max value of an array.
o A1 : The first argument determines the type and kind of the returned value, and of any remaining arguments as well. o A2,A3,... : the remaining arguments of the set of values to search for a maximum in. : There must be at least two arguments to MAX(3).
The return value corresponds to an array of the same shape of any array argument, or a scalar if all arguments are scalar.
The returned value when any argument is an array will be an array of the same shape where each element is the maximum value occurring at that location, treating all the scalar values as arrays of that same shape with all elements set to the scalar value.
Sample program
program demo_max implicit none real :: arr1(4)= [10.0,11.0,30.0,-100.0] real :: arr2(5)= [20.0,21.0,32.0,-200.0,2200.0] integer :: box(3,4)= reshape([-6,-5,-4,-3,-2,-1,1,2,3,4,5,6],shape(box))Results:! basic usage ! this is simple enough when all arguments are scalar
! the most positive value is returned, not the one with the ! largest magnitude write(*,*)scalars:,max(10.0,11.0,30.0,-100.0) write(*,*)scalars:,max(-22222.0,-0.0001)
! strings do not need to be of the same length write(*,*)characters:,max(the,words,order)
! leading spaces are significant; everyone is padded on the right ! to the length of the longest argument write(*,*)characters:,max(c,bb,a) write(*,*)characters:,max( c,b,a)
! elemental ! there must be at least two arguments, so even if A1 is an array ! max(A1) is not valid. See MAXVAL(3) and/or MAXLOC(3) instead.
! strings in a single array do need to be of the same length ! but the different objects can still be of different lengths. write(*,"(*("",a,"":,1x))")MAX([A,Z],[BB,Y ]) ! note the result is now an array with the max of every element ! position, as can be illustrated numerically as well: write(*,(a,*(i3,1x)))box= ,box write(*,(a,*(i3,1x)))box**2=,sign(1,box)*box**2 write(*,(a,*(i3,1x)))max ,max(box,sign(1,box)*box**2)
! Remember if any argument is an array by the definition of an ! elemental function all the array arguments must be the same shape.
! to find the single largest value of multiple arrays you could ! use something like ! MAXVAL([arr1, arr2]) ! or probably better (more likely to avoid creating a large temp array) ! max(maxval(arr1),maxval(arr2)) ! instead
! so this returns an array of the same shape as any input array ! where each result is the maximum that occurs at that position. write(*,*)max(arr1,arr2(1:4)) ! this returns an array just like BOX except all values less than ! zero are set to zero: write(*,*)max(box,0) ! When mixing arrays and scalars you can think of the scalars ! as being a copy of one of the arrays with all values set to ! the scalar value.
end program demo_max
> scalars: 30.00000 > scalars: -9.9999997E-05 > characters:words > characters:c > characters:b > "BB" "Z " > box= -6 -5 -4 -3 -2 -1 1 2 3 4 5 6 > box**2=-36 -25 -16 -9 -4 -1 1 4 9 16 25 36 > max -6 -5 -4 -3 -2 -1 1 4 9 16 25 36 > 20.00000 21.00000 32.00000 -100.0000 > 0 0 0 0 0 0 > 1 2 3 4 5 6
FORTRAN 77
MAXLOC(3), MINLOC(3), MAXVAL(3), MINVAL(3), MIN(3)
Fortran intrinsic descriptions (license: MIT) @urbanjost
Nemo Release 3.1 | max (3fortran) | November 02, 2024 |