|
| 1 | +program test_mean |
| 2 | +use stdlib_experimental_error, only: assert |
| 3 | +use stdlib_experimental_kinds, only: sp, dp |
| 4 | +use stdlib_experimental_io, only: loadtxt |
| 5 | +use stdlib_experimental_stat, only: mean |
| 6 | +use stdlib_experimental_error, only: error_stop |
| 7 | +implicit none |
| 8 | + |
| 9 | +real(sp), allocatable :: s(:, :) |
| 10 | +real(dp), allocatable :: d(:, :) |
| 11 | +real(dp), allocatable :: res(:) |
| 12 | + |
| 13 | +!call loadtxt("array1.dat", s) |
| 14 | +!call print_array(s) |
| 15 | + |
| 16 | +call loadtxt("array1.dat", d) |
| 17 | + |
| 18 | +res = mean(d) |
| 19 | +call print_array(d) |
| 20 | +print *,'Mean = ', res |
| 21 | +call assert(sum( res - [1.5_dp, 3.5_dp, 5.5_dp, 7.5_dp] ) == 0.0_dp) |
| 22 | + |
| 23 | +res = mean(d, dim = 2) |
| 24 | +call print_array(d) |
| 25 | +print *,'Mean = ', res |
| 26 | +call assert(sum( res - [4.0_dp, 5.0_dp] ) == 0.0_dp) |
| 27 | + |
| 28 | +!call loadtxt("array2.dat", d) |
| 29 | +!call print_array(d) |
| 30 | +! |
| 31 | +!call loadtxt("array3.dat", d) |
| 32 | +!call print_array(d) |
| 33 | +! |
| 34 | +!call loadtxt("array4.dat", d) |
| 35 | +!call print_array(d) |
| 36 | + |
| 37 | +contains |
| 38 | + |
| 39 | +subroutine print_array(a) |
| 40 | +class(*),intent(in) :: a(:, :) |
| 41 | +integer :: i |
| 42 | +print *, "Array, shape=(", size(a, 1), ",", size(a, 2), ")" |
| 43 | + |
| 44 | + select type(a) |
| 45 | + type is(real(sp)) |
| 46 | + do i = 1, size(a, 1) |
| 47 | + print *, a(i, :) |
| 48 | + end do |
| 49 | + type is(real(dp)) |
| 50 | + do i = 1, size(a, 1) |
| 51 | + print *, a(i, :) |
| 52 | + end do |
| 53 | + class default |
| 54 | + call error_stop('The proposed type is not supported') |
| 55 | + end select |
| 56 | + |
| 57 | +end subroutine |
| 58 | + |
| 59 | +end program |
0 commit comments