Skip to content

Commit ca10e52

Browse files
committed
feat: add ndarray/base/unary-reduce-strided1d
--- 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: na - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 999b4d7 commit ca10e52

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+12712
-0
lines changed
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
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+
# unaryReduceStrided1d
22+
23+
> Perform a reduction over a list of specified dimensions in an input ndarray via a one-dimensional strided array reduction function and assign results to a provided output 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 unaryReduceStrided1d = require( '@stdlib/ndarray/base/unary-reduce-strided1d' );
37+
```
38+
39+
#### unaryReduceStrided1d( fcn, arrays, dims\[, options] )
40+
41+
Performs a reduction over a list of specified dimensions in an input ndarray via a one-dimensional strided array reduction function and assigns results to a provided output ndarray.
42+
43+
<!-- eslint-disable max-len -->
44+
45+
```javascript
46+
var Float64Array = require( '@stdlib/array/float64' );
47+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
48+
var getStride = require( '@stdlib/ndarray/base/stride' );
49+
var getOffset = require( '@stdlib/ndarray/base/offset' );
50+
var getData = require( '@stdlib/ndarray/base/data-buffer' );
51+
var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' );
52+
var gsum = require( '@stdlib/blas/ext/base/gsum' ).ndarray;
53+
54+
function wrapper( arrays ) {
55+
var x = arrays[ 0 ];
56+
return gsum( numelDimension( x, 0 ), getData( x ), getStride( x, 0 ), getOffset( x ) );
57+
}
58+
59+
// Create data buffers:
60+
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
61+
var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] );
62+
63+
// Define the array shapes:
64+
var xsh = [ 1, 3, 2, 2 ];
65+
var ysh = [ 1, 3 ];
66+
67+
// Define the array strides:
68+
var sx = [ 12, 4, 2, 1 ];
69+
var sy = [ 3, 1 ];
70+
71+
// Define the index offsets:
72+
var ox = 0;
73+
var oy = 0;
74+
75+
// Create an input ndarray-like object:
76+
var x = {
77+
'dtype': 'float64',
78+
'data': xbuf,
79+
'shape': xsh,
80+
'strides': sx,
81+
'offset': ox,
82+
'order': 'row-major'
83+
};
84+
85+
// Create an output ndarray-like object:
86+
var y = {
87+
'dtype': 'float64',
88+
'data': ybuf,
89+
'shape': ysh,
90+
'strides': sy,
91+
'offset': oy,
92+
'order': 'row-major'
93+
};
94+
95+
// Perform a reduction:
96+
unaryReduceStrided1d( wrapper, [ x, y ], [ 2, 3 ] );
97+
98+
var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order );
99+
// returns [ [ 10.0, 26.0, 42.0 ] ]
100+
```
101+
102+
The function accepts the following arguments:
103+
104+
- **fcn**: function which will be applied to a one-dimensional subarray and should reduce the subarray to a single scalar value.
105+
- **arrays**: array-like object containing one input ndarray and one output ndarray, followed by any additional ndarray arguments.
106+
- **dims**: list of dimensions over which to perform a reduction.
107+
- **options**: function options which are passed through to `fcn` (_optional_).
108+
109+
Each provided ndarray should be an object with the following properties:
110+
111+
- **dtype**: data type.
112+
- **data**: data buffer.
113+
- **shape**: dimensions.
114+
- **strides**: stride lengths.
115+
- **offset**: index offset.
116+
- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style).
117+
118+
</section>
119+
120+
<!-- /.usage -->
121+
122+
<section class="notes">
123+
124+
## Notes
125+
126+
- The output ndarray and any additional ndarray arguments are expected to have the same dimensions as the non-reduced dimensions of the input ndarray. When calling the reduction function, any additional ndarray arguments are provided as zero-dimensional ndarray-like objects.
127+
128+
- The reduction function is expected to have the following signature:
129+
130+
```text
131+
fcn( arrays[, options] )
132+
```
133+
134+
where
135+
136+
- **arrays**: array containing a one-dimensional subarray of the input ndarray and any additional ndarray arguments as zero-dimensional ndarrays.
137+
- **options**: function options (_optional_).
138+
139+
- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before performing a reduction in order to achieve better performance.
140+
141+
</section>
142+
143+
<!-- /.notes -->
144+
145+
<section class="examples">
146+
147+
## Examples
148+
149+
<!-- eslint no-undef: "error" -->
150+
151+
```javascript
152+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
153+
var zeros = require( '@stdlib/array/base/zeros' );
154+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
155+
var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' );
156+
var getData = require( '@stdlib/ndarray/base/data-buffer' );
157+
var getStride = require( '@stdlib/ndarray/base/stride' );
158+
var getOffset = require( '@stdlib/ndarray/base/offset' );
159+
var gsum = require( '@stdlib/blas/ext/base/gsum' ).ndarray;
160+
var unaryReduceStrided1d = require( '@stdlib/ndarray/base/unary-reduce-strided1d' );
161+
162+
function wrapper( arrays ) {
163+
var x = arrays[ 0 ];
164+
return gsum( numelDimension( x, 0 ), getData( x ), getStride( x, 0 ), getOffset( x ) ); // eslint-disable-line max-len
165+
}
166+
167+
var N = 10;
168+
var x = {
169+
'dtype': 'generic',
170+
'data': discreteUniform( N, -5, 5, {
171+
'dtype': 'generic'
172+
}),
173+
'shape': [ 1, 5, 2 ],
174+
'strides': [ 10, 2, 1 ],
175+
'offset': 0,
176+
'order': 'row-major'
177+
};
178+
var y = {
179+
'dtype': 'generic',
180+
'data': zeros( 2 ),
181+
'shape': [ 1, 2 ],
182+
'strides': [ 2, 1 ],
183+
'offset': 0,
184+
'order': 'row-major'
185+
};
186+
187+
unaryReduceStrided1d( wrapper, [ x, y ], [ 1 ] );
188+
189+
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
190+
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );
191+
```
192+
193+
</section>
194+
195+
<!-- /.examples -->
196+
197+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
198+
199+
<section class="related">
200+
201+
</section>
202+
203+
<!-- /.related -->
204+
205+
<section class="links">
206+
207+
</section>
208+
209+
<!-- /.links -->
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
2+
{{alias}}( fcn, arrays, dims[, options] )
3+
Performs a reduction over a list of specified dimensions in an input ndarray
4+
via a one-dimensional strided array reduction function and assigns results
5+
to a provided output ndarray.
6+
7+
Each provided "ndarray" should be an object with the following properties:
8+
9+
- dtype: data type.
10+
- data: data buffer.
11+
- shape: dimensions.
12+
- strides: stride lengths.
13+
- offset: index offset.
14+
- order: specifies whether an ndarray is row-major (C-style) or column-major
15+
(Fortran-style).
16+
17+
The output ndarray and any additional ndarray arguments are expected to have
18+
the same dimensions as the non-reduced dimensions of the input ndarray. When
19+
calling the reduction function, any additional ndarray arguments are
20+
provided as zero-dimensional ndarray-like objects.
21+
22+
Parameters
23+
----------
24+
fcn: Function
25+
Function which will be applied to a one-dimensional subarray and should
26+
reduce the subarray to a single scalar value. The function should have
27+
the following signature:
28+
29+
fcn( arrays[, options] )
30+
31+
where
32+
33+
- arrays: array containing a one-dimensional subarray of the input
34+
ndarray and any additional ndarray arguments as zero-dimensional
35+
ndarrays.
36+
- options: function options.
37+
38+
arrays: ArrayLikeObject<ndarray>
39+
Array-like object containing one input ndarray and one output ndarray,
40+
followed by any additional ndarray arguments.
41+
42+
dims: Array<integer>
43+
List of dimensions over which to perform a reduction.
44+
45+
options: Object (optional)
46+
Function options.
47+
48+
Examples
49+
--------
50+
// Define ndarray data and meta data...
51+
> var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0 ] );
52+
> var ybuf = new {{alias:@stdlib/array/float64}}( [ 0.0 ] );
53+
> var dtype = 'float64';
54+
> var shx = [ 2, 2 ];
55+
> var shy = [];
56+
> var sx = [ 2, 1 ];
57+
> var sy = [ 0 ];
58+
> var ox = 0;
59+
> var oy = 0;
60+
> var order = 'row-major';
61+
62+
// Define a wrapper for an extended BLAS function...
63+
> function fcn( arrays ) {
64+
... var x = arrays[ 0 ];
65+
... var N = x.shape[ 0 ];
66+
... var d = x.data;
67+
... var s = x.strides[ 0 ];
68+
... var o = x.offset;
69+
... return {{alias:@stdlib/blas/ext/base/gsum}}.ndarray( N, d, s, o );
70+
... };
71+
72+
// Using minimal ndarray-like objects...
73+
> x = {
74+
... 'dtype': dtype,
75+
... 'data': xbuf,
76+
... 'shape': shx,
77+
... 'strides': sx,
78+
... 'offset': ox,
79+
... 'order': order
80+
... };
81+
> y = {
82+
... 'dtype': dtype,
83+
... 'data': ybuf,
84+
... 'shape': shy,
85+
... 'strides': sy,
86+
... 'offset': oy,
87+
... 'order': order
88+
... };
89+
> {{alias}}( fcn, [ x, y ], [ 0, 1 ] );
90+
> y.data
91+
<Float64Array>[ 10.0 ]
92+
93+
See Also
94+
--------
95+
Lines changed: 59 additions & 0 deletions
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+
'use strict';
20+
21+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
22+
var zeros = require( '@stdlib/array/base/zeros' );
23+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
24+
var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' );
25+
var getData = require( '@stdlib/ndarray/base/data-buffer' );
26+
var getStride = require( '@stdlib/ndarray/base/stride' );
27+
var getOffset = require( '@stdlib/ndarray/base/offset' );
28+
var gsum = require( '@stdlib/blas/ext/base/gsum' ).ndarray;
29+
var unaryReduceStrided1d = require( './../lib' );
30+
31+
function wrapper( arrays ) {
32+
var x = arrays[ 0 ];
33+
return gsum( numelDimension( x, 0 ), getData( x ), getStride( x, 0 ), getOffset( x ) ); // eslint-disable-line max-len
34+
}
35+
36+
var N = 10;
37+
var x = {
38+
'dtype': 'generic',
39+
'data': discreteUniform( N, -5, 5, {
40+
'dtype': 'generic'
41+
}),
42+
'shape': [ 1, 5, 2 ],
43+
'strides': [ 10, 2, 1 ],
44+
'offset': 0,
45+
'order': 'row-major'
46+
};
47+
var y = {
48+
'dtype': 'generic',
49+
'data': zeros( 2 ),
50+
'shape': [ 1, 2 ],
51+
'strides': [ 2, 1 ],
52+
'offset': 0,
53+
'order': 'row-major'
54+
};
55+
56+
unaryReduceStrided1d( wrapper, [ x, y ], [ 1 ] );
57+
58+
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
59+
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );

0 commit comments

Comments
 (0)