Search code examples
fortrangfortran

Fortran - Maxval returns weird results when dimension is specified


I have a 3x2 array and have filled it with the numbers 1-6 so that it looks like
1 4
2 5
3 6

I then call maxval on it, and specify that I wish to find the max value along dimension 1. One would expect that it should return 3, no?

But for some reason my output is '3 6'

PROGRAM maxv
    IMPLICIT None
    INTEGER, DIMENSION(3,2) :: x
    DATA x /1,2,3,4,5,6/

    WRITE(*,*) maxval(x,dim=1)
ENDPROGRAM maxv

I used Gfortran 4.6.3 if the issue lies within my compiler

According to http://www.nsc.liu.se/~boein/f77to90/a5.html , maxval when you specify a dimension is supposed to supply the maxval in that dimension.

Or maybe I have overlooked some stuffs.


Solution

  • Yes, you overlooked some stuffs; maxval is behaving correctly.

    When you write, for a rank-2 array x

    maxval(x,dim=1)
    

    the function returns a rank-1 array with the same number of elements as there are columns in x, each element being the maximum value of the corresponding column in x. Similarly

    maxval(x,dim=2)
    

    would, for your example, return the rank-1 array [4,5,6] -- the maximum value in each row of x.

    The GNU documentation explains the function better than the source you cite, IBM explain it even better and include an example of the function's use.