forked from fortran-lang/stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdlib_experimental_stats_mean.fypp
74 lines (53 loc) · 1.93 KB
/
stdlib_experimental_stats_mean.fypp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#:include "common.fypp"
#:set RANKS = range(1, MAXRANK + 1)
submodule (stdlib_experimental_stats) stdlib_experimental_stats_mean
use stdlib_experimental_error, only: error_stop
implicit none
contains
#:for k1, t1 in REAL_KINDS_TYPES
#:for rank in RANKS
module function mean_${rank}$_all_${k1}$_${k1}$(x) result(res)
${t1}$, intent(in) :: x${ranksuffix(rank)}$
${t1}$ :: res
res = sum(x) / real(size(x, kind = int64), ${k1}$)
end function mean_${rank}$_all_${k1}$_${k1}$
#:endfor
#:endfor
#:for k1, t1 in INT_KINDS_TYPES
#:for rank in RANKS
module function mean_${rank}$_all_${k1}$_dp(x) result(res)
${t1}$, intent(in) :: x${ranksuffix(rank)}$
real(dp) :: res
res = sum(real(x, dp)) / real(size(x, kind = int64), dp)
end function mean_${rank}$_all_${k1}$_dp
#:endfor
#:endfor
#:for k1, t1 in REAL_KINDS_TYPES
#:for rank in RANKS
module function mean_${rank}$_${k1}$_${k1}$(x, dim) result(res)
${t1}$, intent(in) :: x${ranksuffix(rank)}$
integer, intent(in) :: dim
${t1}$ :: res${reduced_shape('x', rank, 'dim')}$
if (dim >= 1 .and. dim <= ${rank}$) then
res = sum(x, dim) / real(size(x, dim), ${k1}$)
else
call error_stop("ERROR (mean): wrong dimension")
end if
end function mean_${rank}$_${k1}$_${k1}$
#:endfor
#:endfor
#:for k1, t1 in INT_KINDS_TYPES
#:for rank in RANKS
module function mean_${rank}$_${k1}$_dp(x, dim) result(res)
${t1}$, intent(in) :: x${ranksuffix(rank)}$
integer, intent(in) :: dim
real(dp) :: res${reduced_shape('x', rank, 'dim')}$
if (dim >= 1 .and. dim <= ${rank}$) then
res = sum(x, dim) / real(size(x, dim), dp)
else
call error_stop("ERROR (mean): wrong dimension")
end if
end function mean_${rank}$_${k1}$_dp
#:endfor
#:endfor
end submodule