Skip to content

Commit 76d6a22

Browse files
committed
feat: add blas/ext/base/ndarray/dcusum
--- 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 a996667 commit 76d6a22

File tree

10 files changed

+963
-0
lines changed

10 files changed

+963
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
# dcusum
22+
23+
> Compute the cumulative sum of a one-dimensional double-precision floating-point ndarray.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var dcusum = require( '@stdlib/blas/ext/base/ndarray/dcusum' );
37+
```
38+
39+
#### dcusum( arrays )
40+
41+
Computes the cumulative sum of a one-dimensional double-precision floating-point ndarray.
42+
43+
```javascript
44+
var Float64Array = require( '@stdlib/array/float64' );
45+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
46+
var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' );
47+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
48+
49+
var xbuf = new Float64Array( [ 1.0, 3.0, 4.0, 2.0 ] );
50+
var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
51+
52+
var ybuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] );
53+
var y = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
54+
55+
var initial = scalar2ndarray( 0.0, 'float64', 'row-major' );
56+
57+
var v = dcusum( [ x, y, initial ] );
58+
// returns <ndarray>
59+
60+
var bool = ( v === y );
61+
// returns true
62+
63+
var arr = ndarray2array( v );
64+
// returns [ 1.0, 4.0, 8.0, 10.0 ]
65+
```
66+
67+
The function has the following parameters:
68+
69+
- **arrays**: array-like object containing a one-dimensional input ndarray, a one-dimensional output ndarray, and a zero-dimensional ndarray containing the initial sum.
70+
71+
</section>
72+
73+
<!-- /.usage -->
74+
75+
<section class="notes">
76+
77+
## Notes
78+
79+
- If provided an empty one-dimensional input ndarray, the function returns the output ndarray unchanged.
80+
81+
</section>
82+
83+
<!-- /.notes -->
84+
85+
<section class="examples">
86+
87+
## Examples
88+
89+
<!-- eslint no-undef: "error" -->
90+
91+
```javascript
92+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
93+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
94+
var zerosLike = require( '@stdlib/ndarray/zeros-like' );
95+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
96+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
97+
var dcusum = require( '@stdlib/blas/ext/base/ndarray/dcusum' );
98+
99+
var xbuf = discreteUniform( 10, -50, 50, {
100+
'dtype': 'float64'
101+
});
102+
var x = new ndarray( 'float64', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
103+
console.log( ndarray2array( x ) );
104+
105+
var y = zerosLike( x );
106+
console.log( ndarray2array( y ) );
107+
108+
var initial = scalar2ndarray( 100.0, {
109+
'dtype': 'float64'
110+
});
111+
112+
var v = dcusum( [ x, y, initial ] );
113+
console.log( ndarray2array( v ) );
114+
```
115+
116+
</section>
117+
118+
<!-- /.examples -->
119+
120+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
121+
122+
<section class="related">
123+
124+
</section>
125+
126+
<!-- /.related -->
127+
128+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
129+
130+
<section class="links">
131+
132+
</section>
133+
134+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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 uniform = require( '@stdlib/random/array/uniform' );
25+
var zeros = require( '@stdlib/array/zeros' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
29+
var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' );
30+
var pkg = require( './../package.json' ).name;
31+
var dcusum = require( './../lib' );
32+
33+
34+
// VARIABLES //
35+
36+
var options = {
37+
'dtype': 'float64'
38+
};
39+
40+
41+
// FUNCTIONS //
42+
43+
/**
44+
* Creates a benchmark function.
45+
*
46+
* @private
47+
* @param {PositiveInteger} len - array length
48+
* @returns {Function} benchmark function
49+
*/
50+
function createBenchmark( len ) {
51+
var initial;
52+
var xbuf;
53+
var ybuf;
54+
var x;
55+
var y;
56+
57+
xbuf = uniform( len, -10.0, 10.0, options );
58+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
59+
60+
initial = scalar2ndarray( 0.0, options.dtype, 'row-major' );
61+
62+
ybuf = zeros( len, options.dtype );
63+
y = new ndarray( options.dtype, ybuf, [ len ], [ 1 ], 0, 'row-major' );
64+
65+
return benchmark;
66+
67+
function benchmark( b ) {
68+
var v;
69+
var i;
70+
71+
b.tic();
72+
for ( i = 0; i < b.iterations; i++ ) {
73+
v = dcusum( [ x, y, initial ] );
74+
if ( typeof v !== 'object' ) {
75+
b.fail( 'should return an ndarray' );
76+
}
77+
}
78+
b.toc();
79+
if ( isnan( v.get( i%len ) ) ) {
80+
b.fail( 'should not return NaN' );
81+
}
82+
b.pass( 'benchmark finished' );
83+
b.end();
84+
}
85+
}
86+
87+
88+
// MAIN //
89+
90+
/**
91+
* Main execution sequence.
92+
*
93+
* @private
94+
*/
95+
function main() {
96+
var len;
97+
var min;
98+
var max;
99+
var f;
100+
var i;
101+
102+
min = 1; // 10^min
103+
max = 6; // 10^max
104+
105+
for ( i = min; i <= max; i++ ) {
106+
len = pow( 10, i );
107+
f = createBenchmark( len );
108+
bench( pkg+':len='+len, f );
109+
}
110+
}
111+
112+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
{{alias}}( arrays )
3+
Computes the cumulative sum of a one-dimensional double-precision floating-
4+
point ndarray.
5+
6+
If provided an empty input ndarray, the function returns the output ndarray
7+
unchanged.
8+
9+
Parameters
10+
----------
11+
arrays: ArrayLikeObject<ndarray>
12+
Array-like object containing a one-dimensional input ndarray, a one-
13+
dimensional output ndarray, and zero-dimensional ndarray containing the
14+
initial sum.
15+
16+
Returns
17+
-------
18+
out: ndarray
19+
Output ndarray.
20+
21+
Examples
22+
--------
23+
> var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 2.0 ] );
24+
> var ybuf = new {{alias:@stdlib/array/float64}}( [ 0.0, 0.0, 0.0 ] );
25+
> var dt = 'float64';
26+
> var sh = [ xbuf.length ];
27+
> var st = [ 1 ];
28+
> var oo = 0;
29+
> var ord = 'row-major';
30+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, st, oo, ord );
31+
> var y = new {{alias:@stdlib/ndarray/ctor}}( dt, ybuf, sh, st, oo, ord );
32+
> var s = {{alias:@stdlib/ndarray/from-scalar}}( 0.0, { 'dtype': dt } );
33+
> {{alias}}( [ x, y, s ] );
34+
> {{alias:@stdlib/ndarray/to-array}}( y )
35+
[ 1.0, -1.0, 1.0 ]
36+
37+
See Also
38+
--------
39+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { float64ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Computes the cumulative sum of a one-dimensional double-precision floating-point ndarray.
27+
*
28+
* @param arrays - array-like object containing an input ndarray and an output ndarray
29+
* @returns output ndarray
30+
*
31+
* @example
32+
* var Float64Array = require( '@stdlib/array/float64' );
33+
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
34+
* var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' );
35+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
36+
*
37+
* var xbuf = new Float64Array( [ 1.0, 3.0, 4.0, 2.0 ] );
38+
* var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
39+
*
40+
* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] );
41+
* var y = new ndarray( 'float64', ybuf, [ 4 ], [ 1 ], 0, 'row-major' );
42+
*
43+
* var initial = scalar2ndarray( 0.0, 'float64', 'row-major' );
44+
*
45+
* var v = dcusum( [ x, y, initial ] );
46+
* // returns <ndarray>
47+
*
48+
* var bool = ( v === y );
49+
* // returns true
50+
*
51+
* var arr = ndarray2array( v );
52+
* // returns [ 1.0, 4.0, 8.0, 10.0 ]
53+
*/
54+
declare function dcusum( arrays: [ float64ndarray, float64ndarray, float64ndarray ] ): float64ndarray;
55+
56+
57+
// EXPORTS //
58+
59+
export = dcusum;

0 commit comments

Comments
 (0)