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

largest, smallest #405

Open
Beliavsky opened this issue Apr 29, 2021 · 1 comment
Open

largest, smallest #405

Beliavsky opened this issue Apr 29, 2021 · 1 comment
Labels
topic: algorithms searching and sorting, merging, ...

Comments

@Beliavsky
Copy link

Beliavsky commented Apr 29, 2021

I suggest adding a largest function so that largest(x(:),n) returns a 1D array containing the largest n elements of x in descending order. It would work for real and integer arrays and possibly arrays of character variables. For n > size(x) there would be a run-time error. A smallest function would return the smallest n elements in ascending order. Largest would have a subset of the functionality of maxk in Matlab.

Associated functions such as sum_largest(x,n) and mean_largest(x,n) could also be considered. In statistics, insurance, and finance, the properties of the n largest or smallest values of a sample are often studied.

middle(x,ntrim_min,ntrim_max,order) returning the values of x after removing the smallest ntrim_min and largest ntrim_max values could be considered. If optional argument order were omitted, the observations would be returned in the same order as x. If order="a" or order="d", the observations would be returned in ascending or descending order. The function middle would return a zero-sized array if size(x) < ntrim_min + ntrim_max. In a previous issue I suggested trimmed_mean. I think a function such as middle is more important, since trimmed_mean would be a byproduct.

ETA: maybe functions largest_loc, smallest_loc, and middle_loc, returning positions in x(:), are the first things to implement, since they are needed for largest, smallest, and middle. They would also have independent value. For two arrays x(:) and y(:) of equal size I often want the y values corresponding to the largest n values of x(:). I could use y(largest_loc(x,nlargest)).

@Beliavsky
Copy link
Author

Largest and smallest would be commonly used. I think the main argument against them is that they are simple for a programmer to implement herself, but the same is true of some other functions in stdlib.

I have implemented largest_loc, largest, smallest_loc, and smallest in qsortd.f90 and have a calling program xqsortd.f90. The implementation is not efficient since it always does a full sort.

program xqsortd
use qsortd_mod, only: qsortd,smallest_loc,smallest,largest_loc,largest,indexx
implicit none
integer, parameter :: dp = kind(1.0d0), n = 20, npick = 5
real(kind=dp)      :: x(n)
character (len=*), parameter :: fmt_cr = "(a15,1000f8.5)"
call random_number(x)
print*,"n =",n
write (*,fmt_cr) "raw",x
write (*,fmt_cr) "ascending",x(indexx(x))
write (*,fmt_cr) "descending",x(indexx(-x))
write (*,fmt_cr) "smallest",x(smallest_loc(x,npick))
write (*,fmt_cr) "smallest",smallest(x,npick)
write (*,fmt_cr) "largest",x(largest_loc(x,npick))
write (*,fmt_cr) "largest",largest(x,npick)
end program xqsortd

On Windows, gfortran qsortd.f90 xqsortd.f90 & a.exe gives

 n =          20
            raw 0.78346 0.75909 0.78432 0.94185 0.54228 0.09335 0.13471 0.95694 0.96032 0.90382 0.30923 0.35247 0.74817 0.58403 0.73146 0.82168 0.45425 0.39220 0.16022 0.61044
      ascending 0.09335 0.13471 0.16022 0.30923 0.35247 0.39220 0.45425 0.54228 0.58403 0.61044 0.73146 0.74817 0.75909 0.78346 0.78432 0.82168 0.90382 0.94185 0.95694 0.96032
     descending 0.96032 0.95694 0.94185 0.90382 0.82168 0.78432 0.78346 0.75909 0.74817 0.73146 0.61044 0.58403 0.54228 0.45425 0.39220 0.35247 0.30923 0.16022 0.13471 0.09335
       smallest 0.09335 0.13471 0.16022 0.30923 0.35247
       smallest 0.09335 0.13471 0.16022 0.30923 0.35247
        largest 0.96032 0.95694 0.94185 0.90382 0.82168
        largest 0.96032 0.95694 0.94185 0.90382 0.82168

@awvwgk awvwgk added the topic: algorithms searching and sorting, merging, ... label Sep 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: algorithms searching and sorting, merging, ...
Projects
None yet
Development

No branches or pull requests

2 participants