From 6fb6ca52103024df374ab39e6bfde7b43525cf93 Mon Sep 17 00:00:00 2001 From: "Vandenplas, Jeremie" Date: Sun, 19 Jan 2020 21:03:46 +0100 Subject: [PATCH] stat_dev: avoid allocatable functions --- src/stdlib_experimental_stat.fypp.f90 | 14 ++++++------ src/stdlib_experimental_stat_mean.fypp.f90 | 26 +++++++++++----------- src/tests/stat/test_mean.f90 | 3 +++ 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/stdlib_experimental_stat.fypp.f90 b/src/stdlib_experimental_stat.fypp.f90 index 2db139b03..bc3a9c26a 100644 --- a/src/stdlib_experimental_stat.fypp.f90 +++ b/src/stdlib_experimental_stat.fypp.f90 @@ -14,25 +14,25 @@ module stdlib_experimental_stat interface mean #:for i1, k1, t1 in ikt - module function mean_1_${k1}$_${k1}$(mat) result(res) - ${t1}$, intent(in) :: mat(:) + module function mean_1_${k1}$_${k1}$(x) result(res) + ${t1}$, intent(in) :: x(:) ${t1}$ ::res end function mean_1_${k1}$_${k1}$ #:endfor #:for i1, k1, t1 in ikt - module function mean_2_all_${k1}$_${k1}$(mat) result(res) - ${t1}$, intent(in) :: mat(:,:) + module function mean_2_all_${k1}$_${k1}$(x) result(res) + ${t1}$, intent(in) :: x(:,:) ${t1}$ ::res end function mean_2_all_${k1}$_${k1}$ #:endfor #:for i1, k1, t1 in ikt - module function mean_2_${k1}$_${k1}$(mat, dim) result(res) - ${t1}$, intent(in) :: mat(:,:) + module function mean_2_${k1}$_${k1}$(x, dim) result(res) + ${t1}$, intent(in) :: x(:,:) integer, intent(in) :: dim - ${t1}$ :: res(size(mat)/size(mat, dim)) + ${t1}$ :: res(size(x)/size(x, dim)) end function mean_2_${k1}$_${k1}$ #:endfor end interface diff --git a/src/stdlib_experimental_stat_mean.fypp.f90 b/src/stdlib_experimental_stat_mean.fypp.f90 index 98f96f482..32504757f 100644 --- a/src/stdlib_experimental_stat_mean.fypp.f90 +++ b/src/stdlib_experimental_stat_mean.fypp.f90 @@ -12,40 +12,40 @@ contains #:for i1, k1, t1 in ikt -module function mean_1_${k1}$_${k1}$(mat) result(res) - ${t1}$, intent(in) :: mat(:) +module function mean_1_${k1}$_${k1}$(x) result(res) + ${t1}$, intent(in) :: x(:) ${t1}$ ::res - res = sum(mat) / real(size(mat), ${k1}$) + res = sum(x) / real(size(x), ${k1}$) end function mean_1_${k1}$_${k1}$ #:endfor #:for i1, k1, t1 in ikt -module function mean_2_all_${k1}$_${k1}$(mat) result(res) - ${t1}$, intent(in) :: mat(:,:) +module function mean_2_all_${k1}$_${k1}$(x) result(res) + ${t1}$, intent(in) :: x(:,:) ${t1}$ ::res - res = sum(mat) / real(size(mat), ${k1}$) + res = sum(x) / real(size(x), ${k1}$) end function mean_2_all_${k1}$_${k1}$ #:endfor #:for i1, k1, t1 in ikt -module function mean_2_${k1}$_${k1}$(mat, dim) result(res) - ${t1}$, intent(in) :: mat(:,:) +module function mean_2_${k1}$_${k1}$(x, dim) result(res) + ${t1}$, intent(in) :: x(:,:) integer, intent(in) :: dim - ${t1}$ :: res(size(mat)/size(mat, dim)) + ${t1}$ :: res(size(x)/size(x, dim)) integer :: i if (dim == 1) then - do i=1, size(mat)/size(mat, dim) - res(i) = mean_1_${k1}$_${k1}$(mat(:,i)) + do i=1, size(x)/size(x, dim) + res(i) = mean_1_${k1}$_${k1}$(x(:,i)) end do else if (dim == 2) then - do i=1, size(mat)/size(mat, dim) - res(i) = mean_1_${k1}$_${k1}$(mat(i,:)) + do i=1, size(x)/size(x, dim) + res(i) = mean_1_${k1}$_${k1}$(x(i,:)) end do end if diff --git a/src/tests/stat/test_mean.f90 b/src/tests/stat/test_mean.f90 index 4a0e83e9b..e17dc902b 100644 --- a/src/tests/stat/test_mean.f90 +++ b/src/tests/stat/test_mean.f90 @@ -15,6 +15,9 @@ program test_mean call assert(sum( mean(s, dim = 1) - [4.0_sp, 5.0_sp] ) == 0.0_sp) call assert(sum( mean(s, dim = 2) - [1.5_dp, 3.5_dp, 5.5_dp, 7.5_dp] ) == 0.0_sp) +call assert( size(mean(s, dim = 1))- size(sum(s, dim = 1)) == 0) +call assert( size(mean(s, dim = 2))- size(sum(s, dim = 2)) == 0) + !dp call loadtxt("array1.dat", d)