|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# cusum |
| 22 | + |
| 23 | +> Compute the cumulative sum along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var cusum = require( '@stdlib/blas/ext/cusum' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### cusum( x\[, initial]\[, options] ) |
| 34 | + |
| 35 | +Computes the cumulative sum along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 39 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 40 | + |
| 41 | +var xbuf = [ -1.0, 2.0, -3.0 ]; |
| 42 | +var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); |
| 43 | + |
| 44 | +var y = cusum( x ); |
| 45 | +// returns <ndarray> |
| 46 | + |
| 47 | +var arr = ndarray2array( y ); |
| 48 | +// returns [ -1.0, 1.0, -2.0 ] |
| 49 | +``` |
| 50 | + |
| 51 | +The function has the following parameters: |
| 52 | + |
| 53 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. |
| 54 | +- **initial**: initial value for the cumulative sum (_optional_). May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] which [promotes][@stdlib/ndarray/promotion-rules] to the data type of the input [ndarray][@stdlib/ndarray/ctor]. If provided a scalar value, the value is cast to the data type of the input [ndarray][@stdlib/ndarray/ctor]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] initial value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] initial value must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. By default, the initial value is the additive identity (i.e., zero). |
| 55 | +- **options**: function options (_optional_). |
| 56 | + |
| 57 | +The function accepts the following options: |
| 58 | + |
| 59 | +- **dims**: list of dimensions over which to perform operation. If not provided, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. |
| 60 | +- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. |
| 61 | + |
| 62 | +By default, the function uses the additive identity when computing the cumulative sum. To begin summing from a different value, provide an `initial` argument. |
| 63 | + |
| 64 | +```javascript |
| 65 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 66 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 67 | + |
| 68 | +var xbuf = [ -1.0, 2.0, -3.0 ]; |
| 69 | +var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); |
| 70 | + |
| 71 | +var y = cusum( x, 10.0 ); |
| 72 | +// returns <ndarray> |
| 73 | + |
| 74 | +var arr = ndarray2array( y ); |
| 75 | +// returns [ 9.0, 11.0, 8.0 ] |
| 76 | +``` |
| 77 | + |
| 78 | +By default, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform the operation over specific dimensions, provide a `dims` option. |
| 79 | + |
| 80 | +```javascript |
| 81 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 82 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 83 | + |
| 84 | +var xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; |
| 85 | +var x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); |
| 86 | + |
| 87 | +var y = cusum( x, { |
| 88 | + 'dims': [ 0 ] |
| 89 | +}); |
| 90 | +// returns <ndarray> |
| 91 | + |
| 92 | +var v = ndarray2array( y ); |
| 93 | +// returns [ [ -1.0, 2.0 ], [ -4.0, 6.0 ] ] |
| 94 | + |
| 95 | +y = cusum( x, { |
| 96 | + 'dims': [ 1 ] |
| 97 | +}); |
| 98 | +// returns <ndarray> |
| 99 | + |
| 100 | +v = ndarray2array( y ); |
| 101 | +// returns [ [ -1.0, 1.0 ], [ -3.0, 1.0 ] ] |
| 102 | + |
| 103 | +y = cusum( x, { |
| 104 | + 'dims': [ 0, 1 ] |
| 105 | +}); |
| 106 | +// returns <ndarray> |
| 107 | + |
| 108 | +v = ndarray2array( y ); |
| 109 | +// returns [ [ -1.0, 1.0 ], [ -2.0, 2.0 ] ] |
| 110 | +``` |
| 111 | + |
| 112 | +By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] determined by the function's output data type [policy][@stdlib/ndarray/output-dtype-policies]. To override the default behavior, set the `dtype` option. |
| 113 | + |
| 114 | +```javascript |
| 115 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 116 | +var dtype = require( '@stdlib/ndarray/dtype' ); |
| 117 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 118 | + |
| 119 | +var xbuf = [ -1.0, 2.0, -3.0 ]; |
| 120 | +var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); |
| 121 | + |
| 122 | +var y = cusum( x, { |
| 123 | + 'dtype': 'float64' |
| 124 | +}); |
| 125 | +// returns <ndarray> |
| 126 | + |
| 127 | +var dt = dtype( y ); |
| 128 | +// returns 'float64' |
| 129 | +``` |
| 130 | + |
| 131 | +#### cusum.assign( x\[, initial], out\[, options] ) |
| 132 | + |
| 133 | +Computes the cumulative sum along one or more [ndarray][@stdlib/ndarray/ctor] dimensions and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor]. |
| 134 | + |
| 135 | +```javascript |
| 136 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 137 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 138 | + |
| 139 | +var xbuf = [ -1.0, 2.0, -3.0 ]; |
| 140 | +var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); |
| 141 | + |
| 142 | +var ybuf = [ 0.0, 0.0, 0.0 ]; |
| 143 | +var y = new ndarray( 'generic', ybuf, [ ybuf.length ], [ 1 ], 0, 'row-major' ); |
| 144 | + |
| 145 | +var out = cusum.assign( x, y ); |
| 146 | +// returns <ndarray> |
| 147 | + |
| 148 | +var v = ndarray2array( out ); |
| 149 | +// returns [ -1.0, 1.0, -2.0 ] |
| 150 | + |
| 151 | +var bool = ( out === y ); |
| 152 | +// returns true |
| 153 | +``` |
| 154 | + |
| 155 | +The method has the following parameters: |
| 156 | + |
| 157 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a numeric or generic [data type][@stdlib/ndarray/dtypes]. |
| 158 | +- **initial**: initial value for the cumulative sum (_optional_). May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] which [promotes][@stdlib/ndarray/promotion-rules] to the data type of the input [ndarray][@stdlib/ndarray/ctor]. If provided a scalar value, the value is cast to the data type of the input [ndarray][@stdlib/ndarray/ctor]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] initial value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] initial value must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. By default, the initial value is the additive identity (i.e., zero). |
| 159 | +- **out**: output [ndarray][@stdlib/ndarray/ctor]. |
| 160 | +- **options**: function options (_optional_). |
| 161 | + |
| 162 | +The method accepts the following options: |
| 163 | + |
| 164 | +- **dims**: list of dimensions over which to perform operation. If not provided, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. |
| 165 | + |
| 166 | +</section> |
| 167 | + |
| 168 | +<!-- /.usage --> |
| 169 | + |
| 170 | +<section class="notes"> |
| 171 | + |
| 172 | +## Notes |
| 173 | + |
| 174 | +- Both functions iterate over [ndarray][@stdlib/ndarray/ctor] elements according to the memory layout of the input [ndarray][@stdlib/ndarray/ctor]. Accordingly, performance degradation is possible when operating over multiple dimensions of a large non-contiguous multi-dimensional input [ndarray][@stdlib/ndarray/ctor]. In such scenarios, one may want to copy an input [ndarray][@stdlib/ndarray/ctor] to contiguous memory before computing the cumulative sum. |
| 175 | +- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, in order to avoid issues arising from integer overflow, the function must return an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] amenable to accumulation. This means that, for integer data types having small value ranges (e.g., `int8`, `uint8`, etc), the main function returns an [ndarray][@stdlib/ndarray/ctor] having at least a 32-bit integer data type. By default, if an input [ndarray][@stdlib/ndarray/ctor] has a floating-point data type, the main function returns an [ndarray][@stdlib/ndarray/ctor] having the same data type. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any [data type][@stdlib/ndarray/dtypes]. |
| 176 | + |
| 177 | +</section> |
| 178 | + |
| 179 | +<!-- /.notes --> |
| 180 | + |
| 181 | +<section class="examples"> |
| 182 | + |
| 183 | +## Examples |
| 184 | + |
| 185 | +<!-- eslint no-undef: "error" --> |
| 186 | + |
| 187 | +```javascript |
| 188 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 189 | +var dtype = require( '@stdlib/ndarray/dtype' ); |
| 190 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 191 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 192 | +var cusum = require( '@stdlib/blas/ext/cusum' ); |
| 193 | + |
| 194 | +// Generate an array of random numbers: |
| 195 | +var xbuf = discreteUniform( 25, 0, 20, { |
| 196 | + 'dtype': 'generic' |
| 197 | +}); |
| 198 | + |
| 199 | +// Wrap in an ndarray: |
| 200 | +var x = new ndarray( 'generic', xbuf, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' ); |
| 201 | +console.log( ndarray2array( x ) ); |
| 202 | + |
| 203 | +// Perform operation: |
| 204 | +var y = cusum( x, 100.0, { |
| 205 | + 'dims': [ 0 ] |
| 206 | +}); |
| 207 | + |
| 208 | +// Resolve the output array data type: |
| 209 | +var dt = dtype( y ); |
| 210 | +console.log( dt ); |
| 211 | + |
| 212 | +// Print the results: |
| 213 | +console.log( ndarray2array( y ) ); |
| 214 | +``` |
| 215 | + |
| 216 | +</section> |
| 217 | + |
| 218 | +<!-- /.examples --> |
| 219 | + |
| 220 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 221 | + |
| 222 | +<section class="related"> |
| 223 | + |
| 224 | +</section> |
| 225 | + |
| 226 | +<!-- /.related --> |
| 227 | + |
| 228 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 229 | + |
| 230 | +<section class="links"> |
| 231 | + |
| 232 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 233 | + |
| 234 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes |
| 235 | + |
| 236 | +[@stdlib/ndarray/promotion-rules]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/promotion-rules |
| 237 | + |
| 238 | +[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies |
| 239 | + |
| 240 | +[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes |
| 241 | + |
| 242 | +</section> |
| 243 | + |
| 244 | +<!-- /.links --> |
0 commit comments