Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple sorting intrinsics #258

Open
w6ws opened this issue Apr 24, 2022 · 17 comments
Open

Add simple sorting intrinsics #258

w6ws opened this issue Apr 24, 2022 · 17 comments
Labels
Clause 16 Standard Clause 16: Intrinsic procedures and modules

Comments

@w6ws
Copy link

w6ws commented Apr 24, 2022

Most languages have sorting procedures in their libraries, and some even have them as operators. Various Fortran implementations have had sorting procedures in their libraries for many many years - yet none are called in a standardized way. Another approach might be to use C Interop to call C's qsort. However it is not as user-friendly as simply calling an intrinsic. It also has performance problems due to the lack of inlining of the required comparison procedure.

I'd like to propose two sets of simple intrinsic functions that accept 1D array arguments of intrinsic types (e.g., integers, reals, and character strings - maybe logicals), and either return a sorted version of the input, or an integer permutation vector ("grade"). The latter should have an optional argument for stability. Using the old HPF function names as examples, but minus HPF's funky DIM= argument:

result = GRADE_DOWN (ARG[, STABLE=logical_scalar]) ! returns permutation vector for accessing input in descending order
result = GRADE_UP (ARG[, STABLE=logical_scalar]) ! returns permutation vector for accessing input in ascending order

result = SORT_DOWN (ARG) ! returns input in descending order
result = SORT_UP (ARG) ! returns input in ascending order

I like having them in functional form for use in expressions. However I could see also adding intrinsic subroutine versions of SORT_[UP|DOWN] for doing in-place sorts. For example:

call SORT_INPLACE_DOWN (ARG)
call SORT_INPLACE_UP (ARG)

The GRADE versions are especially useful with sorting arrays of derived types. Once the permutation vector has been obtained, the individual elements can be accessed or permuted as needed:

type(mytype_t), allocatable :: my_array(:), my_sorted_array(:)
integer, allocatable :: perm_vector(:)
:
perm_vector = GRADE_UP (my_array%key_value)
my_sorted_array = my_array(perm_vector)

or even:

my_sorted_array = my_array(GRADE_UP (my_array%key_value))

@wyphan
Copy link

wyphan commented Apr 24, 2022 via email

@w6ws
Copy link
Author

w6ws commented Apr 24, 2022 via email

@wyphan
Copy link

wyphan commented Apr 24, 2022

The DIM= argument can be optional. Then the GRADE_DOWN and GRADE_UP intrinsic functions can be made ELEMENTAL so it applies to arrays. Maybe add an AXIS= argument too to indicate which dimensions of the array will be sorted.

@w6ws
Copy link
Author

w6ws commented Apr 24, 2022 via email

@womenflyplanes
Copy link

Mathematical definitions are in my dissertation. If you’d like to speak to the original designer and implementor grade up and down in APL It is Luther Woodrum and he can be reached at
lex@skyelogic.org

@w6ws
Copy link
Author

w6ws commented Apr 24, 2022 via email

@womenflyplanes
Copy link

womenflyplanes commented Apr 24, 2022 via email

@womenflyplanes
Copy link

womenflyplanes commented Apr 24, 2022 via email

@certik certik added the Clause 16 Standard Clause 16: Intrinsic procedures and modules label Apr 24, 2022
@womenflyplanes
Copy link

womenflyplanes commented Apr 24, 2022 via email

@womenflyplanes
Copy link

womenflyplanes commented Apr 25, 2022 via email

@womenflyplanes
Copy link

womenflyplanes commented Apr 25, 2022 via email

@womenflyplanes
Copy link

womenflyplanes commented Apr 25, 2022 via email

@certik
Copy link
Member

certik commented May 8, 2022

This seems a duplicate of #101.

Update: the #101 is not a duplicate, but a similar proposal. See the clarification below.

@w6ws
Copy link
Author

w6ws commented May 8, 2022 via email

@certik
Copy link
Member

certik commented May 8, 2022

@w6ws, let's keep this issue here, separate from #101. I can see that they are similar, but distinct proposals, so let's keep both issues open. I wasn't sure if you were aware of the work at #101, but it's now clarified.

@gklimowicz
Copy link
Member

Bye the way, how long has fortran had multidimensional arrays? LW

Since Fortran I in 1956, if you mean 1, 2, or 3-dimensional arrays. I believe that was the case for Fortan II and Fortran 66.

Fortran 77 upped the limit to 7 dimensions, which appears to be the limit in Fortran 90, 95, and 2003.

Fortran 2008 increased the limit to 15 dimensions, but as the sum of dimensions and co-dimensions of an array (or coarray).

@womenflyplanes
Copy link

womenflyplanes commented Oct 11, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Clause 16 Standard Clause 16: Intrinsic procedures and modules
Projects
None yet
Development

No branches or pull requests

5 participants