Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

MAXLOC

164 views
Skip to first unread message

Dave Tholen

unread,
Sep 25, 2021, 6:32:18 AM9/25/21
to
According to gfortran on-line documentation, the MAXLOC intrinsic determines
the location of the element in the array with the maximum value, but what is
the definition of "location" in this instance? The on-line documentation for
IBM's XL Fortran calls it "the subscript of the location", but what if the
subscript doesn't start at 1?

PROGRAM BugTest
INTEGER :: I(-3:3) ! declare seven-element integer array
I = 0 ! initialize entire array to zero
I(-3) = 9 ! make first element have the largest value
WRITE (*,*) 'Maximum value in array ',MAXVAL(I)
WRITE (*,*) 'Location of maximum value ',MAXLOC(I,1)
END PROGRAM BugTest

The above program generates the output:

Maximum value in array 9
Location of maximum value 1

with both gfortran 7.1 and 10.2.

Yes, the largest value is in the first array element, but the subscript for
that element is -3, so shouldn't MAXLOC return -3 for the "location"?

If the compiler is producing correct results, then the documentation really
should clarify the definition of "location" for arrays with subscripts that
do not start at 1.

Thomas Koenig

unread,
Sep 25, 2021, 6:49:26 AM9/25/21
to
Dave Tholen <tho...@antispam.ham> schrieb:
> According to gfortran on-line documentation, the MAXLOC intrinsic determines
> the location of the element in the array with the maximum value, but what is
> the definition of "location" in this instance? The on-line documentation for
> IBM's XL Fortran calls it "the subscript of the location", but what if the
> subscript doesn't start at 1?
>
> PROGRAM BugTest
> INTEGER :: I(-3:3) ! declare seven-element integer array
> I = 0 ! initialize entire array to zero
> I(-3) = 9 ! make first element have the largest value
> WRITE (*,*) 'Maximum value in array ',MAXVAL(I)
> WRITE (*,*) 'Location of maximum value ',MAXLOC(I,1)
> END PROGRAM BugTest
>
> The above program generates the output:
>
> Maximum value in array 9
> Location of maximum value 1
>
> with both gfortran 7.1 and 10.2.
>
> Yes, the largest value is in the first array element, but the subscript for
> that element is -3, so shouldn't MAXLOC return -3 for the "location"?

What gfortran does is correct. The standard is a bit more expansive on
that, it states (among other cases)

# The ith subscript returned lies in the range 1 to ei, where ei is
# the extent of the ith dimension of ARRAY. If ARRAY has size zero,
# all elements of the result are zero.

> If the compiler is producing correct results, then the documentation really
> should clarify the definition of "location" for arrays with subscripts that
> do not start at 1.

gfortran uses the terminology of the standard, but the documentation
certainly could be improved. If you have a suggestion, please send it
to for...@gcc.gnu.org.

(Documentation for MINLOC and FINDLOC would then also be adjusted)

CyrMag

unread,
Sep 25, 2021, 8:03:56 AM9/25/21
to
The F2008 standard answers your question quite clearly in section 16.9.127 : "The i-th subscript returned lies in the range 1 to e_i, where e_i is the extent of the i-th dimension of ARRAY".

CyrMag

unread,
Sep 25, 2021, 8:08:27 AM9/25/21
to
CORRECTION: The F2018 standard, not F2008. The latter has the same text in section 13.7.108.

gah4

unread,
Sep 26, 2021, 10:06:43 AM9/26/21
to
On Saturday, September 25, 2021 at 3:32:18 AM UTC-7, Dave Tholen wrote:
> According to gfortran on-line documentation, the MAXLOC intrinsic determines
> the location of the element in the array with the maximum value, but what is
> the definition of "location" in this instance? The on-line documentation for
> IBM's XL Fortran calls it "the subscript of the location", but what if the
> subscript doesn't start at 1?

I have commented on the numbering system before, but note that array
expressions always start at 1.

A dummy array that is not POINTER or ALLOCATABLE doesn't get the lower
bound from the caller. Since the argument of MAXLOC isn't POINTER or
ALLOCATABLE, then it won't get the lower bound.

But overall, the standard does not make this very easy to follow, finding
the exact cases where arrays lose the lower bound.

Ron Shepard

unread,
Sep 26, 2021, 12:27:48 PM9/26/21
to
On 9/26/21 9:06 AM, gah4 wrote:
> On Saturday, September 25, 2021 at 3:32:18 AM UTC-7, Dave Tholen wrote:
>> According to gfortran on-line documentation, the MAXLOC intrinsic determines
>> the location of the element in the array with the maximum value, but what is
>> the definition of "location" in this instance? The on-line documentation for
>> IBM's XL Fortran calls it "the subscript of the location", but what if the
>> subscript doesn't start at 1?
>
> I have commented on the numbering system before, but note that array
> expressions always start at 1.
>
> A dummy array that is not POINTER or ALLOCATABLE doesn't get the lower
> bound from the caller. Since the argument of MAXLOC isn't POINTER or
> ALLOCATABLE, then it won't get the lower bound.

Yes, this shows that the way MAXLOC works is consistent with the way any
other user-written subroutine works. In fact, MAXLOC could be a
user-written function, there is nothing special about it in that way.

> But overall, the standard does not make this very easy to follow, finding
> the exact cases where arrays lose the lower bound.

Other intrinsic functions such as LBOUND and UBOUND do not work that
way. These functions could NOT be user-written because they return array
metadata directly, metadata that is lost or replaced during the
association of actual and dummy arguments (with the exceptions noted
above). It is tricky sometimes to keep track of the two types of
functions and their exceptions, and MAXLOC is one of those tricky cases
because it could have been defined to work either way.

$.02 -Ron Shepard


Steve Lionel

unread,
Oct 8, 2021, 4:53:17 PM10/8/21
to
On 9/25/2021 6:49 AM, Thomas Koenig wrote:
> hat gfortran does is correct. The standard is a bit more expansive on
> that, it states (among other cases)
>
> # The ith subscript returned lies in the range 1 to ei, where ei is
> # the extent of the ith dimension of ARRAY. If ARRAY has size zero,
> # all elements of the result are zero.
>
>> If the compiler is producing correct results, then the documentation really
>> should clarify the definition of "location" for arrays with subscripts that
>> do not start at 1.
> gfortran uses the terminology of the standard, but the documentation
> certainly could be improved. If you have a suggestion, please send it
> tofo...@gcc.gnu.org.
>
> (Documentation for MINLOC and FINDLOC would then also be adjusted)

The "pseudo-subscript" notation used in the standard for functions such
as MAXLOC has been a source of confusion for many. I struggled with it
myself, and worked with Malcolm Cohen to improve the wording for the
next revision. See https://j3-fortran.org/doc/year/21/21-143r1.txt for
the details.

--
Steve Lionel
ISO/IEC JTC1/SC22/WG5 (Fortran) Convenor
Retired Intel Fortran developer/support
Email: firstname at firstnamelastname dot com
Twitter: @DoctorFortran
LinkedIn: https://www.linkedin.com/in/stevelionel
Blog: https://stevelionel.com/drfortran
WG5: https://wg5-fortran.org

gah4

unread,
Oct 8, 2021, 7:14:00 PM10/8/21
to
On Friday, October 8, 2021 at 1:53:17 PM UTC-7, Steve Lionel wrote:

(snip)

> The "pseudo-subscript" notation used in the standard for functions such
> as MAXLOC has been a source of confusion for many. I struggled with it
> myself, and worked with Malcolm Cohen to improve the wording for the
> next revision. See https://j3-fortran.org/doc/year/21/21-143r1.txt for
> the details.

Not only functions, but for arrays in many cases.

Exactly when a dummy array keeps the lower bound of the actual array
and when it doesn't has come up at least a few times here.

In any case, thanks for helping clear it up in the case of functions.

Steve Lionel

unread,
Oct 9, 2021, 11:44:56 AM10/9/21
to
On 10/8/2021 7:13 PM, gah4 wrote:
> Exactly when a dummy array keeps the lower bound of the actual array
> and when it doesn't has come up at least a few times here.

True, but I think of that as a separate issue related to dummy arguments
and their bounds as related to the bounds of the associated actual
argument.
0 new messages