You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)).
The text was updated successfully, but these errors were encountered:
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
I suggest adding a
largest
function so thatlargest(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. Forn > size(x)
there would be a run-time error. Asmallest
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)
andmean_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 argumentorder
were omitted, the observations would be returned in the same order as x. Iforder="a"
ororder="d"
, the observations would be returned in ascending or descending order. The function middle would return a zero-sized array ifsize(x) < ntrim_min + ntrim_max
. In a previous issue I suggestedtrimmed_mean
. I think a function such asmiddle
is more important, sincetrimmed_mean
would be a byproduct.ETA: maybe functions
largest_loc
,smallest_loc
, andmiddle_loc
, returning positions inx(:)
, are the first things to implement, since they are needed forlargest
,smallest
, andmiddle
. They would also have independent value. For two arraysx(:)
andy(:)
of equal size I often want the y values corresponding to the largest n values ofx(:)
. I could usey(largest_loc(x,nlargest))
.The text was updated successfully, but these errors were encountered: