- Notifications
You must be signed in to change notification settings - Fork 198
Addition of variance function in stdlib_experimental_stats #144
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
Merged
Changes from 1 commit
Commits
Show all changes
8 commits Select commit Hold shift + click to select a range
e966e7b addition of variance
jvdp1 044abc5 varaince_dev: update var modules
jvdp1 d77b6e9 variance_dev: update spec var
jvdp1 baabfc8 variance_dev: changed ieee_value() as proposed
jvdp1 9b19154 variance_dev: remove support of complex because it was wrong
jvdp1 da90a89 variance_dev: addition of variance of complex as (var(real(x)) + var(…
jvdp1 2a0182a variance_dev: use fypp to avoid abs in real functions
jvdp1 01e897c variance_dev:suggestions by @aradi
jvdp1 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
varaince_dev: update var modules
- Loading branch information
commit 044abc55653013202c98025fd1ef34d8a23d2adc
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,6 @@ | ||
| #: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_var | ||
| | ||
| use, intrinsic:: ieee_arithmetic, only: ieee_value, ieee_quiet_nan | ||
| | @@ -12,17 +10,18 @@ submodule (stdlib_experimental_stats) stdlib_experimental_stats_var | |
| | ||
| contains | ||
| | ||
| #:for k1, t1 in REAL_KINDS_TYPES | ||
| #:for k1, t1 in RC_KINDS_TYPES | ||
| #:for rank in RANKS | ||
| module function var_${rank}$_all_${k1}$_${k1}$(x, mask) result(res) | ||
| #:set RName = rname("var_all",rank, t1, k1) | ||
| module function ${RName}$(x, mask) result(res) | ||
| ${t1}$, intent(in) :: x${ranksuffix(rank)}$ | ||
| logical, intent(in), optional :: mask | ||
| ${t1}$ :: res | ||
| | ||
| ${t1}$ :: n, mean | ||
| | ||
| if (.not.optval(mask, .true.)) then | ||
| Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a weird idiom to me. Here, I'd prefer the more obvious if (present(mask)) then if (mask .eqv. .false.) thenBut this is a matter of style rather than substance. Member Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hopefully none of both options will be needed in a future standard. | ||
| res = ieee_value(res, ieee_quiet_nan) | ||
| res = ieee_value(real(res, kind=${k1}$), ieee_quiet_nan) | ||
| return | ||
| end if | ||
| | ||
| | @@ -31,14 +30,15 @@ contains | |
| | ||
| res = sum((x - mean)**2) / (n - 1._${k1}$) | ||
| | ||
| end function var_${rank}$_all_${k1}$_${k1}$ | ||
| end function ${RName}$ | ||
| #:endfor | ||
| #:endfor | ||
| | ||
| | ||
| #:for k1, t1 in INT_KINDS_TYPES | ||
| #:for rank in RANKS | ||
| module function var_${rank}$_all_${k1}$_dp(x, mask) result(res) | ||
| #:set RName = rname("var_all",rank, t1, k1, 'dp') | ||
| module function ${RName}$(x, mask) result(res) | ||
| ${t1}$, intent(in) :: x${ranksuffix(rank)}$ | ||
| logical, intent(in), optional :: mask | ||
| real(dp) :: res | ||
| | @@ -55,14 +55,15 @@ contains | |
| | ||
| res = sum((real(x, dp) - mean)**2) / (n - 1._dp) | ||
| | ||
| end function var_${rank}$_all_${k1}$_dp | ||
| end function ${RName}$ | ||
| #:endfor | ||
| #:endfor | ||
| | ||
| | ||
| #:for k1, t1 in REAL_KINDS_TYPES | ||
| #:for k1, t1 in RC_KINDS_TYPES | ||
| #:for rank in RANKS | ||
| module function var_${rank}$_${k1}$_${k1}$(x, dim, mask) result(res) | ||
| #:set RName = rname("var",rank, t1, k1) | ||
| module function ${RName}$(x, dim, mask) result(res) | ||
| ${t1}$, intent(in) :: x${ranksuffix(rank)}$ | ||
| integer, intent(in) :: dim | ||
| logical, intent(in), optional :: mask | ||
| | @@ -73,7 +74,7 @@ contains | |
| ${t1}$ :: mean${reduced_shape('x', rank, 'dim')}$ | ||
| | ||
| if (.not.optval(mask, .true.)) then | ||
| res = ieee_value(res, ieee_quiet_nan) | ||
| res = ieee_value(real(res, kind=${k1}$), ieee_quiet_nan) | ||
| return | ||
| end if | ||
| | ||
| | @@ -92,14 +93,15 @@ contains | |
| end select | ||
| res = res / (n - 1._${k1}$) | ||
| | ||
| end function var_${rank}$_${k1}$_${k1}$ | ||
| end function ${RName}$ | ||
| #:endfor | ||
| #:endfor | ||
| | ||
| | ||
| #:for k1, t1 in INT_KINDS_TYPES | ||
| #:for rank in RANKS | ||
| module function var_${rank}$_${k1}$_dp(x, dim, mask) result(res) | ||
| #:set RName = rname("var",rank, t1, k1, 'dp') | ||
| module function ${RName}$(x, dim, mask) result(res) | ||
| ${t1}$, intent(in) :: x${ranksuffix(rank)}$ | ||
| integer, intent(in) :: dim | ||
| logical, intent(in), optional :: mask | ||
| | @@ -129,14 +131,15 @@ contains | |
| end select | ||
| res = res / (n - 1._dp) | ||
| | ||
| end function var_${rank}$_${k1}$_dp | ||
| end function ${RName}$ | ||
| #:endfor | ||
| #:endfor | ||
| | ||
| | ||
| #:for k1, t1 in REAL_KINDS_TYPES | ||
| #:for k1, t1 in RC_KINDS_TYPES | ||
| #:for rank in RANKS | ||
| module function var_${rank}$_mask_all_${k1}$_${k1}$(x, mask) result(res) | ||
| #:set RName = rname("var_mask_all",rank, t1, k1) | ||
| module function ${RName}$(x, mask) result(res) | ||
| ${t1}$, intent(in) :: x${ranksuffix(rank)}$ | ||
| logical, intent(in) :: mask${ranksuffix(rank)}$ | ||
| ${t1}$ :: res | ||
| | @@ -148,14 +151,15 @@ contains | |
| | ||
| res = sum((x - mean)**2, mask) / (n - 1._${k1}$) | ||
| | ||
| end function var_${rank}$_mask_all_${k1}$_${k1}$ | ||
| end function ${RName}$ | ||
| #:endfor | ||
| #:endfor | ||
| | ||
| | ||
| #:for k1, t1 in INT_KINDS_TYPES | ||
| #:for rank in RANKS | ||
| module function var_${rank}$_mask_all_${k1}$_dp(x, mask) result(res) | ||
| #:set RName = rname("var_mask_all",rank, t1, k1, 'dp') | ||
| module function ${RName}$(x, mask) result(res) | ||
| ${t1}$, intent(in) :: x${ranksuffix(rank)}$ | ||
| logical, intent(in) :: mask${ranksuffix(rank)}$ | ||
| real(dp) :: res | ||
| | @@ -167,14 +171,15 @@ contains | |
| | ||
| res = sum((real(x, dp) - mean)**2, mask) / (n - 1._dp) | ||
| | ||
| end function var_${rank}$_mask_all_${k1}$_dp | ||
| end function ${RName}$ | ||
| #:endfor | ||
| #:endfor | ||
| | ||
| | ||
| #:for k1, t1 in REAL_KINDS_TYPES | ||
| #:for k1, t1 in RC_KINDS_TYPES | ||
| #:for rank in RANKS | ||
| module function var_${rank}$_mask_${k1}$_${k1}$(x, dim, mask) result(res) | ||
| #:set RName = rname("var_mask",rank, t1, k1) | ||
| module function ${RName}$(x, dim, mask) result(res) | ||
| ${t1}$, intent(in) :: x${ranksuffix(rank)}$ | ||
| integer, intent(in) :: dim | ||
| logical, intent(in) :: mask${ranksuffix(rank)}$ | ||
| | @@ -192,22 +197,28 @@ contains | |
| mean = sum(x, dim, mask) / n | ||
| do i = 1, size(x, dim) | ||
| res = res + merge( (x${rankindice(':', 'i', rank, fi )}$ - mean)**2,& | ||
| 0._${k1}$, mask${rankindice(':', 'i', rank, fi)}$) | ||
| #:if t1[0] == 'r' | ||
| 0._${k1}$,& | ||
| #:else | ||
| cmplx(0._${k1}$, 0._${k1}$, ${k1}$),& | ||
| #:endif | ||
| mask${rankindice(':', 'i', rank, fi)}$) | ||
| end do | ||
| #:endfor | ||
| case default | ||
| call error_stop("ERROR (mean): wrong dimension") | ||
| end select | ||
| res = res / (n - 1._${k1}$) | ||
| | ||
| end function var_${rank}$_mask_${k1}$_${k1}$ | ||
| end function ${RName}$ | ||
| #:endfor | ||
| #:endfor | ||
| | ||
| | ||
| #:for k1, t1 in INT_KINDS_TYPES | ||
| #:for rank in RANKS | ||
| module function var_${rank}$_mask_${k1}$_dp(x, dim, mask) result(res) | ||
| #:set RName = rname("var_mask",rank, t1, k1, 'dp') | ||
| module function ${RName}$(x, dim, mask) result(res) | ||
| ${t1}$, intent(in) :: x${ranksuffix(rank)}$ | ||
| integer, intent(in) :: dim | ||
| logical, intent(in) :: mask${ranksuffix(rank)}$ | ||
| | @@ -233,7 +244,7 @@ contains | |
| end select | ||
| res = res / (n - 1._dp) | ||
| | ||
| end function var_${rank}$_mask_${k1}$_dp | ||
| end function ${RName}$ | ||
| #:endfor | ||
| #:endfor | ||
| | ||
| | ||
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.
We should probably rename this macro to have a more descriptive name. If it is only used to select subarrays by reducing the dimension, we could have:
and use it as
It could also be used, if we need to reduce more than one rank, e.g.
Also the description should be clarified a bit.
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.
Implemented as suggested. The proposed macro is more general and better fit to its aim.
Could you have another review, please?