ParaMonte Fortran 2.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
pm_mathCumSum::setCumSum Interface Reference

Return the cumulative sum of the input array, optionally in the backward direction and optionally, reverse the output cumulative sum array upon return. More...

Detailed Description

Return the cumulative sum of the input array, optionally in the backward direction and optionally, reverse the output cumulative sum array upon return.

Parameters
[out]cumsum: The output contiguous array of the same size, shape, type, and kind as the input array containing the cumulative sum of array in the specified direction.
(optional, if missing, the result will be written to the input/output argument array.)
[in,out]array: The contiguous array of shape (:) of either,
  1. type integer of kind any supported by the processor (e.g., IK, IK8, IK16, IK32, or IK64) or,
  2. type complex of kind any supported by the processor (e.g., CK, CK32, CK64, or CK128) or,
  3. type real of kind any supported by the processor (e.g., RK, RK32, RK64, or RK128),
whose cumulative sum will have to be computed.
  1. If cumsum is present, then array has intent(in).
  2. If cumsum is missing, then array has intent(inout).
    On output, the contents of array will be completely overwritten by the computed cumsum.
[in]direction: The input scalar object that can be,
  1. the constant forward or equivalently, an object of type forward_type, implying that the output cumulative sum has be computed from the first element to the last element of the input array.
    even though the increments will still be written from the first element of cumsum to the last.
  2. the constant backward or equivalently, an object of type backward_type, implying that the output cumulative sum has be computed from the last element to the first element of the input array even though the increments will still be written from the first element of cumsum to the last.
(optional. It must be present if and only if the input argument action is also present.)
[in]action: The input scalar object that can be,
  1. the constant nothing or equivalently, an object of type nothing_type, implying no action to be performed on the elements of the output cumsum.
  2. the constant reverse or equivalently, an object of type reverse_type, implying that the order of the elements of the output cumsum will have be reversed upon return, such that its last element becomes the first.
(optional. It must be present if and only if the input argument direction is also present.)


Possible calling interfaces

call setCumSum(array(:))
call setCumSum(cumsum(:), array(:))
call setCumSum(array(:), direction, action)
call setCumSum(cumsum(:), array(:), direction, action)
Return the cumulative sum of the input array, optionally in the backward direction and optionally,...
This module contains the procedures and interfaces for computing the cumulative sum of an array.
Warning
The condition 0 < size(array) must hold for the corresponding arguments.
The condition size(array) == size(cumsum) must hold for the corresponding arguments.
This condition is verified only if the library is built with the preprocessor macro CHECK_ENABLED=1.
The pure procedure(s) documented herein become impure when the ParaMonte library is compiled with preprocessor macro CHECK_ENABLED=1.
By default, these procedures are pure in release build and impure in debug and testing builds.
Remarks
The procedures under discussion are recursive.
The procedures under this generic interface becomes recursive when array has intent(inout) (i.e., cumsum is missing).
See also
getCumSum
getCumPropExp
setCumPropExp


Example usage

