From 965f37b7847855febea946d645018f8bc0783ca4 Mon Sep 17 00:00:00 2001 From: "Vandenplas, Jeremie" Date: Sun, 19 Jan 2020 11:35:11 +0100 Subject: [PATCH] moved to submodules how to use pure functions in submodules --- src/CMakeLists.txt | 1 + src/stdlib_experimental_stat.f90 | 47 +++++---------------------- src/stdlib_experimental_stat_mean.f90 | 39 ++++++++++++++++++++++ 3 files changed, 49 insertions(+), 38 deletions(-) create mode 100644 src/stdlib_experimental_stat_mean.f90 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index db10a2366..e27ab54d0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,6 +5,7 @@ set(SRC stdlib_experimental_kinds.f90 stdlib_experimental_optval.f90 stdlib_experimental_system.F90 + stdlib_experimental_stat_mean.f90 stdlib_experimental_stat.f90 ) diff --git a/src/stdlib_experimental_stat.f90 b/src/stdlib_experimental_stat.f90 index 45f2c9aba..756b3b43b 100644 --- a/src/stdlib_experimental_stat.f90 +++ b/src/stdlib_experimental_stat.f90 @@ -1,7 +1,5 @@ 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 @@ -9,42 +7,15 @@ module stdlib_experimental_stat 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 diff --git a/src/stdlib_experimental_stat_mean.f90 b/src/stdlib_experimental_stat_mean.f90 new file mode 100644 index 000000000..97cd62186 --- /dev/null +++ b/src/stdlib_experimental_stat_mean.f90 @@ -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