-
Notifications
You must be signed in to change notification settings - Fork 199
Description
Current
(Related to #113)
The current spec for stdlib mean is:
mean - mean of array elements
Description
Returns the mean of all the elements of array, or of the elements of array along dimension dim if provided.
Syntax
result = mean(array)
result = mean(array, dim)
Arguments
array: Shall be an array of type integer, or real.
dim: Shall be a scalar of type integer with a value in the range from 1 to n, where n is the rank of array.
Return value
If array is of type real, the result is of the same type as array.
If array is of type integer, the result is of type double precision.
If dim is absent, a scalar with the mean of all elements in array is returned. Otherwise, an array of rank n-1, where n equals the rank of array, and a shape similar to that of array with dimension dim dropped is returned.
Proposal
I would like to propose to add mask into the API of mean as follows (similar to the intrinsic sum):
mean - mean of array elements
Description
Returns the mean of all the elements of array, or of the elements of array along dimension dim if provided, and if the corresponding element in mask is true.
Syntax
result = mean(array [, mask])
result = mean(array, dim [, mask])
Arguments
array: Shall be an array of type integer, or real.
dim: Shall be a scalar of type integer with a value in the range from 1 to n, where n is the rank of array.
mask (optional): Shall be of type logical and either be a scalar or an array of the same shape as array.
Return value
If array is of type real, the result is of the same type as array.
If array is of type integer, the result is of type double precision.
If dim is absent, a scalar with the mean of all elements in array is returned. Otherwise, an array of rank n-1, where n equals the rank of array, and a shape similar to that of array with dimension dim dropped is returned.
If mask is specified, the result is the mean of all elements of array corresponding to true elements of mask. If every element of mask is false , the result is zero.
This definition of mask for mean agrees with the definition of mask of the intrinsic sum in the Fortran Standard draft:
p. 429:
MASK (optional) shall be of type logical and shall be conformable with ARRAY.
and
p. 9:
3.36
conformable
⟨of two data entities⟩ having the same shape, or one being an array and the other being scalar
Implementation
Each function for mean, e.g.,
module function mean_1_sp_sp(x) result(res)
real(sp), intent(in) :: x(:)
real(sp) :: res
res = sum(x) / real(size(x, kind = int64), dp)
end function mean_1_sp_spshould then become:
module function mean_1_sp_sp(x, mask) result(res)
real(sp), intent(in) :: x(:)
real(sp) :: res
logical, intent(in), optional :: mask
if(.not.optval(mask, .true.))then
res = 0._dp
return
end if
res = sum(x) / real(size(x, kind = int64), dp)
end function mean_1_sp_sp
module function mean_1_mask_sp_sp(x, mask) result(res)
real(sp), intent(in) :: x(:)
real(sp) :: res
logical, intent(in) :: mask(:)
res = sum(x, mask) / real(count(mask, kind = int64), dp)
end function mean_1_mask_sp_spI kown there was some discussions about dim and if it should be considered as optional or not in the spec. I guess the same discussion could appear here since mask is not optional in one of the 2 functions. However, it will behave like optional for the users through the interface mean.