1program example
2
3 use pm_kind, only: SK, IK, LK
4 use pm_io, only: display_type
6 use pm_mathCumSum, only: setCumSum, forward, backward, nothing, reverse
7
8 implicit none
9
10 type(display_type) :: disp
11
12 disp = display_type(file = "main.out.F90")
13
14 block
15 use pm_kind, only: IKG => IK
16 integer(IKG), allocatable :: array(:), cumsum(:)
17 call disp%skip
18 call disp%show("array = [1, 2, 3, 4]")
19 array = [1, 2, 3, 4]
20 call disp%show("call setResized(cumsum, size(array, 1, IK))")
21 call setResized(cumsum, size(array, 1, IK))
22 call disp%show("call setCumSum(cumsum, array)")
23 call setCumSum(cumsum, array)
24 call disp%show("cumsum")
25 call disp%show( cumsum )
26 call disp%show("call setCumSum(cumsum, array, forward, nothing)")
27 call setCumSum(cumsum, array, forward, nothing)
28 call disp%show("cumsum")
29 call disp%show( cumsum )
30 call disp%show("call setCumSum(cumsum, array, forward, reverse)")
31 call setCumSum(cumsum, array, forward, reverse)
32 call disp%show("cumsum")
33 call disp%show( cumsum )
34 call disp%show("call setCumSum(cumsum, array, backward, nothing)")
35 call setCumSum(cumsum, array, backward, nothing)
36 call disp%show("cumsum")
37 call disp%show( cumsum )
38 call disp%show("call setCumSum(cumsum, array, backward, reverse)")
39 call setCumSum(cumsum, array, backward, reverse)
40 call disp%show("cumsum")
41 call disp%show( cumsum )
42 call disp%skip
43 end block
44
45 block
46 use pm_kind, only: IKG => IK
47 integer(IKG), allocatable :: array(:), cumsum(:), reference(:)
48 call disp%skip
49 call disp%show("reference = [1, 2, 3, 4]")
50 reference = [1, 2, 3, 4]
51 call disp%show("array = reference")
52 array = reference
53 call disp%show("call setCumSum(array)")
54 call setCumSum(array)
55 call disp%show("array")
56 call disp%show( array )
57 call disp%show("array = reference")
58 array = reference
59 call disp%show("call setCumSum(array, forward, nothing)")
60 call setCumSum(array, forward, nothing)
61 call disp%show("array")
62 call disp%show( array )
63 call disp%show("array = reference")
64 array = reference
65 call disp%show("call setCumSum(array, forward, reverse)")
66 call setCumSum(array, forward, reverse)
67 call disp%show("array")
68 call disp%show( array )
69 call disp%show("array = reference")
70 array = reference
71 call disp%show("call setCumSum(array, backward, nothing)")
72 call setCumSum(array, backward, nothing)
73 call disp%show("array")
74 call disp%show( array )
75 call disp%show("array = reference")
76 array = reference
77 call disp%show("call setCumSum(array, backward, reverse)")
78 call setCumSum(array, backward, reverse)
79 call disp%show("array")
80 call disp%show( array )
81 call disp%skip
82 end block
83
84 block
85 use pm_kind, only: CKG => CKS
86 complex(CKG), allocatable :: array(:), cumsum(:)
87 call disp%skip
88 call disp%show("array = cmplx([real(CKG) :: 1, 2, 3, 4], -[real(CKG) :: 1, 2, 3, 4], CKG)")
89 array = cmplx([real(CKG) :: 1, 2, 3, 4], -[real(CKG) :: 1, 2, 3, 4], CKG)
90 call disp%show("call setResized(cumsum, size(array, 1, IK))")
91 call setResized(cumsum, size(array, 1, IK))
92 call disp%show("call setCumSum(cumsum, array)")
93 call setCumSum(cumsum, array)
94 call disp%show("cumsum")
95 call disp%show( cumsum )
96 call disp%show("call setCumSum(cumsum, array, forward, nothing)")
97 call setCumSum(cumsum, array, forward, nothing)
98 call disp%show("cumsum")
99 call disp%show( cumsum )
100 call disp%show("call setCumSum(cumsum, array, forward, reverse)")
101 call setCumSum(cumsum, array, forward, reverse)
102 call disp%show("cumsum")
103 call disp%show( cumsum )
104 call disp%show("call setCumSum(cumsum, array, backward, nothing)")
105 call setCumSum(cumsum, array, backward, nothing)
106 call disp%show("cumsum")
107 call disp%show( cumsum )
108 call disp%show("call setCumSum(cumsum, array, backward, reverse)")
109 call setCumSum(cumsum, array, backward, reverse)
110 call disp%show("cumsum")
111 call disp%show( cumsum )
112 call disp%skip
113 end block
114
115 block
116 use pm_kind, only: CKG => CKS
117 complex(CKG), allocatable :: array(:), cumsum(:), reference(:)
118 call disp%skip
119 call disp%show("reference = cmplx([real(CKG) :: 1, 2, 3, 4], -[real(CKG) :: 1, 2, 3, 4], CKG)")
120 reference = cmplx([real(CKG) :: 1, 2, 3, 4], -[real(CKG) :: 1, 2, 3, 4], CKG)
121 call disp%show("array = reference")
122 array = reference
123 call disp%show("call setCumSum(array)")
124 call setCumSum(array)
125 call disp%show("array")
126 call disp%show( array )
127 call disp%show("array = reference")
128 array = reference
129 call disp%show("call setCumSum(array, forward, nothing)")
130 call setCumSum(array, forward, nothing)
131 call disp%show("array")
132 call disp%show( array )
133 call disp%show("array = reference")
134 array = reference
135 call disp%show("call setCumSum(array, forward, reverse)")
136 call setCumSum(array, forward, reverse)
137 call disp%show("array")
138 call disp%show( array )
139 call disp%show("array = reference")
140 array = reference
141 call disp%show("call setCumSum(array, backward, nothing)")
142 call setCumSum(array, backward, nothing)
143 call disp%show("array")
144 call disp%show( array )
145 call disp%show("array = reference")
146 array = reference
147 call disp%show("call setCumSum(array, backward, reverse)")
148 call setCumSum(array, backward, reverse)
149 call disp%show("array")
150 call disp%show( array )
151 call disp%skip
152 end block
153
154 block
155 use pm_kind, only: RKG => RKH
156 real(RKG), allocatable :: array(:), cumsum(:)
157 call disp%skip
158 call disp%show("array = [real(RKG) :: 1, 2, 3, 4]")
159 array = [real(RKG) :: 1, 2, 3, 4]
160 call disp%show("call setResized(cumsum, size(array, 1, IK))")
161 call setResized(cumsum, size(array, 1, IK))
162 call disp%show("call setCumSum(cumsum, array)")
163 call setCumSum(cumsum, array)
164 call disp%show("cumsum")
165 call disp%show( cumsum )
166 call disp%show("call setCumSum(cumsum, array, forward, nothing)")
167 call setCumSum(cumsum, array, forward, nothing)
168 call disp%show("cumsum")
169 call disp%show( cumsum )
170 call disp%show("call setCumSum(cumsum, array, forward, reverse)")
171 call setCumSum(cumsum, array, forward, reverse)
172 call disp%show("cumsum")
173 call disp%show( cumsum )
174 call disp%show("call setCumSum(cumsum, array, backward, nothing)")
175 call setCumSum(cumsum, array, backward, nothing)
176 call disp%show("cumsum")
177 call disp%show( cumsum )
178 call disp%show("call setCumSum(cumsum, array, backward, reverse)")
179 call setCumSum(cumsum, array, backward, reverse)
180 call disp%show("cumsum")
181 call disp%show( cumsum )
182 call disp%skip
183 end block
184
185 block
186 use pm_kind, only: RKG => RKH
187 real(RKG), allocatable :: array(:), cumsum(:), reference(:)
188 call disp%skip
189 call disp%show("reference = [1, 2, 3, 4]")
190 reference = [1, 2, 3, 4]
191 call disp%show("array = reference")
192 array = reference
193 call disp%show("call setCumSum(array)")
194 call setCumSum(array)
195 call disp%show("array")
196 call disp%show( array )
197 call disp%show("array = reference")
198 array = reference
199 call disp%show("call setCumSum(array, forward, nothing)")
200 call setCumSum(array, forward, nothing)
201 call disp%show("array")
202 call disp%show( array )
203 call disp%show("array = reference")
204 array = reference
205 call disp%show("call setCumSum(array, forward, reverse)")
206 call setCumSum(array, forward, reverse)
207 call disp%show("array")
208 call disp%show( array )
209 call disp%show("array = reference")
210 array = reference
211 call disp%show("call setCumSum(array, backward, nothing)")
212 call setCumSum(array, backward, nothing)
213 call disp%show("array")
214 call disp%show( array )
215 call disp%show("array = reference")
216 array = reference
217 call disp%show("call setCumSum(array, backward, reverse)")
218 call setCumSum(array, backward, reverse)
219 call disp%show("array")
220 call disp%show( array )
221 call disp%skip
222 end block
223
224end program example
Allocate or resize (shrink or expand) an input allocatable scalar string or array of rank 1....
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11726
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11508
This module contains procedures and generic interfaces for resizing allocatable arrays of various typ...
This module contains classes and procedures for input/output (IO) or generic display operations on st...
Definition: pm_io.F90:252
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
Definition: pm_io.F90:11393
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
Definition: pm_kind.F90:268
integer, parameter LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
Definition: pm_kind.F90:541
integer, parameter CKS
The single-precision complex kind in Fortran mode. On most platforms, this is a 32-bit real kind.
Definition: pm_kind.F90:570
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
Definition: pm_kind.F90:540
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
Definition: pm_kind.F90:539
integer, parameter RKH
The scalar integer constant of intrinsic default kind, representing the highest-precision real kind t...
Definition: pm_kind.F90:858
Generate and return an object of type display_type.
Definition: pm_io.F90:10282

