forked from fortran-lang/stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
how to use pure functions in submodules
- Loading branch information
Showing
3 changed files
with
49 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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 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,50 +1,21 @@ | ||
module stdlib_experimental_stat | ||
use stdlib_experimental_kinds, only: sp, dp, qp | ||
use stdlib_experimental_error, only: error_stop | ||
use stdlib_experimental_optval, only: optval | ||
implicit none | ||
private | ||
! Public API | ||
public :: mean | ||
|
||
|
||
interface mean | ||
module procedure mean_1_dp_dp | ||
module procedure mean_2_dp_dp | ||
module function mean_1_dp_dp(mat) result(res) | ||
real(dp), intent(in) :: mat(:) | ||
real(dp) ::res | ||
end function | ||
module function mean_2_dp_dp(mat, dim) result(res) | ||
real(dp), intent(in) :: mat(:,:) | ||
integer, intent(in), optional :: dim | ||
real(dp), allocatable ::res(:) | ||
end function | ||
end interface | ||
|
||
contains | ||
|
||
pure function mean_1_dp_dp(mat) result(res) | ||
real(dp), intent(in) :: mat(:) | ||
real(dp) ::res | ||
|
||
res = sum(mat) / real(size(mat), dp) | ||
|
||
end function mean_1_dp_dp | ||
|
||
function mean_2_dp_dp(mat, dim) result(res) | ||
real(dp), intent(in) :: mat(:,:) | ||
integer, intent(in), optional :: dim | ||
real(dp), allocatable ::res(:) | ||
|
||
integer :: i | ||
integer :: dim_ | ||
|
||
dim_ = optval(dim, 1) | ||
|
||
allocate(res(size(mat, dim_))) | ||
|
||
if (dim_ == 1) then | ||
do i=1, size(mat, dim_) | ||
res(i) = mean_1_dp_dp(mat(i,:)) | ||
end do | ||
else if (dim_ == 2) then | ||
do i=1, size(mat, dim_) | ||
res(i) = mean_1_dp_dp(mat(:,i)) | ||
end do | ||
end if | ||
|
||
end function mean_2_dp_dp | ||
|
||
end module |
This file contains 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,39 @@ | ||
submodule (stdlib_experimental_stat) stdlib_experimental_stat_mean | ||
use stdlib_experimental_optval, only: optval | ||
implicit none | ||
|
||
contains | ||
|
||
module function mean_1_dp_dp(mat) result(res) | ||
real(dp), intent(in) :: mat(:) | ||
real(dp) ::res | ||
|
||
res = sum(mat) / real(size(mat), dp) | ||
|
||
end function mean_1_dp_dp | ||
|
||
module function mean_2_dp_dp(mat, dim) result(res) | ||
real(dp), intent(in) :: mat(:,:) | ||
integer, intent(in), optional :: dim | ||
real(dp), allocatable ::res(:) | ||
|
||
integer :: i | ||
integer :: dim_ | ||
|
||
dim_ = optval(dim, 1) | ||
|
||
allocate(res(size(mat, dim_))) | ||
|
||
if (dim_ == 1) then | ||
do i=1, size(mat, dim_) | ||
res(i) = mean_1_dp_dp(mat(i,:)) | ||
end do | ||
else if (dim_ == 2) then | ||
do i=1, size(mat, dim_) | ||
res(i) = mean_1_dp_dp(mat(:,i)) | ||
end do | ||
end if | ||
|
||
end function mean_2_dp_dp | ||
|
||
end submodule |