Skip to content

Commit 0024458

Browse files
committed
feat: add blas/ext/cusum
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 14b1d83 commit 0024458

File tree

16 files changed

+5301
-0
lines changed

16 files changed

+5301
-0
lines changed
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
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 -->
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var uniform = require( '@stdlib/random/array/uniform' );
27+
var zerosLike = require( '@stdlib/ndarray/zeros-like' );
28+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
29+
var pkg = require( './../package.json' ).name;
30+
var cusum = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'float64'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - array length
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( len ) {
50+
var out;
51+
var x;
52+
53+
x = uniform( len, -50.0, 50.0, options );
54+
x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' );
55+
56+
out = zerosLike( x );
57+
58+
return benchmark;
59+
60+
/**
61+
* Benchmark function.
62+
*
63+
* @private
64+
* @param {Benchmark} b - benchmark instance
65+
*/
66+
function benchmark( b ) {
67+
var o;
68+
var i;
69+
70+
b.tic();
71+
for ( i = 0; i < b.iterations; i++ ) {
72+
o = cusum.assign( x, out );
73+
if ( typeof o !== 'object' ) {
74+
b.fail( 'should return an ndarray' );
75+
}
76+
}
77+
b.toc();
78+
if ( isnan( o.get( i%len ) ) ) {
79+
b.fail( 'should not return NaN' );
80+
}
81+
b.pass( 'benchmark finished' );
82+
b.end();
83+
}
84+
}
85+
86+
87+
// MAIN //
88+
89+
/**
90+
* Main execution sequence.
91+
*
92+
* @private
93+
*/
94+
function main() {
95+
var len;
96+
var min;
97+
var max;
98+
var f;
99+
var i;
100+
101+
min = 1; // 10^min
102+
max = 6; // 10^max
103+
104+
for ( i = min; i <= max; i++ ) {
105+
len = pow( 10, i );
106+
f = createBenchmark( len );
107+
bench( pkg+':assign:dtype='+options.dtype+',len='+len, f );
108+
}
109+
}
110+
111+
main();

0 commit comments

Comments
 (0)