Example Unix compile command via Intel ifort compiler
1#!/usr/bin/env sh
2rm main.exe
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example Windows Batch compile command via Intel ifort compiler
1del main.exe
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
4main.exe

Example Unix / MinGW compile command via GNU gfortran compiler
1#!/usr/bin/env sh
2rm main.exe
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example output
1
2array = [1, 2, 3, 4]
3call setResized(cumsum, size(array, 1, IK))
4call setCumSum(cumsum, array)
5cumsum
6+1, +3, +6, +10
7call setCumSum(cumsum, array, forward, nothing)
8cumsum
9+1, +3, +6, +10
10call setCumSum(cumsum, array, forward, reverse)
11cumsum
12+10, +6, +3, +1
13call setCumSum(cumsum, array, backward, nothing)
14cumsum
15+4, +7, +9, +10
16call setCumSum(cumsum, array, backward, reverse)
17cumsum
18+10, +9, +7, +4
19
20
21reference = [1, 2, 3, 4]
22array = reference
23call setCumSum(array)
24array
25+1, +3, +6, +10
26array = reference
27call setCumSum(array, forward, nothing)
28array
29+1, +3, +6, +10
30array = reference
31call setCumSum(array, forward, reverse)
32array
33+10, +6, +3, +1
34array = reference
35call setCumSum(array, backward, nothing)
36array
37+4, +7, +9, +10
38array = reference
39call setCumSum(array, backward, reverse)
40array
41+10, +9, +7, +4
42
43
44array = cmplx([real(CKG) :: 1, 2, 3, 4], -[real(CKG) :: 1, 2, 3, 4], CKG)
45call setResized(cumsum, size(array, 1, IK))
46call setCumSum(cumsum, array)
47cumsum
48(+1.00000000, -1.00000000), (+3.00000000, -3.00000000), (+6.00000000, -6.00000000), (+10.0000000, -10.0000000)
49call setCumSum(cumsum, array, forward, nothing)
50cumsum
51(+1.00000000, -1.00000000), (+3.00000000, -3.00000000), (+6.00000000, -6.00000000), (+10.0000000, -10.0000000)
52call setCumSum(cumsum, array, forward, reverse)
53cumsum
54(+10.0000000, -10.0000000), (+6.00000000, -6.00000000), (+3.00000000, -3.00000000), (+1.00000000, -1.00000000)
55call setCumSum(cumsum, array, backward, nothing)
56cumsum
57(+4.00000000, -4.00000000), (+7.00000000, -7.00000000), (+9.00000000, -9.00000000), (+10.0000000, -10.0000000)
58call setCumSum(cumsum, array, backward, reverse)
59cumsum
60(+10.0000000, -10.0000000), (+9.00000000, -9.00000000), (+7.00000000, -7.00000000), (+4.00000000, -4.00000000)
61
62
63reference = cmplx([real(CKG) :: 1, 2, 3, 4], -[real(CKG) :: 1, 2, 3, 4], CKG)
64array = reference
65call setCumSum(array)
66array
67(+1.00000000, -1.00000000), (+3.00000000, -3.00000000), (+6.00000000, -6.00000000), (+10.0000000, -10.0000000)
68array = reference
69call setCumSum(array, forward, nothing)
70array
71(+1.00000000, -1.00000000), (+3.00000000, -3.00000000), (+6.00000000, -6.00000000), (+10.0000000, -10.0000000)
72array = reference
73call setCumSum(array, forward, reverse)
74array
75(+10.0000000, -10.0000000), (+6.00000000, -6.00000000), (+3.00000000, -3.00000000), (+1.00000000, -1.00000000)
76array = reference
77call setCumSum(array, backward, nothing)
78array
79(+4.00000000, -4.00000000), (+7.00000000, -7.00000000), (+9.00000000, -9.00000000), (+10.0000000, -10.0000000)
80array = reference
81call setCumSum(array, backward, reverse)
82array
83(+10.0000000, -10.0000000), (+9.00000000, -9.00000000), (+7.00000000, -7.00000000), (+4.00000000, -4.00000000)
84
85
86array = [real(RKG) :: 1, 2, 3, 4]
87call setResized(cumsum, size(array, 1, IK))
88call setCumSum(cumsum, array)
89cumsum
90+1.00000000000000000000000000000000000, +3.00000000000000000000000000000000000, +6.00000000000000000000000000000000000, +10.0000000000000000000000000000000000
91call setCumSum(cumsum, array, forward, nothing)
92cumsum
93+1.00000000000000000000000000000000000, +3.00000000000000000000000000000000000, +6.00000000000000000000000000000000000, +10.0000000000000000000000000000000000
94call setCumSum(cumsum, array, forward, reverse)
95cumsum
96+10.0000000000000000000000000000000000, +6.00000000000000000000000000000000000, +3.00000000000000000000000000000000000, +1.00000000000000000000000000000000000
97call setCumSum(cumsum, array, backward, nothing)
98cumsum
99+4.00000000000000000000000000000000000, +7.00000000000000000000000000000000000, +9.00000000000000000000000000000000000, +10.0000000000000000000000000000000000
100call setCumSum(cumsum, array, backward, reverse)
101cumsum
102+10.0000000000000000000000000000000000, +9.00000000000000000000000000000000000, +7.00000000000000000000000000000000000, +4.00000000000000000000000000000000000
103
104
105reference = [1, 2, 3, 4]
106array = reference
107call setCumSum(array)
108array
109+1.00000000000000000000000000000000000, +3.00000000000000000000000000000000000, +6.00000000000000000000000000000000000, +10.0000000000000000000000000000000000
110array = reference
111call setCumSum(array, forward, nothing)
112array
113+1.00000000000000000000000000000000000, +3.00000000000000000000000000000000000, +6.00000000000000000000000000000000000, +10.0000000000000000000000000000000000
114array = reference
115call setCumSum(array, forward, reverse)
116array
117+10.0000000000000000000000000000000000, +6.00000000000000000000000000000000000, +3.00000000000000000000000000000000000, +1.00000000000000000000000000000000000
118array = reference
119call setCumSum(array, backward, nothing)
120array
121+4.00000000000000000000000000000000000, +7.00000000000000000000000000000000000, +9.00000000000000000000000000000000000, +10.0000000000000000000000000000000000
122array = reference
123call setCumSum(array, backward, reverse)
124array
125+10.0000000000000000000000000000000000, +9.00000000000000000000000000000000000, +7.00000000000000000000000000000000000, +4.00000000000000000000000000000000000
126
127
Test:
test_pm_mathCumSum
Todo:
Normal Priority: This generic interface can be expanded to include input arrays with Weights.


Final Remarks


If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.

  1. If you use any parts or concepts from this library to any extent, please acknowledge the usage by citing the relevant publications of the ParaMonte library.
  2. If you regenerate any parts/ideas from this library in a programming environment other than those currently supported by this ParaMonte library (i.e., other than C, C++, Fortran, MATLAB, Python, R), please also ask the end users to cite this original ParaMonte library.

This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.

Author:
Fatemeh Bagheri, Tuesday 08:49 PM, August 10, 2021, Dallas, TX

Definition at line 425 of file pm_mathCumSum.F90.


The documentation for this interface was generated from the following file: