• MAXLOC

    From Dave Tholen@21:1/5 to All on Sat Sep 25 00:32:13 2021
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Koenig@21:1/5 to Dave Tholen on Sat Sep 25 10:49:25 2021
    Dave Tholen <tholen@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 fortran@gcc.gnu.org.

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From CyrMag@21:1/5 to Dave Tholen on Sat Sep 25 05:03:54 2021
    On Saturday, September 25, 2021 at 5:32:18 AM UTC-5, 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?

    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.

    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".

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From CyrMag@21:1/5 to CyrMag on Sat Sep 25 05:08:25 2021
    On Saturday, September 25, 2021 at 7:03:56 AM UTC-5, CyrMag wrote:
    On Saturday, September 25, 2021 at 5:32:18 AM UTC-5, 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?

    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.
    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".

    CORRECTION: The F2018 standard, not F2008. The latter has the same text in section 13.7.108.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From gah4@21:1/5 to Dave Tholen on Sun Sep 26 07:06:41 2021
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ron Shepard@21:1/5 to All on Sun Sep 26 11:27:46 2021
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Steve Lionel@21:1/5 to Thomas Koenig on Fri Oct 8 16:53:13 2021
    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 tofortran@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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From gah4@21:1/5 to Steve Lionel on Fri Oct 8 16:13:59 2021
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Steve Lionel@21:1/5 to All on Sat Oct 9 11:44:52 2021
    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.

    --
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)