Skip to content

Commit

Permalink
stat_dev: avoid allocatable functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jvdp1 committed Jan 19, 2020
1 parent a1c6353 commit 6fb6ca5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
14 changes: 7 additions & 7 deletions src/stdlib_experimental_stat.fypp.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions src/stdlib_experimental_stat_mean.fypp.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions src/tests/stat/test_mean.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 6fb6ca5

Please sign in to comment.