[1765] | 1 | MODULE cumsum_m |
---|
| 2 | |
---|
| 3 | IMPLICIT NONE |
---|
| 4 | |
---|
| 5 | INTEGER, PARAMETER, private :: NPAR_CUMSUM=16 |
---|
| 6 | |
---|
| 7 | INTERFACE cumsum |
---|
| 8 | MODULE PROCEDURE cumsum_r,cumsum_i |
---|
| 9 | END INTERFACE |
---|
| 10 | |
---|
| 11 | private cumsum_r,cumsum_i |
---|
| 12 | |
---|
| 13 | CONTAINS |
---|
| 14 | |
---|
| 15 | RECURSIVE FUNCTION cumsum_r(arr,seed) RESULT(ans) |
---|
| 16 | REAL, DIMENSION(:), INTENT(IN) :: arr |
---|
| 17 | REAL, OPTIONAL, INTENT(IN) :: seed |
---|
| 18 | REAL, DIMENSION(size(arr)) :: ans |
---|
| 19 | INTEGER :: n,j |
---|
| 20 | REAL :: sd |
---|
| 21 | n=size(arr) |
---|
| 22 | if (n == 0) RETURN |
---|
| 23 | sd=0.0 |
---|
| 24 | if (present(seed)) sd=seed |
---|
| 25 | ans(1)=arr(1)+sd |
---|
| 26 | if (n < NPAR_CUMSUM) then |
---|
| 27 | do j=2,n |
---|
| 28 | ans(j)=ans(j-1)+arr(j) |
---|
| 29 | end do |
---|
| 30 | else |
---|
| 31 | ans(2:n:2)=cumsum_r(arr(2:n:2)+arr(1:n-1:2),sd) |
---|
| 32 | ans(3:n:2)=ans(2:n-1:2)+arr(3:n:2) |
---|
| 33 | end if |
---|
| 34 | END FUNCTION cumsum_r |
---|
| 35 | !BL |
---|
| 36 | RECURSIVE FUNCTION cumsum_i(arr,seed) RESULT(ans) |
---|
| 37 | INTEGER, DIMENSION(:), INTENT(IN) :: arr |
---|
| 38 | INTEGER, OPTIONAL, INTENT(IN) :: seed |
---|
| 39 | INTEGER, DIMENSION(size(arr)) :: ans |
---|
| 40 | INTEGER :: n,j,sd |
---|
| 41 | n=size(arr) |
---|
| 42 | if (n == 0) RETURN |
---|
| 43 | sd=0 |
---|
| 44 | if (present(seed)) sd=seed |
---|
| 45 | ans(1)=arr(1)+sd |
---|
| 46 | if (n < NPAR_CUMSUM) then |
---|
| 47 | do j=2,n |
---|
| 48 | ans(j)=ans(j-1)+arr(j) |
---|
| 49 | end do |
---|
| 50 | else |
---|
| 51 | ans(2:n:2)=cumsum_i(arr(2:n:2)+arr(1:n-1:2),sd) |
---|
| 52 | ans(3:n:2)=ans(2:n-1:2)+arr(3:n:2) |
---|
| 53 | end if |
---|
| 54 | END FUNCTION cumsum_i |
---|
| 55 | |
---|
| 56 | END MODULE cumsum_m |
---|