-
Notifications
You must be signed in to change notification settings - Fork 189
Addition of a function to compute the k-th order central moment of an array #153
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
Merged
+1,162
−8
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,265 @@ | ||
#:include "common.fypp" | ||
#:set RANKS = range(1, MAXRANK + 1) | ||
#:set RC_KINDS_TYPES = REAL_KINDS_TYPES + CMPLX_KINDS_TYPES | ||
submodule (stdlib_experimental_stats) stdlib_experimental_stats_moment | ||
|
||
use, intrinsic:: ieee_arithmetic, only: ieee_value, ieee_quiet_nan | ||
use stdlib_experimental_error, only: error_stop | ||
use stdlib_experimental_optval, only: optval | ||
implicit none | ||
|
||
contains | ||
|
||
#:for k1, t1 in RC_KINDS_TYPES | ||
#:for rank in RANKS | ||
#:set RName = rname("moment_all",rank, t1, k1) | ||
module function ${RName}$(x, order, mask) result(res) | ||
${t1}$, intent(in) :: x${ranksuffix(rank)}$ | ||
integer, intent(in) :: order | ||
logical, intent(in), optional :: mask | ||
${t1}$ :: res | ||
|
||
real(${k1}$) :: n | ||
${t1}$ :: mean | ||
|
||
if (.not.optval(mask, .true.)) then | ||
res = ieee_value(1._${k1}$, ieee_quiet_nan) | ||
return | ||
end if | ||
|
||
n = size(x, kind = int64) | ||
mean = sum(x) / n | ||
|
||
res = sum((x - mean)**order) / n | ||
|
||
end function ${RName}$ | ||
#:endfor | ||
#:endfor | ||
|
||
|
||
#:for k1, t1 in INT_KINDS_TYPES | ||
#:for rank in RANKS | ||
#:set RName = rname("moment_all",rank, t1, k1, 'dp') | ||
module function ${RName}$(x, order, mask) result(res) | ||
${t1}$, intent(in) :: x${ranksuffix(rank)}$ | ||
integer, intent(in) :: order | ||
logical, intent(in), optional :: mask | ||
real(dp) :: res | ||
|
||
real(dp) :: n, mean | ||
|
||
if (.not.optval(mask, .true.)) then | ||
res = ieee_value(1._dp, ieee_quiet_nan) | ||
return | ||
end if | ||
|
||
n = size(x, kind = int64) | ||
mean = sum(real(x, dp)) / n | ||
|
||
res = sum((real(x, dp) - mean)**order) / n | ||
|
||
end function ${RName}$ | ||
#:endfor | ||
#:endfor | ||
|
||
|
||
#:for k1, t1 in RC_KINDS_TYPES | ||
#:for rank in RANKS | ||
#:set RName = rname("moment",rank, t1, k1) | ||
module function ${RName}$(x, order, dim, mask) result(res) | ||
${t1}$, intent(in) :: x${ranksuffix(rank)}$ | ||
integer, intent(in) :: order | ||
integer, intent(in) :: dim | ||
logical, intent(in), optional :: mask | ||
${t1}$ :: res${reduced_shape('x', rank, 'dim')}$ | ||
|
||
integer :: i | ||
real(${k1}$) :: n | ||
${t1}$ :: mean${reduced_shape('x', rank, 'dim')}$ | ||
|
||
if (.not.optval(mask, .true.)) then | ||
res = ieee_value(1._${k1}$, ieee_quiet_nan) | ||
return | ||
end if | ||
|
||
n = size(x, dim) | ||
mean = sum(x, dim) / n | ||
|
||
res = 0 | ||
select case(dim) | ||
#:for fi in range(1, rank+1) | ||
case(${fi}$) | ||
do i = 1, size(x, dim) | ||
res = res + (x${select_subarray(rank, [(fi, 'i')])}$ - mean)**order | ||
end do | ||
#:endfor | ||
case default | ||
call error_stop("ERROR (moment): wrong dimension") | ||
end select | ||
res = res / n | ||
|
||
end function ${RName}$ | ||
#:endfor | ||
#:endfor | ||
|
||
|
||
#:for k1, t1 in INT_KINDS_TYPES | ||
#:for rank in RANKS | ||
#:set RName = rname("moment",rank, t1, k1, 'dp') | ||
module function ${RName}$(x, order, dim, mask) result(res) | ||
${t1}$, intent(in) :: x${ranksuffix(rank)}$ | ||
integer, intent(in) :: order | ||
integer, intent(in) :: dim | ||
logical, intent(in), optional :: mask | ||
real(dp) :: res${reduced_shape('x', rank, 'dim')}$ | ||
|
||
integer :: i | ||
real(dp) :: n | ||
real(dp) :: mean${reduced_shape('x', rank, 'dim')}$ | ||
|
||
if (.not.optval(mask, .true.)) then | ||
res = ieee_value(1._dp, ieee_quiet_nan) | ||
return | ||
end if | ||
|
||
n = size(x, dim) | ||
mean = sum(real(x, dp), dim) / n | ||
|
||
res = 0 | ||
select case(dim) | ||
#:for fi in range(1, rank+1) | ||
case(${fi}$) | ||
do i = 1, size(x, dim) | ||
res = res + (real(x${select_subarray(rank, [(fi, 'i')])}$, dp) - mean)**order | ||
end do | ||
#:endfor | ||
case default | ||
call error_stop("ERROR (moment): wrong dimension") | ||
end select | ||
res = res / n | ||
|
||
end function ${RName}$ | ||
#:endfor | ||
#:endfor | ||
|
||
|
||
#:for k1, t1 in RC_KINDS_TYPES | ||
#:for rank in RANKS | ||
#:set RName = rname("moment_mask_all",rank, t1, k1) | ||
module function ${RName}$(x, order, mask) result(res) | ||
${t1}$, intent(in) :: x${ranksuffix(rank)}$ | ||
integer, intent(in) :: order | ||
logical, intent(in) :: mask${ranksuffix(rank)}$ | ||
${t1}$ :: res | ||
|
||
real(${k1}$) :: n | ||
${t1}$ :: mean | ||
|
||
n = count(mask, kind = int64) | ||
mean = sum(x, mask) / n | ||
|
||
res = sum((x - mean)**order, mask) / n | ||
|
||
end function ${RName}$ | ||
#:endfor | ||
#:endfor | ||
|
||
|
||
#:for k1, t1 in INT_KINDS_TYPES | ||
#:for rank in RANKS | ||
#:set RName = rname("moment_mask_all",rank, t1, k1, 'dp') | ||
module function ${RName}$(x, order, mask) result(res) | ||
${t1}$, intent(in) :: x${ranksuffix(rank)}$ | ||
integer, intent(in) :: order | ||
logical, intent(in) :: mask${ranksuffix(rank)}$ | ||
real(dp) :: res | ||
|
||
real(dp) :: n, mean | ||
|
||
n = count(mask, kind = int64) | ||
mean = sum(real(x, dp), mask) / n | ||
|
||
res = sum((real(x, dp) - mean)**order, mask) / n | ||
|
||
end function ${RName}$ | ||
#:endfor | ||
#:endfor | ||
|
||
|
||
#:for k1, t1 in RC_KINDS_TYPES | ||
#:for rank in RANKS | ||
#:set RName = rname("moment_mask",rank, t1, k1) | ||
module function ${RName}$(x, order, dim, mask) result(res) | ||
${t1}$, intent(in) :: x${ranksuffix(rank)}$ | ||
integer, intent(in) :: order | ||
integer, intent(in) :: dim | ||
logical, intent(in) :: mask${ranksuffix(rank)}$ | ||
${t1}$ :: res${reduced_shape('x', rank, 'dim')}$ | ||
|
||
integer :: i | ||
real(${k1}$) :: n${reduced_shape('x', rank, 'dim')}$ | ||
${t1}$ :: mean${reduced_shape('x', rank, 'dim')}$ | ||
|
||
n = count(mask, dim) | ||
mean = sum(x, dim, mask) / n | ||
|
||
res = 0 | ||
select case(dim) | ||
#:for fi in range(1, rank+1) | ||
case(${fi}$) | ||
do i = 1, size(x, dim) | ||
res = res + merge( (x${select_subarray(rank, [(fi, 'i')])}$ - mean)**order,& | ||
#:if t1[0] == 'r' | ||
0._${k1}$,& | ||
#:else | ||
cmplx(0,0,kind=${k1}$),& | ||
#:endif | ||
mask${select_subarray(rank, [(fi, 'i')])}$) | ||
end do | ||
#:endfor | ||
case default | ||
call error_stop("ERROR (moment): wrong dimension") | ||
end select | ||
res = res / n | ||
|
||
end function ${RName}$ | ||
#:endfor | ||
#:endfor | ||
|
||
|
||
#:for k1, t1 in INT_KINDS_TYPES | ||
#:for rank in RANKS | ||
#:set RName = rname("moment_mask",rank, t1, k1, 'dp') | ||
module function ${RName}$(x, order, dim, mask) result(res) | ||
${t1}$, intent(in) :: x${ranksuffix(rank)}$ | ||
integer, intent(in) :: order | ||
integer, intent(in) :: dim | ||
logical, intent(in) :: mask${ranksuffix(rank)}$ | ||
real(dp) :: res${reduced_shape('x', rank, 'dim')}$ | ||
|
||
integer :: i | ||
real(dp) :: n${reduced_shape('x', rank, 'dim')}$ | ||
real(dp) :: mean${reduced_shape('x', rank, 'dim')}$ | ||
|
||
n = count(mask, dim) | ||
mean = sum(real(x, dp), dim, mask) / n | ||
|
||
res = 0 | ||
select case(dim) | ||
#:for fi in range(1, rank+1) | ||
case(${fi}$) | ||
do i = 1, size(x, dim) | ||
res = res + merge((real(x${select_subarray(rank, [(fi, 'i')])}$, dp) - mean)**order,& | ||
0._dp, mask${select_subarray(rank, [(fi, 'i')])}$) | ||
end do | ||
#:endfor | ||
case default | ||
call error_stop("ERROR (moment): wrong dimension") | ||
end select | ||
res = res / n | ||
|
||
end function ${RName}$ | ||
#:endfor | ||
#:endfor | ||
|
||
end submodule |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
ADDTEST(mean) | ||
ADDTEST(moment) | ||
ADDTEST(var) | ||
ADDTEST(varn) | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
PROGS_SRC = test_mean.f90 test_var.f90 | ||
PROGS_SRC = test_mean.f90 test_moment.f90 test_var.f90 | ||
|
||
include ../Makefile.manual.test.mk |
Large diffs are not rendered by default.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not
here? Is there a concern about the overhead from calling
mean()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was indeed to avoid the overhead of calling
mean
(especially whenmean
is an array).